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.
21 #include <linux/config.h>
22 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/init.h>
25 #include <linux/version.h>
26 #include <linux/threads.h>
27 #include <linux/spinlock.h>
28 #include <linux/types.h>
29 #include <linux/pci.h>
30 #include <linux/proc_fs.h>
31 #include <linux/stringify.h>
32 #include <linux/delay.h>
33 #include <linux/initrd.h>
37 #include <asm/abs_addr.h>
39 #include <asm/processor.h>
43 #include <asm/system.h>
45 #include <asm/pgtable.h>
46 #include <asm/bitops.h>
49 #include <asm/iommu.h>
50 #include <asm/bootinfo.h>
51 #include <asm/ppcdebug.h>
52 #include <asm/btext.h>
53 #include <asm/sections.h>
54 #include <asm/machdep.h>
57 #ifdef CONFIG_LOGO_LINUX_CLUT224
58 #include <linux/linux_logo.h>
59 extern const struct linux_logo logo_linux_clut224;
63 * Properties whose value is longer than this get excluded from our
64 * copy of the device tree. This value does need to be big enough to
65 * ensure that we don't lose things like the interrupt-map property
66 * on a PCI-PCI bridge.
68 #define MAX_PROPERTY_LENGTH (1UL * 1024 * 1024)
71 * prom_init() is called very early on, before the kernel text
72 * and data have been mapped to KERNELBASE. At this point the code
73 * is running at whatever address it has been loaded at, so
74 * references to extern and static variables must be relocated
75 * explicitly. The procedure reloc_offset() returns the address
76 * we're currently running at minus the address we were linked at.
77 * (Note that strings count as static variables.)
79 * Because OF may have mapped I/O devices into the area starting at
80 * KERNELBASE, particularly on CHRP machines, we can't safely call
81 * OF once the kernel has been mapped to KERNELBASE. Therefore all
82 * OF calls should be done within prom_init(), and prom_init()
83 * and all routines called within it must be careful to relocate
84 * references as necessary.
86 * Note that the bss is cleared *after* prom_init runs, so we have
87 * to make sure that any static or extern variables it accesses
88 * are put in the data segment.
92 #define PROM_BUG() do { \
93 prom_printf("kernel BUG at %s line 0x%x!\n", \
94 RELOC(__FILE__), __LINE__); \
95 __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR); \
99 #define prom_debug(x...) prom_printf(x)
101 #define prom_debug(x...)
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 are used in calls to call_prom. The 4th and following
183 * arguments to call_prom should be 32-bit values. 64 bit values
184 * are truncated to 32 bits (and fortunately don't get interpreted
187 #define ADDR(x) (u32) ((unsigned long)(x) - offset)
189 /* This is the one and *ONLY* place where we actually call open
190 * firmware from, since we need to make sure we're running in 32b
191 * mode when we do. We switch back to 64b mode upon return.
194 #define PROM_ERROR (-1)
196 static int __init call_prom(const char *service, int nargs, int nret, ...)
199 unsigned long offset = reloc_offset();
200 struct prom_t *_prom = PTRRELOC(&prom);
203 _prom->args.service = ADDR(service);
204 _prom->args.nargs = nargs;
205 _prom->args.nret = nret;
206 _prom->args.rets = (prom_arg_t *)&(_prom->args.args[nargs]);
208 va_start(list, nret);
209 for (i=0; i < nargs; i++)
210 _prom->args.args[i] = va_arg(list, prom_arg_t);
213 for (i=0; i < nret ;i++)
214 _prom->args.rets[i] = 0;
216 enter_prom(&_prom->args);
218 return (nret > 0)? _prom->args.rets[0]: 0;
222 static void __init prom_print(const char *msg)
225 unsigned long offset = reloc_offset();
226 struct prom_t *_prom = PTRRELOC(&prom);
228 if (_prom->stdout == 0)
231 for (p = msg; *p != 0; p = q) {
232 for (q = p; *q != 0 && *q != '\n'; ++q)
235 call_prom("write", 3, 1, _prom->stdout, p, q - p);
239 call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2);
244 static void __init prom_print_hex(unsigned long val)
246 unsigned long offset = reloc_offset();
247 int i, nibbles = sizeof(val)*2;
248 char buf[sizeof(val)*2+1];
249 struct prom_t *_prom = PTRRELOC(&prom);
251 for (i = nibbles-1; i >= 0; i--) {
252 buf[i] = (val & 0xf) + '0';
254 buf[i] += ('a'-'0'-10);
258 call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
262 static void __init prom_printf(const char *format, ...)
264 unsigned long offset = reloc_offset();
265 const char *p, *q, *s;
268 struct prom_t *_prom = PTRRELOC(&prom);
270 va_start(args, format);
271 for (p = PTRRELOC(format); *p != 0; p = q) {
272 for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
275 call_prom("write", 3, 1, _prom->stdout, p, q - p);
280 call_prom("write", 3, 1, _prom->stdout,
290 s = va_arg(args, const char *);
295 v = va_arg(args, unsigned long);
303 static void __init __attribute__((noreturn)) prom_panic(const char *reason)
305 unsigned long offset = reloc_offset();
307 prom_print(PTRRELOC(reason));
308 /* ToDo: should put up an SRC here */
309 call_prom("exit", 0, 0);
311 for (;;) /* should never get here */
316 static int __init prom_next_node(phandle *nodep)
320 if ((node = *nodep) != 0
321 && (*nodep = call_prom("child", 1, 1, node)) != 0)
323 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
326 if ((node = call_prom("parent", 1, 1, node)) == 0)
328 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
333 static int __init prom_getprop(phandle node, const char *pname,
334 void *value, size_t valuelen)
336 unsigned long offset = reloc_offset();
338 return call_prom("getprop", 4, 1, node, ADDR(pname),
339 (u32)(unsigned long) value, (u32) valuelen);
342 static void __init prom_initialize_naca(void)
346 unsigned long num_cpus = 0;
347 unsigned long offset = reloc_offset();
348 struct prom_t *_prom = PTRRELOC(&prom);
349 struct naca_struct *_naca = RELOC(naca);
350 struct systemcfg *_systemcfg = RELOC(systemcfg);
352 /* NOTE: _naca->debug_switch is already initialized. */
353 prom_debug("prom_initialize_naca: start...\n");
355 _naca->pftSize = 0; /* ilog2 of htab size. computed below. */
357 for (node = 0; prom_next_node(&node); ) {
359 prom_getprop(node, "device_type", type, sizeof(type));
361 if (!strcmp(type, RELOC("cpu"))) {
364 /* We're assuming *all* of the CPUs have the same
365 * d-cache and i-cache sizes... -Peter
367 if ( num_cpus == 1 ) {
371 if (_systemcfg->platform == PLATFORM_POWERMAC){
372 dc = "d-cache-block-size";
373 ic = "i-cache-block-size";
375 dc = "d-cache-line-size";
376 ic = "i-cache-line-size";
379 prom_getprop(node, "d-cache-size",
380 &size, sizeof(size));
382 prom_getprop(node, dc, &lsize, sizeof(lsize));
384 _systemcfg->dCacheL1Size = size;
385 _systemcfg->dCacheL1LineSize = lsize;
386 _naca->dCacheL1LogLineSize = __ilog2(lsize);
387 _naca->dCacheL1LinesPerPage = PAGE_SIZE/lsize;
389 prom_getprop(node, "i-cache-size",
390 &size, sizeof(size));
392 prom_getprop(node, ic, &lsize, sizeof(lsize));
394 _systemcfg->iCacheL1Size = size;
395 _systemcfg->iCacheL1LineSize = lsize;
396 _naca->iCacheL1LogLineSize = __ilog2(lsize);
397 _naca->iCacheL1LinesPerPage = PAGE_SIZE/lsize;
399 if (_systemcfg->platform == PLATFORM_PSERIES_LPAR) {
401 prom_getprop(node, "ibm,pft-size",
402 &pft_size, sizeof(pft_size));
403 /* pft_size[0] is the NUMA CEC cookie */
404 _naca->pftSize = pft_size[1];
407 } else if (!strcmp(type, RELOC("serial"))) {
409 struct isa_reg_property reg;
410 union pci_range ranges;
412 if (_systemcfg->platform == PLATFORM_POWERMAC)
415 prom_getprop(node, "ibm,aix-loc", type, sizeof(type));
417 if (strcmp(type, RELOC("S1")))
420 prom_getprop(node, "reg", ®, sizeof(reg));
422 isa = call_prom("parent", 1, 1, node);
425 pci = call_prom("parent", 1, 1, isa);
429 prom_getprop(pci, "ranges", &ranges, sizeof(ranges));
431 if ( _prom->encode_phys_size == 32 )
432 _naca->serialPortAddr = ranges.pci32.phys+reg.address;
434 _naca->serialPortAddr =
435 ((((unsigned long)ranges.pci64.phys_hi) << 32) |
436 (ranges.pci64.phys_lo)) + reg.address;
441 if (_systemcfg->platform == PLATFORM_POWERMAC)
442 _naca->interrupt_controller = IC_OPEN_PIC;
444 _naca->interrupt_controller = IC_INVALID;
445 for (node = 0; prom_next_node(&node); ) {
447 prom_getprop(node, "name", type, sizeof(type));
448 if (strcmp(type, RELOC("interrupt-controller")))
450 prom_getprop(node, "compatible", type, sizeof(type));
451 if (strstr(type, RELOC("open-pic")))
452 _naca->interrupt_controller = IC_OPEN_PIC;
453 else if (strstr(type, RELOC("ppc-xicp")))
454 _naca->interrupt_controller = IC_PPC_XIC;
456 prom_printf("prom: failed to recognize"
457 " interrupt-controller\n");
462 if (_naca->interrupt_controller == IC_INVALID) {
463 prom_printf("prom: failed to find interrupt-controller\n");
467 /* We gotta have at least 1 cpu... */
468 if ( (_systemcfg->processorCount = num_cpus) < 1 )
471 _systemcfg->physicalMemorySize = lmb_phys_mem_size();
473 if (_systemcfg->platform == PLATFORM_PSERIES ||
474 _systemcfg->platform == PLATFORM_POWERMAC) {
475 unsigned long rnd_mem_size, pteg_count;
477 /* round mem_size up to next power of 2 */
478 rnd_mem_size = 1UL << __ilog2(_systemcfg->physicalMemorySize);
479 if (rnd_mem_size < _systemcfg->physicalMemorySize)
483 pteg_count = (rnd_mem_size >> (12 + 1));
485 _naca->pftSize = __ilog2(pteg_count << 7);
488 if (_naca->pftSize == 0) {
489 prom_printf("prom: failed to compute pftSize!\n");
493 /* Add an eye catcher and the systemcfg layout version number */
494 strcpy(_systemcfg->eye_catcher, RELOC("SYSTEMCFG:PPC64"));
495 _systemcfg->version.major = SYSTEMCFG_MAJOR;
496 _systemcfg->version.minor = SYSTEMCFG_MINOR;
497 _systemcfg->processor = _get_PVR();
499 prom_debug("systemcfg->processorCount = 0x%x\n",
500 _systemcfg->processorCount);
501 prom_debug("systemcfg->physicalMemorySize = 0x%x\n",
502 _systemcfg->physicalMemorySize);
503 prom_debug("naca->pftSize = 0x%x\n",
505 prom_debug("systemcfg->dCacheL1LineSize = 0x%x\n",
506 _systemcfg->dCacheL1LineSize);
507 prom_debug("systemcfg->iCacheL1LineSize = 0x%x\n",
508 _systemcfg->iCacheL1LineSize);
509 prom_debug("naca->serialPortAddr = 0x%x\n",
510 _naca->serialPortAddr);
511 prom_debug("naca->interrupt_controller = 0x%x\n",
512 _naca->interrupt_controller);
513 prom_debug("systemcfg->platform = 0x%x\n",
514 _systemcfg->platform);
515 prom_debug("prom_initialize_naca: end...\n");
519 static void __init early_cmdline_parse(void)
521 unsigned long offset = reloc_offset();
523 #ifndef CONFIG_PMAC_DART
524 struct systemcfg *_systemcfg = RELOC(systemcfg);
527 opt = strstr(RELOC(cmd_line), RELOC("iommu="));
529 prom_printf("opt is:%s\n", opt);
531 while (*opt && *opt == ' ')
533 if (!strncmp(opt, RELOC("off"), 3))
534 RELOC(ppc64_iommu_off) = 1;
535 else if (!strncmp(opt, RELOC("force"), 5))
536 RELOC(iommu_force_on) = 1;
539 #ifndef CONFIG_PMAC_DART
540 if (_systemcfg->platform == PLATFORM_POWERMAC) {
541 RELOC(ppc64_iommu_off) = 1;
542 prom_printf("DART disabled on PowerMac !\n");
548 void prom_dump_lmb(void)
551 unsigned long offset = reloc_offset();
552 struct lmb *_lmb = PTRRELOC(&lmb);
554 prom_printf("\nprom_dump_lmb:\n");
555 prom_printf(" memory.cnt = 0x%x\n",
557 prom_printf(" memory.size = 0x%x\n",
559 for (i=0; i < _lmb->memory.cnt ;i++) {
560 prom_printf(" memory.region[0x%x].base = 0x%x\n",
561 i, _lmb->memory.region[i].base);
562 prom_printf(" .physbase = 0x%x\n",
563 _lmb->memory.region[i].physbase);
564 prom_printf(" .size = 0x%x\n",
565 _lmb->memory.region[i].size);
568 prom_printf("\n reserved.cnt = 0x%x\n",
570 prom_printf(" reserved.size = 0x%x\n",
571 _lmb->reserved.size);
572 for (i=0; i < _lmb->reserved.cnt ;i++) {
573 prom_printf(" reserved.region[0x%x\n].base = 0x%x\n",
574 i, _lmb->reserved.region[i].base);
575 prom_printf(" .physbase = 0x%x\n",
576 _lmb->reserved.region[i].physbase);
577 prom_printf(" .size = 0x%x\n",
578 _lmb->reserved.region[i].size);
581 #endif /* DEBUG_PROM */
583 static void __init prom_initialize_lmb(void)
587 unsigned long i, offset = reloc_offset();
588 struct prom_t *_prom = PTRRELOC(&prom);
589 struct systemcfg *_systemcfg = RELOC(systemcfg);
590 union lmb_reg_property reg;
591 unsigned long lmb_base, lmb_size;
592 unsigned long num_regs, bytes_per_reg = (_prom->encode_phys_size*2)/8;
596 /* XXX Quick HACK. Proper fix is to drop those structures and properly use
597 * #address-cells. PowerMac has #size-cell set to 1 and #address-cells to 2
599 if (_systemcfg->platform == PLATFORM_POWERMAC)
602 for (node = 0; prom_next_node(&node); ) {
604 prom_getprop(node, "device_type", type, sizeof(type));
606 if (strcmp(type, RELOC("memory")))
609 num_regs = prom_getprop(node, "reg", ®, sizeof(reg))
612 for (i=0; i < num_regs ;i++) {
613 if (_systemcfg->platform == PLATFORM_POWERMAC) {
614 lmb_base = ((unsigned long)reg.addrPM[i].address_hi) << 32;
615 lmb_base |= (unsigned long)reg.addrPM[i].address_lo;
616 lmb_size = reg.addrPM[i].size;
617 } else if (_prom->encode_phys_size == 32) {
618 lmb_base = reg.addr32[i].address;
619 lmb_size = reg.addr32[i].size;
621 lmb_base = reg.addr64[i].address;
622 lmb_size = reg.addr64[i].size;
625 /* We limit memory to 2GB if the IOMMU is off */
626 if (RELOC(ppc64_iommu_off)) {
627 if (lmb_base >= 0x80000000UL)
630 if ((lmb_base + lmb_size) > 0x80000000UL)
631 lmb_size = 0x80000000UL - lmb_base;
634 if (lmb_add(lmb_base, lmb_size) < 0)
635 prom_printf("Too many LMB's, discarding this one...\n");
643 #endif /* DEBUG_PROM */
647 prom_instantiate_rtas(void)
649 unsigned long offset = reloc_offset();
650 struct prom_t *_prom = PTRRELOC(&prom);
651 struct rtas_t *_rtas = PTRRELOC(&rtas);
652 struct systemcfg *_systemcfg = RELOC(systemcfg);
655 char hypertas_funcs[4];
657 prom_debug("prom_instantiate_rtas: start...\n");
659 prom_rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
660 if (prom_rtas != (ihandle) -1) {
662 x = prom_getprop(prom_rtas, "ibm,hypertas-functions",
663 hypertas_funcs, sizeof(hypertas_funcs));
665 if (x != PROM_ERROR) {
666 prom_printf("Hypertas detected, assuming LPAR !\n");
667 _systemcfg->platform = PLATFORM_PSERIES_LPAR;
670 prom_getprop(prom_rtas, "rtas-size",
671 &getprop_rval, sizeof(getprop_rval));
672 _rtas->size = getprop_rval;
673 prom_printf("instantiating rtas");
674 if (_rtas->size != 0) {
675 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
677 /* Grab some space within the first RTAS_INSTANTIATE_MAX bytes
678 * of physical memory (or within the RMO region) because RTAS
679 * runs in 32-bit mode and relocate off.
681 if ( _systemcfg->platform == PLATFORM_PSERIES_LPAR ) {
682 struct lmb *_lmb = PTRRELOC(&lmb);
683 rtas_region = min(_lmb->rmo_size, RTAS_INSTANTIATE_MAX);
686 _rtas->base = lmb_alloc_base(_rtas->size, PAGE_SIZE, rtas_region);
688 prom_printf(" at 0x%x", _rtas->base);
690 prom_rtas = call_prom("open", 1, 1, ADDR("/rtas"));
693 if (call_prom("call-method", 3, 2,
694 ADDR("instantiate-rtas"),
696 _rtas->base) != PROM_ERROR) {
697 _rtas->entry = (long)_prom->args.rets[1];
700 = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE,
704 if (_rtas->entry <= 0) {
705 prom_printf(" failed\n");
707 prom_printf(" done\n");
710 prom_debug("rtas->base = 0x%x\n", _rtas->base);
711 prom_debug("rtas->entry = 0x%x\n", _rtas->entry);
712 prom_debug("rtas->size = 0x%x\n", _rtas->size);
714 prom_debug("prom_instantiate_rtas: end...\n");
718 #ifdef CONFIG_PMAC_DART
719 static void __init prom_initialize_dart_table(void)
721 unsigned long offset = reloc_offset();
722 extern unsigned long dart_tablebase;
723 extern unsigned long dart_tablesize;
725 /* Only reserve DART space if machine has more than 2GB of RAM
726 * or if requested with iommu=on on cmdline.
728 if (lmb_end_of_DRAM() <= 0x80000000ull && !RELOC(iommu_force_on))
731 /* 512 pages (2MB) is max DART tablesize. */
732 RELOC(dart_tablesize) = 1UL << 21;
733 /* 16MB (1 << 24) alignment. We allocate a full 16Mb chuck since we
734 * will blow up an entire large page anyway in the kernel mapping
736 RELOC(dart_tablebase) = (unsigned long)
737 abs_to_virt(lmb_alloc_base(1UL<<24, 1UL<<24, 0x80000000L));
739 prom_printf("Dart at: %x\n", RELOC(dart_tablebase));
741 #endif /* CONFIG_PMAC_DART */
743 static void __init prom_initialize_tce_table(void)
747 unsigned long offset = reloc_offset();
748 char compatible[64], path[64], type[64], model[64];
749 unsigned long i, table = 0;
750 unsigned long base, vbase, align;
751 unsigned int minalign, minsize;
752 struct of_tce_table *prom_tce_table = RELOC(of_tce_table);
753 unsigned long tce_entry, *tce_entryp;
755 if (RELOC(ppc64_iommu_off))
758 prom_debug("starting prom_initialize_tce_table\n");
760 /* Search all nodes looking for PHBs. */
761 for (node = 0; prom_next_node(&node); ) {
762 if (table == MAX_PHB) {
763 prom_printf("WARNING: PCI host bridge ignored, "
764 "need to increase MAX_PHB\n");
771 prom_getprop(node, "compatible",
772 compatible, sizeof(compatible));
773 prom_getprop(node, "device_type", type, sizeof(type));
774 prom_getprop(node, "model", model, sizeof(model));
776 /* Keep the old logic in tack to avoid regression. */
777 if (compatible[0] != 0) {
778 if((strstr(compatible, RELOC("python")) == NULL) &&
779 (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
780 (strstr(compatible, RELOC("Winnipeg")) == NULL))
782 } else if (model[0] != 0) {
783 if ((strstr(model, RELOC("ython")) == NULL) &&
784 (strstr(model, RELOC("peedwagon")) == NULL) &&
785 (strstr(model, RELOC("innipeg")) == NULL))
789 if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL)) {
793 if (prom_getprop(node, "tce-table-minalign", &minalign,
794 sizeof(minalign)) == PROM_ERROR) {
798 if (prom_getprop(node, "tce-table-minsize", &minsize,
799 sizeof(minsize)) == PROM_ERROR) {
804 * Even though we read what OF wants, we just set the table
805 * size to 4 MB. This is enough to map 2GB of PCI DMA space.
806 * By doing this, we avoid the pitfalls of trying to DMA to
807 * MMIO space and the DMA alias hole.
809 * On POWER4, firmware sets the TCE region by assuming
810 * each TCE table is 8MB. Using this memory for anything
811 * else will impact performance, so we always allocate 8MB.
814 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
819 /* Align to the greater of the align or size */
820 align = max(minalign, minsize);
822 /* Carve out storage for the TCE table. */
823 base = lmb_alloc(minsize, align);
826 prom_panic("ERROR, cannot find space for TCE table.\n");
829 vbase = (unsigned long)abs_to_virt(base);
831 /* Save away the TCE table attributes for later use. */
832 prom_tce_table[table].node = node;
833 prom_tce_table[table].base = vbase;
834 prom_tce_table[table].size = minsize;
836 prom_debug("TCE table: 0x%x\n", table);
837 prom_debug("\tnode = 0x%x\n", node);
838 prom_debug("\tbase = 0x%x\n", vbase);
839 prom_debug("\tsize = 0x%x\n", minsize);
841 /* Initialize the table to have a one-to-one mapping
842 * over the allocated size.
844 tce_entryp = (unsigned long *)base;
845 for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
846 tce_entry = (i << PAGE_SHIFT);
848 *tce_entryp = tce_entry;
851 /* It seems OF doesn't null-terminate the path :-( */
852 memset(path, 0, sizeof(path));
853 /* Call OF to setup the TCE hardware */
854 if (call_prom("package-to-path", 3, 1, node,
855 path, sizeof(path)-1) == PROM_ERROR) {
856 prom_printf("package-to-path failed\n");
858 prom_printf("opening PHB %s", path);
861 phb_node = call_prom("open", 1, 1, path);
862 if ( (long)phb_node <= 0) {
863 prom_printf("... failed\n");
865 prom_printf("... done\n");
867 call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
868 phb_node, -1, minsize,
869 (u32) base, (u32) (base >> 32));
870 call_prom("close", 1, 0, phb_node);
875 /* Flag the first invalid entry */
876 prom_tce_table[table].node = 0;
877 prom_debug("ending prom_initialize_tce_table\n");
881 * With CHRP SMP we need to use the OF to start the other
882 * processors so we can't wait until smp_boot_cpus (the OF is
883 * trashed by then) so we have to put the processors into
884 * a holding pattern controlled by the kernel (not OF) before
887 * This uses a chunk of low memory, puts some holding pattern
888 * code there and sends the other processors off to there until
889 * smp_boot_cpus tells them to do something. The holding pattern
890 * checks that address until its cpu # is there, when it is that
891 * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
892 * of setting those values.
894 * We also use physical address 0x4 here to tell when a cpu
895 * is in its holding pattern code.
897 * Fixup comment... DRENG / PPPBBB - Peter
901 static void __init prom_hold_cpus(unsigned long mem)
906 unsigned long offset = reloc_offset();
907 char type[64], *path;
909 unsigned int interrupt_server[MAX_CPU_THREADS];
910 unsigned int cpu_threads, hw_cpu_num;
912 extern void __secondary_hold(void);
913 extern unsigned long __secondary_hold_spinloop;
914 extern unsigned long __secondary_hold_acknowledge;
915 unsigned long *spinloop
916 = (void *)virt_to_abs(&__secondary_hold_spinloop);
917 unsigned long *acknowledge
918 = (void *)virt_to_abs(&__secondary_hold_acknowledge);
919 unsigned long secondary_hold
920 = virt_to_abs(*PTRRELOC((unsigned long *)__secondary_hold));
921 struct systemcfg *_systemcfg = RELOC(systemcfg);
922 struct paca_struct *lpaca = PTRRELOC(&paca[0]);
923 struct prom_t *_prom = PTRRELOC(&prom);
925 struct naca_struct *_naca = RELOC(naca);
928 /* On pmac, we just fill out the various global bitmasks and
929 * arrays indicating our CPUs are here, they are actually started
930 * later on from pmac_smp
932 if (_systemcfg->platform == PLATFORM_POWERMAC) {
933 for (node = 0; prom_next_node(&node); ) {
935 prom_getprop(node, "device_type", type, sizeof(type));
936 if (strcmp(type, RELOC("cpu")) != 0)
939 prom_getprop(node, "reg", ®, sizeof(reg));
940 lpaca[cpuid].hw_cpu_id = reg;
943 cpu_set(cpuid, RELOC(cpu_available_map));
944 cpu_set(cpuid, RELOC(cpu_possible_map));
945 cpu_set(cpuid, RELOC(cpu_present_at_boot));
947 cpu_set(cpuid, RELOC(cpu_online_map));
948 #endif /* CONFIG_SMP */
954 /* Initially, we must have one active CPU. */
955 _systemcfg->processorCount = 1;
957 prom_debug("prom_hold_cpus: start...\n");
958 prom_debug(" 1) spinloop = 0x%x\n", (unsigned long)spinloop);
959 prom_debug(" 1) *spinloop = 0x%x\n", *spinloop);
960 prom_debug(" 1) acknowledge = 0x%x\n",
961 (unsigned long)acknowledge);
962 prom_debug(" 1) *acknowledge = 0x%x\n", *acknowledge);
963 prom_debug(" 1) secondary_hold = 0x%x\n", secondary_hold);
965 /* Set the common spinloop variable, so all of the secondary cpus
966 * will block when they are awakened from their OF spinloop.
967 * This must occur for both SMP and non SMP kernels, since OF will
968 * be trashed when we move the kernel.
973 for (i=0; i < NR_CPUS; i++) {
974 RELOC(hmt_thread_data)[i].pir = 0xdeadbeef;
978 for (node = 0; prom_next_node(&node); ) {
980 prom_getprop(node, "device_type", type, sizeof(type));
981 if (strcmp(type, RELOC("cpu")) != 0)
984 /* Skip non-configured cpus. */
985 prom_getprop(node, "status", type, sizeof(type));
986 if (strcmp(type, RELOC("okay")) != 0)
990 prom_getprop(node, "reg", ®, sizeof(reg));
993 memset(path, 0, 256);
994 if (call_prom("package-to-path", 3, 1,
995 node, path, 255) == PROM_ERROR)
998 prom_debug("\ncpuid = 0x%x\n", cpuid);
999 prom_debug("cpu hw idx = 0x%x\n", reg);
1000 lpaca[cpuid].hw_cpu_id = reg;
1002 /* Init the acknowledge var which will be reset by
1003 * the secondary cpu when it awakens from its OF
1006 *acknowledge = (unsigned long)-1;
1008 propsize = prom_getprop(node, "ibm,ppc-interrupt-server#s",
1010 sizeof(interrupt_server));
1012 /* no property. old hardware has no SMT */
1014 interrupt_server[0] = reg; /* fake it with phys id */
1016 /* We have a threaded processor */
1017 cpu_threads = propsize / sizeof(u32);
1018 if (cpu_threads > MAX_CPU_THREADS) {
1019 prom_printf("SMT: too many threads!\n"
1020 "SMT: found %x, max is %x\n",
1021 cpu_threads, MAX_CPU_THREADS);
1022 cpu_threads = 1; /* ToDo: panic? */
1026 hw_cpu_num = interrupt_server[0];
1027 if (hw_cpu_num != _prom->cpu) {
1028 /* Primary Thread of non-boot cpu */
1029 prom_printf("%x : starting cpu %s... ", cpuid, path);
1030 call_prom("start-cpu", 3, 0, node,
1031 secondary_hold, cpuid);
1033 for ( i = 0 ; (i < 100000000) &&
1034 (*acknowledge == ((unsigned long)-1)); i++ ) ;
1036 if (*acknowledge == cpuid) {
1037 prom_printf("... done\n");
1038 /* We have to get every CPU out of OF,
1039 * even if we never start it. */
1040 if (cpuid >= NR_CPUS)
1043 /* Set the number of active processors. */
1044 _systemcfg->processorCount++;
1045 cpu_set(cpuid, RELOC(cpu_available_map));
1046 cpu_set(cpuid, RELOC(cpu_possible_map));
1047 cpu_set(cpuid, RELOC(cpu_present_at_boot));
1050 prom_printf("... failed: %x\n", *acknowledge);
1055 prom_printf("%x : booting cpu %s\n", cpuid, path);
1056 cpu_set(cpuid, RELOC(cpu_available_map));
1057 cpu_set(cpuid, RELOC(cpu_possible_map));
1058 cpu_set(cpuid, RELOC(cpu_online_map));
1059 cpu_set(cpuid, RELOC(cpu_present_at_boot));
1064 /* Init paca for secondary threads. They start later. */
1065 for (i=1; i < cpu_threads; i++) {
1067 if (cpuid >= NR_CPUS)
1069 lpaca[cpuid].hw_cpu_id = interrupt_server[i];
1070 prom_printf("%x : preparing thread ... ",
1071 interrupt_server[i]);
1072 if (_naca->smt_state) {
1073 cpu_set(cpuid, RELOC(cpu_available_map));
1074 cpu_set(cpuid, RELOC(cpu_present_at_boot));
1075 prom_printf("available\n");
1077 prom_printf("not available\n");
1084 /* Only enable HMT on processors that provide support. */
1085 if (__is_processor(PV_PULSAR) ||
1086 __is_processor(PV_ICESTAR) ||
1087 __is_processor(PV_SSTAR)) {
1088 prom_printf(" starting secondary threads\n");
1090 for (i = 0; i < NR_CPUS; i += 2) {
1095 unsigned long pir = _get_PIR();
1096 if (__is_processor(PV_PULSAR)) {
1097 RELOC(hmt_thread_data)[i].pir =
1100 RELOC(hmt_thread_data)[i].pir =
1104 /* cpu_set(i+1, cpu_online_map); */
1105 cpu_set(i+1, RELOC(cpu_possible_map));
1107 _systemcfg->processorCount *= 2;
1109 prom_printf("Processor is not HMT capable\n");
1113 if (cpuid > NR_CPUS)
1114 prom_printf("WARNING: maximum CPUs (" __stringify(NR_CPUS)
1115 ") exceeded: ignoring extras\n");
1117 prom_debug("prom_hold_cpus: end...\n");
1120 static void __init smt_setup(void)
1123 char my_smt_enabled = SMT_DYNAMIC;
1124 ihandle prom_options = 0;
1126 unsigned long offset = reloc_offset();
1127 struct naca_struct *_naca = RELOC(naca);
1130 if (strstr(RELOC(cmd_line), RELOC("smt-enabled="))) {
1131 for (q = RELOC(cmd_line); (p = strstr(q, RELOC("smt-enabled="))) != 0; ) {
1133 if (p > RELOC(cmd_line) && p[-1] != ' ')
1136 if (q[0] == 'o' && q[1] == 'f' &&
1137 q[2] == 'f' && (q[3] == ' ' || q[3] == '\0')) {
1138 my_smt_enabled = SMT_OFF;
1139 } else if (q[0]=='o' && q[1] == 'n' &&
1140 (q[2] == ' ' || q[2] == '\0')) {
1141 my_smt_enabled = SMT_ON;
1143 my_smt_enabled = SMT_DYNAMIC;
1148 prom_options = call_prom("finddevice", 1, 1, ADDR("/options"));
1149 if (prom_options != (ihandle) -1) {
1150 prom_getprop(prom_options, "ibm,smt-enabled",
1151 option, sizeof(option));
1152 if (option[0] != 0) {
1154 if (!strcmp(option, RELOC("off")))
1155 my_smt_enabled = SMT_OFF;
1156 else if (!strcmp(option, RELOC("on")))
1157 my_smt_enabled = SMT_ON;
1159 my_smt_enabled = SMT_DYNAMIC;
1165 my_smt_enabled = SMT_DYNAMIC; /* default to on */
1167 _naca->smt_state = my_smt_enabled;
1171 #ifdef CONFIG_BOOTX_TEXT
1173 /* This function will enable the early boot text when doing OF booting. This
1174 * way, xmon output should work too
1176 static void __init setup_disp_fake_bi(ihandle dp)
1178 int width = 640, height = 480, depth = 8, pitch;
1180 struct pci_reg_property addrs[8];
1183 unsigned long offset = reloc_offset();
1185 memset(name, 0, sizeof(name));
1186 prom_getprop(dp, "name", name, sizeof(name));
1187 name[sizeof(name)-1] = 0;
1188 prom_printf("Initializing fake screen: %s\n", name);
1190 prom_getprop(dp, "width", &width, sizeof(width));
1191 prom_getprop(dp, "height", &height, sizeof(height));
1192 prom_getprop(dp, "depth", &depth, sizeof(depth));
1193 pitch = width * ((depth + 7) / 8);
1194 prom_getprop(dp, "linebytes", &pitch, sizeof(pitch));
1196 pitch = 0x1000; /* for strange IBM display */
1199 prom_printf("width %x height %x depth %x linebytes %x\n",
1200 width, height, depth, depth);
1202 prom_getprop(dp, "address", &address, sizeof(address));
1204 /* look for an assigned address with a size of >= 1MB */
1205 naddrs = prom_getprop(dp, "assigned-addresses",
1206 addrs, sizeof(addrs));
1207 naddrs /= sizeof(struct pci_reg_property);
1208 for (i = 0; i < naddrs; ++i) {
1209 if (addrs[i].size_lo >= (1 << 20)) {
1210 address = addrs[i].addr.a_lo;
1211 /* use the BE aperture if possible */
1212 if (addrs[i].size_lo >= (16 << 20))
1213 address += (8 << 20);
1218 prom_printf("Failed to get address of frame buffer\n");
1222 btext_setup_display(width, height, depth, pitch, address);
1223 prom_printf("Addr of fb: %x\n", address);
1224 RELOC(boot_text_mapped) = 0;
1226 #endif /* CONFIG_BOOTX_TEXT */
1228 static void __init prom_init_client_services(unsigned long pp)
1230 unsigned long offset = reloc_offset();
1231 struct prom_t *_prom = PTRRELOC(&prom);
1233 /* Get a handle to the prom entry point before anything else */
1236 /* Init default value for phys size */
1237 _prom->encode_phys_size = 32;
1239 /* get a handle for the stdout device */
1240 _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
1241 if ((long)_prom->chosen <= 0)
1242 prom_panic("cannot find chosen"); /* msg won't be printed :( */
1244 /* get device tree root */
1245 _prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
1246 if ((long)_prom->root <= 0)
1247 prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1250 static void __init prom_init_stdout(void)
1252 unsigned long offset = reloc_offset();
1253 struct prom_t *_prom = PTRRELOC(&prom);
1256 if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
1257 prom_panic("cannot find stdout");
1259 _prom->stdout = val;
1262 static int __init prom_find_machine_type(void)
1264 unsigned long offset = reloc_offset();
1265 struct prom_t *_prom = PTRRELOC(&prom);
1269 len = prom_getprop(_prom->root, "compatible",
1270 compat, sizeof(compat)-1);
1274 char *p = &compat[i];
1278 if (strstr(p, RELOC("Power Macintosh")) ||
1279 strstr(p, RELOC("MacRISC4")))
1280 return PLATFORM_POWERMAC;
1284 /* Default to pSeries */
1285 return PLATFORM_PSERIES;
1288 static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
1290 unsigned long offset = reloc_offset();
1292 return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
1296 * If we have a display that we don't know how to drive,
1297 * we will want to try to execute OF's open method for it
1298 * later. However, OF will probably fall over if we do that
1299 * we've taken over the MMU.
1300 * So we check whether we will need to open the display,
1301 * and if so, open it now.
1303 static unsigned long __init check_display(unsigned long mem)
1308 unsigned long offset = reloc_offset();
1309 struct prom_t *_prom = PTRRELOC(&prom);
1310 char type[16], *path;
1311 static unsigned char default_colors[] = {
1329 const unsigned char *clut;
1331 _prom->disp_node = 0;
1333 prom_printf("Looking for displays\n");
1334 if (RELOC(of_stdout_device) != 0)
1335 prom_printf("OF stdout is : %s\n",
1336 PTRRELOC(RELOC(of_stdout_device)));
1337 for (node = 0; prom_next_node(&node); ) {
1339 prom_getprop(node, "device_type", type, sizeof(type));
1340 if (strcmp(type, RELOC("display")) != 0)
1342 /* It seems OF doesn't null-terminate the path :-( */
1343 path = (char *) mem;
1344 memset(path, 0, 256);
1347 * leave some room at the end of the path for appending extra
1350 if (call_prom("package-to-path", 3, 1, node, path, 250) < 0)
1352 prom_printf("found display : %s\n", path);
1355 * If this display is the device that OF is using for stdout,
1356 * move it to the front of the list.
1358 mem += strlen(path) + 1;
1359 i = RELOC(prom_num_displays);
1360 RELOC(prom_num_displays) = i + 1;
1361 if (RELOC(of_stdout_device) != 0 && i > 0
1362 && strcmp(PTRRELOC(RELOC(of_stdout_device)), path) == 0) {
1363 for (; i > 0; --i) {
1364 RELOC(prom_display_paths[i])
1365 = RELOC(prom_display_paths[i-1]);
1366 RELOC(prom_display_nodes[i])
1367 = RELOC(prom_display_nodes[i-1]);
1369 _prom->disp_node = node;
1371 RELOC(prom_display_paths[i]) = PTRUNRELOC(path);
1372 RELOC(prom_display_nodes[i]) = node;
1373 if (_prom->disp_node == 0)
1374 _prom->disp_node = node;
1375 if (RELOC(prom_num_displays) >= FB_MAX)
1378 prom_printf("Opening displays...\n");
1379 for (j = RELOC(prom_num_displays) - 1; j >= 0; j--) {
1380 path = PTRRELOC(RELOC(prom_display_paths[j]));
1381 prom_printf("opening display : %s", path);
1382 ih = call_prom("open", 1, 1, path);
1383 if (ih == (ihandle)0 || ih == (ihandle)-1) {
1384 prom_printf("... failed\n");
1388 prom_printf("... done\n");
1390 /* Setup a useable color table when the appropriate
1391 * method is available. Should update this to set-colors */
1392 clut = RELOC(default_colors);
1393 for (i = 0; i < 32; i++, clut += 3)
1394 if (prom_set_color(ih, i, clut[0], clut[1],
1398 #ifdef CONFIG_LOGO_LINUX_CLUT224
1399 clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
1400 for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
1401 if (prom_set_color(ih, i + 32, clut[0], clut[1],
1404 #endif /* CONFIG_LOGO_LINUX_CLUT224 */
1407 return DOUBLEWORD_ALIGN(mem);
1410 /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
1411 static void __init *__make_room(unsigned long *mem_start, unsigned long *mem_end,
1412 unsigned long needed, unsigned long align)
1415 unsigned long offset = reloc_offset();
1417 *mem_start = ALIGN(*mem_start, align);
1418 if (*mem_start + needed > *mem_end) {
1419 #ifdef CONFIG_BLK_DEV_INITRD
1420 /* FIXME: Apple OF doesn't map unclaimed mem. If this
1421 * ever happened on G5, we'd need to fix. */
1422 unsigned long initrd_len;
1424 if (*mem_end != RELOC(initrd_start))
1425 prom_panic("No memory for copy_device_tree");
1427 prom_printf("Huge device_tree: moving initrd\n");
1429 initrd_len = RELOC(initrd_end) - RELOC(initrd_start);
1430 *mem_end = RELOC(initrd_start) + 4 * 1024 * 1024;
1431 memmove((void *)*mem_end, (void *)RELOC(initrd_start),
1433 RELOC(initrd_start) = *mem_end;
1434 RELOC(initrd_end) = RELOC(initrd_start) + initrd_len;
1436 prom_panic("No memory for copy_device_tree");
1440 ret = (void *)*mem_start;
1441 *mem_start += needed;
1446 #define make_room(startp, endp, type) \
1447 __make_room(startp, endp, sizeof(type), __alignof__(type))
1450 inspect_node(phandle node, struct device_node *dad,
1451 unsigned long *mem_start, unsigned long *mem_end,
1452 struct device_node ***allnextpp)
1456 struct device_node *np;
1457 struct property *pp, **prev_propp;
1458 char *prev_name, *namep;
1459 unsigned char *valp;
1460 unsigned long offset = reloc_offset();
1461 phandle ibm_phandle;
1463 np = make_room(mem_start, mem_end, struct device_node);
1464 memset(np, 0, sizeof(*np));
1467 **allnextpp = PTRUNRELOC(np);
1468 *allnextpp = &np->allnext;
1470 np->parent = PTRUNRELOC(dad);
1471 /* we temporarily use the `next' field as `last_child'. */
1473 dad->child = PTRUNRELOC(np);
1475 dad->next->sibling = PTRUNRELOC(np);
1479 /* get and store all properties */
1480 prev_propp = &np->properties;
1481 prev_name = RELOC("");
1483 /* 32 is max len of name including nul. */
1484 namep = make_room(mem_start, mem_end, char[32]);
1485 if (call_prom("nextprop", 3, 1, node, prev_name, namep) <= 0) {
1486 /* No more nodes: unwind alloc */
1487 *mem_start = (unsigned long)namep;
1490 /* Trim off some if we can */
1491 *mem_start = DOUBLEWORD_ALIGN((unsigned long)namep
1492 + strlen(namep) + 1);
1493 pp = make_room(mem_start, mem_end, struct property);
1494 pp->name = PTRUNRELOC(namep);
1497 pp->length = call_prom("getproplen", 2, 1, node, namep);
1500 if (pp->length > MAX_PROPERTY_LENGTH) {
1503 prom_printf("WARNING: ignoring large property ");
1504 /* It seems OF doesn't null-terminate the path :-( */
1505 memset(path, 0, sizeof(path));
1506 if (call_prom("package-to-path", 3, 1, node,
1507 path, sizeof(path)-1) > 0)
1508 prom_printf("[%s] ", path);
1509 prom_printf("%s length 0x%x\n", namep, pp->length);
1512 valp = __make_room(mem_start, mem_end, pp->length, 1);
1513 pp->value = PTRUNRELOC(valp);
1514 call_prom("getprop", 4, 1, node, namep, valp, pp->length);
1515 *prev_propp = PTRUNRELOC(pp);
1516 prev_propp = &pp->next;
1519 /* Add a "linux,phandle" property. */
1520 namep = make_room(mem_start, mem_end, char[16]);
1521 strcpy(namep, RELOC("linux,phandle"));
1522 pp = make_room(mem_start, mem_end, struct property);
1523 pp->name = PTRUNRELOC(namep);
1524 pp->length = sizeof(phandle);
1525 valp = make_room(mem_start, mem_end, phandle);
1526 pp->value = PTRUNRELOC(valp);
1527 *(phandle *)valp = node;
1528 *prev_propp = PTRUNRELOC(pp);
1531 /* Set np->linux_phandle to the value of the ibm,phandle property
1532 if it exists, otherwise to the phandle for this node. */
1533 np->linux_phandle = node;
1534 if (prom_getprop(node, "ibm,phandle",
1535 &ibm_phandle, sizeof(ibm_phandle)) > 0)
1536 np->linux_phandle = ibm_phandle;
1538 /* get the node's full name */
1539 namep = (char *)*mem_start;
1540 l = call_prom("package-to-path", 3, 1, node,
1541 namep, *mem_end - *mem_start);
1543 /* Didn't fit? Get more room. */
1544 if (l+1 > *mem_end - *mem_start) {
1545 namep = __make_room(mem_start, mem_end, l+1, 1);
1546 call_prom("package-to-path", 3, 1, node, namep, l);
1548 np->full_name = PTRUNRELOC(namep);
1550 *mem_start = DOUBLEWORD_ALIGN(*mem_start + l + 1);
1553 /* do all our children */
1554 child = call_prom("child", 1, 1, node);
1555 while (child != (phandle)0) {
1556 inspect_node(child, np, mem_start, mem_end,
1558 child = call_prom("peer", 1, 1, child);
1563 * Make a copy of the device tree from the PROM.
1565 static unsigned long __init
1566 copy_device_tree(unsigned long mem_start)
1569 struct device_node **allnextp;
1570 unsigned long offset = reloc_offset();
1571 unsigned long mem_end;
1573 /* We pass mem_end-mem_start to OF: keep it well under 32-bit */
1574 mem_end = mem_start + 1024*1024*1024;
1575 #ifdef CONFIG_BLK_DEV_INITRD
1576 if (RELOC(initrd_start) && RELOC(initrd_start) > mem_start)
1577 mem_end = RELOC(initrd_start);
1578 #endif /* CONFIG_BLK_DEV_INITRD */
1580 root = call_prom("peer", 1, 1, (phandle)0);
1581 if (root == (phandle)0) {
1582 prom_panic("couldn't get device tree root\n");
1584 allnextp = &RELOC(allnodes);
1585 inspect_node(root, 0, &mem_start, &mem_end, &allnextp);
1590 /* Verify bi_recs are good */
1591 static struct bi_record * __init prom_bi_rec_verify(struct bi_record *bi_recs)
1593 struct bi_record *first, *last;
1595 prom_debug("birec_verify: r6=0x%x\n", (unsigned long)bi_recs);
1596 if (bi_recs != NULL)
1597 prom_debug(" tag=0x%x\n", bi_recs->tag);
1599 if ( bi_recs == NULL || bi_recs->tag != BI_FIRST )
1602 last = (struct bi_record *)(long)bi_recs->data[0];
1604 prom_debug(" last=0x%x\n", (unsigned long)last);
1606 prom_debug(" last_tag=0x%x\n", last->tag);
1608 if ( last == NULL || last->tag != BI_LAST )
1611 first = (struct bi_record *)(long)last->data[0];
1612 prom_debug(" first=0x%x\n", (unsigned long)first);
1614 if ( first == NULL || first != bi_recs )
1620 static void __init prom_bi_rec_reserve(void)
1622 unsigned long offset = reloc_offset();
1623 struct prom_t *_prom = PTRRELOC(&prom);
1624 struct bi_record *rec;
1626 if ( _prom->bi_recs != NULL) {
1628 for ( rec=_prom->bi_recs;
1629 rec->tag != BI_LAST;
1630 rec=bi_rec_next(rec) ) {
1631 prom_debug("bi: 0x%x\n", rec->tag);
1633 #ifdef CONFIG_BLK_DEV_INITRD
1635 RELOC(initrd_start) = (unsigned long)(rec->data[0]);
1636 RELOC(initrd_end) = RELOC(initrd_start) + rec->data[1];
1638 #endif /* CONFIG_BLK_DEV_INITRD */
1641 /* The next use of this field will be after relocation
1642 * is enabled, so convert this physical address into a
1645 _prom->bi_recs = PTRUNRELOC(_prom->bi_recs);
1650 * We enter here early on, when the Open Firmware prom is still
1651 * handling exceptions and the MMU hash table for us.
1654 unsigned long __init
1655 prom_init(unsigned long r3, unsigned long r4, unsigned long pp,
1656 unsigned long r6, unsigned long r7)
1661 unsigned long offset = reloc_offset();
1666 struct systemcfg *_systemcfg;
1667 struct paca_struct *lpaca = PTRRELOC(&paca[0]);
1668 struct prom_t *_prom = PTRRELOC(&prom);
1670 /* First zero the BSS -- use memset, some arches don't have
1672 memset(PTRRELOC(&__bss_start), 0, __bss_stop - __bss_start);
1674 /* Setup systemcfg and NACA pointers now */
1675 RELOC(systemcfg) = _systemcfg = (struct systemcfg *)(SYSTEMCFG_VIRT_ADDR - offset);
1676 RELOC(naca) = (struct naca_struct *)(NACA_VIRT_ADDR - offset);
1678 /* Init interface to Open Firmware and pickup bi-recs */
1679 prom_init_client_services(pp);
1681 /* Init prom stdout device */
1684 prom_debug("klimit=0x%x\n", RELOC(klimit));
1685 prom_debug("offset=0x%x\n", offset);
1686 prom_debug("->mem=0x%x\n", RELOC(klimit) - offset);
1688 /* check out if we have bi_recs */
1689 _prom->bi_recs = prom_bi_rec_verify((struct bi_record *)r6);
1690 if ( _prom->bi_recs != NULL ) {
1691 RELOC(klimit) = PTRUNRELOC((unsigned long)_prom->bi_recs +
1692 _prom->bi_recs->data[1]);
1693 prom_debug("bi_recs=0x%x\n", (unsigned long)_prom->bi_recs);
1694 prom_debug("new mem=0x%x\n", RELOC(klimit) - offset);
1697 /* If we don't have birec's or didn't find them, check for an initrd
1698 * using the "yaboot" way
1700 #ifdef CONFIG_BLK_DEV_INITRD
1701 if ( _prom->bi_recs == NULL && r3 && r4 && r4 != 0xdeadbeef) {
1702 RELOC(initrd_start) = (r3 >= KERNELBASE) ? __pa(r3) : r3;
1703 RELOC(initrd_end) = RELOC(initrd_start) + r4;
1704 RELOC(initrd_below_start_ok) = 1;
1706 #endif /* CONFIG_BLK_DEV_INITRD */
1708 /* Default machine type. */
1709 _systemcfg->platform = prom_find_machine_type();
1711 /* On pSeries, copy the CPU hold code */
1712 if (_systemcfg->platform == PLATFORM_PSERIES)
1713 copy_and_flush(0, KERNELBASE - offset, 0x100, 0);
1715 /* Start storing things at klimit */
1716 mem = RELOC(klimit) - offset;
1718 /* Get the full OF pathname of the stdout device */
1721 call_prom("instance-to-path", 3, 1, _prom->stdout, p, 255);
1722 RELOC(of_stdout_device) = PTRUNRELOC(p);
1723 mem += strlen(p) + 1;
1726 prom_getprop(_prom->root, "#size-cells",
1727 &getprop_rval, sizeof(getprop_rval));
1728 _prom->encode_phys_size = (getprop_rval == 1) ? 32 : 64;
1730 /* Determine which cpu is actually running right _now_ */
1731 if (prom_getprop(_prom->chosen, "cpu",
1732 &prom_cpu, sizeof(prom_cpu)) <= 0)
1733 prom_panic("cannot find boot cpu");
1735 cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
1736 prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
1737 _prom->cpu = getprop_rval;
1738 lpaca[0].hw_cpu_id = _prom->cpu;
1740 RELOC(boot_cpuid) = 0;
1742 prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
1744 /* Get the boot device and translate it to a full OF pathname. */
1746 l = prom_getprop(_prom->chosen, "bootpath", p, 1<<20);
1748 p[l] = 0; /* should already be null-terminated */
1749 RELOC(bootpath) = PTRUNRELOC(p);
1753 call_prom("canon", 3, 1, p, d, 1<<20);
1754 RELOC(bootdevice) = PTRUNRELOC(d);
1755 mem = DOUBLEWORD_ALIGN(mem + strlen(d) + 1);
1758 RELOC(cmd_line[0]) = 0;
1759 if ((long)_prom->chosen > 0) {
1760 prom_getprop(_prom->chosen, "bootargs", p, sizeof(cmd_line));
1761 if (p != NULL && p[0] != 0)
1762 strlcpy(RELOC(cmd_line), p, sizeof(cmd_line));
1765 early_cmdline_parse();
1767 prom_initialize_lmb();
1769 prom_bi_rec_reserve();
1771 mem = check_display(mem);
1773 if (_systemcfg->platform != PLATFORM_POWERMAC)
1774 prom_instantiate_rtas();
1776 /* Initialize some system info into the Naca early... */
1777 prom_initialize_naca();
1781 /* If we are on an SMP machine, then we *MUST* do the
1782 * following, regardless of whether we have an SMP
1785 prom_hold_cpus(mem);
1787 prom_debug("after basic inits, mem=0x%x\n", mem);
1788 #ifdef CONFIG_BLK_DEV_INITRD
1789 prom_debug("initrd_start=0x%x\n", RELOC(initrd_start));
1790 prom_debug("initrd_end=0x%x\n", RELOC(initrd_end));
1791 #endif /* CONFIG_BLK_DEV_INITRD */
1792 prom_debug("copying OF device tree...\n");
1794 mem = copy_device_tree(mem);
1796 RELOC(klimit) = mem + offset;
1798 prom_debug("new klimit is\n");
1799 prom_debug("klimit=0x%x\n", RELOC(klimit));
1800 prom_debug(" ->mem=0x%x\n", mem);
1802 lmb_reserve(0, __pa(RELOC(klimit)));
1804 #ifdef CONFIG_BLK_DEV_INITRD
1805 if (RELOC(initrd_start)) {
1806 unsigned long initrd_len;
1807 initrd_len = RELOC(initrd_end) - RELOC(initrd_start);
1809 /* Move initrd if it's where we're going to copy kernel. */
1810 if (RELOC(initrd_start) < __pa(RELOC(klimit))) {
1811 memmove((void *)mem, (void *)RELOC(initrd_start),
1813 RELOC(initrd_start) = mem;
1814 RELOC(initrd_end) = mem + initrd_len;
1817 lmb_reserve(RELOC(initrd_start), initrd_len);
1819 #endif /* CONFIG_BLK_DEV_INITRD */
1821 if (_systemcfg->platform == PLATFORM_PSERIES)
1822 prom_initialize_tce_table();
1824 #ifdef CONFIG_PMAC_DART
1825 if (_systemcfg->platform == PLATFORM_POWERMAC)
1826 prom_initialize_dart_table();
1829 #ifdef CONFIG_BOOTX_TEXT
1830 if (_prom->disp_node) {
1831 prom_printf("Setting up bi display...\n");
1832 setup_disp_fake_bi(_prom->disp_node);
1834 #endif /* CONFIG_BOOTX_TEXT */
1836 prom_printf("Calling quiesce ...\n");
1837 call_prom("quiesce", 0, 0);
1838 phys = KERNELBASE - offset;
1840 #ifdef CONFIG_BLK_DEV_INITRD
1841 /* If we had an initrd, we convert its address to virtual */
1842 if (RELOC(initrd_start)) {
1843 RELOC(initrd_start) = (unsigned long)__va(RELOC(initrd_start));
1844 RELOC(initrd_end) = (unsigned long)__va(RELOC(initrd_end));
1846 #endif /* CONFIG_BLK_DEV_INITRD */
1848 prom_printf("returning from prom_init\n");
1853 * Find the device_node with a given phandle.
1855 static struct device_node * __devinit
1856 find_phandle(phandle ph)
1858 struct device_node *np;
1860 for (np = allnodes; np != 0; np = np->allnext)
1861 if (np->linux_phandle == ph)
1867 * Find the interrupt parent of a node.
1869 static struct device_node * __devinit
1870 intr_parent(struct device_node *p)
1874 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
1877 return find_phandle(*parp);
1881 * Find out the size of each entry of the interrupts property
1884 static int __devinit
1885 prom_n_intr_cells(struct device_node *np)
1887 struct device_node *p;
1890 for (p = np; (p = intr_parent(p)) != NULL; ) {
1891 icp = (unsigned int *)
1892 get_property(p, "#interrupt-cells", NULL);
1895 if (get_property(p, "interrupt-controller", NULL) != NULL
1896 || get_property(p, "interrupt-map", NULL) != NULL) {
1897 printk("oops, node %s doesn't have #interrupt-cells\n",
1903 printk("prom_n_intr_cells failed for %s\n", np->full_name);
1909 * Map an interrupt from a device up to the platform interrupt
1912 static int __devinit
1913 map_interrupt(unsigned int **irq, struct device_node **ictrler,
1914 struct device_node *np, unsigned int *ints, int nintrc)
1916 struct device_node *p, *ipar;
1917 unsigned int *imap, *imask, *ip;
1918 int i, imaplen, match;
1919 int newintrc, newaddrc;
1923 reg = (unsigned int *) get_property(np, "reg", NULL);
1924 naddrc = prom_n_addr_cells(np);
1925 p = intr_parent(np);
1927 if (get_property(p, "interrupt-controller", NULL) != NULL)
1928 /* this node is an interrupt controller, stop here */
1930 imap = (unsigned int *)
1931 get_property(p, "interrupt-map", &imaplen);
1936 imask = (unsigned int *)
1937 get_property(p, "interrupt-map-mask", NULL);
1938 if (imask == NULL) {
1939 printk("oops, %s has interrupt-map but no mask\n",
1943 imaplen /= sizeof(unsigned int);
1946 while (imaplen > 0 && !match) {
1947 /* check the child-interrupt field */
1949 for (i = 0; i < naddrc && match; ++i)
1950 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
1951 for (; i < naddrc + nintrc && match; ++i)
1952 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
1953 imap += naddrc + nintrc;
1954 imaplen -= naddrc + nintrc;
1955 /* grab the interrupt parent */
1956 ipar = find_phandle((phandle) *imap++);
1959 printk("oops, no int parent %x in map of %s\n",
1960 imap[-1], p->full_name);
1963 /* find the parent's # addr and intr cells */
1964 ip = (unsigned int *)
1965 get_property(ipar, "#interrupt-cells", NULL);
1967 printk("oops, no #interrupt-cells on %s\n",
1972 ip = (unsigned int *)
1973 get_property(ipar, "#address-cells", NULL);
1974 newaddrc = (ip == NULL)? 0: *ip;
1975 imap += newaddrc + newintrc;
1976 imaplen -= newaddrc + newintrc;
1979 printk("oops, error decoding int-map on %s, len=%d\n",
1980 p->full_name, imaplen);
1985 printk("oops, no match in %s int-map for %s\n",
1986 p->full_name, np->full_name);
1993 ints = imap - nintrc;
1994 reg = ints - naddrc;
1998 printk("hmmm, int tree for %s doesn't have ctrler\n",
2008 static unsigned long __init
2009 finish_node_interrupts(struct device_node *np, unsigned long mem_start,
2013 int intlen, intrcells, intrcount;
2015 unsigned int *irq, virq;
2016 struct device_node *ic;
2018 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
2021 intrcells = prom_n_intr_cells(np);
2022 intlen /= intrcells * sizeof(unsigned int);
2023 np->intrs = (struct interrupt_info *) mem_start;
2024 mem_start += intlen * sizeof(struct interrupt_info);
2030 for (i = 0; i < intlen; ++i, ints += intrcells) {
2031 n = map_interrupt(&irq, &ic, np, ints, intrcells);
2035 /* don't map IRQ numbers under a cascaded 8259 controller */
2036 if (ic && device_is_compatible(ic, "chrp,iic")) {
2037 np->intrs[intrcount].line = irq[0];
2039 virq = virt_irq_create_mapping(irq[0]);
2040 if (virq == NO_IRQ) {
2041 printk(KERN_CRIT "Could not allocate interrupt"
2042 " number for %s\n", np->full_name);
2045 np->intrs[intrcount].line = irq_offset_up(virq);
2048 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
2049 if (systemcfg->platform == PLATFORM_POWERMAC && ic && ic->parent) {
2050 char *name = get_property(ic->parent, "name", NULL);
2051 if (name && !strcmp(name, "u3"))
2052 np->intrs[intrcount].line += 128;
2054 np->intrs[intrcount].sense = 1;
2056 np->intrs[intrcount].sense = irq[1];
2058 printk("hmmm, got %d intr cells for %s:", n,
2060 for (j = 0; j < n; ++j)
2061 printk(" %d", irq[j]);
2066 np->n_intrs = intrcount;
2071 static unsigned long __init
2072 interpret_pci_props(struct device_node *np, unsigned long mem_start,
2073 int naddrc, int nsizec, int measure_only)
2075 struct address_range *adr;
2076 struct pci_reg_property *pci_addrs;
2079 pci_addrs = (struct pci_reg_property *)
2080 get_property(np, "assigned-addresses", &l);
2081 if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
2083 adr = (struct address_range *) mem_start;
2084 while ((l -= sizeof(struct pci_reg_property)) >= 0) {
2085 if (!measure_only) {
2086 adr[i].space = pci_addrs[i].addr.a_hi;
2087 adr[i].address = pci_addrs[i].addr.a_lo;
2088 adr[i].size = pci_addrs[i].size_lo;
2094 mem_start += i * sizeof(struct address_range);
2099 static unsigned long __init
2100 interpret_dbdma_props(struct device_node *np, unsigned long mem_start,
2101 int naddrc, int nsizec, int measure_only)
2103 struct reg_property32 *rp;
2104 struct address_range *adr;
2105 unsigned long base_address;
2107 struct device_node *db;
2110 if (!measure_only) {
2111 for (db = np->parent; db != NULL; db = db->parent) {
2112 if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
2113 base_address = db->addrs[0].address;
2119 rp = (struct reg_property32 *) get_property(np, "reg", &l);
2120 if (rp != 0 && l >= sizeof(struct reg_property32)) {
2122 adr = (struct address_range *) mem_start;
2123 while ((l -= sizeof(struct reg_property32)) >= 0) {
2124 if (!measure_only) {
2126 adr[i].address = rp[i].address + base_address;
2127 adr[i].size = rp[i].size;
2133 mem_start += i * sizeof(struct address_range);
2139 static unsigned long __init
2140 interpret_macio_props(struct device_node *np, unsigned long mem_start,
2141 int naddrc, int nsizec, int measure_only)
2143 struct reg_property32 *rp;
2144 struct address_range *adr;
2145 unsigned long base_address;
2147 struct device_node *db;
2150 if (!measure_only) {
2151 for (db = np->parent; db != NULL; db = db->parent) {
2152 if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
2153 base_address = db->addrs[0].address;
2159 rp = (struct reg_property32 *) get_property(np, "reg", &l);
2160 if (rp != 0 && l >= sizeof(struct reg_property32)) {
2162 adr = (struct address_range *) mem_start;
2163 while ((l -= sizeof(struct reg_property32)) >= 0) {
2164 if (!measure_only) {
2166 adr[i].address = rp[i].address + base_address;
2167 adr[i].size = rp[i].size;
2173 mem_start += i * sizeof(struct address_range);
2179 static unsigned long __init
2180 interpret_isa_props(struct device_node *np, unsigned long mem_start,
2181 int naddrc, int nsizec, int measure_only)
2183 struct isa_reg_property *rp;
2184 struct address_range *adr;
2187 rp = (struct isa_reg_property *) get_property(np, "reg", &l);
2188 if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
2190 adr = (struct address_range *) mem_start;
2191 while ((l -= sizeof(struct reg_property)) >= 0) {
2192 if (!measure_only) {
2193 adr[i].space = rp[i].space;
2194 adr[i].address = rp[i].address;
2195 adr[i].size = rp[i].size;
2201 mem_start += i * sizeof(struct address_range);
2207 static unsigned long __init
2208 interpret_root_props(struct device_node *np, unsigned long mem_start,
2209 int naddrc, int nsizec, int measure_only)
2211 struct address_range *adr;
2214 int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
2216 rp = (unsigned int *) get_property(np, "reg", &l);
2217 if (rp != 0 && l >= rpsize) {
2219 adr = (struct address_range *) mem_start;
2220 while ((l -= rpsize) >= 0) {
2221 if (!measure_only) {
2223 adr[i].address = rp[naddrc - 1];
2224 adr[i].size = rp[naddrc + nsizec - 1];
2227 rp += naddrc + nsizec;
2231 mem_start += i * sizeof(struct address_range);
2237 static unsigned long __init
2238 finish_node(struct device_node *np, unsigned long mem_start,
2239 interpret_func *ifunc, int naddrc, int nsizec, int measure_only)
2241 struct device_node *child;
2244 np->name = get_property(np, "name", 0);
2245 np->type = get_property(np, "device_type", 0);
2248 np->name = "<NULL>";
2250 np->type = "<NULL>";
2252 /* get the device addresses and interrupts */
2254 mem_start = ifunc(np, mem_start, naddrc, nsizec, measure_only);
2256 mem_start = finish_node_interrupts(np, mem_start, measure_only);
2258 /* Look for #address-cells and #size-cells properties. */
2259 ip = (int *) get_property(np, "#address-cells", 0);
2262 ip = (int *) get_property(np, "#size-cells", 0);
2266 /* the f50 sets the name to 'display' and 'compatible' to what we
2267 * expect for the name -- Cort
2269 if (!strcmp(np->name, "display"))
2270 np->name = get_property(np, "compatible", 0);
2272 if (!strcmp(np->name, "device-tree") || np->parent == NULL)
2273 ifunc = interpret_root_props;
2274 else if (np->type == 0)
2276 else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
2277 ifunc = interpret_pci_props;
2278 else if (!strcmp(np->type, "dbdma"))
2279 ifunc = interpret_dbdma_props;
2280 else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props)
2281 ifunc = interpret_macio_props;
2282 else if (!strcmp(np->type, "isa"))
2283 ifunc = interpret_isa_props;
2284 else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
2285 ifunc = interpret_root_props;
2286 else if (!((ifunc == interpret_dbdma_props
2287 || ifunc == interpret_macio_props)
2288 && (!strcmp(np->type, "escc")
2289 || !strcmp(np->type, "media-bay"))))
2292 for (child = np->child; child != NULL; child = child->sibling)
2293 mem_start = finish_node(child, mem_start, ifunc,
2294 naddrc, nsizec, measure_only);
2300 * finish_device_tree is called once things are running normally
2301 * (i.e. with text and data mapped to the address they were linked at).
2302 * It traverses the device tree and fills in the name, type,
2303 * {n_}addrs and {n_}intrs fields of each node.
2306 finish_device_tree(void)
2308 unsigned long mem = klimit;
2312 dev_tree_size = finish_node(allnodes, 0, NULL, 0, 0, 1);
2313 mem = (long)abs_to_virt(lmb_alloc(dev_tree_size,
2314 __alignof__(struct device_node)));
2315 if (finish_node(allnodes, mem, NULL, 0, 0, 0) != mem + dev_tree_size)
2317 rtas.dev = of_find_node_by_name(NULL, "rtas");
2321 prom_n_addr_cells(struct device_node* np)
2327 ip = (int *) get_property(np, "#address-cells", 0);
2330 } while (np->parent);
2331 /* No #address-cells property for the root node, default to 1 */
2336 prom_n_size_cells(struct device_node* np)
2342 ip = (int *) get_property(np, "#size-cells", 0);
2345 } while (np->parent);
2346 /* No #size-cells property for the root node, default to 1 */
2351 * Work out the sense (active-low level / active-high edge)
2352 * of each interrupt from the device tree.
2355 prom_get_irq_senses(unsigned char *senses, int off, int max)
2357 struct device_node *np;
2360 /* default to level-triggered */
2361 memset(senses, 1, max - off);
2363 for (np = allnodes; np != 0; np = np->allnext) {
2364 for (j = 0; j < np->n_intrs; j++) {
2365 i = np->intrs[j].line;
2366 if (i >= off && i < max)
2367 senses[i-off] = np->intrs[j].sense;
2373 * Construct and return a list of the device_nodes with a given name.
2375 struct device_node *
2376 find_devices(const char *name)
2378 struct device_node *head, **prevp, *np;
2381 for (np = allnodes; np != 0; np = np->allnext) {
2382 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
2392 * Construct and return a list of the device_nodes with a given type.
2394 struct device_node *
2395 find_type_devices(const char *type)
2397 struct device_node *head, **prevp, *np;
2400 for (np = allnodes; np != 0; np = np->allnext) {
2401 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
2411 * Returns all nodes linked together
2413 struct device_node *
2414 find_all_nodes(void)
2416 struct device_node *head, **prevp, *np;
2419 for (np = allnodes; np != 0; np = np->allnext) {
2427 /* Checks if the given "compat" string matches one of the strings in
2428 * the device's "compatible" property
2431 device_is_compatible(struct device_node *device, const char *compat)
2436 cp = (char *) get_property(device, "compatible", &cplen);
2440 if (strncasecmp(cp, compat, strlen(compat)) == 0)
2452 * Indicates whether the root node has a given value in its
2453 * compatible property.
2456 machine_is_compatible(const char *compat)
2458 struct device_node *root;
2461 root = of_find_node_by_path("/");
2463 rc = device_is_compatible(root, compat);
2470 * Construct and return a list of the device_nodes with a given type
2471 * and compatible property.
2473 struct device_node *
2474 find_compatible_devices(const char *type, const char *compat)
2476 struct device_node *head, **prevp, *np;
2479 for (np = allnodes; np != 0; np = np->allnext) {
2481 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
2483 if (device_is_compatible(np, compat)) {
2493 * Find the device_node with a given full_name.
2495 struct device_node *
2496 find_path_device(const char *path)
2498 struct device_node *np;
2500 for (np = allnodes; np != 0; np = np->allnext)
2501 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
2508 * New implementation of the OF "find" APIs, return a refcounted
2509 * object, call of_node_put() when done. The device tree and list
2510 * are protected by a rw_lock.
2512 * Note that property management will need some locking as well,
2513 * this isn't dealt with yet.
2518 * of_find_node_by_name - Find a node by its "name" property
2519 * @from: The node to start searching from or NULL, the node
2520 * you pass will not be searched, only the next one
2521 * will; typically, you pass what the previous call
2522 * returned. of_node_put() will be called on it
2523 * @name: The name string to match against
2525 * Returns a node pointer with refcount incremented, use
2526 * of_node_put() on it when done.
2528 struct device_node *of_find_node_by_name(struct device_node *from,
2531 struct device_node *np;
2533 read_lock(&devtree_lock);
2534 np = from ? from->allnext : allnodes;
2535 for (; np != 0; np = np->allnext)
2536 if (np->name != 0 && strcasecmp(np->name, name) == 0
2541 read_unlock(&devtree_lock);
2544 EXPORT_SYMBOL(of_find_node_by_name);
2547 * of_find_node_by_type - Find a node by its "device_type" property
2548 * @from: The node to start searching from or NULL, the node
2549 * you pass will not be searched, only the next one
2550 * will; typically, you pass what the previous call
2551 * returned. of_node_put() will be called on it
2552 * @name: The type string to match against
2554 * Returns a node pointer with refcount incremented, use
2555 * of_node_put() on it when done.
2557 struct device_node *of_find_node_by_type(struct device_node *from,
2560 struct device_node *np;
2562 read_lock(&devtree_lock);
2563 np = from ? from->allnext : allnodes;
2564 for (; np != 0; np = np->allnext)
2565 if (np->type != 0 && strcasecmp(np->type, type) == 0
2570 read_unlock(&devtree_lock);
2573 EXPORT_SYMBOL(of_find_node_by_type);
2576 * of_find_compatible_node - Find a node based on type and one of the
2577 * tokens in its "compatible" property
2578 * @from: The node to start searching from or NULL, the node
2579 * you pass will not be searched, only the next one
2580 * will; typically, you pass what the previous call
2581 * returned. of_node_put() will be called on it
2582 * @type: The type string to match "device_type" or NULL to ignore
2583 * @compatible: The string to match to one of the tokens in the device
2584 * "compatible" list.
2586 * Returns a node pointer with refcount incremented, use
2587 * of_node_put() on it when done.
2589 struct device_node *of_find_compatible_node(struct device_node *from,
2590 const char *type, const char *compatible)
2592 struct device_node *np;
2594 read_lock(&devtree_lock);
2595 np = from ? from->allnext : allnodes;
2596 for (; np != 0; np = np->allnext) {
2598 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
2600 if (device_is_compatible(np, compatible) && of_node_get(np))
2605 read_unlock(&devtree_lock);
2608 EXPORT_SYMBOL(of_find_compatible_node);
2611 * of_find_node_by_path - Find a node matching a full OF path
2612 * @path: The full path to match
2614 * Returns a node pointer with refcount incremented, use
2615 * of_node_put() on it when done.
2617 struct device_node *of_find_node_by_path(const char *path)
2619 struct device_node *np = allnodes;
2621 read_lock(&devtree_lock);
2622 for (; np != 0; np = np->allnext)
2623 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
2626 read_unlock(&devtree_lock);
2629 EXPORT_SYMBOL(of_find_node_by_path);
2632 * of_find_all_nodes - Get next node in global list
2633 * @prev: Previous node or NULL to start iteration
2634 * of_node_put() will be called on it
2636 * Returns a node pointer with refcount incremented, use
2637 * of_node_put() on it when done.
2639 struct device_node *of_find_all_nodes(struct device_node *prev)
2641 struct device_node *np;
2643 read_lock(&devtree_lock);
2644 np = prev ? prev->allnext : allnodes;
2645 for (; np != 0; np = np->allnext)
2646 if (of_node_get(np))
2650 read_unlock(&devtree_lock);
2653 EXPORT_SYMBOL(of_find_all_nodes);
2656 * of_get_parent - Get a node's parent if any
2657 * @node: Node to get parent
2659 * Returns a node pointer with refcount incremented, use
2660 * of_node_put() on it when done.
2662 struct device_node *of_get_parent(const struct device_node *node)
2664 struct device_node *np;
2669 read_lock(&devtree_lock);
2670 np = of_node_get(node->parent);
2671 read_unlock(&devtree_lock);
2674 EXPORT_SYMBOL(of_get_parent);
2677 * of_get_next_child - Iterate a node childs
2678 * @node: parent node
2679 * @prev: previous child of the parent node, or NULL to get first
2681 * Returns a node pointer with refcount incremented, use
2682 * of_node_put() on it when done.
2684 struct device_node *of_get_next_child(const struct device_node *node,
2685 struct device_node *prev)
2687 struct device_node *next;
2689 read_lock(&devtree_lock);
2690 next = prev ? prev->sibling : node->child;
2691 for (; next != 0; next = next->sibling)
2692 if (of_node_get(next))
2696 read_unlock(&devtree_lock);
2699 EXPORT_SYMBOL(of_get_next_child);
2702 * of_node_get - Increment refcount of a node
2703 * @node: Node to inc refcount, NULL is supported to
2704 * simplify writing of callers
2706 * Returns the node itself or NULL if gone.
2708 struct device_node *of_node_get(struct device_node *node)
2710 if (node && !OF_IS_STALE(node)) {
2711 atomic_inc(&node->_users);
2716 EXPORT_SYMBOL(of_node_get);
2719 * of_node_cleanup - release a dynamically allocated node
2720 * @arg: Node to be released
2722 static void of_node_cleanup(struct device_node *node)
2724 struct property *prop = node->properties;
2726 if (!OF_IS_DYNAMIC(node))
2729 struct property *next = prop->next;
2737 kfree(node->full_name);
2742 * of_node_put - Decrement refcount of a node
2743 * @node: Node to dec refcount, NULL is supported to
2744 * simplify writing of callers
2747 void of_node_put(struct device_node *node)
2752 WARN_ON(0 == atomic_read(&node->_users));
2754 if (OF_IS_STALE(node)) {
2755 if (atomic_dec_and_test(&node->_users)) {
2756 of_node_cleanup(node);
2761 atomic_dec(&node->_users);
2763 EXPORT_SYMBOL(of_node_put);
2766 * derive_parent - basically like dirname(1)
2767 * @path: the full_name of a node to be added to the tree
2769 * Returns the node which should be the parent of the node
2770 * described by path. E.g., for path = "/foo/bar", returns
2771 * the node with full_name = "/foo".
2773 static struct device_node *derive_parent(const char *path)
2775 struct device_node *parent = NULL;
2776 char *parent_path = "/";
2777 size_t parent_path_len = strrchr(path, '/') - path + 1;
2779 /* reject if path is "/" */
2780 if (!strcmp(path, "/"))
2783 if (strrchr(path, '/') != path) {
2784 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
2787 strlcpy(parent_path, path, parent_path_len);
2789 parent = of_find_node_by_path(parent_path);
2790 if (strcmp(parent_path, "/"))
2796 * Routines for "runtime" addition and removal of device tree nodes.
2798 #ifdef CONFIG_PROC_DEVICETREE
2800 * Add a node to /proc/device-tree.
2802 static void add_node_proc_entries(struct device_node *np)
2804 struct proc_dir_entry *ent;
2806 ent = proc_mkdir(strrchr(np->full_name, '/') + 1, np->parent->pde);
2808 proc_device_tree_add_node(np, ent);
2811 static void remove_node_proc_entries(struct device_node *np)
2813 struct property *pp = np->properties;
2814 struct device_node *parent = np->parent;
2817 remove_proc_entry(pp->name, np->pde);
2821 /* Assuming that symlinks have the same parent directory as
2825 remove_proc_entry(np->name_link->name, parent->pde);
2827 remove_proc_entry(np->addr_link->name, parent->pde);
2829 remove_proc_entry(np->pde->name, parent->pde);
2831 #else /* !CONFIG_PROC_DEVICETREE */
2832 static void add_node_proc_entries(struct device_node *np)
2837 static void remove_node_proc_entries(struct device_node *np)
2841 #endif /* CONFIG_PROC_DEVICETREE */
2844 * Fix up n_intrs and intrs fields in a new device node
2847 static int of_finish_dynamic_node_interrupts(struct device_node *node)
2849 int intrcells, intlen, i;
2850 unsigned *irq, *ints, virq;
2851 struct device_node *ic;
2853 ints = (unsigned int *)get_property(node, "interrupts", &intlen);
2854 intrcells = prom_n_intr_cells(node);
2855 intlen /= intrcells * sizeof(unsigned int);
2856 node->n_intrs = intlen;
2857 node->intrs = kmalloc(sizeof(struct interrupt_info) * intlen,
2862 for (i = 0; i < intlen; ++i) {
2864 node->intrs[i].line = 0;
2865 node->intrs[i].sense = 1;
2866 n = map_interrupt(&irq, &ic, node, ints, intrcells);
2869 virq = virt_irq_create_mapping(irq[0]);
2870 if (virq == NO_IRQ) {
2871 printk(KERN_CRIT "Could not allocate interrupt "
2872 "number for %s\n", node->full_name);
2875 node->intrs[i].line = irq_offset_up(virq);
2877 node->intrs[i].sense = irq[1];
2879 printk(KERN_DEBUG "hmmm, got %d intr cells for %s:", n,
2881 for (j = 0; j < n; ++j)
2882 printk(" %d", irq[j]);
2891 * Fix up the uninitialized fields in a new device node:
2892 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
2894 * A lot of boot-time code is duplicated here, because functions such
2895 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
2898 * This should probably be split up into smaller chunks.
2901 static int of_finish_dynamic_node(struct device_node *node)
2903 struct device_node *parent = of_get_parent(node);
2906 phandle *ibm_phandle;
2908 node->name = get_property(node, "name", 0);
2909 node->type = get_property(node, "device_type", 0);
2916 /* We don't support that function on PowerMac, at least
2919 if (systemcfg->platform == PLATFORM_POWERMAC)
2922 /* fix up new node's linux_phandle field */
2923 if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
2924 node->linux_phandle = *ibm_phandle;
2926 /* do the work of interpret_pci_props */
2927 if (parent->type && !strcmp(parent->type, "pci")) {
2928 struct address_range *adr;
2929 struct pci_reg_property *pci_addrs;
2932 pci_addrs = (struct pci_reg_property *)
2933 get_property(node, "assigned-addresses", &l);
2934 if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
2936 adr = kmalloc(sizeof(struct address_range) *
2937 (l / sizeof(struct pci_reg_property)),
2943 while ((l -= sizeof(struct pci_reg_property)) >= 0) {
2944 adr[i].space = pci_addrs[i].addr.a_hi;
2945 adr[i].address = pci_addrs[i].addr.a_lo;
2946 adr[i].size = pci_addrs[i].size_lo;
2954 /* now do the work of finish_node_interrupts */
2955 if (get_property(node, "interrupts", 0)) {
2956 err = of_finish_dynamic_node_interrupts(node);
2960 /* now do the rough equivalent of update_dn_pci_info, this
2961 * probably is not correct for phb's, but should work for
2965 node->phb = parent->phb;
2967 regs = (u32 *)get_property(node, "reg", 0);
2969 node->busno = (regs[0] >> 16) & 0xff;
2970 node->devfn = (regs[0] >> 8) & 0xff;
2973 /* fixing up iommu_table */
2975 if(strcmp(node->name, "pci") == 0 &&
2976 get_property(node, "ibm,dma-window", NULL)) {
2977 node->bussubno = node->busno;
2978 iommu_devnode_init(node);
2981 node->iommu_table = parent->iommu_table;
2984 of_node_put(parent);
2989 * Given a path and a property list, construct an OF device node, add
2990 * it to the device tree and global list, and place it in
2991 * /proc/device-tree. This function may sleep.
2993 int of_add_node(const char *path, struct property *proplist)
2995 struct device_node *np;
2998 np = kmalloc(sizeof(struct device_node), GFP_KERNEL);
3002 memset(np, 0, sizeof(*np));
3004 np->full_name = kmalloc(strlen(path) + 1, GFP_KERNEL);
3005 if (!np->full_name) {
3009 strcpy(np->full_name, path);
3011 np->properties = proplist;
3012 OF_MARK_DYNAMIC(np);
3014 np->parent = derive_parent(path);
3017 return -EINVAL; /* could also be ENOMEM, though */
3020 if (0 != (err = of_finish_dynamic_node(np))) {
3025 write_lock(&devtree_lock);
3026 np->sibling = np->parent->child;
3027 np->allnext = allnodes;
3028 np->parent->child = np;
3030 write_unlock(&devtree_lock);
3032 add_node_proc_entries(np);
3034 of_node_put(np->parent);
3040 * Remove an OF device node from the system.
3041 * Caller should have already "gotten" np.
3043 int of_remove_node(struct device_node *np)
3045 struct device_node *parent, *child;
3047 parent = of_get_parent(np);
3051 if ((child = of_get_next_child(np, NULL))) {
3056 write_lock(&devtree_lock);
3058 remove_node_proc_entries(np);
3060 allnodes = np->allnext;
3062 struct device_node *prev;
3063 for (prev = allnodes;
3064 prev->allnext != np;
3065 prev = prev->allnext)
3067 prev->allnext = np->allnext;
3070 if (parent->child == np)
3071 parent->child = np->sibling;
3073 struct device_node *prevsib;
3074 for (prevsib = np->parent->child;
3075 prevsib->sibling != np;
3076 prevsib = prevsib->sibling)
3078 prevsib->sibling = np->sibling;
3080 write_unlock(&devtree_lock);
3081 of_node_put(parent);
3086 * Find a property with a given name for a given node
3087 * and return the value.
3090 get_property(struct device_node *np, const char *name, int *lenp)
3092 struct property *pp;
3094 for (pp = np->properties; pp != 0; pp = pp->next)
3095 if (strcmp(pp->name, name) == 0) {
3104 * Add a property to a node
3107 prom_add_property(struct device_node* np, struct property* prop)
3109 struct property **next = &np->properties;
3113 next = &(*next)->next;
3119 print_properties(struct device_node *np)
3121 struct property *pp;
3125 for (pp = np->properties; pp != 0; pp = pp->next) {
3126 printk(KERN_INFO "%s", pp->name);
3127 for (i = strlen(pp->name); i < 16; ++i)
3129 cp = (char *) pp->value;
3130 for (i = pp->length; i > 0; --i, ++cp)
3131 if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
3132 || (i == 1 && *cp != 0))
3134 if (i == 0 && pp->length > 1) {
3135 /* looks like a string */
3136 printk(" %s\n", (char *) pp->value);
3138 /* dump it in hex */
3142 if (pp->length % 4 == 0) {
3143 unsigned int *p = (unsigned int *) pp->value;
3146 for (i = 0; i < n; ++i) {
3147 if (i != 0 && (i % 4) == 0)
3149 printk(" %08x", *p++);
3152 unsigned char *bp = pp->value;
3154 for (i = 0; i < n; ++i) {
3155 if (i != 0 && (i % 16) == 0)
3157 printk(" %02x", *bp++);
3161 if (pp->length > 64)
3162 printk(" ... (length = %d)\n",