blob: 2498afeef291b2929b79a4b3b3e881c080e8f84d [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>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100033
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>
Michael Ellerman0cc47462005-12-04 18:39:37 +110041#include <asm/kdump.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100042#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>
Paul Mackerras40ef8cb2005-10-10 22:50:37 +100052#include <asm/pci-bridge.h>
Michael Ellerman2babf5c2006-05-17 18:00:46 +100053#include <asm/kexec.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100054
55#ifdef DEBUG
56#define DBG(fmt...) printk(KERN_ERR fmt)
57#else
58#define DBG(fmt...)
59#endif
60
Paul Mackerras9b6b5632005-10-06 12:06:20 +100061
Paul Mackerras9b6b5632005-10-06 12:06:20 +100062static int __initdata dt_root_addr_cells;
63static int __initdata dt_root_size_cells;
64
65#ifdef CONFIG_PPC64
Olof Johansson28897732006-04-12 21:52:33 -050066int __initdata iommu_is_off;
Paul Mackerras9b6b5632005-10-06 12:06:20 +100067int __initdata iommu_force_on;
Paul Mackerrascf00a8d2005-10-31 13:07:02 +110068unsigned long tce_alloc_start, tce_alloc_end;
Paul Mackerras9b6b5632005-10-06 12:06:20 +100069#endif
70
71typedef u32 cell_t;
72
73#if 0
74static struct boot_param_header *initial_boot_params __initdata;
75#else
76struct boot_param_header *initial_boot_params;
77#endif
78
79static struct device_node *allnodes = NULL;
80
81/* use when traversing tree through the allnext, child, sibling,
82 * or parent members of struct device_node.
83 */
84static DEFINE_RWLOCK(devtree_lock);
85
86/* export that to outside world */
87struct device_node *of_chosen;
88
89struct device_node *dflt_interrupt_controller;
90int num_interrupt_controllers;
91
Paul Mackerras9b6b5632005-10-06 12:06:20 +100092/*
93 * Wrapper for allocating memory for various data that needs to be
94 * attached to device nodes as they are processed at boot or when
95 * added to the device tree later (e.g. DLPAR). At boot there is
96 * already a region reserved so we just increment *mem_start by size;
97 * otherwise we call kmalloc.
98 */
99static void * prom_alloc(unsigned long size, unsigned long *mem_start)
100{
101 unsigned long tmp;
102
103 if (!mem_start)
104 return kmalloc(size, GFP_KERNEL);
105
106 tmp = *mem_start;
107 *mem_start += size;
108 return (void *)tmp;
109}
110
111/*
112 * Find the device_node with a given phandle.
113 */
114static struct device_node * find_phandle(phandle ph)
115{
116 struct device_node *np;
117
118 for (np = allnodes; np != 0; np = np->allnext)
119 if (np->linux_phandle == ph)
120 return np;
121 return NULL;
122}
123
124/*
125 * Find the interrupt parent of a node.
126 */
127static struct device_node * __devinit intr_parent(struct device_node *p)
128{
129 phandle *parp;
130
131 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
132 if (parp == NULL)
133 return p->parent;
134 p = find_phandle(*parp);
135 if (p != NULL)
136 return p;
137 /*
138 * On a powermac booted with BootX, we don't get to know the
139 * phandles for any nodes, so find_phandle will return NULL.
140 * Fortunately these machines only have one interrupt controller
141 * so there isn't in fact any ambiguity. -- paulus
142 */
143 if (num_interrupt_controllers == 1)
144 p = dflt_interrupt_controller;
145 return p;
146}
147
148/*
149 * Find out the size of each entry of the interrupts property
150 * for a node.
151 */
152int __devinit prom_n_intr_cells(struct device_node *np)
153{
154 struct device_node *p;
155 unsigned int *icp;
156
157 for (p = np; (p = intr_parent(p)) != NULL; ) {
158 icp = (unsigned int *)
159 get_property(p, "#interrupt-cells", NULL);
160 if (icp != NULL)
161 return *icp;
162 if (get_property(p, "interrupt-controller", NULL) != NULL
163 || get_property(p, "interrupt-map", NULL) != NULL) {
164 printk("oops, node %s doesn't have #interrupt-cells\n",
165 p->full_name);
166 return 1;
167 }
168 }
169#ifdef DEBUG_IRQ
170 printk("prom_n_intr_cells failed for %s\n", np->full_name);
171#endif
172 return 1;
173}
174
175/*
176 * Map an interrupt from a device up to the platform interrupt
177 * descriptor.
178 */
179static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
180 struct device_node *np, unsigned int *ints,
181 int nintrc)
182{
183 struct device_node *p, *ipar;
184 unsigned int *imap, *imask, *ip;
185 int i, imaplen, match;
186 int newintrc = 0, newaddrc = 0;
187 unsigned int *reg;
188 int naddrc;
189
190 reg = (unsigned int *) get_property(np, "reg", NULL);
191 naddrc = prom_n_addr_cells(np);
192 p = intr_parent(np);
193 while (p != NULL) {
194 if (get_property(p, "interrupt-controller", NULL) != NULL)
195 /* this node is an interrupt controller, stop here */
196 break;
197 imap = (unsigned int *)
198 get_property(p, "interrupt-map", &imaplen);
199 if (imap == NULL) {
200 p = intr_parent(p);
201 continue;
202 }
203 imask = (unsigned int *)
204 get_property(p, "interrupt-map-mask", NULL);
205 if (imask == NULL) {
206 printk("oops, %s has interrupt-map but no mask\n",
207 p->full_name);
208 return 0;
209 }
210 imaplen /= sizeof(unsigned int);
211 match = 0;
212 ipar = NULL;
213 while (imaplen > 0 && !match) {
214 /* check the child-interrupt field */
215 match = 1;
216 for (i = 0; i < naddrc && match; ++i)
217 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
218 for (; i < naddrc + nintrc && match; ++i)
219 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
220 imap += naddrc + nintrc;
221 imaplen -= naddrc + nintrc;
222 /* grab the interrupt parent */
223 ipar = find_phandle((phandle) *imap++);
224 --imaplen;
225 if (ipar == NULL && num_interrupt_controllers == 1)
226 /* cope with BootX not giving us phandles */
227 ipar = dflt_interrupt_controller;
228 if (ipar == NULL) {
229 printk("oops, no int parent %x in map of %s\n",
230 imap[-1], p->full_name);
231 return 0;
232 }
233 /* find the parent's # addr and intr cells */
234 ip = (unsigned int *)
235 get_property(ipar, "#interrupt-cells", NULL);
236 if (ip == NULL) {
237 printk("oops, no #interrupt-cells on %s\n",
238 ipar->full_name);
239 return 0;
240 }
241 newintrc = *ip;
242 ip = (unsigned int *)
243 get_property(ipar, "#address-cells", NULL);
244 newaddrc = (ip == NULL)? 0: *ip;
245 imap += newaddrc + newintrc;
246 imaplen -= newaddrc + newintrc;
247 }
248 if (imaplen < 0) {
249 printk("oops, error decoding int-map on %s, len=%d\n",
250 p->full_name, imaplen);
251 return 0;
252 }
253 if (!match) {
254#ifdef DEBUG_IRQ
255 printk("oops, no match in %s int-map for %s\n",
256 p->full_name, np->full_name);
257#endif
258 return 0;
259 }
260 p = ipar;
261 naddrc = newaddrc;
262 nintrc = newintrc;
263 ints = imap - nintrc;
264 reg = ints - naddrc;
265 }
266 if (p == NULL) {
267#ifdef DEBUG_IRQ
268 printk("hmmm, int tree for %s doesn't have ctrler\n",
269 np->full_name);
270#endif
271 return 0;
272 }
273 *irq = ints;
274 *ictrler = p;
275 return nintrc;
276}
277
Paul Mackerras6d0124f2005-10-26 17:19:06 +1000278static unsigned char map_isa_senses[4] = {
279 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
280 IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
281 IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE,
282 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE
283};
284
285static unsigned char map_mpic_senses[4] = {
286 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE,
287 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
288 /* 2 seems to be used for the 8259 cascade... */
289 IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
290 IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE,
291};
292
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000293static int __devinit finish_node_interrupts(struct device_node *np,
294 unsigned long *mem_start,
295 int measure_only)
296{
297 unsigned int *ints;
298 int intlen, intrcells, intrcount;
Paul Mackerras6d0124f2005-10-26 17:19:06 +1000299 int i, j, n, sense;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000300 unsigned int *irq, virq;
301 struct device_node *ic;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100302 int trace = 0;
303
304 //#define TRACE(fmt...) do { if (trace) { printk(fmt); mdelay(1000); } } while(0)
305#define TRACE(fmt...)
306
307 if (!strcmp(np->name, "smu-doorbell"))
308 trace = 1;
309
310 TRACE("Finishing SMU doorbell ! num_interrupt_controllers = %d\n",
311 num_interrupt_controllers);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000312
Paul Mackerrasa575b802005-10-23 17:23:21 +1000313 if (num_interrupt_controllers == 0) {
314 /*
315 * Old machines just have a list of interrupt numbers
316 * and no interrupt-controller nodes.
317 */
318 ints = (unsigned int *) get_property(np, "AAPL,interrupts",
319 &intlen);
320 /* XXX old interpret_pci_props looked in parent too */
321 /* XXX old interpret_macio_props looked for interrupts
322 before AAPL,interrupts */
323 if (ints == NULL)
324 ints = (unsigned int *) get_property(np, "interrupts",
325 &intlen);
326 if (ints == NULL)
327 return 0;
328
329 np->n_intrs = intlen / sizeof(unsigned int);
330 np->intrs = prom_alloc(np->n_intrs * sizeof(np->intrs[0]),
331 mem_start);
332 if (!np->intrs)
333 return -ENOMEM;
334 if (measure_only)
335 return 0;
336
337 for (i = 0; i < np->n_intrs; ++i) {
338 np->intrs[i].line = *ints++;
Paul Mackerras6d0124f2005-10-26 17:19:06 +1000339 np->intrs[i].sense = IRQ_SENSE_LEVEL
340 | IRQ_POLARITY_NEGATIVE;
Paul Mackerrasa575b802005-10-23 17:23:21 +1000341 }
342 return 0;
343 }
344
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000345 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100346 TRACE("ints=%p, intlen=%d\n", ints, intlen);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000347 if (ints == NULL)
348 return 0;
349 intrcells = prom_n_intr_cells(np);
350 intlen /= intrcells * sizeof(unsigned int);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100351 TRACE("intrcells=%d, new intlen=%d\n", intrcells, intlen);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000352 np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start);
353 if (!np->intrs)
354 return -ENOMEM;
355
356 if (measure_only)
357 return 0;
358
359 intrcount = 0;
360 for (i = 0; i < intlen; ++i, ints += intrcells) {
361 n = map_interrupt(&irq, &ic, np, ints, intrcells);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100362 TRACE("map, irq=%d, ic=%p, n=%d\n", irq, ic, n);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000363 if (n <= 0)
364 continue;
365
366 /* don't map IRQ numbers under a cascaded 8259 controller */
367 if (ic && device_is_compatible(ic, "chrp,iic")) {
368 np->intrs[intrcount].line = irq[0];
Paul Mackerras6d0124f2005-10-26 17:19:06 +1000369 sense = (n > 1)? (irq[1] & 3): 3;
370 np->intrs[intrcount].sense = map_isa_senses[sense];
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000371 } else {
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000372 virq = virt_irq_create_mapping(irq[0]);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100373 TRACE("virq=%d\n", virq);
Paul Mackerras6d0124f2005-10-26 17:19:06 +1000374#ifdef CONFIG_PPC64
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000375 if (virq == NO_IRQ) {
376 printk(KERN_CRIT "Could not allocate interrupt"
377 " number for %s\n", np->full_name);
378 continue;
379 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000380#endif
Paul Mackerras6d0124f2005-10-26 17:19:06 +1000381 np->intrs[intrcount].line = irq_offset_up(virq);
382 sense = (n > 1)? (irq[1] & 3): 1;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100383
384 /* Apple uses bits in there in a different way, let's
385 * only keep the real sense bit on macs
386 */
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100387 if (machine_is(powermac))
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100388 sense &= 0x1;
Paul Mackerras6d0124f2005-10-26 17:19:06 +1000389 np->intrs[intrcount].sense = map_mpic_senses[sense];
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000390 }
391
392#ifdef CONFIG_PPC64
393 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100394 if (machine_is(powermac) && ic && ic->parent) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000395 char *name = get_property(ic->parent, "name", NULL);
396 if (name && !strcmp(name, "u3"))
397 np->intrs[intrcount].line += 128;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100398 else if (!(name && (!strcmp(name, "mac-io") ||
399 !strcmp(name, "u4"))))
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000400 /* ignore other cascaded controllers, such as
401 the k2-sata-root */
402 break;
403 }
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100404#endif /* CONFIG_PPC64 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000405 if (n > 2) {
406 printk("hmmm, got %d intr cells for %s:", n,
407 np->full_name);
408 for (j = 0; j < n; ++j)
409 printk(" %d", irq[j]);
410 printk("\n");
411 }
412 ++intrcount;
413 }
414 np->n_intrs = intrcount;
415
416 return 0;
417}
418
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000419static int __devinit finish_node(struct device_node *np,
420 unsigned long *mem_start,
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000421 int measure_only)
422{
423 struct device_node *child;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100424 int rc = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000425
426 rc = finish_node_interrupts(np, mem_start, measure_only);
427 if (rc)
428 goto out;
429
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000430 for (child = np->child; child != NULL; child = child->sibling) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100431 rc = finish_node(child, mem_start, measure_only);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000432 if (rc)
433 goto out;
434 }
435out:
436 return rc;
437}
438
439static void __init scan_interrupt_controllers(void)
440{
441 struct device_node *np;
442 int n = 0;
443 char *name, *ic;
444 int iclen;
445
446 for (np = allnodes; np != NULL; np = np->allnext) {
447 ic = get_property(np, "interrupt-controller", &iclen);
448 name = get_property(np, "name", NULL);
449 /* checking iclen makes sure we don't get a false
450 match on /chosen.interrupt_controller */
451 if ((name != NULL
452 && strcmp(name, "interrupt-controller") == 0)
453 || (ic != NULL && iclen == 0
454 && strcmp(name, "AppleKiwi"))) {
455 if (n == 0)
456 dflt_interrupt_controller = np;
457 ++n;
458 }
459 }
460 num_interrupt_controllers = n;
461}
462
463/**
464 * finish_device_tree is called once things are running normally
465 * (i.e. with text and data mapped to the address they were linked at).
466 * It traverses the device tree and fills in some of the additional,
467 * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
468 * mapping is also initialized at this point.
469 */
470void __init finish_device_tree(void)
471{
472 unsigned long start, end, size = 0;
473
474 DBG(" -> finish_device_tree\n");
475
476#ifdef CONFIG_PPC64
477 /* Initialize virtual IRQ map */
478 virt_irq_init();
479#endif
480 scan_interrupt_controllers();
481
482 /*
483 * Finish device-tree (pre-parsing some properties etc...)
484 * We do this in 2 passes. One with "measure_only" set, which
485 * will only measure the amount of memory needed, then we can
486 * allocate that memory, and call finish_node again. However,
487 * we must be careful as most routines will fail nowadays when
488 * prom_alloc() returns 0, so we must make sure our first pass
489 * doesn't start at 0. We pre-initialize size to 16 for that
490 * reason and then remove those additional 16 bytes
491 */
492 size = 16;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100493 finish_node(allnodes, &size, 1);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000494 size -= 16;
Michael Ellermanfa938952006-01-25 21:31:25 +1300495
496 if (0 == size)
497 end = start = 0;
498 else
499 end = start = (unsigned long)__va(lmb_alloc(size, 128));
500
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100501 finish_node(allnodes, &end, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000502 BUG_ON(end != start + size);
503
504 DBG(" <- finish_device_tree\n");
505}
506
507static inline char *find_flat_dt_string(u32 offset)
508{
509 return ((char *)initial_boot_params) +
510 initial_boot_params->off_dt_strings + offset;
511}
512
513/**
514 * This function is used to scan the flattened device-tree, it is
515 * used to extract the memory informations at boot before we can
516 * unflatten the tree
517 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100518int __init of_scan_flat_dt(int (*it)(unsigned long node,
519 const char *uname, int depth,
520 void *data),
521 void *data)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000522{
523 unsigned long p = ((unsigned long)initial_boot_params) +
524 initial_boot_params->off_dt_struct;
525 int rc = 0;
526 int depth = -1;
527
528 do {
529 u32 tag = *((u32 *)p);
530 char *pathp;
531
532 p += 4;
533 if (tag == OF_DT_END_NODE) {
534 depth --;
535 continue;
536 }
537 if (tag == OF_DT_NOP)
538 continue;
539 if (tag == OF_DT_END)
540 break;
541 if (tag == OF_DT_PROP) {
542 u32 sz = *((u32 *)p);
543 p += 8;
544 if (initial_boot_params->version < 0x10)
545 p = _ALIGN(p, sz >= 8 ? 8 : 4);
546 p += sz;
547 p = _ALIGN(p, 4);
548 continue;
549 }
550 if (tag != OF_DT_BEGIN_NODE) {
551 printk(KERN_WARNING "Invalid tag %x scanning flattened"
552 " device tree !\n", tag);
553 return -EINVAL;
554 }
555 depth++;
556 pathp = (char *)p;
557 p = _ALIGN(p + strlen(pathp) + 1, 4);
558 if ((*pathp) == '/') {
559 char *lp, *np;
560 for (lp = NULL, np = pathp; *np; np++)
561 if ((*np) == '/')
562 lp = np+1;
563 if (lp != NULL)
564 pathp = lp;
565 }
566 rc = it(p, pathp, depth, data);
567 if (rc != 0)
568 break;
569 } while(1);
570
571 return rc;
572}
573
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100574unsigned long __init of_get_flat_dt_root(void)
575{
576 unsigned long p = ((unsigned long)initial_boot_params) +
577 initial_boot_params->off_dt_struct;
578
579 while(*((u32 *)p) == OF_DT_NOP)
580 p += 4;
581 BUG_ON (*((u32 *)p) != OF_DT_BEGIN_NODE);
582 p += 4;
583 return _ALIGN(p + strlen((char *)p) + 1, 4);
584}
585
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000586/**
587 * This function can be used within scan_flattened_dt callback to get
588 * access to properties
589 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100590void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
591 unsigned long *size)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000592{
593 unsigned long p = node;
594
595 do {
596 u32 tag = *((u32 *)p);
597 u32 sz, noff;
598 const char *nstr;
599
600 p += 4;
601 if (tag == OF_DT_NOP)
602 continue;
603 if (tag != OF_DT_PROP)
604 return NULL;
605
606 sz = *((u32 *)p);
607 noff = *((u32 *)(p + 4));
608 p += 8;
609 if (initial_boot_params->version < 0x10)
610 p = _ALIGN(p, sz >= 8 ? 8 : 4);
611
612 nstr = find_flat_dt_string(noff);
613 if (nstr == NULL) {
614 printk(KERN_WARNING "Can't find property index"
615 " name !\n");
616 return NULL;
617 }
618 if (strcmp(name, nstr) == 0) {
619 if (size)
620 *size = sz;
621 return (void *)p;
622 }
623 p += sz;
624 p = _ALIGN(p, 4);
625 } while(1);
626}
627
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100628int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
629{
630 const char* cp;
631 unsigned long cplen, l;
632
633 cp = of_get_flat_dt_prop(node, "compatible", &cplen);
634 if (cp == NULL)
635 return 0;
636 while (cplen > 0) {
637 if (strncasecmp(cp, compat, strlen(compat)) == 0)
638 return 1;
639 l = strlen(cp) + 1;
640 cp += l;
641 cplen -= l;
642 }
643
644 return 0;
645}
646
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000647static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
648 unsigned long align)
649{
650 void *res;
651
652 *mem = _ALIGN(*mem, align);
653 res = (void *)*mem;
654 *mem += size;
655
656 return res;
657}
658
659static unsigned long __init unflatten_dt_node(unsigned long mem,
660 unsigned long *p,
661 struct device_node *dad,
662 struct device_node ***allnextpp,
663 unsigned long fpsize)
664{
665 struct device_node *np;
666 struct property *pp, **prev_pp = NULL;
667 char *pathp;
668 u32 tag;
669 unsigned int l, allocl;
670 int has_name = 0;
671 int new_format = 0;
672
673 tag = *((u32 *)(*p));
674 if (tag != OF_DT_BEGIN_NODE) {
675 printk("Weird tag at start of node: %x\n", tag);
676 return mem;
677 }
678 *p += 4;
679 pathp = (char *)*p;
680 l = allocl = strlen(pathp) + 1;
681 *p = _ALIGN(*p + l, 4);
682
683 /* version 0x10 has a more compact unit name here instead of the full
684 * path. we accumulate the full path size using "fpsize", we'll rebuild
685 * it later. We detect this because the first character of the name is
686 * not '/'.
687 */
688 if ((*pathp) != '/') {
689 new_format = 1;
690 if (fpsize == 0) {
691 /* root node: special case. fpsize accounts for path
692 * plus terminating zero. root node only has '/', so
693 * fpsize should be 2, but we want to avoid the first
694 * level nodes to have two '/' so we use fpsize 1 here
695 */
696 fpsize = 1;
697 allocl = 2;
698 } else {
699 /* account for '/' and path size minus terminal 0
700 * already in 'l'
701 */
702 fpsize += l;
703 allocl = fpsize;
704 }
705 }
706
707
708 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
709 __alignof__(struct device_node));
710 if (allnextpp) {
711 memset(np, 0, sizeof(*np));
712 np->full_name = ((char*)np) + sizeof(struct device_node);
713 if (new_format) {
714 char *p = np->full_name;
715 /* rebuild full path for new format */
716 if (dad && dad->parent) {
717 strcpy(p, dad->full_name);
718#ifdef DEBUG
719 if ((strlen(p) + l + 1) != allocl) {
720 DBG("%s: p: %d, l: %d, a: %d\n",
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100721 pathp, (int)strlen(p), l, allocl);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000722 }
723#endif
724 p += strlen(p);
725 }
726 *(p++) = '/';
727 memcpy(p, pathp, l);
728 } else
729 memcpy(np->full_name, pathp, l);
730 prev_pp = &np->properties;
731 **allnextpp = np;
732 *allnextpp = &np->allnext;
733 if (dad != NULL) {
734 np->parent = dad;
735 /* we temporarily use the next field as `last_child'*/
736 if (dad->next == 0)
737 dad->child = np;
738 else
739 dad->next->sibling = np;
740 dad->next = np;
741 }
742 kref_init(&np->kref);
743 }
744 while(1) {
745 u32 sz, noff;
746 char *pname;
747
748 tag = *((u32 *)(*p));
749 if (tag == OF_DT_NOP) {
750 *p += 4;
751 continue;
752 }
753 if (tag != OF_DT_PROP)
754 break;
755 *p += 4;
756 sz = *((u32 *)(*p));
757 noff = *((u32 *)((*p) + 4));
758 *p += 8;
759 if (initial_boot_params->version < 0x10)
760 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
761
762 pname = find_flat_dt_string(noff);
763 if (pname == NULL) {
764 printk("Can't find property name in list !\n");
765 break;
766 }
767 if (strcmp(pname, "name") == 0)
768 has_name = 1;
769 l = strlen(pname) + 1;
770 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
771 __alignof__(struct property));
772 if (allnextpp) {
773 if (strcmp(pname, "linux,phandle") == 0) {
774 np->node = *((u32 *)*p);
775 if (np->linux_phandle == 0)
776 np->linux_phandle = np->node;
777 }
778 if (strcmp(pname, "ibm,phandle") == 0)
779 np->linux_phandle = *((u32 *)*p);
780 pp->name = pname;
781 pp->length = sz;
782 pp->value = (void *)*p;
783 *prev_pp = pp;
784 prev_pp = &pp->next;
785 }
786 *p = _ALIGN((*p) + sz, 4);
787 }
788 /* with version 0x10 we may not have the name property, recreate
789 * it here from the unit name if absent
790 */
791 if (!has_name) {
792 char *p = pathp, *ps = pathp, *pa = NULL;
793 int sz;
794
795 while (*p) {
796 if ((*p) == '@')
797 pa = p;
798 if ((*p) == '/')
799 ps = p + 1;
800 p++;
801 }
802 if (pa < ps)
803 pa = p;
804 sz = (pa - ps) + 1;
805 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
806 __alignof__(struct property));
807 if (allnextpp) {
808 pp->name = "name";
809 pp->length = sz;
810 pp->value = (unsigned char *)(pp + 1);
811 *prev_pp = pp;
812 prev_pp = &pp->next;
813 memcpy(pp->value, ps, sz - 1);
814 ((char *)pp->value)[sz - 1] = 0;
815 DBG("fixed up name for %s -> %s\n", pathp, pp->value);
816 }
817 }
818 if (allnextpp) {
819 *prev_pp = NULL;
820 np->name = get_property(np, "name", NULL);
821 np->type = get_property(np, "device_type", NULL);
822
823 if (!np->name)
824 np->name = "<NULL>";
825 if (!np->type)
826 np->type = "<NULL>";
827 }
828 while (tag == OF_DT_BEGIN_NODE) {
829 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
830 tag = *((u32 *)(*p));
831 }
832 if (tag != OF_DT_END_NODE) {
833 printk("Weird tag at end of node: %x\n", tag);
834 return mem;
835 }
836 *p += 4;
837 return mem;
838}
839
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000840static int __init early_parse_mem(char *p)
841{
842 if (!p)
843 return 1;
844
845 memory_limit = PAGE_ALIGN(memparse(p, &p));
846 DBG("memory limit = 0x%lx\n", memory_limit);
847
848 return 0;
849}
850early_param("mem", early_parse_mem);
851
852/*
853 * The device tree may be allocated below our memory limit, or inside the
854 * crash kernel region for kdump. If so, move it out now.
855 */
856static void move_device_tree(void)
857{
858 unsigned long start, size;
859 void *p;
860
861 DBG("-> move_device_tree\n");
862
863 start = __pa(initial_boot_params);
864 size = initial_boot_params->totalsize;
865
866 if ((memory_limit && (start + size) > memory_limit) ||
867 overlaps_crashkernel(start, size)) {
868 p = __va(lmb_alloc_base(size, PAGE_SIZE, lmb.rmo_size));
869 memcpy(p, initial_boot_params, size);
870 initial_boot_params = (struct boot_param_header *)p;
871 DBG("Moved device tree to 0x%p\n", p);
872 }
873
874 DBG("<- move_device_tree\n");
875}
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000876
877/**
878 * unflattens the device-tree passed by the firmware, creating the
879 * tree of struct device_node. It also fills the "name" and "type"
880 * pointers of the nodes so the normal device-tree walking functions
881 * can be used (this used to be done by finish_device_tree)
882 */
883void __init unflatten_device_tree(void)
884{
885 unsigned long start, mem, size;
886 struct device_node **allnextp = &allnodes;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000887
888 DBG(" -> unflatten_device_tree()\n");
889
890 /* First pass, scan for size */
891 start = ((unsigned long)initial_boot_params) +
892 initial_boot_params->off_dt_struct;
893 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
894 size = (size | 3) + 1;
895
896 DBG(" size is %lx, allocating...\n", size);
897
898 /* Allocate memory for the expanded device tree */
899 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000900 mem = (unsigned long) __va(mem);
901
902 ((u32 *)mem)[size / 4] = 0xdeadbeef;
903
904 DBG(" unflattening %lx...\n", mem);
905
906 /* Second pass, do actual unflattening */
907 start = ((unsigned long)initial_boot_params) +
908 initial_boot_params->off_dt_struct;
909 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
910 if (*((u32 *)start) != OF_DT_END)
911 printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
912 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
913 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
914 ((u32 *)mem)[size / 4] );
915 *allnextp = NULL;
916
917 /* Get pointer to OF "/chosen" node for use everywhere */
918 of_chosen = of_find_node_by_path("/chosen");
Paul Mackerrasa575b802005-10-23 17:23:21 +1000919 if (of_chosen == NULL)
920 of_chosen = of_find_node_by_path("/chosen@0");
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000921
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000922 DBG(" <- unflatten_device_tree()\n");
923}
924
Paul Mackerrasd2058192006-05-03 23:04:37 +1000925/*
926 * ibm,pa-features is a per-cpu property that contains a string of
927 * attribute descriptors, each of which has a 2 byte header plus up
928 * to 254 bytes worth of processor attribute bits. First header
929 * byte specifies the number of bytes following the header.
930 * Second header byte is an "attribute-specifier" type, of which
931 * zero is the only currently-defined value.
932 * Implementation: Pass in the byte and bit offset for the feature
933 * that we are interested in. The function will return -1 if the
934 * pa-features property is missing, or a 1/0 to indicate if the feature
935 * is supported/not supported. Note that the bit numbers are
936 * big-endian to match the definition in PAPR.
937 */
938static struct ibm_pa_feature {
939 unsigned long cpu_features; /* CPU_FTR_xxx bit */
940 unsigned int cpu_user_ftrs; /* PPC_FEATURE_xxx bit */
941 unsigned char pabyte; /* byte number in ibm,pa-features */
942 unsigned char pabit; /* bit number (big-endian) */
943 unsigned char invert; /* if 1, pa bit set => clear feature */
944} ibm_pa_features[] __initdata = {
945 {0, PPC_FEATURE_HAS_MMU, 0, 0, 0},
946 {0, PPC_FEATURE_HAS_FPU, 0, 1, 0},
947 {CPU_FTR_SLB, 0, 0, 2, 0},
948 {CPU_FTR_CTRL, 0, 0, 3, 0},
949 {CPU_FTR_NOEXECUTE, 0, 0, 6, 0},
950 {CPU_FTR_NODSISRALIGN, 0, 1, 1, 1},
951 {CPU_FTR_CI_LARGE_PAGE, 0, 1, 2, 0},
952};
953
954static void __init check_cpu_pa_features(unsigned long node)
955{
956 unsigned char *pa_ftrs;
957 unsigned long len, tablelen, i, bit;
958
959 pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
960 if (pa_ftrs == NULL)
961 return;
962
963 /* find descriptor with type == 0 */
964 for (;;) {
965 if (tablelen < 3)
966 return;
967 len = 2 + pa_ftrs[0];
968 if (tablelen < len)
969 return; /* descriptor 0 not found */
970 if (pa_ftrs[1] == 0)
971 break;
972 tablelen -= len;
973 pa_ftrs += len;
974 }
975
976 /* loop over bits we know about */
977 for (i = 0; i < ARRAY_SIZE(ibm_pa_features); ++i) {
978 struct ibm_pa_feature *fp = &ibm_pa_features[i];
979
980 if (fp->pabyte >= pa_ftrs[0])
981 continue;
982 bit = (pa_ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
983 if (bit ^ fp->invert) {
984 cur_cpu_spec->cpu_features |= fp->cpu_features;
985 cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
986 } else {
987 cur_cpu_spec->cpu_features &= ~fp->cpu_features;
988 cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
989 }
990 }
991}
992
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000993static int __init early_init_dt_scan_cpus(unsigned long node,
Anton Blanchard4df20462006-03-25 17:25:17 +1100994 const char *uname, int depth,
995 void *data)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000996{
Anton Blanchard4df20462006-03-25 17:25:17 +1100997 static int logical_cpuid = 0;
998 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
Stephen Rothwell4d177fb2006-03-28 17:14:44 +1100999#ifdef CONFIG_ALTIVEC
1000 u32 *prop;
1001#endif
1002 u32 *intserv;
Anton Blanchard4df20462006-03-25 17:25:17 +11001003 int i, nthreads;
1004 unsigned long len;
1005 int found = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001006
1007 /* We are scanning "cpu" nodes only */
1008 if (type == NULL || strcmp(type, "cpu") != 0)
1009 return 0;
1010
Anton Blanchard4df20462006-03-25 17:25:17 +11001011 /* Get physical cpuid */
1012 intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
1013 if (intserv) {
1014 nthreads = len / sizeof(int);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001015 } else {
Anton Blanchard4df20462006-03-25 17:25:17 +11001016 intserv = of_get_flat_dt_prop(node, "reg", NULL);
1017 nthreads = 1;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001018 }
Anton Blanchard4df20462006-03-25 17:25:17 +11001019
1020 /*
1021 * Now see if any of these threads match our boot cpu.
1022 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
1023 */
1024 for (i = 0; i < nthreads; i++) {
1025 /*
1026 * version 2 of the kexec param format adds the phys cpuid of
1027 * booted proc.
1028 */
1029 if (initial_boot_params && initial_boot_params->version >= 2) {
1030 if (intserv[i] ==
1031 initial_boot_params->boot_cpuid_phys) {
1032 found = 1;
1033 break;
1034 }
1035 } else {
1036 /*
1037 * Check if it's the boot-cpu, set it's hw index now,
1038 * unfortunately this format did not support booting
1039 * off secondary threads.
1040 */
1041 if (of_get_flat_dt_prop(node,
1042 "linux,boot-cpu", NULL) != NULL) {
1043 found = 1;
1044 break;
1045 }
1046 }
1047
1048#ifdef CONFIG_SMP
1049 /* logical cpu id is always 0 on UP kernels */
1050 logical_cpuid++;
1051#endif
1052 }
1053
1054 if (found) {
1055 DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
1056 intserv[i]);
1057 boot_cpuid = logical_cpuid;
1058 set_hard_smp_processor_id(boot_cpuid, intserv[i]);
1059 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001060
1061#ifdef CONFIG_ALTIVEC
1062 /* Check if we have a VMX and eventually update CPU features */
Stephen Rothwell676e2492005-11-10 14:16:21 +11001063 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,vmx", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001064 if (prop && (*prop) > 0) {
1065 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
1066 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
1067 }
1068
1069 /* Same goes for Apple's "altivec" property */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001070 prop = (u32 *)of_get_flat_dt_prop(node, "altivec", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001071 if (prop) {
1072 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
1073 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
1074 }
1075#endif /* CONFIG_ALTIVEC */
1076
Paul Mackerrasd2058192006-05-03 23:04:37 +10001077 check_cpu_pa_features(node);
1078
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001079#ifdef CONFIG_PPC_PSERIES
Anton Blanchard4df20462006-03-25 17:25:17 +11001080 if (nthreads > 1)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001081 cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
Anton Blanchard4df20462006-03-25 17:25:17 +11001082 else
1083 cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001084#endif
1085
1086 return 0;
1087}
1088
1089static int __init early_init_dt_scan_chosen(unsigned long node,
1090 const char *uname, int depth, void *data)
1091{
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001092 unsigned long *lprop;
Kumar Gala329dda02006-02-24 10:54:52 -06001093 unsigned long l;
1094 char *p;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001095
1096 DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
1097
Paul Mackerrasa575b802005-10-23 17:23:21 +10001098 if (depth != 1 ||
1099 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001100 return 0;
1101
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001102#ifdef CONFIG_PPC64
1103 /* check if iommu is forced on or off */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001104 if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001105 iommu_is_off = 1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001106 if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001107 iommu_force_on = 1;
1108#endif
1109
Michael Ellerman2babf5c2006-05-17 18:00:46 +10001110 /* mem=x on the command line is the preferred mechanism */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001111 lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001112 if (lprop)
1113 memory_limit = *lprop;
1114
1115#ifdef CONFIG_PPC64
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001116 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001117 if (lprop)
1118 tce_alloc_start = *lprop;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001119 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001120 if (lprop)
1121 tce_alloc_end = *lprop;
1122#endif
1123
1124#ifdef CONFIG_PPC_RTAS
Adrian Bunk943ffb52006-01-10 00:10:13 +01001125 /* To help early debugging via the front panel, we retrieve a minimal
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001126 * set of RTAS infos now if available
1127 */
1128 {
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001129 u64 *basep, *entryp, *sizep;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001130
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001131 basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
1132 entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001133 sizep = of_get_flat_dt_prop(node, "linux,rtas-size", NULL);
1134 if (basep && entryp && sizep) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001135 rtas.base = *basep;
1136 rtas.entry = *entryp;
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001137 rtas.size = *sizep;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001138 }
1139 }
1140#endif /* CONFIG_PPC_RTAS */
1141
Michael Ellermandcee3032005-12-04 18:39:48 +11001142#ifdef CONFIG_KEXEC
1143 lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
1144 if (lprop)
1145 crashk_res.start = *lprop;
1146
1147 lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
1148 if (lprop)
1149 crashk_res.end = crashk_res.start + *lprop - 1;
1150#endif
1151
Kumar Gala329dda02006-02-24 10:54:52 -06001152 /* Retreive command line */
1153 p = of_get_flat_dt_prop(node, "bootargs", &l);
1154 if (p != NULL && l > 0)
1155 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
1156
1157#ifdef CONFIG_CMDLINE
1158 if (l == 0 || (l == 1 && (*p) == 0))
1159 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1160#endif /* CONFIG_CMDLINE */
1161
1162 DBG("Command line is: %s\n", cmd_line);
1163
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001164 /* break now */
1165 return 1;
1166}
1167
1168static int __init early_init_dt_scan_root(unsigned long node,
1169 const char *uname, int depth, void *data)
1170{
1171 u32 *prop;
1172
1173 if (depth != 0)
1174 return 0;
1175
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001176 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001177 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
1178 DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
1179
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001180 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001181 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
1182 DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
1183
1184 /* break now */
1185 return 1;
1186}
1187
1188static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
1189{
1190 cell_t *p = *cellp;
1191 unsigned long r;
1192
1193 /* Ignore more than 2 cells */
1194 while (s > sizeof(unsigned long) / 4) {
1195 p++;
1196 s--;
1197 }
1198 r = *p++;
1199#ifdef CONFIG_PPC64
1200 if (s > 1) {
1201 r <<= 32;
1202 r |= *(p++);
1203 s--;
1204 }
1205#endif
1206
1207 *cellp = p;
1208 return r;
1209}
1210
1211
1212static int __init early_init_dt_scan_memory(unsigned long node,
1213 const char *uname, int depth, void *data)
1214{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001215 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001216 cell_t *reg, *endp;
1217 unsigned long l;
1218
1219 /* We are scanning "memory" nodes only */
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001220 if (type == NULL) {
1221 /*
1222 * The longtrail doesn't have a device_type on the
1223 * /memory node, so look for the node called /memory@0.
1224 */
1225 if (depth != 1 || strcmp(uname, "memory@0") != 0)
1226 return 0;
1227 } else if (strcmp(type, "memory") != 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001228 return 0;
1229
Michael Ellermanba759482005-12-04 18:39:55 +11001230 reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
1231 if (reg == NULL)
1232 reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001233 if (reg == NULL)
1234 return 0;
1235
1236 endp = reg + (l / sizeof(cell_t));
1237
Michael Ellerman358c86f2005-11-03 15:39:09 +11001238 DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001239 uname, l, reg[0], reg[1], reg[2], reg[3]);
1240
1241 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1242 unsigned long base, size;
1243
1244 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
1245 size = dt_mem_next_cell(dt_root_size_cells, &reg);
1246
1247 if (size == 0)
1248 continue;
1249 DBG(" - %lx , %lx\n", base, size);
1250#ifdef CONFIG_PPC64
1251 if (iommu_is_off) {
1252 if (base >= 0x80000000ul)
1253 continue;
1254 if ((base + size) > 0x80000000ul)
1255 size = 0x80000000ul - base;
1256 }
1257#endif
1258 lmb_add(base, size);
1259 }
1260 return 0;
1261}
1262
1263static void __init early_reserve_mem(void)
1264{
Kumar Galacbbcf342006-01-11 17:57:13 -06001265 u64 base, size;
1266 u64 *reserve_map;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001267
Kumar Galacbbcf342006-01-11 17:57:13 -06001268 reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001269 initial_boot_params->off_mem_rsvmap);
Kumar Galacbbcf342006-01-11 17:57:13 -06001270#ifdef CONFIG_PPC32
1271 /*
1272 * Handle the case where we might be booting from an old kexec
1273 * image that setup the mem_rsvmap as pairs of 32-bit values
1274 */
1275 if (*reserve_map > 0xffffffffull) {
1276 u32 base_32, size_32;
1277 u32 *reserve_map_32 = (u32 *)reserve_map;
1278
1279 while (1) {
1280 base_32 = *(reserve_map_32++);
1281 size_32 = *(reserve_map_32++);
1282 if (size_32 == 0)
1283 break;
Kumar Gala329dda02006-02-24 10:54:52 -06001284 DBG("reserving: %x -> %x\n", base_32, size_32);
Kumar Galacbbcf342006-01-11 17:57:13 -06001285 lmb_reserve(base_32, size_32);
1286 }
1287 return;
1288 }
1289#endif
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001290 while (1) {
1291 base = *(reserve_map++);
1292 size = *(reserve_map++);
1293 if (size == 0)
1294 break;
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
1312 /* Retrieve various informations from the /chosen node of the
1313 * device-tree, including the platform type, initrd location and
1314 * size, TCE reserve, and more ...
1315 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001316 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001317
1318 /* Scan memory nodes and rebuild LMBs */
1319 lmb_init();
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001320 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1321 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
Michael Ellerman846f77b2006-05-17 18:00:45 +10001322
1323 /* Save command line for /proc/cmdline and then parse parameters */
1324 strlcpy(saved_command_line, cmd_line, COMMAND_LINE_SIZE);
1325 parse_early_param();
1326
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001327 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
Michael Ellerman0cc47462005-12-04 18:39:37 +11001328 lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
Michael Ellerman47310412006-05-17 18:00:49 +10001329 reserve_kdump_trampoline();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001330 early_reserve_mem();
1331
Michael Ellerman2babf5c2006-05-17 18:00:46 +10001332 lmb_enforce_memory_limit(memory_limit);
1333 lmb_analyze();
1334
1335 DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
1336
1337 /* We may need to relocate the flat tree, do it now.
1338 * FIXME .. and the initrd too? */
1339 move_device_tree();
1340
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001341 DBG("Scanning CPUs ...\n");
1342
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001343 /* Retreive CPU related informations from the flat tree
1344 * (altivec support, boot CPU ID, ...)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001345 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001346 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001347
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001348 DBG(" <- early_init_devtree()\n");
1349}
1350
1351#undef printk
1352
1353int
1354prom_n_addr_cells(struct device_node* np)
1355{
1356 int* ip;
1357 do {
1358 if (np->parent)
1359 np = np->parent;
1360 ip = (int *) get_property(np, "#address-cells", NULL);
1361 if (ip != NULL)
1362 return *ip;
1363 } while (np->parent);
1364 /* No #address-cells property for the root node, default to 1 */
1365 return 1;
1366}
Paul Mackerras1dfc6772005-11-14 17:30:40 +11001367EXPORT_SYMBOL(prom_n_addr_cells);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001368
1369int
1370prom_n_size_cells(struct device_node* np)
1371{
1372 int* ip;
1373 do {
1374 if (np->parent)
1375 np = np->parent;
1376 ip = (int *) get_property(np, "#size-cells", NULL);
1377 if (ip != NULL)
1378 return *ip;
1379 } while (np->parent);
1380 /* No #size-cells property for the root node, default to 1 */
1381 return 1;
1382}
Paul Mackerras1dfc6772005-11-14 17:30:40 +11001383EXPORT_SYMBOL(prom_n_size_cells);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001384
1385/**
1386 * Work out the sense (active-low level / active-high edge)
1387 * of each interrupt from the device tree.
1388 */
1389void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
1390{
1391 struct device_node *np;
1392 int i, j;
1393
1394 /* default to level-triggered */
Paul Mackerras6d0124f2005-10-26 17:19:06 +10001395 memset(senses, IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, max - off);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001396
1397 for (np = allnodes; np != 0; np = np->allnext) {
1398 for (j = 0; j < np->n_intrs; j++) {
1399 i = np->intrs[j].line;
1400 if (i >= off && i < max)
Paul Mackerras6d0124f2005-10-26 17:19:06 +10001401 senses[i-off] = np->intrs[j].sense;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001402 }
1403 }
1404}
1405
1406/**
1407 * Construct and return a list of the device_nodes with a given name.
1408 */
1409struct device_node *find_devices(const char *name)
1410{
1411 struct device_node *head, **prevp, *np;
1412
1413 prevp = &head;
1414 for (np = allnodes; np != 0; np = np->allnext) {
1415 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
1416 *prevp = np;
1417 prevp = &np->next;
1418 }
1419 }
1420 *prevp = NULL;
1421 return head;
1422}
1423EXPORT_SYMBOL(find_devices);
1424
1425/**
1426 * Construct and return a list of the device_nodes with a given type.
1427 */
1428struct device_node *find_type_devices(const char *type)
1429{
1430 struct device_node *head, **prevp, *np;
1431
1432 prevp = &head;
1433 for (np = allnodes; np != 0; np = np->allnext) {
1434 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
1435 *prevp = np;
1436 prevp = &np->next;
1437 }
1438 }
1439 *prevp = NULL;
1440 return head;
1441}
1442EXPORT_SYMBOL(find_type_devices);
1443
1444/**
1445 * Returns all nodes linked together
1446 */
1447struct device_node *find_all_nodes(void)
1448{
1449 struct device_node *head, **prevp, *np;
1450
1451 prevp = &head;
1452 for (np = allnodes; np != 0; np = np->allnext) {
1453 *prevp = np;
1454 prevp = &np->next;
1455 }
1456 *prevp = NULL;
1457 return head;
1458}
1459EXPORT_SYMBOL(find_all_nodes);
1460
1461/** Checks if the given "compat" string matches one of the strings in
1462 * the device's "compatible" property
1463 */
1464int device_is_compatible(struct device_node *device, const char *compat)
1465{
1466 const char* cp;
1467 int cplen, l;
1468
1469 cp = (char *) get_property(device, "compatible", &cplen);
1470 if (cp == NULL)
1471 return 0;
1472 while (cplen > 0) {
1473 if (strncasecmp(cp, compat, strlen(compat)) == 0)
1474 return 1;
1475 l = strlen(cp) + 1;
1476 cp += l;
1477 cplen -= l;
1478 }
1479
1480 return 0;
1481}
1482EXPORT_SYMBOL(device_is_compatible);
1483
1484
1485/**
1486 * Indicates whether the root node has a given value in its
1487 * compatible property.
1488 */
1489int machine_is_compatible(const char *compat)
1490{
1491 struct device_node *root;
1492 int rc = 0;
1493
1494 root = of_find_node_by_path("/");
1495 if (root) {
1496 rc = device_is_compatible(root, compat);
1497 of_node_put(root);
1498 }
1499 return rc;
1500}
1501EXPORT_SYMBOL(machine_is_compatible);
1502
1503/**
1504 * Construct and return a list of the device_nodes with a given type
1505 * and compatible property.
1506 */
1507struct device_node *find_compatible_devices(const char *type,
1508 const char *compat)
1509{
1510 struct device_node *head, **prevp, *np;
1511
1512 prevp = &head;
1513 for (np = allnodes; np != 0; np = np->allnext) {
1514 if (type != NULL
1515 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1516 continue;
1517 if (device_is_compatible(np, compat)) {
1518 *prevp = np;
1519 prevp = &np->next;
1520 }
1521 }
1522 *prevp = NULL;
1523 return head;
1524}
1525EXPORT_SYMBOL(find_compatible_devices);
1526
1527/**
1528 * Find the device_node with a given full_name.
1529 */
1530struct device_node *find_path_device(const char *path)
1531{
1532 struct device_node *np;
1533
1534 for (np = allnodes; np != 0; np = np->allnext)
1535 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1536 return np;
1537 return NULL;
1538}
1539EXPORT_SYMBOL(find_path_device);
1540
1541/*******
1542 *
1543 * New implementation of the OF "find" APIs, return a refcounted
1544 * object, call of_node_put() when done. The device tree and list
1545 * are protected by a rw_lock.
1546 *
1547 * Note that property management will need some locking as well,
1548 * this isn't dealt with yet.
1549 *
1550 *******/
1551
1552/**
1553 * of_find_node_by_name - Find a node by its "name" property
1554 * @from: The node to start searching from or NULL, the node
1555 * you pass will not be searched, only the next one
1556 * will; typically, you pass what the previous call
1557 * returned. of_node_put() will be called on it
1558 * @name: The name string to match against
1559 *
1560 * Returns a node pointer with refcount incremented, use
1561 * of_node_put() on it when done.
1562 */
1563struct device_node *of_find_node_by_name(struct device_node *from,
1564 const char *name)
1565{
1566 struct device_node *np;
1567
1568 read_lock(&devtree_lock);
1569 np = from ? from->allnext : allnodes;
Olaf Hering090db7c2006-02-04 12:44:56 +01001570 for (; np != NULL; np = np->allnext)
1571 if (np->name != NULL && strcasecmp(np->name, name) == 0
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001572 && of_node_get(np))
1573 break;
1574 if (from)
1575 of_node_put(from);
1576 read_unlock(&devtree_lock);
1577 return np;
1578}
1579EXPORT_SYMBOL(of_find_node_by_name);
1580
1581/**
1582 * of_find_node_by_type - Find a node by its "device_type" property
1583 * @from: The node to start searching from or NULL, the node
1584 * you pass will not be searched, only the next one
1585 * will; typically, you pass what the previous call
1586 * returned. of_node_put() will be called on it
1587 * @name: The type string to match against
1588 *
1589 * Returns a node pointer with refcount incremented, use
1590 * of_node_put() on it when done.
1591 */
1592struct device_node *of_find_node_by_type(struct device_node *from,
1593 const char *type)
1594{
1595 struct device_node *np;
1596
1597 read_lock(&devtree_lock);
1598 np = from ? from->allnext : allnodes;
1599 for (; np != 0; np = np->allnext)
1600 if (np->type != 0 && strcasecmp(np->type, type) == 0
1601 && of_node_get(np))
1602 break;
1603 if (from)
1604 of_node_put(from);
1605 read_unlock(&devtree_lock);
1606 return np;
1607}
1608EXPORT_SYMBOL(of_find_node_by_type);
1609
1610/**
1611 * of_find_compatible_node - Find a node based on type and one of the
1612 * tokens in its "compatible" property
1613 * @from: The node to start searching from or NULL, the node
1614 * you pass will not be searched, only the next one
1615 * will; typically, you pass what the previous call
1616 * returned. of_node_put() will be called on it
1617 * @type: The type string to match "device_type" or NULL to ignore
1618 * @compatible: The string to match to one of the tokens in the device
1619 * "compatible" list.
1620 *
1621 * Returns a node pointer with refcount incremented, use
1622 * of_node_put() on it when done.
1623 */
1624struct device_node *of_find_compatible_node(struct device_node *from,
1625 const char *type, const char *compatible)
1626{
1627 struct device_node *np;
1628
1629 read_lock(&devtree_lock);
1630 np = from ? from->allnext : allnodes;
1631 for (; np != 0; np = np->allnext) {
1632 if (type != NULL
1633 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1634 continue;
1635 if (device_is_compatible(np, compatible) && of_node_get(np))
1636 break;
1637 }
1638 if (from)
1639 of_node_put(from);
1640 read_unlock(&devtree_lock);
1641 return np;
1642}
1643EXPORT_SYMBOL(of_find_compatible_node);
1644
1645/**
1646 * of_find_node_by_path - Find a node matching a full OF path
1647 * @path: The full path to match
1648 *
1649 * Returns a node pointer with refcount incremented, use
1650 * of_node_put() on it when done.
1651 */
1652struct device_node *of_find_node_by_path(const char *path)
1653{
1654 struct device_node *np = allnodes;
1655
1656 read_lock(&devtree_lock);
1657 for (; np != 0; np = np->allnext) {
1658 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
1659 && of_node_get(np))
1660 break;
1661 }
1662 read_unlock(&devtree_lock);
1663 return np;
1664}
1665EXPORT_SYMBOL(of_find_node_by_path);
1666
1667/**
1668 * of_find_node_by_phandle - Find a node given a phandle
1669 * @handle: phandle of the node to find
1670 *
1671 * Returns a node pointer with refcount incremented, use
1672 * of_node_put() on it when done.
1673 */
1674struct device_node *of_find_node_by_phandle(phandle handle)
1675{
1676 struct device_node *np;
1677
1678 read_lock(&devtree_lock);
1679 for (np = allnodes; np != 0; np = np->allnext)
1680 if (np->linux_phandle == handle)
1681 break;
1682 if (np)
1683 of_node_get(np);
1684 read_unlock(&devtree_lock);
1685 return np;
1686}
1687EXPORT_SYMBOL(of_find_node_by_phandle);
1688
1689/**
1690 * of_find_all_nodes - Get next node in global list
1691 * @prev: Previous node or NULL to start iteration
1692 * of_node_put() will be called on it
1693 *
1694 * Returns a node pointer with refcount incremented, use
1695 * of_node_put() on it when done.
1696 */
1697struct device_node *of_find_all_nodes(struct device_node *prev)
1698{
1699 struct device_node *np;
1700
1701 read_lock(&devtree_lock);
1702 np = prev ? prev->allnext : allnodes;
1703 for (; np != 0; np = np->allnext)
1704 if (of_node_get(np))
1705 break;
1706 if (prev)
1707 of_node_put(prev);
1708 read_unlock(&devtree_lock);
1709 return np;
1710}
1711EXPORT_SYMBOL(of_find_all_nodes);
1712
1713/**
1714 * of_get_parent - Get a node's parent if any
1715 * @node: Node to get parent
1716 *
1717 * Returns a node pointer with refcount incremented, use
1718 * of_node_put() on it when done.
1719 */
1720struct device_node *of_get_parent(const struct device_node *node)
1721{
1722 struct device_node *np;
1723
1724 if (!node)
1725 return NULL;
1726
1727 read_lock(&devtree_lock);
1728 np = of_node_get(node->parent);
1729 read_unlock(&devtree_lock);
1730 return np;
1731}
1732EXPORT_SYMBOL(of_get_parent);
1733
1734/**
1735 * of_get_next_child - Iterate a node childs
1736 * @node: parent node
1737 * @prev: previous child of the parent node, or NULL to get first
1738 *
1739 * Returns a node pointer with refcount incremented, use
1740 * of_node_put() on it when done.
1741 */
1742struct device_node *of_get_next_child(const struct device_node *node,
1743 struct device_node *prev)
1744{
1745 struct device_node *next;
1746
1747 read_lock(&devtree_lock);
1748 next = prev ? prev->sibling : node->child;
1749 for (; next != 0; next = next->sibling)
1750 if (of_node_get(next))
1751 break;
1752 if (prev)
1753 of_node_put(prev);
1754 read_unlock(&devtree_lock);
1755 return next;
1756}
1757EXPORT_SYMBOL(of_get_next_child);
1758
1759/**
1760 * of_node_get - Increment refcount of a node
1761 * @node: Node to inc refcount, NULL is supported to
1762 * simplify writing of callers
1763 *
1764 * Returns node.
1765 */
1766struct device_node *of_node_get(struct device_node *node)
1767{
1768 if (node)
1769 kref_get(&node->kref);
1770 return node;
1771}
1772EXPORT_SYMBOL(of_node_get);
1773
1774static inline struct device_node * kref_to_device_node(struct kref *kref)
1775{
1776 return container_of(kref, struct device_node, kref);
1777}
1778
1779/**
1780 * of_node_release - release a dynamically allocated node
1781 * @kref: kref element of the node to be released
1782 *
1783 * In of_node_put() this function is passed to kref_put()
1784 * as the destructor.
1785 */
1786static void of_node_release(struct kref *kref)
1787{
1788 struct device_node *node = kref_to_device_node(kref);
1789 struct property *prop = node->properties;
1790
1791 if (!OF_IS_DYNAMIC(node))
1792 return;
1793 while (prop) {
1794 struct property *next = prop->next;
1795 kfree(prop->name);
1796 kfree(prop->value);
1797 kfree(prop);
1798 prop = next;
Dave C Boutcher088186d2006-01-12 16:08:27 -06001799
1800 if (!prop) {
1801 prop = node->deadprops;
1802 node->deadprops = NULL;
1803 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001804 }
1805 kfree(node->intrs);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001806 kfree(node->full_name);
1807 kfree(node->data);
1808 kfree(node);
1809}
1810
1811/**
1812 * of_node_put - Decrement refcount of a node
1813 * @node: Node to dec refcount, NULL is supported to
1814 * simplify writing of callers
1815 *
1816 */
1817void of_node_put(struct device_node *node)
1818{
1819 if (node)
1820 kref_put(&node->kref, of_node_release);
1821}
1822EXPORT_SYMBOL(of_node_put);
1823
1824/*
1825 * Plug a device node into the tree and global list.
1826 */
1827void of_attach_node(struct device_node *np)
1828{
1829 write_lock(&devtree_lock);
1830 np->sibling = np->parent->child;
1831 np->allnext = allnodes;
1832 np->parent->child = np;
1833 allnodes = np;
1834 write_unlock(&devtree_lock);
1835}
1836
1837/*
1838 * "Unplug" a node from the device tree. The caller must hold
1839 * a reference to the node. The memory associated with the node
1840 * is not freed until its refcount goes to zero.
1841 */
1842void of_detach_node(const struct device_node *np)
1843{
1844 struct device_node *parent;
1845
1846 write_lock(&devtree_lock);
1847
1848 parent = np->parent;
1849
1850 if (allnodes == np)
1851 allnodes = np->allnext;
1852 else {
1853 struct device_node *prev;
1854 for (prev = allnodes;
1855 prev->allnext != np;
1856 prev = prev->allnext)
1857 ;
1858 prev->allnext = np->allnext;
1859 }
1860
1861 if (parent->child == np)
1862 parent->child = np->sibling;
1863 else {
1864 struct device_node *prevsib;
1865 for (prevsib = np->parent->child;
1866 prevsib->sibling != np;
1867 prevsib = prevsib->sibling)
1868 ;
1869 prevsib->sibling = np->sibling;
1870 }
1871
1872 write_unlock(&devtree_lock);
1873}
1874
1875#ifdef CONFIG_PPC_PSERIES
1876/*
1877 * Fix up the uninitialized fields in a new device node:
1878 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1879 *
1880 * A lot of boot-time code is duplicated here, because functions such
1881 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
1882 * slab allocator.
1883 *
1884 * This should probably be split up into smaller chunks.
1885 */
1886
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001887static int of_finish_dynamic_node(struct device_node *node)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001888{
1889 struct device_node *parent = of_get_parent(node);
1890 int err = 0;
1891 phandle *ibm_phandle;
1892
1893 node->name = get_property(node, "name", NULL);
1894 node->type = get_property(node, "device_type", NULL);
1895
1896 if (!parent) {
1897 err = -ENODEV;
1898 goto out;
1899 }
1900
1901 /* We don't support that function on PowerMac, at least
1902 * not yet
1903 */
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001904 if (machine_is(powermac))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001905 return -ENODEV;
1906
1907 /* fix up new node's linux_phandle field */
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001908 if ((ibm_phandle = (unsigned int *)get_property(node,
1909 "ibm,phandle", NULL)))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001910 node->linux_phandle = *ibm_phandle;
1911
1912out:
1913 of_node_put(parent);
1914 return err;
1915}
1916
1917static int prom_reconfig_notifier(struct notifier_block *nb,
1918 unsigned long action, void *node)
1919{
1920 int err;
1921
1922 switch (action) {
1923 case PSERIES_RECONFIG_ADD:
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001924 err = of_finish_dynamic_node(node);
1925 if (!err)
1926 finish_node(node, NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001927 if (err < 0) {
1928 printk(KERN_ERR "finish_node returned %d\n", err);
1929 err = NOTIFY_BAD;
1930 }
1931 break;
1932 default:
1933 err = NOTIFY_DONE;
1934 break;
1935 }
1936 return err;
1937}
1938
1939static struct notifier_block prom_reconfig_nb = {
1940 .notifier_call = prom_reconfig_notifier,
1941 .priority = 10, /* This one needs to run first */
1942};
1943
1944static int __init prom_reconfig_setup(void)
1945{
1946 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1947}
1948__initcall(prom_reconfig_setup);
1949#endif
1950
Dave C Boutcherecaa8b02006-01-12 16:09:29 -06001951struct property *of_find_property(struct device_node *np, const char *name,
1952 int *lenp)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001953{
1954 struct property *pp;
1955
Dave C Boutcher088186d2006-01-12 16:08:27 -06001956 read_lock(&devtree_lock);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001957 for (pp = np->properties; pp != 0; pp = pp->next)
1958 if (strcmp(pp->name, name) == 0) {
1959 if (lenp != 0)
1960 *lenp = pp->length;
Dave C Boutcher088186d2006-01-12 16:08:27 -06001961 break;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001962 }
Dave C Boutcher088186d2006-01-12 16:08:27 -06001963 read_unlock(&devtree_lock);
1964
Dave C Boutcherecaa8b02006-01-12 16:09:29 -06001965 return pp;
1966}
1967
1968/*
1969 * Find a property with a given name for a given node
1970 * and return the value.
1971 */
1972unsigned char *get_property(struct device_node *np, const char *name,
1973 int *lenp)
1974{
1975 struct property *pp = of_find_property(np,name,lenp);
Dave C Boutcher088186d2006-01-12 16:08:27 -06001976 return pp ? pp->value : NULL;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001977}
1978EXPORT_SYMBOL(get_property);
1979
1980/*
1981 * Add a property to a node
1982 */
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11001983int prom_add_property(struct device_node* np, struct property* prop)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001984{
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11001985 struct property **next;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001986
1987 prop->next = NULL;
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11001988 write_lock(&devtree_lock);
1989 next = &np->properties;
1990 while (*next) {
1991 if (strcmp(prop->name, (*next)->name) == 0) {
1992 /* duplicate ! don't insert it */
1993 write_unlock(&devtree_lock);
1994 return -1;
1995 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001996 next = &(*next)->next;
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11001997 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001998 *next = prop;
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11001999 write_unlock(&devtree_lock);
2000
Paul Mackerras799d6042005-11-10 13:37:51 +11002001#ifdef CONFIG_PROC_DEVICETREE
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11002002 /* try to add to proc as well if it was initialized */
2003 if (np->pde)
2004 proc_device_tree_add_prop(np->pde, prop);
Paul Mackerras799d6042005-11-10 13:37:51 +11002005#endif /* CONFIG_PROC_DEVICETREE */
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11002006
2007 return 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002008}
2009
Dave C Boutcher088186d2006-01-12 16:08:27 -06002010/*
2011 * Remove a property from a node. Note that we don't actually
2012 * remove it, since we have given out who-knows-how-many pointers
2013 * to the data using get-property. Instead we just move the property
2014 * to the "dead properties" list, so it won't be found any more.
2015 */
2016int prom_remove_property(struct device_node *np, struct property *prop)
2017{
2018 struct property **next;
2019 int found = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002020
Dave C Boutcher088186d2006-01-12 16:08:27 -06002021 write_lock(&devtree_lock);
2022 next = &np->properties;
2023 while (*next) {
2024 if (*next == prop) {
2025 /* found the node */
2026 *next = prop->next;
2027 prop->next = np->deadprops;
2028 np->deadprops = prop;
2029 found = 1;
2030 break;
2031 }
2032 next = &(*next)->next;
2033 }
2034 write_unlock(&devtree_lock);
2035
2036 if (!found)
2037 return -ENODEV;
2038
2039#ifdef CONFIG_PROC_DEVICETREE
2040 /* try to remove the proc node as well */
2041 if (np->pde)
2042 proc_device_tree_remove_prop(np->pde, prop);
2043#endif /* CONFIG_PROC_DEVICETREE */
2044
2045 return 0;
2046}
2047
2048/*
2049 * Update a property in a node. Note that we don't actually
2050 * remove it, since we have given out who-knows-how-many pointers
2051 * to the data using get-property. Instead we just move the property
2052 * to the "dead properties" list, and add the new property to the
2053 * property list
2054 */
2055int prom_update_property(struct device_node *np,
2056 struct property *newprop,
2057 struct property *oldprop)
2058{
2059 struct property **next;
2060 int found = 0;
2061
2062 write_lock(&devtree_lock);
2063 next = &np->properties;
2064 while (*next) {
2065 if (*next == oldprop) {
2066 /* found the node */
2067 newprop->next = oldprop->next;
2068 *next = newprop;
2069 oldprop->next = np->deadprops;
2070 np->deadprops = oldprop;
2071 found = 1;
2072 break;
2073 }
2074 next = &(*next)->next;
2075 }
2076 write_unlock(&devtree_lock);
2077
2078 if (!found)
2079 return -ENODEV;
2080
2081#ifdef CONFIG_PROC_DEVICETREE
2082 /* try to add to proc as well if it was initialized */
2083 if (np->pde)
2084 proc_device_tree_update_prop(np->pde, newprop, oldprop);
2085#endif /* CONFIG_PROC_DEVICETREE */
2086
2087 return 0;
2088}
Michael Ellermanb68239e2006-02-03 19:05:47 +11002089