4 * Procedures for interfacing to Open Firmware.
6 * Paul Mackerras August 1996.
7 * Copyright (C) 1996 Paul Mackerras.
9 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
10 * {engebret|bergner}@us.ibm.com
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.
23 #include <linux/config.h>
24 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <linux/init.h>
27 #include <linux/version.h>
28 #include <linux/threads.h>
29 #include <linux/spinlock.h>
30 #include <linux/types.h>
31 #include <linux/pci.h>
32 #include <linux/proc_fs.h>
33 #include <linux/stringify.h>
34 #include <linux/delay.h>
35 #include <linux/initrd.h>
39 #include <asm/abs_addr.h>
41 #include <asm/processor.h>
45 #include <asm/system.h>
47 #include <asm/pgtable.h>
48 #include <asm/bitops.h>
51 #include <asm/iommu.h>
52 #include <asm/bootinfo.h>
53 #include <asm/ppcdebug.h>
54 #include <asm/btext.h>
55 #include <asm/sections.h>
56 #include <asm/machdep.h>
59 #ifdef CONFIG_LOGO_LINUX_CLUT224
60 #include <linux/linux_logo.h>
61 extern const struct linux_logo logo_linux_clut224;
65 * Properties whose value is longer than this get excluded from our
66 * copy of the device tree. This value does need to be big enough to
67 * ensure that we don't lose things like the interrupt-map property
68 * on a PCI-PCI bridge.
70 #define MAX_PROPERTY_LENGTH (1UL * 1024 * 1024)
73 * prom_init() is called very early on, before the kernel text
74 * and data have been mapped to KERNELBASE. At this point the code
75 * is running at whatever address it has been loaded at, so
76 * references to extern and static variables must be relocated
77 * explicitly. The procedure reloc_offset() returns the address
78 * we're currently running at minus the address we were linked at.
79 * (Note that strings count as static variables.)
81 * Because OF may have mapped I/O devices into the area starting at
82 * KERNELBASE, particularly on CHRP machines, we can't safely call
83 * OF once the kernel has been mapped to KERNELBASE. Therefore all
84 * OF calls should be done within prom_init(), and prom_init()
85 * and all routines called within it must be careful to relocate
86 * references as necessary.
88 * Note that the bss is cleared *after* prom_init runs, so we have
89 * to make sure that any static or extern variables it accesses
90 * are put in the data segment.
94 #define PROM_BUG() do { \
95 prom_print(RELOC("kernel BUG at ")); \
96 prom_print(RELOC(__FILE__)); \
97 prom_print(RELOC(":")); \
98 prom_print_hex(__LINE__); \
99 prom_print(RELOC("!\n")); \
100 __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR); \
105 struct pci_reg_property {
106 struct pci_address addr;
112 struct isa_reg_property {
118 struct pci_intr_map {
119 struct pci_address addr;
126 typedef unsigned long interpret_func(struct device_node *, unsigned long,
129 #ifndef FB_MAX /* avoid pulling in all of the fb stuff */
136 char *prom_display_paths[FB_MAX] __initdata = { 0, };
137 phandle prom_display_nodes[FB_MAX] __initdata;
138 unsigned int prom_num_displays = 0;
139 char *of_stdout_device = 0;
141 static int iommu_force_on;
144 extern struct rtas_t rtas;
145 extern unsigned long klimit;
146 extern struct lmb lmb;
148 #define MAX_PHB (32 * 6) /* 32 drawers * 6 PHBs/drawer */
149 struct of_tce_table of_tce_table[MAX_PHB + 1];
152 char *bootdevice = 0;
155 #define MAX_CPU_THREADS 2
157 struct device_node *allnodes = 0;
158 /* use when traversing tree through the allnext, child, sibling,
159 * or parent members of struct device_node.
161 static rwlock_t devtree_lock = RW_LOCK_UNLOCKED;
163 extern unsigned long reloc_offset(void);
165 extern void enter_prom(struct prom_args *args);
166 extern void copy_and_flush(unsigned long dest, unsigned long src,
167 unsigned long size, unsigned long offset);
169 unsigned long dev_tree_size;
170 unsigned long _get_PIR(void);
175 unsigned int threadid;
176 } hmt_thread_data[NR_CPUS];
177 #endif /* CONFIG_HMT */
179 char testString[] = "LINUX\n";
182 /* This is the one and *ONLY* place where we actually call open
183 * firmware from, since we need to make sure we're running in 32b
184 * mode when we do. We switch back to 64b mode upon return.
187 #define PROM_ERROR (0x00000000fffffffful)
189 static unsigned long __init call_prom(const char *service, int nargs, int nret, ...)
192 unsigned long offset = reloc_offset();
193 struct prom_t *_prom = PTRRELOC(&prom);
196 _prom->args.service = (u32)LONG_LSW(service);
197 _prom->args.nargs = nargs;
198 _prom->args.nret = nret;
199 _prom->args.rets = (prom_arg_t *)&(_prom->args.args[nargs]);
201 va_start(list, nret);
202 for (i=0; i < nargs ;i++)
203 _prom->args.args[i] = (prom_arg_t)LONG_LSW(va_arg(list, unsigned long));
206 for (i=0; i < nret ;i++)
207 _prom->args.rets[i] = 0;
209 enter_prom(&_prom->args);
211 return (unsigned long)((nret > 0) ? _prom->args.rets[0] : 0);
215 static void __init prom_print(const char *msg)
218 unsigned long offset = reloc_offset();
219 struct prom_t *_prom = PTRRELOC(&prom);
221 if (_prom->stdout == 0)
224 for (p = msg; *p != 0; p = q) {
225 for (q = p; *q != 0 && *q != '\n'; ++q)
228 call_prom(RELOC("write"), 3, 1, _prom->stdout,
232 call_prom(RELOC("write"), 3, 1, _prom->stdout,
239 static void __init prom_print_hex(unsigned long val)
241 int i, nibbles = sizeof(val)*2;
242 char buf[sizeof(val)*2+1];
244 for (i = nibbles-1; i >= 0; i--) {
245 buf[i] = (val & 0xf) + '0';
247 buf[i] += ('a'-'0'-10);
255 static void __init prom_print_nl(void)
257 unsigned long offset = reloc_offset();
258 prom_print(RELOC("\n"));
262 static void __init prom_panic(const char *reason)
264 unsigned long offset = reloc_offset();
267 /* ToDo: should put up an SRC here */
268 call_prom(RELOC("exit"), 0, 0);
270 for (;;) /* should never get here */
275 static int __init prom_next_node(phandle *nodep)
278 unsigned long offset = reloc_offset();
280 if ((node = *nodep) != 0
281 && (*nodep = call_prom(RELOC("child"), 1, 1, node)) != 0)
283 if ((*nodep = call_prom(RELOC("peer"), 1, 1, node)) != 0)
286 if ((node = call_prom(RELOC("parent"), 1, 1, node)) == 0)
288 if ((*nodep = call_prom(RELOC("peer"), 1, 1, node)) != 0)
294 static void __init prom_initialize_naca(void)
298 unsigned long num_cpus = 0;
299 unsigned long offset = reloc_offset();
300 struct prom_t *_prom = PTRRELOC(&prom);
301 struct naca_struct *_naca = RELOC(naca);
302 struct systemcfg *_systemcfg = RELOC(systemcfg);
304 /* NOTE: _naca->debug_switch is already initialized. */
306 prom_print(RELOC("prom_initialize_naca: start...\n"));
309 _naca->pftSize = 0; /* ilog2 of htab size. computed below. */
311 for (node = 0; prom_next_node(&node); ) {
313 call_prom(RELOC("getprop"), 4, 1, node, RELOC("device_type"),
316 if (!strcmp(type, RELOC("cpu"))) {
319 /* We're assuming *all* of the CPUs have the same
320 * d-cache and i-cache sizes... -Peter
322 if ( num_cpus == 1 ) {
325 call_prom(RELOC("getprop"), 4, 1, node,
326 RELOC("d-cache-size"),
327 &size, sizeof(size));
329 if (_systemcfg->platform == PLATFORM_POWERMAC)
330 call_prom(RELOC("getprop"), 4, 1, node,
331 RELOC("d-cache-block-size"),
332 &lsize, sizeof(lsize));
334 call_prom(RELOC("getprop"), 4, 1, node,
335 RELOC("d-cache-line-size"),
336 &lsize, sizeof(lsize));
338 _systemcfg->dCacheL1Size = size;
339 _systemcfg->dCacheL1LineSize = lsize;
340 _naca->dCacheL1LogLineSize = __ilog2(lsize);
341 _naca->dCacheL1LinesPerPage = PAGE_SIZE/lsize;
343 call_prom(RELOC("getprop"), 4, 1, node,
344 RELOC("i-cache-size"),
345 &size, sizeof(size));
347 if (_systemcfg->platform == PLATFORM_POWERMAC)
348 call_prom(RELOC("getprop"), 4, 1, node,
349 RELOC("i-cache-block-size"),
350 &lsize, sizeof(lsize));
352 call_prom(RELOC("getprop"), 4, 1, node,
353 RELOC("i-cache-line-size"),
354 &lsize, sizeof(lsize));
356 _systemcfg->iCacheL1Size = size;
357 _systemcfg->iCacheL1LineSize = lsize;
358 _naca->iCacheL1LogLineSize = __ilog2(lsize);
359 _naca->iCacheL1LinesPerPage = PAGE_SIZE/lsize;
361 if (_systemcfg->platform == PLATFORM_PSERIES_LPAR) {
363 call_prom(RELOC("getprop"), 4, 1, node,
364 RELOC("ibm,pft-size"),
365 &pft_size, sizeof(pft_size));
366 /* pft_size[0] is the NUMA CEC cookie */
367 _naca->pftSize = pft_size[1];
370 } else if (!strcmp(type, RELOC("serial"))) {
372 struct isa_reg_property reg;
373 union pci_range ranges;
375 if (_systemcfg->platform == PLATFORM_POWERMAC)
378 call_prom(RELOC("getprop"), 4, 1, node,
379 RELOC("ibm,aix-loc"), type, sizeof(type));
381 if (strcmp(type, RELOC("S1")))
384 call_prom(RELOC("getprop"), 4, 1, node, RELOC("reg"),
387 isa = call_prom(RELOC("parent"), 1, 1, node);
390 pci = call_prom(RELOC("parent"), 1, 1, isa);
394 call_prom(RELOC("getprop"), 4, 1, pci, RELOC("ranges"),
395 &ranges, sizeof(ranges));
397 if ( _prom->encode_phys_size == 32 )
398 _naca->serialPortAddr = ranges.pci32.phys+reg.address;
400 _naca->serialPortAddr =
401 ((((unsigned long)ranges.pci64.phys_hi) << 32) |
402 (ranges.pci64.phys_lo)) + reg.address;
407 if (_systemcfg->platform == PLATFORM_POWERMAC)
408 _naca->interrupt_controller = IC_OPEN_PIC;
410 _naca->interrupt_controller = IC_INVALID;
411 for (node = 0; prom_next_node(&node); ) {
413 call_prom(RELOC("getprop"), 4, 1, node, RELOC("name"),
415 if (strcmp(type, RELOC("interrupt-controller")))
417 call_prom(RELOC("getprop"), 4, 1, node, RELOC("compatible"),
419 if (strstr(type, RELOC("open-pic")))
420 _naca->interrupt_controller = IC_OPEN_PIC;
421 else if (strstr(type, RELOC("ppc-xicp")))
422 _naca->interrupt_controller = IC_PPC_XIC;
424 prom_print(RELOC("prom: failed to recognize"
425 " interrupt-controller\n"));
430 if (_naca->interrupt_controller == IC_INVALID) {
431 prom_print(RELOC("prom: failed to find interrupt-controller\n"));
435 /* We gotta have at least 1 cpu... */
436 if ( (_systemcfg->processorCount = num_cpus) < 1 )
439 _systemcfg->physicalMemorySize = lmb_phys_mem_size();
441 if (_systemcfg->platform == PLATFORM_PSERIES ||
442 _systemcfg->platform == PLATFORM_POWERMAC) {
443 unsigned long rnd_mem_size, pteg_count;
445 /* round mem_size up to next power of 2 */
446 rnd_mem_size = 1UL << __ilog2(_systemcfg->physicalMemorySize);
447 if (rnd_mem_size < _systemcfg->physicalMemorySize)
451 pteg_count = (rnd_mem_size >> (12 + 1));
453 _naca->pftSize = __ilog2(pteg_count << 7);
456 if (_naca->pftSize == 0) {
457 prom_print(RELOC("prom: failed to compute pftSize!\n"));
462 * Hardcode to GP size. I am not sure where to get this info
463 * in general, as there does not appear to be a slb-size OF
464 * entry. At least in Condor and earlier. DRENG
466 _naca->slb_size = 64;
468 /* Add an eye catcher and the systemcfg layout version number */
469 strcpy(_systemcfg->eye_catcher, RELOC("SYSTEMCFG:PPC64"));
470 _systemcfg->version.major = SYSTEMCFG_MAJOR;
471 _systemcfg->version.minor = SYSTEMCFG_MINOR;
472 _systemcfg->processor = _get_PVR();
475 prom_print(RELOC("systemcfg->processorCount = 0x"));
476 prom_print_hex(_systemcfg->processorCount);
479 prom_print(RELOC("systemcfg->physicalMemorySize = 0x"));
480 prom_print_hex(_systemcfg->physicalMemorySize);
483 prom_print(RELOC("naca->pftSize = 0x"));
484 prom_print_hex(_naca->pftSize);
487 prom_print(RELOC("systemcfg->dCacheL1LineSize = 0x"));
488 prom_print_hex(_systemcfg->dCacheL1LineSize);
491 prom_print(RELOC("systemcfg->iCacheL1LineSize = 0x"));
492 prom_print_hex(_systemcfg->iCacheL1LineSize);
495 prom_print(RELOC("naca->serialPortAddr = 0x"));
496 prom_print_hex(_naca->serialPortAddr);
499 prom_print(RELOC("naca->interrupt_controller = 0x"));
500 prom_print_hex(_naca->interrupt_controller);
503 prom_print(RELOC("systemcfg->platform = 0x"));
504 prom_print_hex(_systemcfg->platform);
507 prom_print(RELOC("prom_initialize_naca: end...\n"));
512 static void __init early_cmdline_parse(void)
514 unsigned long offset = reloc_offset();
516 #ifndef CONFIG_PMAC_DART
517 struct systemcfg *_systemcfg = RELOC(systemcfg);
520 opt = strstr(RELOC(cmd_line), RELOC("iommu="));
522 prom_print(RELOC("opt is:"));
524 prom_print(RELOC("\n"));
526 while (*opt && *opt == ' ')
528 if (!strncmp(opt, RELOC("off"), 3))
529 RELOC(ppc64_iommu_off) = 1;
530 else if (!strncmp(opt, RELOC("force"), 5))
531 RELOC(iommu_force_on) = 1;
534 #ifndef CONFIG_PMAC_DART
535 if (_systemcfg->platform == PLATFORM_POWERMAC) {
536 RELOC(ppc64_iommu_off) = 1;
537 prom_print(RELOC("DART disabled on PowerMac !\n"));
543 void prom_dump_lmb(void)
546 unsigned long offset = reloc_offset();
547 struct lmb *_lmb = PTRRELOC(&lmb);
549 prom_print(RELOC("\nprom_dump_lmb:\n"));
550 prom_print(RELOC(" memory.cnt = 0x"));
551 prom_print_hex(_lmb->memory.cnt);
553 prom_print(RELOC(" memory.size = 0x"));
554 prom_print_hex(_lmb->memory.size);
556 for (i=0; i < _lmb->memory.cnt ;i++) {
557 prom_print(RELOC(" memory.region[0x"));
559 prom_print(RELOC("].base = 0x"));
560 prom_print_hex(_lmb->memory.region[i].base);
562 prom_print(RELOC(" .physbase = 0x"));
563 prom_print_hex(_lmb->memory.region[i].physbase);
565 prom_print(RELOC(" .size = 0x"));
566 prom_print_hex(_lmb->memory.region[i].size);
571 prom_print(RELOC(" reserved.cnt = 0x"));
572 prom_print_hex(_lmb->reserved.cnt);
574 prom_print(RELOC(" reserved.size = 0x"));
575 prom_print_hex(_lmb->reserved.size);
577 for (i=0; i < _lmb->reserved.cnt ;i++) {
578 prom_print(RELOC(" reserved.region[0x"));
580 prom_print(RELOC("].base = 0x"));
581 prom_print_hex(_lmb->reserved.region[i].base);
583 prom_print(RELOC(" .physbase = 0x"));
584 prom_print_hex(_lmb->reserved.region[i].physbase);
586 prom_print(RELOC(" .size = 0x"));
587 prom_print_hex(_lmb->reserved.region[i].size);
591 #endif /* DEBUG_PROM */
593 static void __init prom_initialize_lmb(void)
597 unsigned long i, offset = reloc_offset();
598 struct prom_t *_prom = PTRRELOC(&prom);
599 struct systemcfg *_systemcfg = RELOC(systemcfg);
600 union lmb_reg_property reg;
601 unsigned long lmb_base, lmb_size;
602 unsigned long num_regs, bytes_per_reg = (_prom->encode_phys_size*2)/8;
606 /* XXX Quick HACK. Proper fix is to drop those structures and properly use
607 * #address-cells. PowerMac has #size-cell set to 1 and #address-cells to 2
609 if (_systemcfg->platform == PLATFORM_POWERMAC)
612 for (node = 0; prom_next_node(&node); ) {
614 call_prom(RELOC("getprop"), 4, 1, node, RELOC("device_type"),
617 if (strcmp(type, RELOC("memory")))
620 num_regs = call_prom(RELOC("getprop"), 4, 1, node, RELOC("reg"),
621 ®, sizeof(reg)) / bytes_per_reg;
623 for (i=0; i < num_regs ;i++) {
624 if (_systemcfg->platform == PLATFORM_POWERMAC) {
625 lmb_base = ((unsigned long)reg.addrPM[i].address_hi) << 32;
626 lmb_base |= (unsigned long)reg.addrPM[i].address_lo;
627 lmb_size = reg.addrPM[i].size;
628 } else if (_prom->encode_phys_size == 32) {
629 lmb_base = reg.addr32[i].address;
630 lmb_size = reg.addr32[i].size;
632 lmb_base = reg.addr64[i].address;
633 lmb_size = reg.addr64[i].size;
636 /* We limit memory to 2GB if the IOMMU is off */
637 if (RELOC(ppc64_iommu_off)) {
638 if (lmb_base >= 0x80000000UL)
641 if ((lmb_base + lmb_size) > 0x80000000UL)
642 lmb_size = 0x80000000UL - lmb_base;
645 if (lmb_add(lmb_base, lmb_size) < 0)
646 prom_print(RELOC("Too many LMB's, discarding this one...\n"));
654 #endif /* DEBUG_PROM */
657 static char hypertas_funcs[1024];
660 prom_instantiate_rtas(void)
662 unsigned long offset = reloc_offset();
663 struct prom_t *_prom = PTRRELOC(&prom);
664 struct rtas_t *_rtas = PTRRELOC(&rtas);
665 struct systemcfg *_systemcfg = RELOC(systemcfg);
670 prom_print(RELOC("prom_instantiate_rtas: start...\n"));
672 prom_rtas = (ihandle)call_prom(RELOC("finddevice"), 1, 1, RELOC("/rtas"));
673 if (prom_rtas != (ihandle) -1) {
675 x = call_prom(RELOC("getprop"),
677 RELOC("ibm,hypertas-functions"),
679 sizeof(hypertas_funcs));
681 if (x != PROM_ERROR) {
682 prom_print(RELOC("Hypertas detected, assuming LPAR !\n"));
683 _systemcfg->platform = PLATFORM_PSERIES_LPAR;
686 call_prom(RELOC("getprop"),
690 sizeof(getprop_rval));
691 _rtas->size = getprop_rval;
692 prom_print(RELOC("instantiating rtas"));
693 if (_rtas->size != 0) {
694 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
696 /* Grab some space within the first RTAS_INSTANTIATE_MAX bytes
697 * of physical memory (or within the RMO region) because RTAS
698 * runs in 32-bit mode and relocate off.
700 if ( _systemcfg->platform == PLATFORM_PSERIES_LPAR ) {
701 struct lmb *_lmb = PTRRELOC(&lmb);
702 rtas_region = min(_lmb->rmo_size, RTAS_INSTANTIATE_MAX);
705 _rtas->base = lmb_alloc_base(_rtas->size, PAGE_SIZE, rtas_region);
707 prom_print(RELOC(" at 0x"));
708 prom_print_hex(_rtas->base);
710 prom_rtas = (ihandle)call_prom(RELOC("open"),
711 1, 1, RELOC("/rtas"));
712 prom_print(RELOC("..."));
714 if (call_prom(RELOC("call-method"), 3, 2,
715 RELOC("instantiate-rtas"),
717 _rtas->base) != PROM_ERROR) {
718 _rtas->entry = (long)_prom->args.rets[1];
721 = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE,
725 if (_rtas->entry <= 0) {
726 prom_print(RELOC(" failed\n"));
728 prom_print(RELOC(" done\n"));
732 prom_print(RELOC("rtas->base = 0x"));
733 prom_print_hex(_rtas->base);
735 prom_print(RELOC("rtas->entry = 0x"));
736 prom_print_hex(_rtas->entry);
738 prom_print(RELOC("rtas->size = 0x"));
739 prom_print_hex(_rtas->size);
744 prom_print(RELOC("prom_instantiate_rtas: end...\n"));
749 #ifdef CONFIG_PMAC_DART
750 static void __init prom_initialize_dart_table(void)
752 unsigned long offset = reloc_offset();
753 extern unsigned long dart_tablebase;
754 extern unsigned long dart_tablesize;
756 /* Only reserve DART space if machine has more than 2GB of RAM
757 * or if requested with iommu=on on cmdline.
759 if (lmb_end_of_DRAM() <= 0x80000000ull && !RELOC(iommu_force_on))
762 /* 512 pages (2MB) is max DART tablesize. */
763 RELOC(dart_tablesize) = 1UL << 21;
764 /* 16MB (1 << 24) alignment. We allocate a full 16Mb chuck since we
765 * will blow up an entire large page anyway in the kernel mapping
767 RELOC(dart_tablebase) = (unsigned long)
768 abs_to_virt(lmb_alloc_base(1UL<<24, 1UL<<24, 0x80000000L));
770 prom_print(RELOC("Dart at: "));
771 prom_print_hex(RELOC(dart_tablebase));
772 prom_print(RELOC("\n"));
774 #endif /* CONFIG_PMAC_DART */
776 static void __init prom_initialize_tce_table(void)
780 unsigned long offset = reloc_offset();
781 char compatible[64], path[64], type[64], model[64];
782 unsigned long i, table = 0;
783 unsigned long base, vbase, align;
784 unsigned int minalign, minsize;
785 struct of_tce_table *prom_tce_table = RELOC(of_tce_table);
786 unsigned long tce_entry, *tce_entryp;
788 if (RELOC(ppc64_iommu_off))
792 prom_print(RELOC("starting prom_initialize_tce_table\n"));
795 /* Search all nodes looking for PHBs. */
796 for (node = 0; prom_next_node(&node); ) {
797 if (table == MAX_PHB) {
798 prom_print(RELOC("WARNING: PCI host bridge ignored, "
799 "need to increase MAX_PHB\n"));
806 call_prom(RELOC("getprop"), 4, 1, node, RELOC("compatible"),
807 compatible, sizeof(compatible));
808 call_prom(RELOC("getprop"), 4, 1, node, RELOC("device_type"),
810 call_prom(RELOC("getprop"), 4, 1, node, RELOC("model"),
811 model, sizeof(model));
813 /* Keep the old logic in tack to avoid regression. */
814 if (compatible[0] != 0) {
815 if((strstr(compatible, RELOC("python")) == NULL) &&
816 (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
817 (strstr(compatible, RELOC("Winnipeg")) == NULL))
819 } else if (model[0] != 0) {
820 if ((strstr(model, RELOC("ython")) == NULL) &&
821 (strstr(model, RELOC("peedwagon")) == NULL) &&
822 (strstr(model, RELOC("innipeg")) == NULL))
826 if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL)) {
830 if (call_prom(RELOC("getprop"), 4, 1, node,
831 RELOC("tce-table-minalign"), &minalign,
832 sizeof(minalign)) == PROM_ERROR) {
836 if (call_prom(RELOC("getprop"), 4, 1, node,
837 RELOC("tce-table-minsize"), &minsize,
838 sizeof(minsize)) == PROM_ERROR) {
843 * Even though we read what OF wants, we just set the table
844 * size to 4 MB. This is enough to map 2GB of PCI DMA space.
845 * By doing this, we avoid the pitfalls of trying to DMA to
846 * MMIO space and the DMA alias hole.
848 * On POWER4, firmware sets the TCE region by assuming
849 * each TCE table is 8MB. Using this memory for anything
850 * else will impact performance, so we always allocate 8MB.
853 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
858 /* Align to the greater of the align or size */
859 align = max(minalign, minsize);
861 /* Carve out storage for the TCE table. */
862 base = lmb_alloc(minsize, align);
865 prom_panic(RELOC("ERROR, cannot find space for TCE table.\n"));
868 vbase = (unsigned long)abs_to_virt(base);
870 /* Save away the TCE table attributes for later use. */
871 prom_tce_table[table].node = node;
872 prom_tce_table[table].base = vbase;
873 prom_tce_table[table].size = minsize;
876 prom_print(RELOC("TCE table: 0x"));
877 prom_print_hex(table);
880 prom_print(RELOC("\tnode = 0x"));
881 prom_print_hex(node);
884 prom_print(RELOC("\tbase = 0x"));
885 prom_print_hex(vbase);
888 prom_print(RELOC("\tsize = 0x"));
889 prom_print_hex(minsize);
893 /* Initialize the table to have a one-to-one mapping
894 * over the allocated size.
896 tce_entryp = (unsigned long *)base;
897 for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
898 tce_entry = (i << PAGE_SHIFT);
900 *tce_entryp = tce_entry;
903 /* It seems OF doesn't null-terminate the path :-( */
904 memset(path, 0, sizeof(path));
905 /* Call OF to setup the TCE hardware */
906 if (call_prom(RELOC("package-to-path"), 3, 1, node,
907 path, sizeof(path)-1) == PROM_ERROR) {
908 prom_print(RELOC("package-to-path failed\n"));
910 prom_print(RELOC("opening PHB "));
914 phb_node = (ihandle)call_prom(RELOC("open"), 1, 1, path);
915 if ( (long)phb_node <= 0) {
916 prom_print(RELOC("... failed\n"));
918 prom_print(RELOC("... done\n"));
920 call_prom(RELOC("call-method"), 6, 0,
921 RELOC("set-64-bit-addressing"),
926 (base >> 32) & 0xffffffff);
927 call_prom(RELOC("close"), 1, 0, phb_node);
932 /* Flag the first invalid entry */
933 prom_tce_table[table].node = 0;
935 prom_print(RELOC("ending prom_initialize_tce_table\n"));
940 * With CHRP SMP we need to use the OF to start the other
941 * processors so we can't wait until smp_boot_cpus (the OF is
942 * trashed by then) so we have to put the processors into
943 * a holding pattern controlled by the kernel (not OF) before
946 * This uses a chunk of low memory, puts some holding pattern
947 * code there and sends the other processors off to there until
948 * smp_boot_cpus tells them to do something. The holding pattern
949 * checks that address until its cpu # is there, when it is that
950 * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
951 * of setting those values.
953 * We also use physical address 0x4 here to tell when a cpu
954 * is in its holding pattern code.
956 * Fixup comment... DRENG / PPPBBB - Peter
960 static void __init prom_hold_cpus(unsigned long mem)
965 unsigned long offset = reloc_offset();
966 char type[64], *path;
968 unsigned int interrupt_server[MAX_CPU_THREADS];
969 unsigned int cpu_threads, hw_cpu_num;
971 extern void __secondary_hold(void);
972 extern unsigned long __secondary_hold_spinloop;
973 extern unsigned long __secondary_hold_acknowledge;
974 unsigned long *spinloop
975 = (void *)virt_to_abs(&__secondary_hold_spinloop);
976 unsigned long *acknowledge
977 = (void *)virt_to_abs(&__secondary_hold_acknowledge);
978 unsigned long secondary_hold
979 = virt_to_abs(*PTRRELOC((unsigned long *)__secondary_hold));
980 struct systemcfg *_systemcfg = RELOC(systemcfg);
981 struct paca_struct *_xPaca = PTRRELOC(&paca[0]);
982 struct prom_t *_prom = PTRRELOC(&prom);
984 struct naca_struct *_naca = RELOC(naca);
987 /* On pmac, we just fill out the various global bitmasks and
988 * arrays indicating our CPUs are here, they are actually started
989 * later on from pmac_smp
991 if (_systemcfg->platform == PLATFORM_POWERMAC) {
992 for (node = 0; prom_next_node(&node); ) {
994 call_prom(RELOC("getprop"), 4, 1, node, RELOC("device_type"),
996 if (strcmp(type, RELOC("cpu")) != 0)
999 call_prom(RELOC("getprop"), 4, 1, node, RELOC("reg"),
1001 _xPaca[cpuid].xHwProcNum = reg;
1004 cpu_set(cpuid, RELOC(cpu_available_map));
1005 cpu_set(cpuid, RELOC(cpu_possible_map));
1006 cpu_set(cpuid, RELOC(cpu_present_at_boot));
1008 cpu_set(cpuid, RELOC(cpu_online_map));
1009 #endif /* CONFIG_SMP */
1015 /* Initially, we must have one active CPU. */
1016 _systemcfg->processorCount = 1;
1019 prom_print(RELOC("prom_hold_cpus: start...\n"));
1020 prom_print(RELOC(" 1) spinloop = 0x"));
1021 prom_print_hex((unsigned long)spinloop);
1023 prom_print(RELOC(" 1) *spinloop = 0x"));
1024 prom_print_hex(*spinloop);
1026 prom_print(RELOC(" 1) acknowledge = 0x"));
1027 prom_print_hex((unsigned long)acknowledge);
1029 prom_print(RELOC(" 1) *acknowledge = 0x"));
1030 prom_print_hex(*acknowledge);
1032 prom_print(RELOC(" 1) secondary_hold = 0x"));
1033 prom_print_hex(secondary_hold);
1037 /* Set the common spinloop variable, so all of the secondary cpus
1038 * will block when they are awakened from their OF spinloop.
1039 * This must occur for both SMP and non SMP kernels, since OF will
1040 * be trashed when we move the kernel.
1045 for (i=0; i < NR_CPUS; i++) {
1046 RELOC(hmt_thread_data)[i].pir = 0xdeadbeef;
1050 for (node = 0; prom_next_node(&node); ) {
1052 call_prom(RELOC("getprop"), 4, 1, node, RELOC("device_type"),
1053 type, sizeof(type));
1054 if (strcmp(type, RELOC("cpu")) != 0)
1057 /* Skip non-configured cpus. */
1058 call_prom(RELOC("getprop"), 4, 1, node, RELOC("status"),
1059 type, sizeof(type));
1060 if (strcmp(type, RELOC("okay")) != 0)
1064 call_prom(RELOC("getprop"), 4, 1, node, RELOC("reg"),
1067 path = (char *) mem;
1068 memset(path, 0, 256);
1069 if ((long) call_prom(RELOC("package-to-path"), 3, 1,
1070 node, path, 255) == PROM_ERROR)
1075 prom_print(RELOC("cpuid = 0x"));
1076 prom_print_hex(cpuid);
1078 prom_print(RELOC("cpu hw idx = 0x"));
1079 prom_print_hex(reg);
1082 _xPaca[cpuid].xHwProcNum = reg;
1084 /* Init the acknowledge var which will be reset by
1085 * the secondary cpu when it awakens from its OF
1088 *acknowledge = (unsigned long)-1;
1090 propsize = call_prom(RELOC("getprop"), 4, 1, node,
1091 RELOC("ibm,ppc-interrupt-server#s"),
1093 sizeof(interrupt_server));
1095 /* no property. old hardware has no SMT */
1097 interrupt_server[0] = reg; /* fake it with phys id */
1099 /* We have a threaded processor */
1100 cpu_threads = propsize / sizeof(u32);
1101 if (cpu_threads > MAX_CPU_THREADS) {
1102 prom_print(RELOC("SMT: too many threads!\nSMT: found "));
1103 prom_print_hex(cpu_threads);
1104 prom_print(RELOC(", max is "));
1105 prom_print_hex(MAX_CPU_THREADS);
1107 cpu_threads = 1; /* ToDo: panic? */
1111 hw_cpu_num = interrupt_server[0];
1112 if (hw_cpu_num != _prom->cpu) {
1113 /* Primary Thread of non-boot cpu */
1114 prom_print_hex(cpuid);
1115 prom_print(RELOC(" : starting cpu "));
1117 prom_print(RELOC("... "));
1118 call_prom(RELOC("start-cpu"), 3, 0, node,
1119 secondary_hold, cpuid);
1121 for ( i = 0 ; (i < 100000000) &&
1122 (*acknowledge == ((unsigned long)-1)); i++ ) ;
1124 if (*acknowledge == cpuid) {
1125 prom_print(RELOC("... done\n"));
1126 /* We have to get every CPU out of OF,
1127 * even if we never start it. */
1128 if (cpuid >= NR_CPUS)
1131 /* Set the number of active processors. */
1132 _systemcfg->processorCount++;
1133 cpu_set(cpuid, RELOC(cpu_available_map));
1134 cpu_set(cpuid, RELOC(cpu_possible_map));
1135 cpu_set(cpuid, RELOC(cpu_present_at_boot));
1138 prom_print(RELOC("... failed: "));
1139 prom_print_hex(*acknowledge);
1145 prom_print_hex(cpuid);
1146 prom_print(RELOC(" : booting cpu "));
1149 cpu_set(cpuid, RELOC(cpu_available_map));
1150 cpu_set(cpuid, RELOC(cpu_possible_map));
1151 cpu_set(cpuid, RELOC(cpu_online_map));
1152 cpu_set(cpuid, RELOC(cpu_present_at_boot));
1157 /* Init paca for secondary threads. They start later. */
1158 for (i=1; i < cpu_threads; i++) {
1160 if (cpuid >= NR_CPUS)
1162 _xPaca[cpuid].xHwProcNum = interrupt_server[i];
1163 prom_print_hex(interrupt_server[i]);
1164 prom_print(RELOC(" : preparing thread ... "));
1165 if (_naca->smt_state) {
1166 cpu_set(cpuid, RELOC(cpu_available_map));
1167 cpu_set(cpuid, RELOC(cpu_present_at_boot));
1168 prom_print(RELOC("available"));
1170 prom_print(RELOC("not available"));
1178 /* Only enable HMT on processors that provide support. */
1179 if (__is_processor(PV_PULSAR) ||
1180 __is_processor(PV_ICESTAR) ||
1181 __is_processor(PV_SSTAR)) {
1182 prom_print(RELOC(" starting secondary threads\n"));
1184 for (i = 0; i < NR_CPUS; i += 2) {
1189 unsigned long pir = _get_PIR();
1190 if (__is_processor(PV_PULSAR)) {
1191 RELOC(hmt_thread_data)[i].pir =
1194 RELOC(hmt_thread_data)[i].pir =
1198 /* cpu_set(i+1, cpu_online_map); */
1199 cpu_set(i+1, RELOC(cpu_possible_map));
1201 _systemcfg->processorCount *= 2;
1203 prom_print(RELOC("Processor is not HMT capable\n"));
1207 if (cpuid >= NR_CPUS)
1208 prom_print(RELOC("WARNING: maximum CPUs (" __stringify(NR_CPUS)
1209 ") exceeded: ignoring extras\n"));
1212 prom_print(RELOC("prom_hold_cpus: end...\n"));
1216 static void __init smt_setup(void)
1219 char my_smt_enabled = SMT_DYNAMIC;
1220 ihandle prom_options = NULL;
1222 unsigned long offset = reloc_offset();
1223 struct naca_struct *_naca = RELOC(naca);
1226 if (strstr(RELOC(cmd_line), RELOC("smt-enabled="))) {
1227 for (q = RELOC(cmd_line); (p = strstr(q, RELOC("smt-enabled="))) != 0; ) {
1229 if (p > RELOC(cmd_line) && p[-1] != ' ')
1232 if (q[0] == 'o' && q[1] == 'f' &&
1233 q[2] == 'f' && (q[3] == ' ' || q[3] == '\0')) {
1234 my_smt_enabled = SMT_OFF;
1235 } else if (q[0]=='o' && q[1] == 'n' &&
1236 (q[2] == ' ' || q[2] == '\0')) {
1237 my_smt_enabled = SMT_ON;
1239 my_smt_enabled = SMT_DYNAMIC;
1244 prom_options = (ihandle)call_prom(RELOC("finddevice"), 1, 1, RELOC("/options"));
1245 if (prom_options != (ihandle) -1) {
1246 call_prom(RELOC("getprop"),
1248 RELOC("ibm,smt-enabled"),
1251 if (option[0] != 0) {
1253 if (!strcmp(option, RELOC("off")))
1254 my_smt_enabled = SMT_OFF;
1255 else if (!strcmp(option, RELOC("on")))
1256 my_smt_enabled = SMT_ON;
1258 my_smt_enabled = SMT_DYNAMIC;
1264 my_smt_enabled = SMT_DYNAMIC; /* default to on */
1266 _naca->smt_state = my_smt_enabled;
1270 #ifdef CONFIG_BOOTX_TEXT
1272 /* This function will enable the early boot text when doing OF booting. This
1273 * way, xmon output should work too
1275 static void __init setup_disp_fake_bi(ihandle dp)
1277 int width = 640, height = 480, depth = 8, pitch;
1279 struct pci_reg_property addrs[8];
1282 unsigned long offset = reloc_offset();
1283 char *getprop = RELOC("getprop");
1285 prom_print(RELOC("Initializing fake screen: "));
1287 memset(name, 0, sizeof(name));
1288 call_prom(getprop, 4, 1, dp, RELOC("name"), name, sizeof(name));
1289 name[sizeof(name)-1] = 0;
1291 prom_print(RELOC("\n"));
1292 call_prom(getprop, 4, 1, dp, RELOC("width"), &width, sizeof(width));
1293 call_prom(getprop, 4, 1, dp, RELOC("height"), &height, sizeof(height));
1294 call_prom(getprop, 4, 1, dp, RELOC("depth"), &depth, sizeof(depth));
1295 pitch = width * ((depth + 7) / 8);
1296 call_prom(getprop, 4, 1, dp, RELOC("linebytes"),
1297 &pitch, sizeof(pitch));
1299 pitch = 0x1000; /* for strange IBM display */
1302 prom_print(RELOC("width "));
1303 prom_print_hex(width);
1304 prom_print(RELOC(" height "));
1305 prom_print_hex(height);
1306 prom_print(RELOC(" depth "));
1307 prom_print_hex(depth);
1308 prom_print(RELOC(" linebytes "));
1309 prom_print_hex(pitch);
1310 prom_print(RELOC("\n"));
1313 call_prom(getprop, 4, 1, dp, RELOC("address"),
1314 &address, sizeof(address));
1316 /* look for an assigned address with a size of >= 1MB */
1317 naddrs = (int) call_prom(getprop, 4, 1, dp,
1318 RELOC("assigned-addresses"),
1319 addrs, sizeof(addrs));
1320 naddrs /= sizeof(struct pci_reg_property);
1321 for (i = 0; i < naddrs; ++i) {
1322 if (addrs[i].size_lo >= (1 << 20)) {
1323 address = addrs[i].addr.a_lo;
1324 /* use the BE aperture if possible */
1325 if (addrs[i].size_lo >= (16 << 20))
1326 address += (8 << 20);
1331 prom_print(RELOC("Failed to get address of frame buffer\n"));
1335 btext_setup_display(width, height, depth, pitch, address);
1336 prom_print(RELOC("Addr of fb: "));
1337 prom_print_hex(address);
1339 RELOC(boot_text_mapped) = 0;
1341 #endif /* CONFIG_BOOTX_TEXT */
1343 static void __init prom_init_client_services(unsigned long pp)
1345 unsigned long offset = reloc_offset();
1346 struct prom_t *_prom = PTRRELOC(&prom);
1348 /* Get a handle to the prom entry point before anything else */
1351 /* Init default value for phys size */
1352 _prom->encode_phys_size = 32;
1354 /* get a handle for the stdout device */
1355 _prom->chosen = (ihandle)call_prom(RELOC("finddevice"), 1, 1,
1357 if ((long)_prom->chosen <= 0)
1358 prom_panic(RELOC("cannot find chosen")); /* msg won't be printed :( */
1360 /* get device tree root */
1361 _prom->root = (ihandle)call_prom(RELOC("finddevice"), 1, 1, RELOC("/"));
1362 if ((long)_prom->root <= 0)
1363 prom_panic(RELOC("cannot find device tree root")); /* msg won't be printed :( */
1366 static void __init prom_init_stdout(void)
1368 unsigned long offset = reloc_offset();
1369 struct prom_t *_prom = PTRRELOC(&prom);
1372 if ((long)call_prom(RELOC("getprop"), 4, 1, _prom->chosen,
1373 RELOC("stdout"), &val,
1375 prom_panic(RELOC("cannot find stdout"));
1377 _prom->stdout = (ihandle)(unsigned long)val;
1380 static int __init prom_find_machine_type(void)
1382 unsigned long offset = reloc_offset();
1383 struct prom_t *_prom = PTRRELOC(&prom);
1387 len = (int)(long)call_prom(RELOC("getprop"), 4, 1, _prom->root,
1388 RELOC("compatible"),
1389 compat, sizeof(compat)-1);
1393 char *p = &compat[i];
1397 if (strstr(p, RELOC("Power Macintosh")) ||
1398 strstr(p, RELOC("MacRISC4")))
1399 return PLATFORM_POWERMAC;
1403 /* Default to pSeries */
1404 return PLATFORM_PSERIES;
1407 static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
1409 unsigned long offset = reloc_offset();
1411 return (int)(long)call_prom(RELOC("call-method"), 6, 1,
1421 * If we have a display that we don't know how to drive,
1422 * we will want to try to execute OF's open method for it
1423 * later. However, OF will probably fall over if we do that
1424 * we've taken over the MMU.
1425 * So we check whether we will need to open the display,
1426 * and if so, open it now.
1428 static unsigned long __init check_display(unsigned long mem)
1433 unsigned long offset = reloc_offset();
1434 struct prom_t *_prom = PTRRELOC(&prom);
1435 char type[16], *path;
1436 static unsigned char default_colors[] = {
1454 const unsigned char *clut;
1456 _prom->disp_node = 0;
1458 prom_print(RELOC("Looking for displays\n"));
1459 if (RELOC(of_stdout_device) != 0) {
1460 prom_print(RELOC("OF stdout is : "));
1461 prom_print(PTRRELOC(RELOC(of_stdout_device)));
1462 prom_print(RELOC("\n"));
1464 for (node = 0; prom_next_node(&node); ) {
1466 call_prom(RELOC("getprop"), 4, 1, node, RELOC("device_type"),
1467 type, sizeof(type));
1468 if (strcmp(type, RELOC("display")) != 0)
1470 /* It seems OF doesn't null-terminate the path :-( */
1471 path = (char *) mem;
1472 memset(path, 0, 256);
1475 * leave some room at the end of the path for appending extra
1478 if ((long) call_prom(RELOC("package-to-path"), 3, 1,
1479 node, path, 250) < 0)
1481 prom_print(RELOC("found display : "));
1483 prom_print(RELOC("\n"));
1486 * If this display is the device that OF is using for stdout,
1487 * move it to the front of the list.
1489 mem += strlen(path) + 1;
1490 i = RELOC(prom_num_displays);
1491 RELOC(prom_num_displays) = i + 1;
1492 if (RELOC(of_stdout_device) != 0 && i > 0
1493 && strcmp(PTRRELOC(RELOC(of_stdout_device)), path) == 0) {
1494 for (; i > 0; --i) {
1495 RELOC(prom_display_paths[i])
1496 = RELOC(prom_display_paths[i-1]);
1497 RELOC(prom_display_nodes[i])
1498 = RELOC(prom_display_nodes[i-1]);
1500 _prom->disp_node = (ihandle)(unsigned long)node;
1502 RELOC(prom_display_paths[i]) = PTRUNRELOC(path);
1503 RELOC(prom_display_nodes[i]) = node;
1504 if (_prom->disp_node == 0)
1505 _prom->disp_node = (ihandle)(unsigned long)node;
1506 if (RELOC(prom_num_displays) >= FB_MAX)
1509 prom_print(RELOC("Opening displays...\n"));
1510 for (j = RELOC(prom_num_displays) - 1; j >= 0; j--) {
1511 path = PTRRELOC(RELOC(prom_display_paths[j]));
1512 prom_print(RELOC("opening display : "));
1514 ih = (ihandle)call_prom(RELOC("open"), 1, 1, path);
1515 if (ih == (ihandle)0 || ih == (ihandle)-1) {
1516 prom_print(RELOC("... failed\n"));
1520 prom_print(RELOC("... done\n"));
1522 /* Setup a useable color table when the appropriate
1523 * method is available. Should update this to set-colors */
1524 clut = RELOC(default_colors);
1525 for (i = 0; i < 32; i++, clut += 3)
1526 if (prom_set_color(ih, i, clut[0], clut[1],
1530 #ifdef CONFIG_LOGO_LINUX_CLUT224
1531 clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
1532 for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
1533 if (prom_set_color(ih, i + 32, clut[0], clut[1],
1536 #endif /* CONFIG_LOGO_LINUX_CLUT224 */
1539 return DOUBLEWORD_ALIGN(mem);
1542 /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
1543 static void __init *__make_room(unsigned long *mem_start, unsigned long *mem_end,
1544 unsigned long needed, unsigned long align)
1547 unsigned long offset = reloc_offset();
1549 *mem_start = ALIGN(*mem_start, align);
1550 if (*mem_start + needed > *mem_end) {
1551 #ifdef CONFIG_BLK_DEV_INITRD
1552 /* FIXME: Apple OF doesn't map unclaimed mem. If this
1553 * ever happened on G5, we'd need to fix. */
1554 unsigned long initrd_len;
1556 if (*mem_end != RELOC(initrd_start))
1557 prom_panic(RELOC("No memory for copy_device_tree"));
1559 prom_print("Huge device_tree: moving initrd\n");
1561 initrd_len = RELOC(initrd_end) - RELOC(initrd_start);
1562 *mem_end = RELOC(initrd_start) + 4 * 1024 * 1024;
1563 memmove((void *)*mem_end, (void *)RELOC(initrd_start),
1565 RELOC(initrd_start) = *mem_end;
1566 RELOC(initrd_end) = RELOC(initrd_start) + initrd_len;
1568 prom_panic(RELOC("No memory for copy_device_tree"));
1572 ret = (void *)*mem_start;
1573 *mem_start += needed;
1578 #define make_room(startp, endp, type) \
1579 __make_room(startp, endp, sizeof(type), __alignof__(type))
1582 inspect_node(phandle node, struct device_node *dad,
1583 unsigned long *mem_start, unsigned long *mem_end,
1584 struct device_node ***allnextpp)
1588 struct device_node *np;
1589 struct property *pp, **prev_propp;
1590 char *prev_name, *namep;
1591 unsigned char *valp;
1592 unsigned long offset = reloc_offset();
1594 np = make_room(mem_start, mem_end, struct device_node);
1595 memset(np, 0, sizeof(*np));
1598 **allnextpp = PTRUNRELOC(np);
1599 *allnextpp = &np->allnext;
1601 np->parent = PTRUNRELOC(dad);
1602 /* we temporarily use the `next' field as `last_child'. */
1604 dad->child = PTRUNRELOC(np);
1606 dad->next->sibling = PTRUNRELOC(np);
1610 /* get and store all properties */
1611 prev_propp = &np->properties;
1612 prev_name = RELOC("");
1614 /* 32 is max len of name including nul. */
1615 namep = make_room(mem_start, mem_end, char[32]);
1616 if ((long) call_prom(RELOC("nextprop"), 3, 1, node, prev_name,
1618 /* No more nodes: unwind alloc */
1619 *mem_start = (unsigned long)namep;
1622 /* Trim off some if we can */
1623 *mem_start = DOUBLEWORD_ALIGN((unsigned long)namep
1624 + strlen(namep) + 1);
1625 pp = make_room(mem_start, mem_end, struct property);
1626 pp->name = PTRUNRELOC(namep);
1629 pp->length = call_prom(RELOC("getproplen"), 2, 1, node, namep);
1632 if (pp->length > MAX_PROPERTY_LENGTH) {
1635 prom_print(RELOC("WARNING: ignoring large property "));
1636 /* It seems OF doesn't null-terminate the path :-( */
1637 memset(path, 0, sizeof(path));
1638 if (call_prom(RELOC("package-to-path"), 3, 1, node,
1639 path, sizeof(path)-1) > 0)
1642 prom_print(RELOC(" length 0x"));
1643 prom_print_hex(pp->length);
1648 valp = __make_room(mem_start, mem_end, pp->length, 1);
1649 pp->value = PTRUNRELOC(valp);
1650 call_prom(RELOC("getprop"), 4, 1, node, namep,valp,pp->length);
1651 *prev_propp = PTRUNRELOC(pp);
1652 prev_propp = &pp->next;
1655 /* Add a "linux_phandle" value */
1657 u32 ibm_phandle = 0;
1660 /* First see if "ibm,phandle" exists and use its value */
1662 call_prom(RELOC("getprop"), 4, 1, node, RELOC("ibm,phandle"),
1663 &ibm_phandle, sizeof(ibm_phandle));
1665 np->linux_phandle = np->node;
1667 np->linux_phandle = ibm_phandle;
1673 /* get the node's full name */
1674 namep = (char *)*mem_start;
1675 l = (long) call_prom(RELOC("package-to-path"), 3, 1, node,
1676 namep, *mem_end - *mem_start);
1678 /* Didn't fit? Get more room. */
1679 if (l+1 > *mem_end - *mem_start) {
1680 namep = __make_room(mem_start, mem_end, l+1, 1);
1681 call_prom(RELOC("package-to-path"),3,1,node,namep,l);
1683 np->full_name = PTRUNRELOC(namep);
1685 *mem_start = DOUBLEWORD_ALIGN(*mem_start + l + 1);
1688 /* do all our children */
1689 child = call_prom(RELOC("child"), 1, 1, node);
1690 while (child != (phandle)0) {
1691 inspect_node(child, np, mem_start, mem_end,
1693 child = call_prom(RELOC("peer"), 1, 1, child);
1698 * Make a copy of the device tree from the PROM.
1700 static unsigned long __init
1701 copy_device_tree(unsigned long mem_start)
1704 struct device_node **allnextp;
1705 unsigned long offset = reloc_offset();
1706 unsigned long mem_end;
1708 /* We pass mem_end-mem_start to OF: keep it well under 32-bit */
1709 mem_end = mem_start + 1024*1024*1024;
1710 #ifdef CONFIG_BLK_DEV_INITRD
1711 if (RELOC(initrd_start) && RELOC(initrd_start) > mem_start)
1712 mem_end = RELOC(initrd_start);
1713 #endif /* CONFIG_BLK_DEV_INITRD */
1715 root = call_prom(RELOC("peer"), 1, 1, (phandle)0);
1716 if (root == (phandle)0) {
1717 prom_panic(RELOC("couldn't get device tree root\n"));
1719 allnextp = &RELOC(allnodes);
1720 inspect_node(root, 0, &mem_start, &mem_end, &allnextp);
1725 /* Verify bi_recs are good */
1726 static struct bi_record * __init prom_bi_rec_verify(struct bi_record *bi_recs)
1728 struct bi_record *first, *last;
1730 unsigned long offset = reloc_offset();
1732 prom_print(RELOC("birec_verify: r6=0x"));
1733 prom_print_hex((unsigned long)bi_recs);
1735 if (bi_recs != NULL) {
1736 prom_print(RELOC(" tag=0x"));
1737 prom_print_hex(bi_recs->tag);
1740 #endif /* DEBUG_PROM */
1742 if ( bi_recs == NULL || bi_recs->tag != BI_FIRST )
1745 last = (struct bi_record *)(long)bi_recs->data[0];
1748 prom_print(RELOC(" last=0x"));
1749 prom_print_hex((unsigned long)last);
1752 prom_print(RELOC(" last_tag=0x"));
1753 prom_print_hex(last->tag);
1756 #endif /* DEBUG_PROM */
1758 if ( last == NULL || last->tag != BI_LAST )
1761 first = (struct bi_record *)(long)last->data[0];
1763 prom_print(RELOC(" first=0x"));
1764 prom_print_hex((unsigned long)first);
1766 #endif /* DEBUG_PROM */
1768 if ( first == NULL || first != bi_recs )
1774 static void __init prom_bi_rec_reserve(void)
1776 unsigned long offset = reloc_offset();
1777 struct prom_t *_prom = PTRRELOC(&prom);
1778 struct bi_record *rec;
1780 if ( _prom->bi_recs != NULL) {
1782 for ( rec=_prom->bi_recs;
1783 rec->tag != BI_LAST;
1784 rec=bi_rec_next(rec) ) {
1786 prom_print(RELOC("bi: 0x"));
1787 prom_print_hex(rec->tag);
1789 #endif /* DEBUG_PROM */
1791 #ifdef CONFIG_BLK_DEV_INITRD
1793 RELOC(initrd_start) = (unsigned long)(rec->data[0]);
1794 RELOC(initrd_end) = RELOC(initrd_start) + rec->data[1];
1796 #endif /* CONFIG_BLK_DEV_INITRD */
1799 /* The next use of this field will be after relocation
1800 * is enabled, so convert this physical address into a
1803 _prom->bi_recs = PTRUNRELOC(_prom->bi_recs);
1808 * We enter here early on, when the Open Firmware prom is still
1809 * handling exceptions and the MMU hash table for us.
1812 unsigned long __init
1813 prom_init(unsigned long r3, unsigned long r4, unsigned long pp,
1814 unsigned long r6, unsigned long r7)
1819 unsigned long offset = reloc_offset();
1824 struct systemcfg *_systemcfg;
1825 struct paca_struct *_xPaca = PTRRELOC(&paca[0]);
1826 struct prom_t *_prom = PTRRELOC(&prom);
1828 /* First zero the BSS -- use memset, some arches don't have
1830 memset(PTRRELOC(&__bss_start), 0, __bss_stop - __bss_start);
1832 /* Setup systemcfg and NACA pointers now */
1833 RELOC(systemcfg) = _systemcfg = (struct systemcfg *)(SYSTEMCFG_VIRT_ADDR - offset);
1834 RELOC(naca) = (struct naca_struct *)(NACA_VIRT_ADDR - offset);
1836 /* Init interface to Open Firmware and pickup bi-recs */
1837 prom_init_client_services(pp);
1839 /* Init prom stdout device */
1843 prom_print(RELOC("klimit=0x"));
1844 prom_print_hex(RELOC(klimit));
1846 prom_print(RELOC("offset=0x"));
1847 prom_print_hex(offset);
1849 prom_print(RELOC("->mem=0x"));
1850 prom_print_hex(RELOC(klimit) - offset);
1852 #endif /* DEBUG_PROM */
1854 /* check out if we have bi_recs */
1855 _prom->bi_recs = prom_bi_rec_verify((struct bi_record *)r6);
1856 if ( _prom->bi_recs != NULL ) {
1857 RELOC(klimit) = PTRUNRELOC((unsigned long)_prom->bi_recs +
1858 _prom->bi_recs->data[1]);
1860 prom_print(RELOC("bi_recs=0x"));
1861 prom_print_hex((unsigned long)_prom->bi_recs);
1863 prom_print(RELOC("new mem=0x"));
1864 prom_print_hex(RELOC(klimit) - offset);
1866 #endif /* DEBUG_PROM */
1869 /* If we don't have birec's or didn't find them, check for an initrd
1870 * using the "yaboot" way
1872 #ifdef CONFIG_BLK_DEV_INITRD
1873 if ( _prom->bi_recs == NULL && r3 && r4 && r4 != 0xdeadbeef) {
1874 RELOC(initrd_start) = (r3 >= KERNELBASE) ? __pa(r3) : r3;
1875 RELOC(initrd_end) = RELOC(initrd_start) + r4;
1876 RELOC(initrd_below_start_ok) = 1;
1878 #endif /* CONFIG_BLK_DEV_INITRD */
1880 /* Default machine type. */
1881 _systemcfg->platform = prom_find_machine_type();
1883 /* On pSeries, copy the CPU hold code */
1884 if (_systemcfg->platform == PLATFORM_PSERIES)
1885 copy_and_flush(0, KERNELBASE - offset, 0x100, 0);
1887 /* Start storing things at klimit */
1888 mem = RELOC(klimit) - offset;
1890 /* Get the full OF pathname of the stdout device */
1893 call_prom(RELOC("instance-to-path"), 3, 1, _prom->stdout, p, 255);
1894 RELOC(of_stdout_device) = PTRUNRELOC(p);
1895 mem += strlen(p) + 1;
1898 call_prom(RELOC("getprop"), 4, 1,
1899 _prom->root, RELOC("#size-cells"),
1900 &getprop_rval, sizeof(getprop_rval));
1901 _prom->encode_phys_size = (getprop_rval == 1) ? 32 : 64;
1903 /* Determine which cpu is actually running right _now_ */
1904 if ((long)call_prom(RELOC("getprop"), 4, 1, _prom->chosen,
1905 RELOC("cpu"), &getprop_rval,
1906 sizeof(getprop_rval)) <= 0)
1907 prom_panic(RELOC("cannot find boot cpu"));
1909 prom_cpu = (ihandle)(unsigned long)getprop_rval;
1910 cpu_pkg = call_prom(RELOC("instance-to-package"), 1, 1, prom_cpu);
1911 call_prom(RELOC("getprop"), 4, 1,
1912 cpu_pkg, RELOC("reg"),
1913 &getprop_rval, sizeof(getprop_rval));
1914 _prom->cpu = (int)(unsigned long)getprop_rval;
1915 _xPaca[0].xHwProcNum = _prom->cpu;
1917 RELOC(boot_cpuid) = 0;
1920 prom_print(RELOC("Booting CPU hw index = 0x"));
1921 prom_print_hex(_prom->cpu);
1925 /* Get the boot device and translate it to a full OF pathname. */
1927 l = (long) call_prom(RELOC("getprop"), 4, 1, _prom->chosen,
1928 RELOC("bootpath"), p, 1<<20);
1930 p[l] = 0; /* should already be null-terminated */
1931 RELOC(bootpath) = PTRUNRELOC(p);
1935 call_prom(RELOC("canon"), 3, 1, p, d, 1<<20);
1936 RELOC(bootdevice) = PTRUNRELOC(d);
1937 mem = DOUBLEWORD_ALIGN(mem + strlen(d) + 1);
1940 RELOC(cmd_line[0]) = 0;
1941 if ((long)_prom->chosen > 0) {
1942 call_prom(RELOC("getprop"), 4, 1, _prom->chosen,
1943 RELOC("bootargs"), p, sizeof(cmd_line));
1944 if (p != NULL && p[0] != 0)
1945 strlcpy(RELOC(cmd_line), p, sizeof(cmd_line));
1948 early_cmdline_parse();
1950 prom_initialize_lmb();
1952 prom_bi_rec_reserve();
1954 mem = check_display(mem);
1956 if (_systemcfg->platform != PLATFORM_POWERMAC)
1957 prom_instantiate_rtas();
1959 /* Initialize some system info into the Naca early... */
1960 prom_initialize_naca();
1964 /* If we are on an SMP machine, then we *MUST* do the
1965 * following, regardless of whether we have an SMP
1968 prom_hold_cpus(mem);
1971 prom_print(RELOC("after basic inits, mem=0x"));
1972 prom_print_hex(mem);
1974 #ifdef CONFIG_BLK_DEV_INITRD
1975 prom_print(RELOC("initrd_start=0x"));
1976 prom_print_hex(RELOC(initrd_start));
1978 prom_print(RELOC("initrd_end=0x"));
1979 prom_print_hex(RELOC(initrd_end));
1981 #endif /* CONFIG_BLK_DEV_INITRD */
1982 prom_print(RELOC("copying OF device tree...\n"));
1983 #endif /* DEBUG_PROM */
1984 mem = copy_device_tree(mem);
1986 RELOC(klimit) = mem + offset;
1989 prom_print(RELOC("new klimit is\n"));
1990 prom_print(RELOC("klimit=0x"));
1991 prom_print_hex(RELOC(klimit));
1992 prom_print(RELOC(" ->mem=0x\n"));
1993 prom_print(RELOC("klimit=0x"));
1994 prom_print_hex(mem);
1996 #endif /* DEBUG_PROM */
1998 lmb_reserve(0, __pa(RELOC(klimit)));
2000 #ifdef CONFIG_BLK_DEV_INITRD
2001 if (RELOC(initrd_start)) {
2002 unsigned long initrd_len;
2003 initrd_len = RELOC(initrd_end) - RELOC(initrd_start);
2005 /* Move initrd if it's where we're going to copy kernel. */
2006 if (RELOC(initrd_start) < __pa(RELOC(klimit))) {
2007 memmove((void *)mem, (void *)RELOC(initrd_start),
2009 RELOC(initrd_start) = mem;
2010 RELOC(initrd_end) = mem + initrd_len;
2013 lmb_reserve(RELOC(initrd_start), initrd_len);
2015 #endif /* CONFIG_BLK_DEV_INITRD */
2017 if (_systemcfg->platform == PLATFORM_PSERIES)
2018 prom_initialize_tce_table();
2020 #ifdef CONFIG_PMAC_DART
2021 if (_systemcfg->platform == PLATFORM_POWERMAC)
2022 prom_initialize_dart_table();
2025 #ifdef CONFIG_BOOTX_TEXT
2026 if(_prom->disp_node) {
2027 prom_print(RELOC("Setting up bi display...\n"));
2028 setup_disp_fake_bi(_prom->disp_node);
2030 #endif /* CONFIG_BOOTX_TEXT */
2032 prom_print(RELOC("Calling quiesce ...\n"));
2033 call_prom(RELOC("quiesce"), 0, 0);
2034 phys = KERNELBASE - offset;
2036 #ifdef CONFIG_BLK_DEV_INITRD
2037 /* If we had an initrd, we convert its address to virtual */
2038 if (RELOC(initrd_start)) {
2039 RELOC(initrd_start) = (unsigned long)__va(RELOC(initrd_start));
2040 RELOC(initrd_end) = (unsigned long)__va(RELOC(initrd_end));
2042 #endif /* CONFIG_BLK_DEV_INITRD */
2044 prom_print(RELOC("returning from prom_init\n"));
2049 * Find the device_node with a given phandle.
2051 static struct device_node * __devinit
2052 find_phandle(phandle ph)
2054 struct device_node *np;
2056 for (np = allnodes; np != 0; np = np->allnext)
2057 if (np->linux_phandle == ph)
2063 * Find the interrupt parent of a node.
2065 static struct device_node * __devinit
2066 intr_parent(struct device_node *p)
2070 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
2073 return find_phandle(*parp);
2077 * Find out the size of each entry of the interrupts property
2080 static int __devinit
2081 prom_n_intr_cells(struct device_node *np)
2083 struct device_node *p;
2086 for (p = np; (p = intr_parent(p)) != NULL; ) {
2087 icp = (unsigned int *)
2088 get_property(p, "#interrupt-cells", NULL);
2091 if (get_property(p, "interrupt-controller", NULL) != NULL
2092 || get_property(p, "interrupt-map", NULL) != NULL) {
2093 printk("oops, node %s doesn't have #interrupt-cells\n",
2099 printk("prom_n_intr_cells failed for %s\n", np->full_name);
2105 * Map an interrupt from a device up to the platform interrupt
2108 static int __devinit
2109 map_interrupt(unsigned int **irq, struct device_node **ictrler,
2110 struct device_node *np, unsigned int *ints, int nintrc)
2112 struct device_node *p, *ipar;
2113 unsigned int *imap, *imask, *ip;
2114 int i, imaplen, match;
2115 int newintrc, newaddrc;
2119 reg = (unsigned int *) get_property(np, "reg", NULL);
2120 naddrc = prom_n_addr_cells(np);
2121 p = intr_parent(np);
2123 if (get_property(p, "interrupt-controller", NULL) != NULL)
2124 /* this node is an interrupt controller, stop here */
2126 imap = (unsigned int *)
2127 get_property(p, "interrupt-map", &imaplen);
2132 imask = (unsigned int *)
2133 get_property(p, "interrupt-map-mask", NULL);
2134 if (imask == NULL) {
2135 printk("oops, %s has interrupt-map but no mask\n",
2139 imaplen /= sizeof(unsigned int);
2142 while (imaplen > 0 && !match) {
2143 /* check the child-interrupt field */
2145 for (i = 0; i < naddrc && match; ++i)
2146 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
2147 for (; i < naddrc + nintrc && match; ++i)
2148 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
2149 imap += naddrc + nintrc;
2150 imaplen -= naddrc + nintrc;
2151 /* grab the interrupt parent */
2152 ipar = find_phandle((phandle) *imap++);
2155 printk("oops, no int parent %x in map of %s\n",
2156 imap[-1], p->full_name);
2159 /* find the parent's # addr and intr cells */
2160 ip = (unsigned int *)
2161 get_property(ipar, "#interrupt-cells", NULL);
2163 printk("oops, no #interrupt-cells on %s\n",
2168 ip = (unsigned int *)
2169 get_property(ipar, "#address-cells", NULL);
2170 newaddrc = (ip == NULL)? 0: *ip;
2171 imap += newaddrc + newintrc;
2172 imaplen -= newaddrc + newintrc;
2175 printk("oops, error decoding int-map on %s, len=%d\n",
2176 p->full_name, imaplen);
2181 printk("oops, no match in %s int-map for %s\n",
2182 p->full_name, np->full_name);
2189 ints = imap - nintrc;
2190 reg = ints - naddrc;
2194 printk("hmmm, int tree for %s doesn't have ctrler\n",
2202 static unsigned long __init
2203 finish_node_interrupts(struct device_node *np, unsigned long mem_start,
2207 int intlen, intrcells;
2209 unsigned int *irq, virq;
2210 struct device_node *ic;
2212 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
2215 intrcells = prom_n_intr_cells(np);
2216 intlen /= intrcells * sizeof(unsigned int);
2217 np->n_intrs = intlen;
2218 np->intrs = (struct interrupt_info *) mem_start;
2219 mem_start += intlen * sizeof(struct interrupt_info);
2224 for (i = 0; i < intlen; ++i) {
2225 np->intrs[i].line = 0;
2226 np->intrs[i].sense = 1;
2227 n = map_interrupt(&irq, &ic, np, ints, intrcells);
2230 virq = virt_irq_create_mapping(irq[0]);
2231 if (virq == NO_IRQ) {
2232 printk(KERN_CRIT "Could not allocate interrupt "
2233 "number for %s\n", np->full_name);
2235 np->intrs[i].line = irq_offset_up(virq);
2237 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
2238 if (systemcfg->platform == PLATFORM_POWERMAC && ic && ic->parent) {
2239 char *name = get_property(ic->parent, "name", NULL);
2240 if (name && !strcmp(name, "u3"))
2241 np->intrs[i].line += 128;
2244 np->intrs[i].sense = irq[1];
2246 printk("hmmm, got %d intr cells for %s:", n,
2248 for (j = 0; j < n; ++j)
2249 printk(" %d", irq[j]);
2258 static unsigned long __init
2259 interpret_pci_props(struct device_node *np, unsigned long mem_start,
2260 int naddrc, int nsizec, int measure_only)
2262 struct address_range *adr;
2263 struct pci_reg_property *pci_addrs;
2266 pci_addrs = (struct pci_reg_property *)
2267 get_property(np, "assigned-addresses", &l);
2268 if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
2270 adr = (struct address_range *) mem_start;
2271 while ((l -= sizeof(struct pci_reg_property)) >= 0) {
2272 if (!measure_only) {
2273 adr[i].space = pci_addrs[i].addr.a_hi;
2274 adr[i].address = pci_addrs[i].addr.a_lo;
2275 adr[i].size = pci_addrs[i].size_lo;
2281 mem_start += i * sizeof(struct address_range);
2286 static unsigned long __init
2287 interpret_dbdma_props(struct device_node *np, unsigned long mem_start,
2288 int naddrc, int nsizec, int measure_only)
2290 struct reg_property32 *rp;
2291 struct address_range *adr;
2292 unsigned long base_address;
2294 struct device_node *db;
2297 if (!measure_only) {
2298 for (db = np->parent; db != NULL; db = db->parent) {
2299 if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
2300 base_address = db->addrs[0].address;
2306 rp = (struct reg_property32 *) get_property(np, "reg", &l);
2307 if (rp != 0 && l >= sizeof(struct reg_property32)) {
2309 adr = (struct address_range *) mem_start;
2310 while ((l -= sizeof(struct reg_property32)) >= 0) {
2311 if (!measure_only) {
2313 adr[i].address = rp[i].address + base_address;
2314 adr[i].size = rp[i].size;
2320 mem_start += i * sizeof(struct address_range);
2326 static unsigned long __init
2327 interpret_macio_props(struct device_node *np, unsigned long mem_start,
2328 int naddrc, int nsizec, int measure_only)
2330 struct reg_property32 *rp;
2331 struct address_range *adr;
2332 unsigned long base_address;
2334 struct device_node *db;
2337 if (!measure_only) {
2338 for (db = np->parent; db != NULL; db = db->parent) {
2339 if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
2340 base_address = db->addrs[0].address;
2346 rp = (struct reg_property32 *) get_property(np, "reg", &l);
2347 if (rp != 0 && l >= sizeof(struct reg_property32)) {
2349 adr = (struct address_range *) mem_start;
2350 while ((l -= sizeof(struct reg_property32)) >= 0) {
2351 if (!measure_only) {
2353 adr[i].address = rp[i].address + base_address;
2354 adr[i].size = rp[i].size;
2360 mem_start += i * sizeof(struct address_range);
2366 static unsigned long __init
2367 interpret_isa_props(struct device_node *np, unsigned long mem_start,
2368 int naddrc, int nsizec, int measure_only)
2370 struct isa_reg_property *rp;
2371 struct address_range *adr;
2374 rp = (struct isa_reg_property *) get_property(np, "reg", &l);
2375 if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
2377 adr = (struct address_range *) mem_start;
2378 while ((l -= sizeof(struct reg_property)) >= 0) {
2379 if (!measure_only) {
2380 adr[i].space = rp[i].space;
2381 adr[i].address = rp[i].address;
2382 adr[i].size = rp[i].size;
2388 mem_start += i * sizeof(struct address_range);
2394 static unsigned long __init
2395 interpret_root_props(struct device_node *np, unsigned long mem_start,
2396 int naddrc, int nsizec, int measure_only)
2398 struct address_range *adr;
2401 int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
2403 rp = (unsigned int *) get_property(np, "reg", &l);
2404 if (rp != 0 && l >= rpsize) {
2406 adr = (struct address_range *) mem_start;
2407 while ((l -= rpsize) >= 0) {
2408 if (!measure_only) {
2410 adr[i].address = rp[naddrc - 1];
2411 adr[i].size = rp[naddrc + nsizec - 1];
2414 rp += naddrc + nsizec;
2418 mem_start += i * sizeof(struct address_range);
2424 static unsigned long __init
2425 finish_node(struct device_node *np, unsigned long mem_start,
2426 interpret_func *ifunc, int naddrc, int nsizec, int measure_only)
2428 struct device_node *child;
2431 np->name = get_property(np, "name", 0);
2432 np->type = get_property(np, "device_type", 0);
2435 np->name = "<NULL>";
2437 np->type = "<NULL>";
2439 /* get the device addresses and interrupts */
2441 mem_start = ifunc(np, mem_start, naddrc, nsizec, measure_only);
2443 mem_start = finish_node_interrupts(np, mem_start, measure_only);
2445 /* Look for #address-cells and #size-cells properties. */
2446 ip = (int *) get_property(np, "#address-cells", 0);
2449 ip = (int *) get_property(np, "#size-cells", 0);
2453 /* the f50 sets the name to 'display' and 'compatible' to what we
2454 * expect for the name -- Cort
2456 if (!strcmp(np->name, "display"))
2457 np->name = get_property(np, "compatible", 0);
2459 if (!strcmp(np->name, "device-tree") || np->parent == NULL)
2460 ifunc = interpret_root_props;
2461 else if (np->type == 0)
2463 else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
2464 ifunc = interpret_pci_props;
2465 else if (!strcmp(np->type, "dbdma"))
2466 ifunc = interpret_dbdma_props;
2467 else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props)
2468 ifunc = interpret_macio_props;
2469 else if (!strcmp(np->type, "isa"))
2470 ifunc = interpret_isa_props;
2471 else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
2472 ifunc = interpret_root_props;
2473 else if (!((ifunc == interpret_dbdma_props
2474 || ifunc == interpret_macio_props)
2475 && (!strcmp(np->type, "escc")
2476 || !strcmp(np->type, "media-bay"))))
2479 for (child = np->child; child != NULL; child = child->sibling)
2480 mem_start = finish_node(child, mem_start, ifunc,
2481 naddrc, nsizec, measure_only);
2487 * finish_device_tree is called once things are running normally
2488 * (i.e. with text and data mapped to the address they were linked at).
2489 * It traverses the device tree and fills in the name, type,
2490 * {n_}addrs and {n_}intrs fields of each node.
2493 finish_device_tree(void)
2495 unsigned long mem = klimit;
2499 dev_tree_size = finish_node(allnodes, 0, NULL, 0, 0, 1);
2500 mem = (long)abs_to_virt(lmb_alloc(dev_tree_size,
2501 __alignof__(struct device_node)));
2502 if (finish_node(allnodes, mem, NULL, 0, 0, 0) != mem + dev_tree_size)
2504 rtas.dev = of_find_node_by_name(NULL, "rtas");
2508 prom_n_addr_cells(struct device_node* np)
2514 ip = (int *) get_property(np, "#address-cells", 0);
2517 } while (np->parent);
2518 /* No #address-cells property for the root node, default to 1 */
2523 prom_n_size_cells(struct device_node* np)
2529 ip = (int *) get_property(np, "#size-cells", 0);
2532 } while (np->parent);
2533 /* No #size-cells property for the root node, default to 1 */
2538 * Work out the sense (active-low level / active-high edge)
2539 * of each interrupt from the device tree.
2542 prom_get_irq_senses(unsigned char *senses, int off, int max)
2544 struct device_node *np;
2547 /* default to level-triggered */
2548 memset(senses, 1, max - off);
2550 for (np = allnodes; np != 0; np = np->allnext) {
2551 for (j = 0; j < np->n_intrs; j++) {
2552 i = np->intrs[j].line;
2553 if (i >= off && i < max)
2554 senses[i-off] = np->intrs[j].sense;
2560 * Construct and return a list of the device_nodes with a given name.
2562 struct device_node *
2563 find_devices(const char *name)
2565 struct device_node *head, **prevp, *np;
2568 for (np = allnodes; np != 0; np = np->allnext) {
2569 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
2579 * Construct and return a list of the device_nodes with a given type.
2581 struct device_node *
2582 find_type_devices(const char *type)
2584 struct device_node *head, **prevp, *np;
2587 for (np = allnodes; np != 0; np = np->allnext) {
2588 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
2598 * Returns all nodes linked together
2600 struct device_node *
2601 find_all_nodes(void)
2603 struct device_node *head, **prevp, *np;
2606 for (np = allnodes; np != 0; np = np->allnext) {
2614 /* Checks if the given "compat" string matches one of the strings in
2615 * the device's "compatible" property
2618 device_is_compatible(struct device_node *device, const char *compat)
2623 cp = (char *) get_property(device, "compatible", &cplen);
2627 if (strncasecmp(cp, compat, strlen(compat)) == 0)
2639 * Indicates whether the root node has a given value in its
2640 * compatible property.
2643 machine_is_compatible(const char *compat)
2645 struct device_node *root;
2648 root = of_find_node_by_path("/");
2650 rc = device_is_compatible(root, compat);
2657 * Construct and return a list of the device_nodes with a given type
2658 * and compatible property.
2660 struct device_node *
2661 find_compatible_devices(const char *type, const char *compat)
2663 struct device_node *head, **prevp, *np;
2666 for (np = allnodes; np != 0; np = np->allnext) {
2668 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
2670 if (device_is_compatible(np, compat)) {
2680 * Find the device_node with a given full_name.
2682 struct device_node *
2683 find_path_device(const char *path)
2685 struct device_node *np;
2687 for (np = allnodes; np != 0; np = np->allnext)
2688 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
2695 * New implementation of the OF "find" APIs, return a refcounted
2696 * object, call of_node_put() when done. The device tree and list
2697 * are protected by a rw_lock.
2699 * Note that property management will need some locking as well,
2700 * this isn't dealt with yet.
2705 * of_find_node_by_name - Find a node by its "name" property
2706 * @from: The node to start searching from or NULL, the node
2707 * you pass will not be searched, only the next one
2708 * will; typically, you pass what the previous call
2709 * returned. of_node_put() will be called on it
2710 * @name: The name string to match against
2712 * Returns a node pointer with refcount incremented, use
2713 * of_node_put() on it when done.
2715 struct device_node *of_find_node_by_name(struct device_node *from,
2718 struct device_node *np;
2720 read_lock(&devtree_lock);
2721 np = from ? from->allnext : allnodes;
2722 for (; np != 0; np = np->allnext)
2723 if (np->name != 0 && strcasecmp(np->name, name) == 0
2728 read_unlock(&devtree_lock);
2731 EXPORT_SYMBOL(of_find_node_by_name);
2734 * of_find_node_by_type - Find a node by its "device_type" property
2735 * @from: The node to start searching from or NULL, the node
2736 * you pass will not be searched, only the next one
2737 * will; typically, you pass what the previous call
2738 * returned. of_node_put() will be called on it
2739 * @name: The type string to match against
2741 * Returns a node pointer with refcount incremented, use
2742 * of_node_put() on it when done.
2744 struct device_node *of_find_node_by_type(struct device_node *from,
2747 struct device_node *np;
2749 read_lock(&devtree_lock);
2750 np = from ? from->allnext : allnodes;
2751 for (; np != 0; np = np->allnext)
2752 if (np->type != 0 && strcasecmp(np->type, type) == 0
2757 read_unlock(&devtree_lock);
2760 EXPORT_SYMBOL(of_find_node_by_type);
2763 * of_find_compatible_node - Find a node based on type and one of the
2764 * tokens in its "compatible" property
2765 * @from: The node to start searching from or NULL, the node
2766 * you pass will not be searched, only the next one
2767 * will; typically, you pass what the previous call
2768 * returned. of_node_put() will be called on it
2769 * @type: The type string to match "device_type" or NULL to ignore
2770 * @compatible: The string to match to one of the tokens in the device
2771 * "compatible" list.
2773 * Returns a node pointer with refcount incremented, use
2774 * of_node_put() on it when done.
2776 struct device_node *of_find_compatible_node(struct device_node *from,
2777 const char *type, const char *compatible)
2779 struct device_node *np;
2781 read_lock(&devtree_lock);
2782 np = from ? from->allnext : allnodes;
2783 for (; np != 0; np = np->allnext) {
2785 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
2787 if (device_is_compatible(np, compatible) && of_node_get(np))
2792 read_unlock(&devtree_lock);
2795 EXPORT_SYMBOL(of_find_compatible_node);
2798 * of_find_node_by_path - Find a node matching a full OF path
2799 * @path: The full path to match
2801 * Returns a node pointer with refcount incremented, use
2802 * of_node_put() on it when done.
2804 struct device_node *of_find_node_by_path(const char *path)
2806 struct device_node *np = allnodes;
2808 read_lock(&devtree_lock);
2809 for (; np != 0; np = np->allnext)
2810 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
2813 read_unlock(&devtree_lock);
2816 EXPORT_SYMBOL(of_find_node_by_path);
2819 * of_find_all_nodes - Get next node in global list
2820 * @prev: Previous node or NULL to start iteration
2821 * of_node_put() will be called on it
2823 * Returns a node pointer with refcount incremented, use
2824 * of_node_put() on it when done.
2826 struct device_node *of_find_all_nodes(struct device_node *prev)
2828 struct device_node *np;
2830 read_lock(&devtree_lock);
2831 np = prev ? prev->allnext : allnodes;
2832 for (; np != 0; np = np->allnext)
2833 if (of_node_get(np))
2837 read_unlock(&devtree_lock);
2840 EXPORT_SYMBOL(of_find_all_nodes);
2843 * of_get_parent - Get a node's parent if any
2844 * @node: Node to get parent
2846 * Returns a node pointer with refcount incremented, use
2847 * of_node_put() on it when done.
2849 struct device_node *of_get_parent(const struct device_node *node)
2851 struct device_node *np;
2856 read_lock(&devtree_lock);
2857 np = of_node_get(node->parent);
2858 read_unlock(&devtree_lock);
2861 EXPORT_SYMBOL(of_get_parent);
2864 * of_get_next_child - Iterate a node childs
2865 * @node: parent node
2866 * @prev: previous child of the parent node, or NULL to get first
2868 * Returns a node pointer with refcount incremented, use
2869 * of_node_put() on it when done.
2871 struct device_node *of_get_next_child(const struct device_node *node,
2872 struct device_node *prev)
2874 struct device_node *next;
2876 read_lock(&devtree_lock);
2877 next = prev ? prev->sibling : node->child;
2878 for (; next != 0; next = next->sibling)
2879 if (of_node_get(next))
2883 read_unlock(&devtree_lock);
2886 EXPORT_SYMBOL(of_get_next_child);
2889 * of_node_get - Increment refcount of a node
2890 * @node: Node to inc refcount, NULL is supported to
2891 * simplify writing of callers
2893 * Returns the node itself or NULL if gone.
2895 struct device_node *of_node_get(struct device_node *node)
2897 if (node && !OF_IS_STALE(node)) {
2898 atomic_inc(&node->_users);
2903 EXPORT_SYMBOL(of_node_get);
2906 * of_node_cleanup - release a dynamically allocated node
2907 * @arg: Node to be released
2909 static void of_node_cleanup(struct device_node *node)
2911 struct property *prop = node->properties;
2913 if (!OF_IS_DYNAMIC(node))
2916 struct property *next = prop->next;
2924 kfree(node->full_name);
2929 * of_node_put - Decrement refcount of a node
2930 * @node: Node to dec refcount, NULL is supported to
2931 * simplify writing of callers
2934 void of_node_put(struct device_node *node)
2939 WARN_ON(0 == atomic_read(&node->_users));
2941 if (OF_IS_STALE(node)) {
2942 if (atomic_dec_and_test(&node->_users)) {
2943 of_node_cleanup(node);
2948 atomic_dec(&node->_users);
2950 EXPORT_SYMBOL(of_node_put);
2953 * derive_parent - basically like dirname(1)
2954 * @path: the full_name of a node to be added to the tree
2956 * Returns the node which should be the parent of the node
2957 * described by path. E.g., for path = "/foo/bar", returns
2958 * the node with full_name = "/foo".
2960 static struct device_node *derive_parent(const char *path)
2962 struct device_node *parent = NULL;
2963 char *parent_path = "/";
2964 size_t parent_path_len = strrchr(path, '/') - path + 1;
2966 /* reject if path is "/" */
2967 if (!strcmp(path, "/"))
2970 if (strrchr(path, '/') != path) {
2971 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
2974 strlcpy(parent_path, path, parent_path_len);
2976 parent = of_find_node_by_path(parent_path);
2977 if (strcmp(parent_path, "/"))
2983 * Routines for "runtime" addition and removal of device tree nodes.
2985 #ifdef CONFIG_PROC_DEVICETREE
2987 * Add a node to /proc/device-tree.
2989 static void add_node_proc_entries(struct device_node *np)
2991 struct proc_dir_entry *ent;
2993 ent = proc_mkdir(strrchr(np->full_name, '/') + 1, np->parent->pde);
2995 proc_device_tree_add_node(np, ent);
2998 static void remove_node_proc_entries(struct device_node *np)
3000 struct property *pp = np->properties;
3001 struct device_node *parent = np->parent;
3004 remove_proc_entry(pp->name, np->pde);
3008 /* Assuming that symlinks have the same parent directory as
3012 remove_proc_entry(np->name_link->name, parent->pde);
3014 remove_proc_entry(np->addr_link->name, parent->pde);
3016 remove_proc_entry(np->pde->name, parent->pde);
3018 #else /* !CONFIG_PROC_DEVICETREE */
3019 static void add_node_proc_entries(struct device_node *np)
3024 static void remove_node_proc_entries(struct device_node *np)
3028 #endif /* CONFIG_PROC_DEVICETREE */
3031 * Fix up n_intrs and intrs fields in a new device node
3034 static int of_finish_dynamic_node_interrupts(struct device_node *node)
3036 int intrcells, intlen, i;
3037 unsigned *irq, *ints, virq;
3038 struct device_node *ic;
3040 ints = (unsigned int *)get_property(node, "interrupts", &intlen);
3041 intrcells = prom_n_intr_cells(node);
3042 intlen /= intrcells * sizeof(unsigned int);
3043 node->n_intrs = intlen;
3044 node->intrs = kmalloc(sizeof(struct interrupt_info) * intlen,
3049 for (i = 0; i < intlen; ++i) {
3051 node->intrs[i].line = 0;
3052 node->intrs[i].sense = 1;
3053 n = map_interrupt(&irq, &ic, node, ints, intrcells);
3056 virq = virt_irq_create_mapping(irq[0]);
3057 if (virq == NO_IRQ) {
3058 printk(KERN_CRIT "Could not allocate interrupt "
3059 "number for %s\n", node->full_name);
3062 node->intrs[i].line = irq_offset_up(virq);
3064 node->intrs[i].sense = irq[1];
3066 printk(KERN_DEBUG "hmmm, got %d intr cells for %s:", n,
3068 for (j = 0; j < n; ++j)
3069 printk(" %d", irq[j]);
3078 * Fix up the uninitialized fields in a new device node:
3079 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
3081 * A lot of boot-time code is duplicated here, because functions such
3082 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
3085 * This should probably be split up into smaller chunks.
3088 static int of_finish_dynamic_node(struct device_node *node)
3090 struct device_node *parent = of_get_parent(node);
3093 phandle *ibm_phandle;
3095 node->name = get_property(node, "name", 0);
3096 node->type = get_property(node, "device_type", 0);
3103 /* We don't support that function on PowerMac, at least
3106 if (systemcfg->platform == PLATFORM_POWERMAC)
3109 /* fix up new node's linux_phandle field */
3110 if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
3111 node->linux_phandle = *ibm_phandle;
3113 /* do the work of interpret_pci_props */
3114 if (parent->type && !strcmp(parent->type, "pci")) {
3115 struct address_range *adr;
3116 struct pci_reg_property *pci_addrs;
3119 pci_addrs = (struct pci_reg_property *)
3120 get_property(node, "assigned-addresses", &l);
3121 if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
3123 adr = kmalloc(sizeof(struct address_range) *
3124 (l / sizeof(struct pci_reg_property)),
3130 while ((l -= sizeof(struct pci_reg_property)) >= 0) {
3131 adr[i].space = pci_addrs[i].addr.a_hi;
3132 adr[i].address = pci_addrs[i].addr.a_lo;
3133 adr[i].size = pci_addrs[i].size_lo;
3141 /* now do the work of finish_node_interrupts */
3142 if (get_property(node, "interrupts", 0)) {
3143 err = of_finish_dynamic_node_interrupts(node);
3147 /* now do the rough equivalent of update_dn_pci_info, this
3148 * probably is not correct for phb's, but should work for
3152 node->phb = parent->phb;
3154 regs = (u32 *)get_property(node, "reg", 0);
3156 node->busno = (regs[0] >> 16) & 0xff;
3157 node->devfn = (regs[0] >> 8) & 0xff;
3160 /* fixing up iommu_table */
3162 if(strcmp(node->name, "pci") == 0 &&
3163 get_property(node, "ibm,dma-window", NULL)) {
3164 node->bussubno = node->busno;
3165 iommu_devnode_init(node);
3168 node->iommu_table = parent->iommu_table;
3171 of_node_put(parent);
3176 * Given a path and a property list, construct an OF device node, add
3177 * it to the device tree and global list, and place it in
3178 * /proc/device-tree. This function may sleep.
3180 int of_add_node(const char *path, struct property *proplist)
3182 struct device_node *np;
3185 np = kmalloc(sizeof(struct device_node), GFP_KERNEL);
3189 memset(np, 0, sizeof(*np));
3191 np->full_name = kmalloc(strlen(path) + 1, GFP_KERNEL);
3192 if (!np->full_name) {
3196 strcpy(np->full_name, path);
3198 np->properties = proplist;
3199 OF_MARK_DYNAMIC(np);
3201 np->parent = derive_parent(path);
3204 return -EINVAL; /* could also be ENOMEM, though */
3207 if (0 != (err = of_finish_dynamic_node(np))) {
3212 write_lock(&devtree_lock);
3213 np->sibling = np->parent->child;
3214 np->allnext = allnodes;
3215 np->parent->child = np;
3217 write_unlock(&devtree_lock);
3219 add_node_proc_entries(np);
3221 of_node_put(np->parent);
3227 * Remove an OF device node from the system.
3228 * Caller should have already "gotten" np.
3230 int of_remove_node(struct device_node *np)
3232 struct device_node *parent, *child;
3234 parent = of_get_parent(np);
3238 if ((child = of_get_next_child(np, NULL))) {
3243 write_lock(&devtree_lock);
3245 remove_node_proc_entries(np);
3247 allnodes = np->allnext;
3249 struct device_node *prev;
3250 for (prev = allnodes;
3251 prev->allnext != np;
3252 prev = prev->allnext)
3254 prev->allnext = np->allnext;
3257 if (parent->child == np)
3258 parent->child = np->sibling;
3260 struct device_node *prevsib;
3261 for (prevsib = np->parent->child;
3262 prevsib->sibling != np;
3263 prevsib = prevsib->sibling)
3265 prevsib->sibling = np->sibling;
3267 write_unlock(&devtree_lock);
3268 of_node_put(parent);
3273 * Find a property with a given name for a given node
3274 * and return the value.
3277 get_property(struct device_node *np, const char *name, int *lenp)
3279 struct property *pp;
3281 for (pp = np->properties; pp != 0; pp = pp->next)
3282 if (strcmp(pp->name, name) == 0) {
3291 * Add a property to a node
3294 prom_add_property(struct device_node* np, struct property* prop)
3296 struct property **next = &np->properties;
3300 next = &(*next)->next;
3306 print_properties(struct device_node *np)
3308 struct property *pp;
3312 for (pp = np->properties; pp != 0; pp = pp->next) {
3313 printk(KERN_INFO "%s", pp->name);
3314 for (i = strlen(pp->name); i < 16; ++i)
3316 cp = (char *) pp->value;
3317 for (i = pp->length; i > 0; --i, ++cp)
3318 if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
3319 || (i == 1 && *cp != 0))
3321 if (i == 0 && pp->length > 1) {
3322 /* looks like a string */
3323 printk(" %s\n", (char *) pp->value);
3325 /* dump it in hex */
3329 if (pp->length % 4 == 0) {
3330 unsigned int *p = (unsigned int *) pp->value;
3333 for (i = 0; i < n; ++i) {
3334 if (i != 0 && (i % 4) == 0)
3336 printk(" %08x", *p++);
3339 unsigned char *bp = pp->value;
3341 for (i = 0; i < n; ++i) {
3342 if (i != 0 && (i % 16) == 0)
3344 printk(" %02x", *bp++);
3348 if (pp->length > 64)
3349 printk(" ... (length = %d)\n",