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