blob: ce02c056ac3f9f53e24eaae05092e7ea5be29510 [file] [log] [blame]
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001/*
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>
Michael Ellermandcee3032005-12-04 18:39:48 +110032#include <linux/kexec.h>
Michael Ellerman7a4571a2006-06-23 18:16:03 +100033#include <linux/debugfs.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100034
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>
Michael Ellerman0cc47462005-12-04 18:39:37 +110042#include <asm/kdump.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100043#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>
Paul Mackerras40ef8cb2005-10-10 22:50:37 +100053#include <asm/pci-bridge.h>
Michael Ellerman2babf5c2006-05-17 18:00:46 +100054#include <asm/kexec.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100055
56#ifdef DEBUG
57#define DBG(fmt...) printk(KERN_ERR fmt)
58#else
59#define DBG(fmt...)
60#endif
61
Paul Mackerras9b6b5632005-10-06 12:06:20 +100062
Paul Mackerras9b6b5632005-10-06 12:06:20 +100063static int __initdata dt_root_addr_cells;
64static int __initdata dt_root_size_cells;
65
66#ifdef CONFIG_PPC64
Olof Johansson28897732006-04-12 21:52:33 -050067int __initdata iommu_is_off;
Paul Mackerras9b6b5632005-10-06 12:06:20 +100068int __initdata iommu_force_on;
Paul Mackerrascf00a8d2005-10-31 13:07:02 +110069unsigned long tce_alloc_start, tce_alloc_end;
Paul Mackerras9b6b5632005-10-06 12:06:20 +100070#endif
71
72typedef u32 cell_t;
73
74#if 0
75static struct boot_param_header *initial_boot_params __initdata;
76#else
77struct boot_param_header *initial_boot_params;
78#endif
79
80static 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 */
85static DEFINE_RWLOCK(devtree_lock);
86
87/* export that to outside world */
88struct device_node *of_chosen;
89
90struct device_node *dflt_interrupt_controller;
91int num_interrupt_controllers;
92
Paul Mackerras9b6b5632005-10-06 12:06:20 +100093/*
94 * Wrapper for allocating memory for various data that needs to be
95 * attached to device nodes as they are processed at boot or when
96 * added to the device tree later (e.g. DLPAR). At boot there is
97 * already a region reserved so we just increment *mem_start by size;
98 * otherwise we call kmalloc.
99 */
100static void * prom_alloc(unsigned long size, unsigned long *mem_start)
101{
102 unsigned long tmp;
103
104 if (!mem_start)
105 return kmalloc(size, GFP_KERNEL);
106
107 tmp = *mem_start;
108 *mem_start += size;
109 return (void *)tmp;
110}
111
112/*
113 * Find the device_node with a given phandle.
114 */
115static struct device_node * find_phandle(phandle ph)
116{
117 struct device_node *np;
118
119 for (np = allnodes; np != 0; np = np->allnext)
120 if (np->linux_phandle == ph)
121 return np;
122 return NULL;
123}
124
125/*
126 * Find the interrupt parent of a node.
127 */
128static struct device_node * __devinit intr_parent(struct device_node *p)
129{
130 phandle *parp;
131
132 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
133 if (parp == NULL)
134 return p->parent;
135 p = find_phandle(*parp);
136 if (p != NULL)
137 return p;
138 /*
139 * On a powermac booted with BootX, we don't get to know the
140 * phandles for any nodes, so find_phandle will return NULL.
141 * Fortunately these machines only have one interrupt controller
142 * so there isn't in fact any ambiguity. -- paulus
143 */
144 if (num_interrupt_controllers == 1)
145 p = dflt_interrupt_controller;
146 return p;
147}
148
149/*
150 * Find out the size of each entry of the interrupts property
151 * for a node.
152 */
153int __devinit prom_n_intr_cells(struct device_node *np)
154{
155 struct device_node *p;
156 unsigned int *icp;
157
158 for (p = np; (p = intr_parent(p)) != NULL; ) {
159 icp = (unsigned int *)
160 get_property(p, "#interrupt-cells", NULL);
161 if (icp != NULL)
162 return *icp;
163 if (get_property(p, "interrupt-controller", NULL) != NULL
164 || get_property(p, "interrupt-map", NULL) != NULL) {
165 printk("oops, node %s doesn't have #interrupt-cells\n",
166 p->full_name);
167 return 1;
168 }
169 }
170#ifdef DEBUG_IRQ
171 printk("prom_n_intr_cells failed for %s\n", np->full_name);
172#endif
173 return 1;
174}
175
176/*
177 * Map an interrupt from a device up to the platform interrupt
178 * descriptor.
179 */
180static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
181 struct device_node *np, unsigned int *ints,
182 int nintrc)
183{
184 struct device_node *p, *ipar;
185 unsigned int *imap, *imask, *ip;
186 int i, imaplen, match;
187 int newintrc = 0, newaddrc = 0;
188 unsigned int *reg;
189 int naddrc;
190
191 reg = (unsigned int *) get_property(np, "reg", NULL);
192 naddrc = prom_n_addr_cells(np);
193 p = intr_parent(np);
194 while (p != NULL) {
195 if (get_property(p, "interrupt-controller", NULL) != NULL)
196 /* this node is an interrupt controller, stop here */
197 break;
198 imap = (unsigned int *)
199 get_property(p, "interrupt-map", &imaplen);
200 if (imap == NULL) {
201 p = intr_parent(p);
202 continue;
203 }
204 imask = (unsigned int *)
205 get_property(p, "interrupt-map-mask", NULL);
206 if (imask == NULL) {
207 printk("oops, %s has interrupt-map but no mask\n",
208 p->full_name);
209 return 0;
210 }
211 imaplen /= sizeof(unsigned int);
212 match = 0;
213 ipar = NULL;
214 while (imaplen > 0 && !match) {
215 /* check the child-interrupt field */
216 match = 1;
217 for (i = 0; i < naddrc && match; ++i)
218 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
219 for (; i < naddrc + nintrc && match; ++i)
220 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
221 imap += naddrc + nintrc;
222 imaplen -= naddrc + nintrc;
223 /* grab the interrupt parent */
224 ipar = find_phandle((phandle) *imap++);
225 --imaplen;
226 if (ipar == NULL && num_interrupt_controllers == 1)
227 /* cope with BootX not giving us phandles */
228 ipar = dflt_interrupt_controller;
229 if (ipar == NULL) {
230 printk("oops, no int parent %x in map of %s\n",
231 imap[-1], p->full_name);
232 return 0;
233 }
234 /* find the parent's # addr and intr cells */
235 ip = (unsigned int *)
236 get_property(ipar, "#interrupt-cells", NULL);
237 if (ip == NULL) {
238 printk("oops, no #interrupt-cells on %s\n",
239 ipar->full_name);
240 return 0;
241 }
242 newintrc = *ip;
243 ip = (unsigned int *)
244 get_property(ipar, "#address-cells", NULL);
245 newaddrc = (ip == NULL)? 0: *ip;
246 imap += newaddrc + newintrc;
247 imaplen -= newaddrc + newintrc;
248 }
249 if (imaplen < 0) {
250 printk("oops, error decoding int-map on %s, len=%d\n",
251 p->full_name, imaplen);
252 return 0;
253 }
254 if (!match) {
255#ifdef DEBUG_IRQ
256 printk("oops, no match in %s int-map for %s\n",
257 p->full_name, np->full_name);
258#endif
259 return 0;
260 }
261 p = ipar;
262 naddrc = newaddrc;
263 nintrc = newintrc;
264 ints = imap - nintrc;
265 reg = ints - naddrc;
266 }
267 if (p == NULL) {
268#ifdef DEBUG_IRQ
269 printk("hmmm, int tree for %s doesn't have ctrler\n",
270 np->full_name);
271#endif
272 return 0;
273 }
274 *irq = ints;
275 *ictrler = p;
276 return nintrc;
277}
278
Paul Mackerras6d0124f2005-10-26 17:19:06 +1000279static unsigned char map_isa_senses[4] = {
280 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
281 IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
282 IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE,
283 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE
284};
285
286static unsigned char map_mpic_senses[4] = {
287 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE,
288 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
289 /* 2 seems to be used for the 8259 cascade... */
290 IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
291 IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE,
292};
293
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000294static int __devinit finish_node_interrupts(struct device_node *np,
295 unsigned long *mem_start,
296 int measure_only)
297{
298 unsigned int *ints;
299 int intlen, intrcells, intrcount;
Paul Mackerras6d0124f2005-10-26 17:19:06 +1000300 int i, j, n, sense;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000301 unsigned int *irq, virq;
302 struct device_node *ic;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100303 int trace = 0;
304
305 //#define TRACE(fmt...) do { if (trace) { printk(fmt); mdelay(1000); } } while(0)
306#define TRACE(fmt...)
307
308 if (!strcmp(np->name, "smu-doorbell"))
309 trace = 1;
310
311 TRACE("Finishing SMU doorbell ! num_interrupt_controllers = %d\n",
312 num_interrupt_controllers);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000313
Paul Mackerrasa575b802005-10-23 17:23:21 +1000314 if (num_interrupt_controllers == 0) {
315 /*
316 * Old machines just have a list of interrupt numbers
317 * and no interrupt-controller nodes.
318 */
319 ints = (unsigned int *) get_property(np, "AAPL,interrupts",
320 &intlen);
321 /* XXX old interpret_pci_props looked in parent too */
322 /* XXX old interpret_macio_props looked for interrupts
323 before AAPL,interrupts */
324 if (ints == NULL)
325 ints = (unsigned int *) get_property(np, "interrupts",
326 &intlen);
327 if (ints == NULL)
328 return 0;
329
330 np->n_intrs = intlen / sizeof(unsigned int);
331 np->intrs = prom_alloc(np->n_intrs * sizeof(np->intrs[0]),
332 mem_start);
333 if (!np->intrs)
334 return -ENOMEM;
335 if (measure_only)
336 return 0;
337
338 for (i = 0; i < np->n_intrs; ++i) {
339 np->intrs[i].line = *ints++;
Paul Mackerras6d0124f2005-10-26 17:19:06 +1000340 np->intrs[i].sense = IRQ_SENSE_LEVEL
341 | IRQ_POLARITY_NEGATIVE;
Paul Mackerrasa575b802005-10-23 17:23:21 +1000342 }
343 return 0;
344 }
345
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000346 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100347 TRACE("ints=%p, intlen=%d\n", ints, intlen);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000348 if (ints == NULL)
349 return 0;
350 intrcells = prom_n_intr_cells(np);
351 intlen /= intrcells * sizeof(unsigned int);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100352 TRACE("intrcells=%d, new intlen=%d\n", intrcells, intlen);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000353 np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start);
354 if (!np->intrs)
355 return -ENOMEM;
356
357 if (measure_only)
358 return 0;
359
360 intrcount = 0;
361 for (i = 0; i < intlen; ++i, ints += intrcells) {
362 n = map_interrupt(&irq, &ic, np, ints, intrcells);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100363 TRACE("map, irq=%d, ic=%p, n=%d\n", irq, ic, n);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000364 if (n <= 0)
365 continue;
366
367 /* don't map IRQ numbers under a cascaded 8259 controller */
368 if (ic && device_is_compatible(ic, "chrp,iic")) {
369 np->intrs[intrcount].line = irq[0];
Paul Mackerras6d0124f2005-10-26 17:19:06 +1000370 sense = (n > 1)? (irq[1] & 3): 3;
371 np->intrs[intrcount].sense = map_isa_senses[sense];
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000372 } else {
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000373 virq = virt_irq_create_mapping(irq[0]);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100374 TRACE("virq=%d\n", virq);
Paul Mackerras6d0124f2005-10-26 17:19:06 +1000375#ifdef CONFIG_PPC64
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000376 if (virq == NO_IRQ) {
377 printk(KERN_CRIT "Could not allocate interrupt"
378 " number for %s\n", np->full_name);
379 continue;
380 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000381#endif
Paul Mackerras6d0124f2005-10-26 17:19:06 +1000382 np->intrs[intrcount].line = irq_offset_up(virq);
383 sense = (n > 1)? (irq[1] & 3): 1;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100384
385 /* Apple uses bits in there in a different way, let's
386 * only keep the real sense bit on macs
387 */
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100388 if (machine_is(powermac))
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100389 sense &= 0x1;
Paul Mackerras6d0124f2005-10-26 17:19:06 +1000390 np->intrs[intrcount].sense = map_mpic_senses[sense];
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000391 }
392
393#ifdef CONFIG_PPC64
394 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100395 if (machine_is(powermac) && ic && ic->parent) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000396 char *name = get_property(ic->parent, "name", NULL);
397 if (name && !strcmp(name, "u3"))
398 np->intrs[intrcount].line += 128;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100399 else if (!(name && (!strcmp(name, "mac-io") ||
400 !strcmp(name, "u4"))))
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000401 /* ignore other cascaded controllers, such as
402 the k2-sata-root */
403 break;
404 }
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100405#endif /* CONFIG_PPC64 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000406 if (n > 2) {
407 printk("hmmm, got %d intr cells for %s:", n,
408 np->full_name);
409 for (j = 0; j < n; ++j)
410 printk(" %d", irq[j]);
411 printk("\n");
412 }
413 ++intrcount;
414 }
415 np->n_intrs = intrcount;
416
417 return 0;
418}
419
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000420static int __devinit finish_node(struct device_node *np,
421 unsigned long *mem_start,
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000422 int measure_only)
423{
424 struct device_node *child;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100425 int rc = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000426
427 rc = finish_node_interrupts(np, mem_start, measure_only);
428 if (rc)
429 goto out;
430
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000431 for (child = np->child; child != NULL; child = child->sibling) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100432 rc = finish_node(child, mem_start, measure_only);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000433 if (rc)
434 goto out;
435 }
436out:
437 return rc;
438}
439
440static void __init scan_interrupt_controllers(void)
441{
442 struct device_node *np;
443 int n = 0;
444 char *name, *ic;
445 int iclen;
446
447 for (np = allnodes; np != NULL; np = np->allnext) {
448 ic = get_property(np, "interrupt-controller", &iclen);
449 name = get_property(np, "name", NULL);
450 /* checking iclen makes sure we don't get a false
451 match on /chosen.interrupt_controller */
452 if ((name != NULL
453 && strcmp(name, "interrupt-controller") == 0)
454 || (ic != NULL && iclen == 0
455 && strcmp(name, "AppleKiwi"))) {
456 if (n == 0)
457 dflt_interrupt_controller = np;
458 ++n;
459 }
460 }
461 num_interrupt_controllers = n;
462}
463
464/**
465 * finish_device_tree is called once things are running normally
466 * (i.e. with text and data mapped to the address they were linked at).
467 * It traverses the device tree and fills in some of the additional,
468 * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
469 * mapping is also initialized at this point.
470 */
471void __init finish_device_tree(void)
472{
473 unsigned long start, end, size = 0;
474
475 DBG(" -> finish_device_tree\n");
476
477#ifdef CONFIG_PPC64
478 /* Initialize virtual IRQ map */
479 virt_irq_init();
480#endif
481 scan_interrupt_controllers();
482
483 /*
484 * Finish device-tree (pre-parsing some properties etc...)
485 * We do this in 2 passes. One with "measure_only" set, which
486 * will only measure the amount of memory needed, then we can
487 * allocate that memory, and call finish_node again. However,
488 * we must be careful as most routines will fail nowadays when
489 * prom_alloc() returns 0, so we must make sure our first pass
490 * doesn't start at 0. We pre-initialize size to 16 for that
491 * reason and then remove those additional 16 bytes
492 */
493 size = 16;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100494 finish_node(allnodes, &size, 1);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000495 size -= 16;
Michael Ellermanfa938952006-01-25 21:31:25 +1300496
497 if (0 == size)
498 end = start = 0;
499 else
500 end = start = (unsigned long)__va(lmb_alloc(size, 128));
501
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100502 finish_node(allnodes, &end, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000503 BUG_ON(end != start + size);
504
505 DBG(" <- finish_device_tree\n");
506}
507
508static inline char *find_flat_dt_string(u32 offset)
509{
510 return ((char *)initial_boot_params) +
511 initial_boot_params->off_dt_strings + offset;
512}
513
514/**
515 * This function is used to scan the flattened device-tree, it is
516 * used to extract the memory informations at boot before we can
517 * unflatten the tree
518 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100519int __init of_scan_flat_dt(int (*it)(unsigned long node,
520 const char *uname, int depth,
521 void *data),
522 void *data)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000523{
524 unsigned long p = ((unsigned long)initial_boot_params) +
525 initial_boot_params->off_dt_struct;
526 int rc = 0;
527 int depth = -1;
528
529 do {
530 u32 tag = *((u32 *)p);
531 char *pathp;
532
533 p += 4;
534 if (tag == OF_DT_END_NODE) {
535 depth --;
536 continue;
537 }
538 if (tag == OF_DT_NOP)
539 continue;
540 if (tag == OF_DT_END)
541 break;
542 if (tag == OF_DT_PROP) {
543 u32 sz = *((u32 *)p);
544 p += 8;
545 if (initial_boot_params->version < 0x10)
546 p = _ALIGN(p, sz >= 8 ? 8 : 4);
547 p += sz;
548 p = _ALIGN(p, 4);
549 continue;
550 }
551 if (tag != OF_DT_BEGIN_NODE) {
552 printk(KERN_WARNING "Invalid tag %x scanning flattened"
553 " device tree !\n", tag);
554 return -EINVAL;
555 }
556 depth++;
557 pathp = (char *)p;
558 p = _ALIGN(p + strlen(pathp) + 1, 4);
559 if ((*pathp) == '/') {
560 char *lp, *np;
561 for (lp = NULL, np = pathp; *np; np++)
562 if ((*np) == '/')
563 lp = np+1;
564 if (lp != NULL)
565 pathp = lp;
566 }
567 rc = it(p, pathp, depth, data);
568 if (rc != 0)
569 break;
570 } while(1);
571
572 return rc;
573}
574
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100575unsigned long __init of_get_flat_dt_root(void)
576{
577 unsigned long p = ((unsigned long)initial_boot_params) +
578 initial_boot_params->off_dt_struct;
579
580 while(*((u32 *)p) == OF_DT_NOP)
581 p += 4;
582 BUG_ON (*((u32 *)p) != OF_DT_BEGIN_NODE);
583 p += 4;
584 return _ALIGN(p + strlen((char *)p) + 1, 4);
585}
586
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000587/**
588 * This function can be used within scan_flattened_dt callback to get
589 * access to properties
590 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100591void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
592 unsigned long *size)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000593{
594 unsigned long p = node;
595
596 do {
597 u32 tag = *((u32 *)p);
598 u32 sz, noff;
599 const char *nstr;
600
601 p += 4;
602 if (tag == OF_DT_NOP)
603 continue;
604 if (tag != OF_DT_PROP)
605 return NULL;
606
607 sz = *((u32 *)p);
608 noff = *((u32 *)(p + 4));
609 p += 8;
610 if (initial_boot_params->version < 0x10)
611 p = _ALIGN(p, sz >= 8 ? 8 : 4);
612
613 nstr = find_flat_dt_string(noff);
614 if (nstr == NULL) {
615 printk(KERN_WARNING "Can't find property index"
616 " name !\n");
617 return NULL;
618 }
619 if (strcmp(name, nstr) == 0) {
620 if (size)
621 *size = sz;
622 return (void *)p;
623 }
624 p += sz;
625 p = _ALIGN(p, 4);
626 } while(1);
627}
628
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100629int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
630{
631 const char* cp;
632 unsigned long cplen, l;
633
634 cp = of_get_flat_dt_prop(node, "compatible", &cplen);
635 if (cp == NULL)
636 return 0;
637 while (cplen > 0) {
638 if (strncasecmp(cp, compat, strlen(compat)) == 0)
639 return 1;
640 l = strlen(cp) + 1;
641 cp += l;
642 cplen -= l;
643 }
644
645 return 0;
646}
647
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000648static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
649 unsigned long align)
650{
651 void *res;
652
653 *mem = _ALIGN(*mem, align);
654 res = (void *)*mem;
655 *mem += size;
656
657 return res;
658}
659
660static unsigned long __init unflatten_dt_node(unsigned long mem,
661 unsigned long *p,
662 struct device_node *dad,
663 struct device_node ***allnextpp,
664 unsigned long fpsize)
665{
666 struct device_node *np;
667 struct property *pp, **prev_pp = NULL;
668 char *pathp;
669 u32 tag;
670 unsigned int l, allocl;
671 int has_name = 0;
672 int new_format = 0;
673
674 tag = *((u32 *)(*p));
675 if (tag != OF_DT_BEGIN_NODE) {
676 printk("Weird tag at start of node: %x\n", tag);
677 return mem;
678 }
679 *p += 4;
680 pathp = (char *)*p;
681 l = allocl = strlen(pathp) + 1;
682 *p = _ALIGN(*p + l, 4);
683
684 /* version 0x10 has a more compact unit name here instead of the full
685 * path. we accumulate the full path size using "fpsize", we'll rebuild
686 * it later. We detect this because the first character of the name is
687 * not '/'.
688 */
689 if ((*pathp) != '/') {
690 new_format = 1;
691 if (fpsize == 0) {
692 /* root node: special case. fpsize accounts for path
693 * plus terminating zero. root node only has '/', so
694 * fpsize should be 2, but we want to avoid the first
695 * level nodes to have two '/' so we use fpsize 1 here
696 */
697 fpsize = 1;
698 allocl = 2;
699 } else {
700 /* account for '/' and path size minus terminal 0
701 * already in 'l'
702 */
703 fpsize += l;
704 allocl = fpsize;
705 }
706 }
707
708
709 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
710 __alignof__(struct device_node));
711 if (allnextpp) {
712 memset(np, 0, sizeof(*np));
713 np->full_name = ((char*)np) + sizeof(struct device_node);
714 if (new_format) {
715 char *p = np->full_name;
716 /* rebuild full path for new format */
717 if (dad && dad->parent) {
718 strcpy(p, dad->full_name);
719#ifdef DEBUG
720 if ((strlen(p) + l + 1) != allocl) {
721 DBG("%s: p: %d, l: %d, a: %d\n",
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100722 pathp, (int)strlen(p), l, allocl);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000723 }
724#endif
725 p += strlen(p);
726 }
727 *(p++) = '/';
728 memcpy(p, pathp, l);
729 } else
730 memcpy(np->full_name, pathp, l);
731 prev_pp = &np->properties;
732 **allnextpp = np;
733 *allnextpp = &np->allnext;
734 if (dad != NULL) {
735 np->parent = dad;
736 /* we temporarily use the next field as `last_child'*/
737 if (dad->next == 0)
738 dad->child = np;
739 else
740 dad->next->sibling = np;
741 dad->next = np;
742 }
743 kref_init(&np->kref);
744 }
745 while(1) {
746 u32 sz, noff;
747 char *pname;
748
749 tag = *((u32 *)(*p));
750 if (tag == OF_DT_NOP) {
751 *p += 4;
752 continue;
753 }
754 if (tag != OF_DT_PROP)
755 break;
756 *p += 4;
757 sz = *((u32 *)(*p));
758 noff = *((u32 *)((*p) + 4));
759 *p += 8;
760 if (initial_boot_params->version < 0x10)
761 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
762
763 pname = find_flat_dt_string(noff);
764 if (pname == NULL) {
765 printk("Can't find property name in list !\n");
766 break;
767 }
768 if (strcmp(pname, "name") == 0)
769 has_name = 1;
770 l = strlen(pname) + 1;
771 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
772 __alignof__(struct property));
773 if (allnextpp) {
774 if (strcmp(pname, "linux,phandle") == 0) {
775 np->node = *((u32 *)*p);
776 if (np->linux_phandle == 0)
777 np->linux_phandle = np->node;
778 }
779 if (strcmp(pname, "ibm,phandle") == 0)
780 np->linux_phandle = *((u32 *)*p);
781 pp->name = pname;
782 pp->length = sz;
783 pp->value = (void *)*p;
784 *prev_pp = pp;
785 prev_pp = &pp->next;
786 }
787 *p = _ALIGN((*p) + sz, 4);
788 }
789 /* with version 0x10 we may not have the name property, recreate
790 * it here from the unit name if absent
791 */
792 if (!has_name) {
793 char *p = pathp, *ps = pathp, *pa = NULL;
794 int sz;
795
796 while (*p) {
797 if ((*p) == '@')
798 pa = p;
799 if ((*p) == '/')
800 ps = p + 1;
801 p++;
802 }
803 if (pa < ps)
804 pa = p;
805 sz = (pa - ps) + 1;
806 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
807 __alignof__(struct property));
808 if (allnextpp) {
809 pp->name = "name";
810 pp->length = sz;
811 pp->value = (unsigned char *)(pp + 1);
812 *prev_pp = pp;
813 prev_pp = &pp->next;
814 memcpy(pp->value, ps, sz - 1);
815 ((char *)pp->value)[sz - 1] = 0;
816 DBG("fixed up name for %s -> %s\n", pathp, pp->value);
817 }
818 }
819 if (allnextpp) {
820 *prev_pp = NULL;
821 np->name = get_property(np, "name", NULL);
822 np->type = get_property(np, "device_type", NULL);
823
824 if (!np->name)
825 np->name = "<NULL>";
826 if (!np->type)
827 np->type = "<NULL>";
828 }
829 while (tag == OF_DT_BEGIN_NODE) {
830 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
831 tag = *((u32 *)(*p));
832 }
833 if (tag != OF_DT_END_NODE) {
834 printk("Weird tag at end of node: %x\n", tag);
835 return mem;
836 }
837 *p += 4;
838 return mem;
839}
840
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000841static int __init early_parse_mem(char *p)
842{
843 if (!p)
844 return 1;
845
846 memory_limit = PAGE_ALIGN(memparse(p, &p));
847 DBG("memory limit = 0x%lx\n", memory_limit);
848
849 return 0;
850}
851early_param("mem", early_parse_mem);
852
853/*
854 * The device tree may be allocated below our memory limit, or inside the
855 * crash kernel region for kdump. If so, move it out now.
856 */
857static void move_device_tree(void)
858{
859 unsigned long start, size;
860 void *p;
861
862 DBG("-> move_device_tree\n");
863
864 start = __pa(initial_boot_params);
865 size = initial_boot_params->totalsize;
866
867 if ((memory_limit && (start + size) > memory_limit) ||
868 overlaps_crashkernel(start, size)) {
869 p = __va(lmb_alloc_base(size, PAGE_SIZE, lmb.rmo_size));
870 memcpy(p, initial_boot_params, size);
871 initial_boot_params = (struct boot_param_header *)p;
872 DBG("Moved device tree to 0x%p\n", p);
873 }
874
875 DBG("<- move_device_tree\n");
876}
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000877
878/**
879 * unflattens the device-tree passed by the firmware, creating the
880 * tree of struct device_node. It also fills the "name" and "type"
881 * pointers of the nodes so the normal device-tree walking functions
882 * can be used (this used to be done by finish_device_tree)
883 */
884void __init unflatten_device_tree(void)
885{
886 unsigned long start, mem, size;
887 struct device_node **allnextp = &allnodes;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000888
889 DBG(" -> unflatten_device_tree()\n");
890
891 /* First pass, scan for size */
892 start = ((unsigned long)initial_boot_params) +
893 initial_boot_params->off_dt_struct;
894 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
895 size = (size | 3) + 1;
896
897 DBG(" size is %lx, allocating...\n", size);
898
899 /* Allocate memory for the expanded device tree */
900 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000901 mem = (unsigned long) __va(mem);
902
903 ((u32 *)mem)[size / 4] = 0xdeadbeef;
904
905 DBG(" unflattening %lx...\n", mem);
906
907 /* Second pass, do actual unflattening */
908 start = ((unsigned long)initial_boot_params) +
909 initial_boot_params->off_dt_struct;
910 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
911 if (*((u32 *)start) != OF_DT_END)
912 printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
913 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
914 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
915 ((u32 *)mem)[size / 4] );
916 *allnextp = NULL;
917
918 /* Get pointer to OF "/chosen" node for use everywhere */
919 of_chosen = of_find_node_by_path("/chosen");
Paul Mackerrasa575b802005-10-23 17:23:21 +1000920 if (of_chosen == NULL)
921 of_chosen = of_find_node_by_path("/chosen@0");
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000922
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000923 DBG(" <- unflatten_device_tree()\n");
924}
925
Paul Mackerrasd2058192006-05-03 23:04:37 +1000926/*
927 * ibm,pa-features is a per-cpu property that contains a string of
928 * attribute descriptors, each of which has a 2 byte header plus up
929 * to 254 bytes worth of processor attribute bits. First header
930 * byte specifies the number of bytes following the header.
931 * Second header byte is an "attribute-specifier" type, of which
932 * zero is the only currently-defined value.
933 * Implementation: Pass in the byte and bit offset for the feature
934 * that we are interested in. The function will return -1 if the
935 * pa-features property is missing, or a 1/0 to indicate if the feature
936 * is supported/not supported. Note that the bit numbers are
937 * big-endian to match the definition in PAPR.
938 */
939static struct ibm_pa_feature {
940 unsigned long cpu_features; /* CPU_FTR_xxx bit */
941 unsigned int cpu_user_ftrs; /* PPC_FEATURE_xxx bit */
942 unsigned char pabyte; /* byte number in ibm,pa-features */
943 unsigned char pabit; /* bit number (big-endian) */
944 unsigned char invert; /* if 1, pa bit set => clear feature */
945} ibm_pa_features[] __initdata = {
946 {0, PPC_FEATURE_HAS_MMU, 0, 0, 0},
947 {0, PPC_FEATURE_HAS_FPU, 0, 1, 0},
948 {CPU_FTR_SLB, 0, 0, 2, 0},
949 {CPU_FTR_CTRL, 0, 0, 3, 0},
950 {CPU_FTR_NOEXECUTE, 0, 0, 6, 0},
951 {CPU_FTR_NODSISRALIGN, 0, 1, 1, 1},
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000952#if 0
953 /* put this back once we know how to test if firmware does 64k IO */
Paul Mackerrasd2058192006-05-03 23:04:37 +1000954 {CPU_FTR_CI_LARGE_PAGE, 0, 1, 2, 0},
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000955#endif
Paul Mackerrasd2058192006-05-03 23:04:37 +1000956};
957
958static void __init check_cpu_pa_features(unsigned long node)
959{
960 unsigned char *pa_ftrs;
961 unsigned long len, tablelen, i, bit;
962
963 pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
964 if (pa_ftrs == NULL)
965 return;
966
967 /* find descriptor with type == 0 */
968 for (;;) {
969 if (tablelen < 3)
970 return;
971 len = 2 + pa_ftrs[0];
972 if (tablelen < len)
973 return; /* descriptor 0 not found */
974 if (pa_ftrs[1] == 0)
975 break;
976 tablelen -= len;
977 pa_ftrs += len;
978 }
979
980 /* loop over bits we know about */
981 for (i = 0; i < ARRAY_SIZE(ibm_pa_features); ++i) {
982 struct ibm_pa_feature *fp = &ibm_pa_features[i];
983
984 if (fp->pabyte >= pa_ftrs[0])
985 continue;
986 bit = (pa_ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
987 if (bit ^ fp->invert) {
988 cur_cpu_spec->cpu_features |= fp->cpu_features;
989 cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
990 } else {
991 cur_cpu_spec->cpu_features &= ~fp->cpu_features;
992 cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
993 }
994 }
995}
996
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000997static int __init early_init_dt_scan_cpus(unsigned long node,
Anton Blanchard4df20462006-03-25 17:25:17 +1100998 const char *uname, int depth,
999 void *data)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001000{
Anton Blanchard4df20462006-03-25 17:25:17 +11001001 static int logical_cpuid = 0;
1002 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
Stephen Rothwell4d177fb2006-03-28 17:14:44 +11001003#ifdef CONFIG_ALTIVEC
1004 u32 *prop;
1005#endif
1006 u32 *intserv;
Anton Blanchard4df20462006-03-25 17:25:17 +11001007 int i, nthreads;
1008 unsigned long len;
1009 int found = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001010
1011 /* We are scanning "cpu" nodes only */
1012 if (type == NULL || strcmp(type, "cpu") != 0)
1013 return 0;
1014
Anton Blanchard4df20462006-03-25 17:25:17 +11001015 /* Get physical cpuid */
1016 intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
1017 if (intserv) {
1018 nthreads = len / sizeof(int);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001019 } else {
Anton Blanchard4df20462006-03-25 17:25:17 +11001020 intserv = of_get_flat_dt_prop(node, "reg", NULL);
1021 nthreads = 1;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001022 }
Anton Blanchard4df20462006-03-25 17:25:17 +11001023
1024 /*
1025 * Now see if any of these threads match our boot cpu.
1026 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
1027 */
1028 for (i = 0; i < nthreads; i++) {
1029 /*
1030 * version 2 of the kexec param format adds the phys cpuid of
1031 * booted proc.
1032 */
1033 if (initial_boot_params && initial_boot_params->version >= 2) {
1034 if (intserv[i] ==
1035 initial_boot_params->boot_cpuid_phys) {
1036 found = 1;
1037 break;
1038 }
1039 } else {
1040 /*
1041 * Check if it's the boot-cpu, set it's hw index now,
1042 * unfortunately this format did not support booting
1043 * off secondary threads.
1044 */
1045 if (of_get_flat_dt_prop(node,
1046 "linux,boot-cpu", NULL) != NULL) {
1047 found = 1;
1048 break;
1049 }
1050 }
1051
1052#ifdef CONFIG_SMP
1053 /* logical cpu id is always 0 on UP kernels */
1054 logical_cpuid++;
1055#endif
1056 }
1057
1058 if (found) {
1059 DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
1060 intserv[i]);
1061 boot_cpuid = logical_cpuid;
1062 set_hard_smp_processor_id(boot_cpuid, intserv[i]);
1063 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001064
1065#ifdef CONFIG_ALTIVEC
1066 /* Check if we have a VMX and eventually update CPU features */
Stephen Rothwell676e2492005-11-10 14:16:21 +11001067 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,vmx", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001068 if (prop && (*prop) > 0) {
1069 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
1070 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
1071 }
1072
1073 /* Same goes for Apple's "altivec" property */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001074 prop = (u32 *)of_get_flat_dt_prop(node, "altivec", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001075 if (prop) {
1076 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
1077 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
1078 }
1079#endif /* CONFIG_ALTIVEC */
1080
Paul Mackerrasd2058192006-05-03 23:04:37 +10001081 check_cpu_pa_features(node);
1082
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001083#ifdef CONFIG_PPC_PSERIES
Anton Blanchard4df20462006-03-25 17:25:17 +11001084 if (nthreads > 1)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001085 cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
Anton Blanchard4df20462006-03-25 17:25:17 +11001086 else
1087 cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001088#endif
1089
1090 return 0;
1091}
1092
1093static int __init early_init_dt_scan_chosen(unsigned long node,
1094 const char *uname, int depth, void *data)
1095{
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001096 unsigned long *lprop;
Kumar Gala329dda02006-02-24 10:54:52 -06001097 unsigned long l;
1098 char *p;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001099
1100 DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
1101
Paul Mackerrasa575b802005-10-23 17:23:21 +10001102 if (depth != 1 ||
1103 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001104 return 0;
1105
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001106#ifdef CONFIG_PPC64
1107 /* check if iommu is forced on or off */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001108 if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001109 iommu_is_off = 1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001110 if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001111 iommu_force_on = 1;
1112#endif
1113
Michael Ellerman2babf5c2006-05-17 18:00:46 +10001114 /* mem=x on the command line is the preferred mechanism */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001115 lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001116 if (lprop)
1117 memory_limit = *lprop;
1118
1119#ifdef CONFIG_PPC64
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001120 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001121 if (lprop)
1122 tce_alloc_start = *lprop;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001123 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001124 if (lprop)
1125 tce_alloc_end = *lprop;
1126#endif
1127
Michael Ellermandcee3032005-12-04 18:39:48 +11001128#ifdef CONFIG_KEXEC
1129 lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
1130 if (lprop)
1131 crashk_res.start = *lprop;
1132
1133 lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
1134 if (lprop)
1135 crashk_res.end = crashk_res.start + *lprop - 1;
1136#endif
1137
Kumar Gala329dda02006-02-24 10:54:52 -06001138 /* Retreive command line */
1139 p = of_get_flat_dt_prop(node, "bootargs", &l);
1140 if (p != NULL && l > 0)
1141 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
1142
1143#ifdef CONFIG_CMDLINE
1144 if (l == 0 || (l == 1 && (*p) == 0))
1145 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1146#endif /* CONFIG_CMDLINE */
1147
1148 DBG("Command line is: %s\n", cmd_line);
1149
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001150 /* break now */
1151 return 1;
1152}
1153
1154static int __init early_init_dt_scan_root(unsigned long node,
1155 const char *uname, int depth, void *data)
1156{
1157 u32 *prop;
1158
1159 if (depth != 0)
1160 return 0;
1161
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001162 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001163 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
1164 DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
1165
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001166 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001167 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
1168 DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
1169
1170 /* break now */
1171 return 1;
1172}
1173
1174static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
1175{
1176 cell_t *p = *cellp;
1177 unsigned long r;
1178
1179 /* Ignore more than 2 cells */
1180 while (s > sizeof(unsigned long) / 4) {
1181 p++;
1182 s--;
1183 }
1184 r = *p++;
1185#ifdef CONFIG_PPC64
1186 if (s > 1) {
1187 r <<= 32;
1188 r |= *(p++);
1189 s--;
1190 }
1191#endif
1192
1193 *cellp = p;
1194 return r;
1195}
1196
1197
1198static int __init early_init_dt_scan_memory(unsigned long node,
1199 const char *uname, int depth, void *data)
1200{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001201 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001202 cell_t *reg, *endp;
1203 unsigned long l;
1204
1205 /* We are scanning "memory" nodes only */
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001206 if (type == NULL) {
1207 /*
1208 * The longtrail doesn't have a device_type on the
1209 * /memory node, so look for the node called /memory@0.
1210 */
1211 if (depth != 1 || strcmp(uname, "memory@0") != 0)
1212 return 0;
1213 } else if (strcmp(type, "memory") != 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001214 return 0;
1215
Michael Ellermanba759482005-12-04 18:39:55 +11001216 reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
1217 if (reg == NULL)
1218 reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001219 if (reg == NULL)
1220 return 0;
1221
1222 endp = reg + (l / sizeof(cell_t));
1223
Michael Ellerman358c86f2005-11-03 15:39:09 +11001224 DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001225 uname, l, reg[0], reg[1], reg[2], reg[3]);
1226
1227 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1228 unsigned long base, size;
1229
1230 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
1231 size = dt_mem_next_cell(dt_root_size_cells, &reg);
1232
1233 if (size == 0)
1234 continue;
1235 DBG(" - %lx , %lx\n", base, size);
1236#ifdef CONFIG_PPC64
1237 if (iommu_is_off) {
1238 if (base >= 0x80000000ul)
1239 continue;
1240 if ((base + size) > 0x80000000ul)
1241 size = 0x80000000ul - base;
1242 }
1243#endif
1244 lmb_add(base, size);
1245 }
1246 return 0;
1247}
1248
1249static void __init early_reserve_mem(void)
1250{
Kumar Galacbbcf342006-01-11 17:57:13 -06001251 u64 base, size;
1252 u64 *reserve_map;
Jon Loeliger8a300882006-06-17 17:51:09 -05001253 unsigned long self_base;
1254 unsigned long self_size;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001255
Kumar Galacbbcf342006-01-11 17:57:13 -06001256 reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001257 initial_boot_params->off_mem_rsvmap);
Jimi Xenidis4d1f3f22006-05-18 17:03:05 -05001258
1259 /* before we do anything, lets reserve the dt blob */
Jon Loeliger8a300882006-06-17 17:51:09 -05001260 self_base = __pa((unsigned long)initial_boot_params);
1261 self_size = initial_boot_params->totalsize;
1262 lmb_reserve(self_base, self_size);
Jimi Xenidis4d1f3f22006-05-18 17:03:05 -05001263
Kumar Galacbbcf342006-01-11 17:57:13 -06001264#ifdef CONFIG_PPC32
1265 /*
1266 * Handle the case where we might be booting from an old kexec
1267 * image that setup the mem_rsvmap as pairs of 32-bit values
1268 */
1269 if (*reserve_map > 0xffffffffull) {
1270 u32 base_32, size_32;
1271 u32 *reserve_map_32 = (u32 *)reserve_map;
1272
1273 while (1) {
1274 base_32 = *(reserve_map_32++);
1275 size_32 = *(reserve_map_32++);
1276 if (size_32 == 0)
1277 break;
Jon Loeliger8a300882006-06-17 17:51:09 -05001278 /* skip if the reservation is for the blob */
1279 if (base_32 == self_base && size_32 == self_size)
1280 continue;
Kumar Gala329dda02006-02-24 10:54:52 -06001281 DBG("reserving: %x -> %x\n", base_32, size_32);
Kumar Galacbbcf342006-01-11 17:57:13 -06001282 lmb_reserve(base_32, size_32);
1283 }
1284 return;
1285 }
1286#endif
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001287 while (1) {
1288 base = *(reserve_map++);
1289 size = *(reserve_map++);
1290 if (size == 0)
1291 break;
Jon Loeliger8a300882006-06-17 17:51:09 -05001292 /* skip if the reservation is for the blob */
1293 if (base == self_base && size == self_size)
1294 continue;
Kumar Galacbbcf342006-01-11 17:57:13 -06001295 DBG("reserving: %llx -> %llx\n", base, size);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001296 lmb_reserve(base, size);
1297 }
1298
1299#if 0
1300 DBG("memory reserved, lmbs :\n");
1301 lmb_dump_all();
1302#endif
1303}
1304
1305void __init early_init_devtree(void *params)
1306{
1307 DBG(" -> early_init_devtree()\n");
1308
1309 /* Setup flat device-tree pointer */
1310 initial_boot_params = params;
1311
Michael Ellerman458148c2006-06-23 18:20:13 +10001312#ifdef CONFIG_PPC_RTAS
1313 /* Some machines might need RTAS info for debugging, grab it now. */
1314 of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
1315#endif
1316
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001317 /* Retrieve various informations from the /chosen node of the
1318 * device-tree, including the platform type, initrd location and
1319 * size, TCE reserve, and more ...
1320 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001321 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001322
1323 /* Scan memory nodes and rebuild LMBs */
1324 lmb_init();
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001325 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1326 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
Michael Ellerman846f77b2006-05-17 18:00:45 +10001327
1328 /* Save command line for /proc/cmdline and then parse parameters */
1329 strlcpy(saved_command_line, cmd_line, COMMAND_LINE_SIZE);
1330 parse_early_param();
1331
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001332 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
Michael Ellerman0cc47462005-12-04 18:39:37 +11001333 lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
Michael Ellerman47310412006-05-17 18:00:49 +10001334 reserve_kdump_trampoline();
Michael Ellerman35dd5432006-05-18 11:16:11 +10001335 reserve_crashkernel();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001336 early_reserve_mem();
1337
Michael Ellerman2babf5c2006-05-17 18:00:46 +10001338 lmb_enforce_memory_limit(memory_limit);
1339 lmb_analyze();
1340
1341 DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
1342
1343 /* We may need to relocate the flat tree, do it now.
1344 * FIXME .. and the initrd too? */
1345 move_device_tree();
1346
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001347 DBG("Scanning CPUs ...\n");
1348
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001349 /* Retreive CPU related informations from the flat tree
1350 * (altivec support, boot CPU ID, ...)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001351 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001352 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001353
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001354 DBG(" <- early_init_devtree()\n");
1355}
1356
1357#undef printk
1358
1359int
1360prom_n_addr_cells(struct device_node* np)
1361{
1362 int* ip;
1363 do {
1364 if (np->parent)
1365 np = np->parent;
1366 ip = (int *) get_property(np, "#address-cells", NULL);
1367 if (ip != NULL)
1368 return *ip;
1369 } while (np->parent);
1370 /* No #address-cells property for the root node, default to 1 */
1371 return 1;
1372}
Paul Mackerras1dfc6772005-11-14 17:30:40 +11001373EXPORT_SYMBOL(prom_n_addr_cells);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001374
1375int
1376prom_n_size_cells(struct device_node* np)
1377{
1378 int* ip;
1379 do {
1380 if (np->parent)
1381 np = np->parent;
1382 ip = (int *) get_property(np, "#size-cells", NULL);
1383 if (ip != NULL)
1384 return *ip;
1385 } while (np->parent);
1386 /* No #size-cells property for the root node, default to 1 */
1387 return 1;
1388}
Paul Mackerras1dfc6772005-11-14 17:30:40 +11001389EXPORT_SYMBOL(prom_n_size_cells);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001390
1391/**
1392 * Work out the sense (active-low level / active-high edge)
1393 * of each interrupt from the device tree.
1394 */
1395void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
1396{
1397 struct device_node *np;
1398 int i, j;
1399
1400 /* default to level-triggered */
Paul Mackerras6d0124f2005-10-26 17:19:06 +10001401 memset(senses, IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, max - off);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001402
1403 for (np = allnodes; np != 0; np = np->allnext) {
1404 for (j = 0; j < np->n_intrs; j++) {
1405 i = np->intrs[j].line;
1406 if (i >= off && i < max)
Paul Mackerras6d0124f2005-10-26 17:19:06 +10001407 senses[i-off] = np->intrs[j].sense;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001408 }
1409 }
1410}
1411
1412/**
1413 * Construct and return a list of the device_nodes with a given name.
1414 */
1415struct device_node *find_devices(const char *name)
1416{
1417 struct device_node *head, **prevp, *np;
1418
1419 prevp = &head;
1420 for (np = allnodes; np != 0; np = np->allnext) {
1421 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
1422 *prevp = np;
1423 prevp = &np->next;
1424 }
1425 }
1426 *prevp = NULL;
1427 return head;
1428}
1429EXPORT_SYMBOL(find_devices);
1430
1431/**
1432 * Construct and return a list of the device_nodes with a given type.
1433 */
1434struct device_node *find_type_devices(const char *type)
1435{
1436 struct device_node *head, **prevp, *np;
1437
1438 prevp = &head;
1439 for (np = allnodes; np != 0; np = np->allnext) {
1440 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
1441 *prevp = np;
1442 prevp = &np->next;
1443 }
1444 }
1445 *prevp = NULL;
1446 return head;
1447}
1448EXPORT_SYMBOL(find_type_devices);
1449
1450/**
1451 * Returns all nodes linked together
1452 */
1453struct device_node *find_all_nodes(void)
1454{
1455 struct device_node *head, **prevp, *np;
1456
1457 prevp = &head;
1458 for (np = allnodes; np != 0; np = np->allnext) {
1459 *prevp = np;
1460 prevp = &np->next;
1461 }
1462 *prevp = NULL;
1463 return head;
1464}
1465EXPORT_SYMBOL(find_all_nodes);
1466
1467/** Checks if the given "compat" string matches one of the strings in
1468 * the device's "compatible" property
1469 */
1470int device_is_compatible(struct device_node *device, const char *compat)
1471{
1472 const char* cp;
1473 int cplen, l;
1474
1475 cp = (char *) get_property(device, "compatible", &cplen);
1476 if (cp == NULL)
1477 return 0;
1478 while (cplen > 0) {
1479 if (strncasecmp(cp, compat, strlen(compat)) == 0)
1480 return 1;
1481 l = strlen(cp) + 1;
1482 cp += l;
1483 cplen -= l;
1484 }
1485
1486 return 0;
1487}
1488EXPORT_SYMBOL(device_is_compatible);
1489
1490
1491/**
1492 * Indicates whether the root node has a given value in its
1493 * compatible property.
1494 */
1495int machine_is_compatible(const char *compat)
1496{
1497 struct device_node *root;
1498 int rc = 0;
1499
1500 root = of_find_node_by_path("/");
1501 if (root) {
1502 rc = device_is_compatible(root, compat);
1503 of_node_put(root);
1504 }
1505 return rc;
1506}
1507EXPORT_SYMBOL(machine_is_compatible);
1508
1509/**
1510 * Construct and return a list of the device_nodes with a given type
1511 * and compatible property.
1512 */
1513struct device_node *find_compatible_devices(const char *type,
1514 const char *compat)
1515{
1516 struct device_node *head, **prevp, *np;
1517
1518 prevp = &head;
1519 for (np = allnodes; np != 0; np = np->allnext) {
1520 if (type != NULL
1521 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1522 continue;
1523 if (device_is_compatible(np, compat)) {
1524 *prevp = np;
1525 prevp = &np->next;
1526 }
1527 }
1528 *prevp = NULL;
1529 return head;
1530}
1531EXPORT_SYMBOL(find_compatible_devices);
1532
1533/**
1534 * Find the device_node with a given full_name.
1535 */
1536struct device_node *find_path_device(const char *path)
1537{
1538 struct device_node *np;
1539
1540 for (np = allnodes; np != 0; np = np->allnext)
1541 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1542 return np;
1543 return NULL;
1544}
1545EXPORT_SYMBOL(find_path_device);
1546
1547/*******
1548 *
1549 * New implementation of the OF "find" APIs, return a refcounted
1550 * object, call of_node_put() when done. The device tree and list
1551 * are protected by a rw_lock.
1552 *
1553 * Note that property management will need some locking as well,
1554 * this isn't dealt with yet.
1555 *
1556 *******/
1557
1558/**
1559 * of_find_node_by_name - Find a node by its "name" property
1560 * @from: The node to start searching from or NULL, the node
1561 * you pass will not be searched, only the next one
1562 * will; typically, you pass what the previous call
1563 * returned. of_node_put() will be called on it
1564 * @name: The name string to match against
1565 *
1566 * Returns a node pointer with refcount incremented, use
1567 * of_node_put() on it when done.
1568 */
1569struct device_node *of_find_node_by_name(struct device_node *from,
1570 const char *name)
1571{
1572 struct device_node *np;
1573
1574 read_lock(&devtree_lock);
1575 np = from ? from->allnext : allnodes;
Olaf Hering090db7c2006-02-04 12:44:56 +01001576 for (; np != NULL; np = np->allnext)
1577 if (np->name != NULL && strcasecmp(np->name, name) == 0
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001578 && of_node_get(np))
1579 break;
1580 if (from)
1581 of_node_put(from);
1582 read_unlock(&devtree_lock);
1583 return np;
1584}
1585EXPORT_SYMBOL(of_find_node_by_name);
1586
1587/**
1588 * of_find_node_by_type - Find a node by its "device_type" property
1589 * @from: The node to start searching from or NULL, the node
1590 * you pass will not be searched, only the next one
1591 * will; typically, you pass what the previous call
1592 * returned. of_node_put() will be called on it
1593 * @name: The type string to match against
1594 *
1595 * Returns a node pointer with refcount incremented, use
1596 * of_node_put() on it when done.
1597 */
1598struct device_node *of_find_node_by_type(struct device_node *from,
1599 const char *type)
1600{
1601 struct device_node *np;
1602
1603 read_lock(&devtree_lock);
1604 np = from ? from->allnext : allnodes;
1605 for (; np != 0; np = np->allnext)
1606 if (np->type != 0 && strcasecmp(np->type, type) == 0
1607 && of_node_get(np))
1608 break;
1609 if (from)
1610 of_node_put(from);
1611 read_unlock(&devtree_lock);
1612 return np;
1613}
1614EXPORT_SYMBOL(of_find_node_by_type);
1615
1616/**
1617 * of_find_compatible_node - Find a node based on type and one of the
1618 * tokens in its "compatible" property
1619 * @from: The node to start searching from or NULL, the node
1620 * you pass will not be searched, only the next one
1621 * will; typically, you pass what the previous call
1622 * returned. of_node_put() will be called on it
1623 * @type: The type string to match "device_type" or NULL to ignore
1624 * @compatible: The string to match to one of the tokens in the device
1625 * "compatible" list.
1626 *
1627 * Returns a node pointer with refcount incremented, use
1628 * of_node_put() on it when done.
1629 */
1630struct device_node *of_find_compatible_node(struct device_node *from,
1631 const char *type, const char *compatible)
1632{
1633 struct device_node *np;
1634
1635 read_lock(&devtree_lock);
1636 np = from ? from->allnext : allnodes;
1637 for (; np != 0; np = np->allnext) {
1638 if (type != NULL
1639 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1640 continue;
1641 if (device_is_compatible(np, compatible) && of_node_get(np))
1642 break;
1643 }
1644 if (from)
1645 of_node_put(from);
1646 read_unlock(&devtree_lock);
1647 return np;
1648}
1649EXPORT_SYMBOL(of_find_compatible_node);
1650
1651/**
1652 * of_find_node_by_path - Find a node matching a full OF path
1653 * @path: The full path to match
1654 *
1655 * Returns a node pointer with refcount incremented, use
1656 * of_node_put() on it when done.
1657 */
1658struct device_node *of_find_node_by_path(const char *path)
1659{
1660 struct device_node *np = allnodes;
1661
1662 read_lock(&devtree_lock);
1663 for (; np != 0; np = np->allnext) {
1664 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
1665 && of_node_get(np))
1666 break;
1667 }
1668 read_unlock(&devtree_lock);
1669 return np;
1670}
1671EXPORT_SYMBOL(of_find_node_by_path);
1672
1673/**
1674 * of_find_node_by_phandle - Find a node given a phandle
1675 * @handle: phandle of the node to find
1676 *
1677 * Returns a node pointer with refcount incremented, use
1678 * of_node_put() on it when done.
1679 */
1680struct device_node *of_find_node_by_phandle(phandle handle)
1681{
1682 struct device_node *np;
1683
1684 read_lock(&devtree_lock);
1685 for (np = allnodes; np != 0; np = np->allnext)
1686 if (np->linux_phandle == handle)
1687 break;
1688 if (np)
1689 of_node_get(np);
1690 read_unlock(&devtree_lock);
1691 return np;
1692}
1693EXPORT_SYMBOL(of_find_node_by_phandle);
1694
1695/**
1696 * of_find_all_nodes - Get next node in global list
1697 * @prev: Previous node or NULL to start iteration
1698 * of_node_put() will be called on it
1699 *
1700 * Returns a node pointer with refcount incremented, use
1701 * of_node_put() on it when done.
1702 */
1703struct device_node *of_find_all_nodes(struct device_node *prev)
1704{
1705 struct device_node *np;
1706
1707 read_lock(&devtree_lock);
1708 np = prev ? prev->allnext : allnodes;
1709 for (; np != 0; np = np->allnext)
1710 if (of_node_get(np))
1711 break;
1712 if (prev)
1713 of_node_put(prev);
1714 read_unlock(&devtree_lock);
1715 return np;
1716}
1717EXPORT_SYMBOL(of_find_all_nodes);
1718
1719/**
1720 * of_get_parent - Get a node's parent if any
1721 * @node: Node to get parent
1722 *
1723 * Returns a node pointer with refcount incremented, use
1724 * of_node_put() on it when done.
1725 */
1726struct device_node *of_get_parent(const struct device_node *node)
1727{
1728 struct device_node *np;
1729
1730 if (!node)
1731 return NULL;
1732
1733 read_lock(&devtree_lock);
1734 np = of_node_get(node->parent);
1735 read_unlock(&devtree_lock);
1736 return np;
1737}
1738EXPORT_SYMBOL(of_get_parent);
1739
1740/**
1741 * of_get_next_child - Iterate a node childs
1742 * @node: parent node
1743 * @prev: previous child of the parent node, or NULL to get first
1744 *
1745 * Returns a node pointer with refcount incremented, use
1746 * of_node_put() on it when done.
1747 */
1748struct device_node *of_get_next_child(const struct device_node *node,
1749 struct device_node *prev)
1750{
1751 struct device_node *next;
1752
1753 read_lock(&devtree_lock);
1754 next = prev ? prev->sibling : node->child;
1755 for (; next != 0; next = next->sibling)
1756 if (of_node_get(next))
1757 break;
1758 if (prev)
1759 of_node_put(prev);
1760 read_unlock(&devtree_lock);
1761 return next;
1762}
1763EXPORT_SYMBOL(of_get_next_child);
1764
1765/**
1766 * of_node_get - Increment refcount of a node
1767 * @node: Node to inc refcount, NULL is supported to
1768 * simplify writing of callers
1769 *
1770 * Returns node.
1771 */
1772struct device_node *of_node_get(struct device_node *node)
1773{
1774 if (node)
1775 kref_get(&node->kref);
1776 return node;
1777}
1778EXPORT_SYMBOL(of_node_get);
1779
1780static inline struct device_node * kref_to_device_node(struct kref *kref)
1781{
1782 return container_of(kref, struct device_node, kref);
1783}
1784
1785/**
1786 * of_node_release - release a dynamically allocated node
1787 * @kref: kref element of the node to be released
1788 *
1789 * In of_node_put() this function is passed to kref_put()
1790 * as the destructor.
1791 */
1792static void of_node_release(struct kref *kref)
1793{
1794 struct device_node *node = kref_to_device_node(kref);
1795 struct property *prop = node->properties;
1796
1797 if (!OF_IS_DYNAMIC(node))
1798 return;
1799 while (prop) {
1800 struct property *next = prop->next;
1801 kfree(prop->name);
1802 kfree(prop->value);
1803 kfree(prop);
1804 prop = next;
Dave C Boutcher088186d2006-01-12 16:08:27 -06001805
1806 if (!prop) {
1807 prop = node->deadprops;
1808 node->deadprops = NULL;
1809 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001810 }
1811 kfree(node->intrs);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001812 kfree(node->full_name);
1813 kfree(node->data);
1814 kfree(node);
1815}
1816
1817/**
1818 * of_node_put - Decrement refcount of a node
1819 * @node: Node to dec refcount, NULL is supported to
1820 * simplify writing of callers
1821 *
1822 */
1823void of_node_put(struct device_node *node)
1824{
1825 if (node)
1826 kref_put(&node->kref, of_node_release);
1827}
1828EXPORT_SYMBOL(of_node_put);
1829
1830/*
1831 * Plug a device node into the tree and global list.
1832 */
1833void of_attach_node(struct device_node *np)
1834{
1835 write_lock(&devtree_lock);
1836 np->sibling = np->parent->child;
1837 np->allnext = allnodes;
1838 np->parent->child = np;
1839 allnodes = np;
1840 write_unlock(&devtree_lock);
1841}
1842
1843/*
1844 * "Unplug" a node from the device tree. The caller must hold
1845 * a reference to the node. The memory associated with the node
1846 * is not freed until its refcount goes to zero.
1847 */
1848void of_detach_node(const struct device_node *np)
1849{
1850 struct device_node *parent;
1851
1852 write_lock(&devtree_lock);
1853
1854 parent = np->parent;
1855
1856 if (allnodes == np)
1857 allnodes = np->allnext;
1858 else {
1859 struct device_node *prev;
1860 for (prev = allnodes;
1861 prev->allnext != np;
1862 prev = prev->allnext)
1863 ;
1864 prev->allnext = np->allnext;
1865 }
1866
1867 if (parent->child == np)
1868 parent->child = np->sibling;
1869 else {
1870 struct device_node *prevsib;
1871 for (prevsib = np->parent->child;
1872 prevsib->sibling != np;
1873 prevsib = prevsib->sibling)
1874 ;
1875 prevsib->sibling = np->sibling;
1876 }
1877
1878 write_unlock(&devtree_lock);
1879}
1880
1881#ifdef CONFIG_PPC_PSERIES
1882/*
1883 * Fix up the uninitialized fields in a new device node:
1884 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1885 *
1886 * A lot of boot-time code is duplicated here, because functions such
1887 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
1888 * slab allocator.
1889 *
1890 * This should probably be split up into smaller chunks.
1891 */
1892
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001893static int of_finish_dynamic_node(struct device_node *node)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001894{
1895 struct device_node *parent = of_get_parent(node);
1896 int err = 0;
1897 phandle *ibm_phandle;
1898
1899 node->name = get_property(node, "name", NULL);
1900 node->type = get_property(node, "device_type", NULL);
1901
1902 if (!parent) {
1903 err = -ENODEV;
1904 goto out;
1905 }
1906
1907 /* We don't support that function on PowerMac, at least
1908 * not yet
1909 */
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001910 if (machine_is(powermac))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001911 return -ENODEV;
1912
1913 /* fix up new node's linux_phandle field */
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001914 if ((ibm_phandle = (unsigned int *)get_property(node,
1915 "ibm,phandle", NULL)))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001916 node->linux_phandle = *ibm_phandle;
1917
1918out:
1919 of_node_put(parent);
1920 return err;
1921}
1922
1923static int prom_reconfig_notifier(struct notifier_block *nb,
1924 unsigned long action, void *node)
1925{
1926 int err;
1927
1928 switch (action) {
1929 case PSERIES_RECONFIG_ADD:
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001930 err = of_finish_dynamic_node(node);
1931 if (!err)
1932 finish_node(node, NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001933 if (err < 0) {
1934 printk(KERN_ERR "finish_node returned %d\n", err);
1935 err = NOTIFY_BAD;
1936 }
1937 break;
1938 default:
1939 err = NOTIFY_DONE;
1940 break;
1941 }
1942 return err;
1943}
1944
1945static struct notifier_block prom_reconfig_nb = {
1946 .notifier_call = prom_reconfig_notifier,
1947 .priority = 10, /* This one needs to run first */
1948};
1949
1950static int __init prom_reconfig_setup(void)
1951{
1952 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1953}
1954__initcall(prom_reconfig_setup);
1955#endif
1956
Dave C Boutcherecaa8b02006-01-12 16:09:29 -06001957struct property *of_find_property(struct device_node *np, const char *name,
1958 int *lenp)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001959{
1960 struct property *pp;
1961
Dave C Boutcher088186d2006-01-12 16:08:27 -06001962 read_lock(&devtree_lock);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001963 for (pp = np->properties; pp != 0; pp = pp->next)
1964 if (strcmp(pp->name, name) == 0) {
1965 if (lenp != 0)
1966 *lenp = pp->length;
Dave C Boutcher088186d2006-01-12 16:08:27 -06001967 break;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001968 }
Dave C Boutcher088186d2006-01-12 16:08:27 -06001969 read_unlock(&devtree_lock);
1970
Dave C Boutcherecaa8b02006-01-12 16:09:29 -06001971 return pp;
1972}
1973
1974/*
1975 * Find a property with a given name for a given node
1976 * and return the value.
1977 */
1978unsigned char *get_property(struct device_node *np, const char *name,
1979 int *lenp)
1980{
1981 struct property *pp = of_find_property(np,name,lenp);
Dave C Boutcher088186d2006-01-12 16:08:27 -06001982 return pp ? pp->value : NULL;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001983}
1984EXPORT_SYMBOL(get_property);
1985
1986/*
1987 * Add a property to a node
1988 */
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11001989int prom_add_property(struct device_node* np, struct property* prop)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001990{
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11001991 struct property **next;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001992
1993 prop->next = NULL;
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11001994 write_lock(&devtree_lock);
1995 next = &np->properties;
1996 while (*next) {
1997 if (strcmp(prop->name, (*next)->name) == 0) {
1998 /* duplicate ! don't insert it */
1999 write_unlock(&devtree_lock);
2000 return -1;
2001 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002002 next = &(*next)->next;
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11002003 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002004 *next = prop;
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11002005 write_unlock(&devtree_lock);
2006
Paul Mackerras799d6042005-11-10 13:37:51 +11002007#ifdef CONFIG_PROC_DEVICETREE
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11002008 /* try to add to proc as well if it was initialized */
2009 if (np->pde)
2010 proc_device_tree_add_prop(np->pde, prop);
Paul Mackerras799d6042005-11-10 13:37:51 +11002011#endif /* CONFIG_PROC_DEVICETREE */
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11002012
2013 return 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002014}
2015
Dave C Boutcher088186d2006-01-12 16:08:27 -06002016/*
2017 * Remove a property from a node. Note that we don't actually
2018 * remove it, since we have given out who-knows-how-many pointers
2019 * to the data using get-property. Instead we just move the property
2020 * to the "dead properties" list, so it won't be found any more.
2021 */
2022int prom_remove_property(struct device_node *np, struct property *prop)
2023{
2024 struct property **next;
2025 int found = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002026
Dave C Boutcher088186d2006-01-12 16:08:27 -06002027 write_lock(&devtree_lock);
2028 next = &np->properties;
2029 while (*next) {
2030 if (*next == prop) {
2031 /* found the node */
2032 *next = prop->next;
2033 prop->next = np->deadprops;
2034 np->deadprops = prop;
2035 found = 1;
2036 break;
2037 }
2038 next = &(*next)->next;
2039 }
2040 write_unlock(&devtree_lock);
2041
2042 if (!found)
2043 return -ENODEV;
2044
2045#ifdef CONFIG_PROC_DEVICETREE
2046 /* try to remove the proc node as well */
2047 if (np->pde)
2048 proc_device_tree_remove_prop(np->pde, prop);
2049#endif /* CONFIG_PROC_DEVICETREE */
2050
2051 return 0;
2052}
2053
2054/*
2055 * Update a property in a node. Note that we don't actually
2056 * remove it, since we have given out who-knows-how-many pointers
2057 * to the data using get-property. Instead we just move the property
2058 * to the "dead properties" list, and add the new property to the
2059 * property list
2060 */
2061int prom_update_property(struct device_node *np,
2062 struct property *newprop,
2063 struct property *oldprop)
2064{
2065 struct property **next;
2066 int found = 0;
2067
2068 write_lock(&devtree_lock);
2069 next = &np->properties;
2070 while (*next) {
2071 if (*next == oldprop) {
2072 /* found the node */
2073 newprop->next = oldprop->next;
2074 *next = newprop;
2075 oldprop->next = np->deadprops;
2076 np->deadprops = oldprop;
2077 found = 1;
2078 break;
2079 }
2080 next = &(*next)->next;
2081 }
2082 write_unlock(&devtree_lock);
2083
2084 if (!found)
2085 return -ENODEV;
2086
2087#ifdef CONFIG_PROC_DEVICETREE
2088 /* try to add to proc as well if it was initialized */
2089 if (np->pde)
2090 proc_device_tree_update_prop(np->pde, newprop, oldprop);
2091#endif /* CONFIG_PROC_DEVICETREE */
2092
2093 return 0;
2094}
Michael Ellermanb68239e2006-02-03 19:05:47 +11002095
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +02002096
2097/* Find the device node for a given logical cpu number, also returns the cpu
2098 * local thread number (index in ibm,interrupt-server#s) if relevant and
2099 * asked for (non NULL)
2100 */
2101struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
2102{
2103 int hardid;
2104 struct device_node *np;
2105
2106 hardid = get_hard_smp_processor_id(cpu);
2107
2108 for_each_node_by_type(np, "cpu") {
2109 u32 *intserv;
2110 unsigned int plen, t;
2111
2112 /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
2113 * fallback to "reg" property and assume no threads
2114 */
2115 intserv = (u32 *)get_property(np, "ibm,ppc-interrupt-server#s",
2116 &plen);
2117 if (intserv == NULL) {
2118 u32 *reg = (u32 *)get_property(np, "reg", NULL);
2119 if (reg == NULL)
2120 continue;
2121 if (*reg == hardid) {
2122 if (thread)
2123 *thread = 0;
2124 return np;
2125 }
2126 } else {
2127 plen /= sizeof(u32);
2128 for (t = 0; t < plen; t++) {
2129 if (hardid == intserv[t]) {
2130 if (thread)
2131 *thread = t;
2132 return np;
2133 }
2134 }
2135 }
2136 }
2137 return NULL;
2138}
Michael Ellerman7a4571a2006-06-23 18:16:03 +10002139
2140#ifdef DEBUG
2141static struct debugfs_blob_wrapper flat_dt_blob;
2142
2143static int __init export_flat_device_tree(void)
2144{
2145 struct dentry *d;
2146
2147 d = debugfs_create_dir("powerpc", NULL);
2148 if (!d)
2149 return 1;
2150
2151 flat_dt_blob.data = initial_boot_params;
2152 flat_dt_blob.size = initial_boot_params->totalsize;
2153
2154 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
2155 d, &flat_dt_blob);
2156 if (!d)
2157 return 1;
2158
2159 return 0;
2160}
2161__initcall(export_flat_device_tree);
2162#endif