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 = { NULL, };
137 phandle prom_display_nodes[FB_MAX] __initdata;
138 unsigned int prom_num_displays = 0;
139 char *of_stdout_device = NULL;
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];
151 char *bootpath = NULL;
152 char *bootdevice = NULL;
155 #define MAX_CPU_THREADS 2
157 struct device_node *allnodes = NULL;
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)
1416 *mem_start = ALIGN(*mem_start, align);
1417 if (*mem_start + needed > *mem_end) {
1418 #ifdef CONFIG_BLK_DEV_INITRD
1419 unsigned long offset = reloc_offset();
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, NULL, &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 int __devinit prom_n_intr_cells(struct device_node *np)
1886 struct device_node *p;
1889 for (p = np; (p = intr_parent(p)) != NULL; ) {
1890 icp = (unsigned int *)
1891 get_property(p, "#interrupt-cells", NULL);
1894 if (get_property(p, "interrupt-controller", NULL) != NULL
1895 || get_property(p, "interrupt-map", NULL) != NULL) {
1896 printk("oops, node %s doesn't have #interrupt-cells\n",
1902 printk("prom_n_intr_cells failed for %s\n", np->full_name);
1908 * Map an interrupt from a device up to the platform interrupt
1911 static int __devinit
1912 map_interrupt(unsigned int **irq, struct device_node **ictrler,
1913 struct device_node *np, unsigned int *ints, int nintrc)
1915 struct device_node *p, *ipar;
1916 unsigned int *imap, *imask, *ip;
1917 int i, imaplen, match;
1918 int newintrc, newaddrc;
1922 reg = (unsigned int *) get_property(np, "reg", NULL);
1923 naddrc = prom_n_addr_cells(np);
1924 p = intr_parent(np);
1926 if (get_property(p, "interrupt-controller", NULL) != NULL)
1927 /* this node is an interrupt controller, stop here */
1929 imap = (unsigned int *)
1930 get_property(p, "interrupt-map", &imaplen);
1935 imask = (unsigned int *)
1936 get_property(p, "interrupt-map-mask", NULL);
1937 if (imask == NULL) {
1938 printk("oops, %s has interrupt-map but no mask\n",
1942 imaplen /= sizeof(unsigned int);
1945 while (imaplen > 0 && !match) {
1946 /* check the child-interrupt field */
1948 for (i = 0; i < naddrc && match; ++i)
1949 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
1950 for (; i < naddrc + nintrc && match; ++i)
1951 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
1952 imap += naddrc + nintrc;
1953 imaplen -= naddrc + nintrc;
1954 /* grab the interrupt parent */
1955 ipar = find_phandle((phandle) *imap++);
1958 printk("oops, no int parent %x in map of %s\n",
1959 imap[-1], p->full_name);
1962 /* find the parent's # addr and intr cells */
1963 ip = (unsigned int *)
1964 get_property(ipar, "#interrupt-cells", NULL);
1966 printk("oops, no #interrupt-cells on %s\n",
1971 ip = (unsigned int *)
1972 get_property(ipar, "#address-cells", NULL);
1973 newaddrc = (ip == NULL)? 0: *ip;
1974 imap += newaddrc + newintrc;
1975 imaplen -= newaddrc + newintrc;
1978 printk("oops, error decoding int-map on %s, len=%d\n",
1979 p->full_name, imaplen);
1984 printk("oops, no match in %s int-map for %s\n",
1985 p->full_name, np->full_name);
1992 ints = imap - nintrc;
1993 reg = ints - naddrc;
1997 printk("hmmm, int tree for %s doesn't have ctrler\n",
2007 static unsigned long __init
2008 finish_node_interrupts(struct device_node *np, unsigned long mem_start,
2012 int intlen, intrcells, intrcount;
2014 unsigned int *irq, virq;
2015 struct device_node *ic;
2017 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
2020 intrcells = prom_n_intr_cells(np);
2021 intlen /= intrcells * sizeof(unsigned int);
2022 np->intrs = (struct interrupt_info *) mem_start;
2023 mem_start += intlen * sizeof(struct interrupt_info);
2029 for (i = 0; i < intlen; ++i, ints += intrcells) {
2030 n = map_interrupt(&irq, &ic, np, ints, intrcells);
2034 /* don't map IRQ numbers under a cascaded 8259 controller */
2035 if (ic && device_is_compatible(ic, "chrp,iic")) {
2036 np->intrs[intrcount].line = irq[0];
2038 virq = virt_irq_create_mapping(irq[0]);
2039 if (virq == NO_IRQ) {
2040 printk(KERN_CRIT "Could not allocate interrupt"
2041 " number for %s\n", np->full_name);
2044 np->intrs[intrcount].line = irq_offset_up(virq);
2047 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
2048 if (systemcfg->platform == PLATFORM_POWERMAC && ic && ic->parent) {
2049 char *name = get_property(ic->parent, "name", NULL);
2050 if (name && !strcmp(name, "u3"))
2051 np->intrs[intrcount].line += 128;
2053 np->intrs[intrcount].sense = 1;
2055 np->intrs[intrcount].sense = irq[1];
2057 printk("hmmm, got %d intr cells for %s:", n,
2059 for (j = 0; j < n; ++j)
2060 printk(" %d", irq[j]);
2065 np->n_intrs = intrcount;
2070 static unsigned long __init
2071 interpret_pci_props(struct device_node *np, unsigned long mem_start,
2072 int naddrc, int nsizec, int measure_only)
2074 struct address_range *adr;
2075 struct pci_reg_property *pci_addrs;
2078 pci_addrs = (struct pci_reg_property *)
2079 get_property(np, "assigned-addresses", &l);
2080 if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
2082 adr = (struct address_range *) mem_start;
2083 while ((l -= sizeof(struct pci_reg_property)) >= 0) {
2084 if (!measure_only) {
2085 adr[i].space = pci_addrs[i].addr.a_hi;
2086 adr[i].address = pci_addrs[i].addr.a_lo;
2087 adr[i].size = pci_addrs[i].size_lo;
2093 mem_start += i * sizeof(struct address_range);
2098 static unsigned long __init
2099 interpret_dbdma_props(struct device_node *np, unsigned long mem_start,
2100 int naddrc, int nsizec, int measure_only)
2102 struct reg_property32 *rp;
2103 struct address_range *adr;
2104 unsigned long base_address;
2106 struct device_node *db;
2109 if (!measure_only) {
2110 for (db = np->parent; db != NULL; db = db->parent) {
2111 if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
2112 base_address = db->addrs[0].address;
2118 rp = (struct reg_property32 *) get_property(np, "reg", &l);
2119 if (rp != 0 && l >= sizeof(struct reg_property32)) {
2121 adr = (struct address_range *) mem_start;
2122 while ((l -= sizeof(struct reg_property32)) >= 0) {
2123 if (!measure_only) {
2125 adr[i].address = rp[i].address + base_address;
2126 adr[i].size = rp[i].size;
2132 mem_start += i * sizeof(struct address_range);
2138 static unsigned long __init
2139 interpret_macio_props(struct device_node *np, unsigned long mem_start,
2140 int naddrc, int nsizec, int measure_only)
2142 struct reg_property32 *rp;
2143 struct address_range *adr;
2144 unsigned long base_address;
2146 struct device_node *db;
2149 if (!measure_only) {
2150 for (db = np->parent; db != NULL; db = db->parent) {
2151 if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
2152 base_address = db->addrs[0].address;
2158 rp = (struct reg_property32 *) get_property(np, "reg", &l);
2159 if (rp != 0 && l >= sizeof(struct reg_property32)) {
2161 adr = (struct address_range *) mem_start;
2162 while ((l -= sizeof(struct reg_property32)) >= 0) {
2163 if (!measure_only) {
2165 adr[i].address = rp[i].address + base_address;
2166 adr[i].size = rp[i].size;
2172 mem_start += i * sizeof(struct address_range);
2178 static unsigned long __init
2179 interpret_isa_props(struct device_node *np, unsigned long mem_start,
2180 int naddrc, int nsizec, int measure_only)
2182 struct isa_reg_property *rp;
2183 struct address_range *adr;
2186 rp = (struct isa_reg_property *) get_property(np, "reg", &l);
2187 if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
2189 adr = (struct address_range *) mem_start;
2190 while ((l -= sizeof(struct reg_property)) >= 0) {
2191 if (!measure_only) {
2192 adr[i].space = rp[i].space;
2193 adr[i].address = rp[i].address;
2194 adr[i].size = rp[i].size;
2200 mem_start += i * sizeof(struct address_range);
2206 static unsigned long __init
2207 interpret_root_props(struct device_node *np, unsigned long mem_start,
2208 int naddrc, int nsizec, int measure_only)
2210 struct address_range *adr;
2213 int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
2215 rp = (unsigned int *) get_property(np, "reg", &l);
2216 if (rp != 0 && l >= rpsize) {
2218 adr = (struct address_range *) mem_start;
2219 while ((l -= rpsize) >= 0) {
2220 if (!measure_only) {
2222 adr[i].address = rp[naddrc - 1];
2223 adr[i].size = rp[naddrc + nsizec - 1];
2226 rp += naddrc + nsizec;
2230 mem_start += i * sizeof(struct address_range);
2236 static unsigned long __init
2237 finish_node(struct device_node *np, unsigned long mem_start,
2238 interpret_func *ifunc, int naddrc, int nsizec, int measure_only)
2240 struct device_node *child;
2243 np->name = get_property(np, "name", NULL);
2244 np->type = get_property(np, "device_type", NULL);
2247 np->name = "<NULL>";
2249 np->type = "<NULL>";
2251 /* get the device addresses and interrupts */
2253 mem_start = ifunc(np, mem_start, naddrc, nsizec, measure_only);
2255 mem_start = finish_node_interrupts(np, mem_start, measure_only);
2257 /* Look for #address-cells and #size-cells properties. */
2258 ip = (int *) get_property(np, "#address-cells", NULL);
2261 ip = (int *) get_property(np, "#size-cells", NULL);
2265 /* the f50 sets the name to 'display' and 'compatible' to what we
2266 * expect for the name -- Cort
2268 if (!strcmp(np->name, "display"))
2269 np->name = get_property(np, "compatible", NULL);
2271 if (!strcmp(np->name, "device-tree") || np->parent == NULL)
2272 ifunc = interpret_root_props;
2273 else if (np->type == 0)
2275 else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
2276 ifunc = interpret_pci_props;
2277 else if (!strcmp(np->type, "dbdma"))
2278 ifunc = interpret_dbdma_props;
2279 else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props)
2280 ifunc = interpret_macio_props;
2281 else if (!strcmp(np->type, "isa"))
2282 ifunc = interpret_isa_props;
2283 else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
2284 ifunc = interpret_root_props;
2285 else if (!((ifunc == interpret_dbdma_props
2286 || ifunc == interpret_macio_props)
2287 && (!strcmp(np->type, "escc")
2288 || !strcmp(np->type, "media-bay"))))
2291 for (child = np->child; child != NULL; child = child->sibling)
2292 mem_start = finish_node(child, mem_start, ifunc,
2293 naddrc, nsizec, measure_only);
2299 * finish_device_tree is called once things are running normally
2300 * (i.e. with text and data mapped to the address they were linked at).
2301 * It traverses the device tree and fills in the name, type,
2302 * {n_}addrs and {n_}intrs fields of each node.
2305 finish_device_tree(void)
2307 unsigned long mem = klimit;
2311 dev_tree_size = finish_node(allnodes, 0, NULL, 0, 0, 1);
2312 mem = (long)abs_to_virt(lmb_alloc(dev_tree_size,
2313 __alignof__(struct device_node)));
2314 if (finish_node(allnodes, mem, NULL, 0, 0, 0) != mem + dev_tree_size)
2316 rtas.dev = of_find_node_by_name(NULL, "rtas");
2320 prom_n_addr_cells(struct device_node* np)
2326 ip = (int *) get_property(np, "#address-cells", NULL);
2329 } while (np->parent);
2330 /* No #address-cells property for the root node, default to 1 */
2335 prom_n_size_cells(struct device_node* np)
2341 ip = (int *) get_property(np, "#size-cells", NULL);
2344 } while (np->parent);
2345 /* No #size-cells property for the root node, default to 1 */
2350 * Work out the sense (active-low level / active-high edge)
2351 * of each interrupt from the device tree.
2354 prom_get_irq_senses(unsigned char *senses, int off, int max)
2356 struct device_node *np;
2359 /* default to level-triggered */
2360 memset(senses, 1, max - off);
2362 for (np = allnodes; np != 0; np = np->allnext) {
2363 for (j = 0; j < np->n_intrs; j++) {
2364 i = np->intrs[j].line;
2365 if (i >= off && i < max)
2366 senses[i-off] = np->intrs[j].sense;
2372 * Construct and return a list of the device_nodes with a given name.
2374 struct device_node *
2375 find_devices(const char *name)
2377 struct device_node *head, **prevp, *np;
2380 for (np = allnodes; np != 0; np = np->allnext) {
2381 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
2391 * Construct and return a list of the device_nodes with a given type.
2393 struct device_node *
2394 find_type_devices(const char *type)
2396 struct device_node *head, **prevp, *np;
2399 for (np = allnodes; np != 0; np = np->allnext) {
2400 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
2410 * Returns all nodes linked together
2412 struct device_node *
2413 find_all_nodes(void)
2415 struct device_node *head, **prevp, *np;
2418 for (np = allnodes; np != 0; np = np->allnext) {
2426 /** Checks if the given "compat" string matches one of the strings in
2427 * the device's "compatible" property
2430 device_is_compatible(struct device_node *device, const char *compat)
2435 cp = (char *) get_property(device, "compatible", &cplen);
2439 if (strncasecmp(cp, compat, strlen(compat)) == 0)
2451 * Indicates whether the root node has a given value in its
2452 * compatible property.
2455 machine_is_compatible(const char *compat)
2457 struct device_node *root;
2460 root = of_find_node_by_path("/");
2462 rc = device_is_compatible(root, compat);
2469 * Construct and return a list of the device_nodes with a given type
2470 * and compatible property.
2472 struct device_node *
2473 find_compatible_devices(const char *type, const char *compat)
2475 struct device_node *head, **prevp, *np;
2478 for (np = allnodes; np != 0; np = np->allnext) {
2480 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
2482 if (device_is_compatible(np, compat)) {
2492 * Find the device_node with a given full_name.
2494 struct device_node *
2495 find_path_device(const char *path)
2497 struct device_node *np;
2499 for (np = allnodes; np != 0; np = np->allnext)
2500 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
2507 * New implementation of the OF "find" APIs, return a refcounted
2508 * object, call of_node_put() when done. The device tree and list
2509 * are protected by a rw_lock.
2511 * Note that property management will need some locking as well,
2512 * this isn't dealt with yet.
2517 * of_find_node_by_name - Find a node by its "name" property
2518 * @from: The node to start searching from or NULL, the node
2519 * you pass will not be searched, only the next one
2520 * will; typically, you pass what the previous call
2521 * returned. of_node_put() will be called on it
2522 * @name: The name string to match against
2524 * Returns a node pointer with refcount incremented, use
2525 * of_node_put() on it when done.
2527 struct device_node *of_find_node_by_name(struct device_node *from,
2530 struct device_node *np;
2532 read_lock(&devtree_lock);
2533 np = from ? from->allnext : allnodes;
2534 for (; np != 0; np = np->allnext)
2535 if (np->name != 0 && strcasecmp(np->name, name) == 0
2540 read_unlock(&devtree_lock);
2543 EXPORT_SYMBOL(of_find_node_by_name);
2546 * of_find_node_by_type - Find a node by its "device_type" property
2547 * @from: The node to start searching from or NULL, the node
2548 * you pass will not be searched, only the next one
2549 * will; typically, you pass what the previous call
2550 * returned. of_node_put() will be called on it
2551 * @name: The type string to match against
2553 * Returns a node pointer with refcount incremented, use
2554 * of_node_put() on it when done.
2556 struct device_node *of_find_node_by_type(struct device_node *from,
2559 struct device_node *np;
2561 read_lock(&devtree_lock);
2562 np = from ? from->allnext : allnodes;
2563 for (; np != 0; np = np->allnext)
2564 if (np->type != 0 && strcasecmp(np->type, type) == 0
2569 read_unlock(&devtree_lock);
2572 EXPORT_SYMBOL(of_find_node_by_type);
2575 * of_find_compatible_node - Find a node based on type and one of the
2576 * tokens in its "compatible" property
2577 * @from: The node to start searching from or NULL, the node
2578 * you pass will not be searched, only the next one
2579 * will; typically, you pass what the previous call
2580 * returned. of_node_put() will be called on it
2581 * @type: The type string to match "device_type" or NULL to ignore
2582 * @compatible: The string to match to one of the tokens in the device
2583 * "compatible" list.
2585 * Returns a node pointer with refcount incremented, use
2586 * of_node_put() on it when done.
2588 struct device_node *of_find_compatible_node(struct device_node *from,
2589 const char *type, const char *compatible)
2591 struct device_node *np;
2593 read_lock(&devtree_lock);
2594 np = from ? from->allnext : allnodes;
2595 for (; np != 0; np = np->allnext) {
2597 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
2599 if (device_is_compatible(np, compatible) && of_node_get(np))
2604 read_unlock(&devtree_lock);
2607 EXPORT_SYMBOL(of_find_compatible_node);
2610 * of_find_node_by_path - Find a node matching a full OF path
2611 * @path: The full path to match
2613 * Returns a node pointer with refcount incremented, use
2614 * of_node_put() on it when done.
2616 struct device_node *of_find_node_by_path(const char *path)
2618 struct device_node *np = allnodes;
2620 read_lock(&devtree_lock);
2621 for (; np != 0; np = np->allnext)
2622 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
2625 read_unlock(&devtree_lock);
2628 EXPORT_SYMBOL(of_find_node_by_path);
2631 * of_find_all_nodes - Get next node in global list
2632 * @prev: Previous node or NULL to start iteration
2633 * of_node_put() will be called on it
2635 * Returns a node pointer with refcount incremented, use
2636 * of_node_put() on it when done.
2638 struct device_node *of_find_all_nodes(struct device_node *prev)
2640 struct device_node *np;
2642 read_lock(&devtree_lock);
2643 np = prev ? prev->allnext : allnodes;
2644 for (; np != 0; np = np->allnext)
2645 if (of_node_get(np))
2649 read_unlock(&devtree_lock);
2652 EXPORT_SYMBOL(of_find_all_nodes);
2655 * of_get_parent - Get a node's parent if any
2656 * @node: Node to get parent
2658 * Returns a node pointer with refcount incremented, use
2659 * of_node_put() on it when done.
2661 struct device_node *of_get_parent(const struct device_node *node)
2663 struct device_node *np;
2668 read_lock(&devtree_lock);
2669 np = of_node_get(node->parent);
2670 read_unlock(&devtree_lock);
2673 EXPORT_SYMBOL(of_get_parent);
2676 * of_get_next_child - Iterate a node childs
2677 * @node: parent node
2678 * @prev: previous child of the parent node, or NULL to get first
2680 * Returns a node pointer with refcount incremented, use
2681 * of_node_put() on it when done.
2683 struct device_node *of_get_next_child(const struct device_node *node,
2684 struct device_node *prev)
2686 struct device_node *next;
2688 read_lock(&devtree_lock);
2689 next = prev ? prev->sibling : node->child;
2690 for (; next != 0; next = next->sibling)
2691 if (of_node_get(next))
2695 read_unlock(&devtree_lock);
2698 EXPORT_SYMBOL(of_get_next_child);
2701 * of_node_get - Increment refcount of a node
2702 * @node: Node to inc refcount, NULL is supported to
2703 * simplify writing of callers
2705 * Returns the node itself or NULL if gone.
2707 struct device_node *of_node_get(struct device_node *node)
2709 if (node && !OF_IS_STALE(node)) {
2710 atomic_inc(&node->_users);
2715 EXPORT_SYMBOL(of_node_get);
2718 * of_node_cleanup - release a dynamically allocated node
2719 * @arg: Node to be released
2721 static void of_node_cleanup(struct device_node *node)
2723 struct property *prop = node->properties;
2725 if (!OF_IS_DYNAMIC(node))
2728 struct property *next = prop->next;
2736 kfree(node->full_name);
2741 * of_node_put - Decrement refcount of a node
2742 * @node: Node to dec refcount, NULL is supported to
2743 * simplify writing of callers
2746 void of_node_put(struct device_node *node)
2751 WARN_ON(0 == atomic_read(&node->_users));
2753 if (OF_IS_STALE(node)) {
2754 if (atomic_dec_and_test(&node->_users)) {
2755 of_node_cleanup(node);
2760 atomic_dec(&node->_users);
2762 EXPORT_SYMBOL(of_node_put);
2765 * derive_parent - basically like dirname(1)
2766 * @path: the full_name of a node to be added to the tree
2768 * Returns the node which should be the parent of the node
2769 * described by path. E.g., for path = "/foo/bar", returns
2770 * the node with full_name = "/foo".
2772 static struct device_node *derive_parent(const char *path)
2774 struct device_node *parent = NULL;
2775 char *parent_path = "/";
2776 size_t parent_path_len = strrchr(path, '/') - path + 1;
2778 /* reject if path is "/" */
2779 if (!strcmp(path, "/"))
2782 if (strrchr(path, '/') != path) {
2783 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
2786 strlcpy(parent_path, path, parent_path_len);
2788 parent = of_find_node_by_path(parent_path);
2789 if (strcmp(parent_path, "/"))
2795 * Routines for "runtime" addition and removal of device tree nodes.
2797 #ifdef CONFIG_PROC_DEVICETREE
2799 * Add a node to /proc/device-tree.
2801 static void add_node_proc_entries(struct device_node *np)
2803 struct proc_dir_entry *ent;
2805 ent = proc_mkdir(strrchr(np->full_name, '/') + 1, np->parent->pde);
2807 proc_device_tree_add_node(np, ent);
2810 static void remove_node_proc_entries(struct device_node *np)
2812 struct property *pp = np->properties;
2813 struct device_node *parent = np->parent;
2816 remove_proc_entry(pp->name, np->pde);
2820 /* Assuming that symlinks have the same parent directory as
2824 remove_proc_entry(np->name_link->name, parent->pde);
2826 remove_proc_entry(np->addr_link->name, parent->pde);
2828 remove_proc_entry(np->pde->name, parent->pde);
2830 #else /* !CONFIG_PROC_DEVICETREE */
2831 static void add_node_proc_entries(struct device_node *np)
2836 static void remove_node_proc_entries(struct device_node *np)
2840 #endif /* CONFIG_PROC_DEVICETREE */
2843 * Fix up n_intrs and intrs fields in a new device node
2846 static int of_finish_dynamic_node_interrupts(struct device_node *node)
2848 int intrcells, intlen, i;
2849 unsigned *irq, *ints, virq;
2850 struct device_node *ic;
2852 ints = (unsigned int *)get_property(node, "interrupts", &intlen);
2853 intrcells = prom_n_intr_cells(node);
2854 intlen /= intrcells * sizeof(unsigned int);
2855 node->n_intrs = intlen;
2856 node->intrs = kmalloc(sizeof(struct interrupt_info) * intlen,
2861 for (i = 0; i < intlen; ++i) {
2863 node->intrs[i].line = 0;
2864 node->intrs[i].sense = 1;
2865 n = map_interrupt(&irq, &ic, node, ints, intrcells);
2868 virq = virt_irq_create_mapping(irq[0]);
2869 if (virq == NO_IRQ) {
2870 printk(KERN_CRIT "Could not allocate interrupt "
2871 "number for %s\n", node->full_name);
2874 node->intrs[i].line = irq_offset_up(virq);
2876 node->intrs[i].sense = irq[1];
2878 printk(KERN_DEBUG "hmmm, got %d intr cells for %s:", n,
2880 for (j = 0; j < n; ++j)
2881 printk(" %d", irq[j]);
2890 * Fix up the uninitialized fields in a new device node:
2891 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
2893 * A lot of boot-time code is duplicated here, because functions such
2894 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
2897 * This should probably be split up into smaller chunks.
2900 static int of_finish_dynamic_node(struct device_node *node)
2902 struct device_node *parent = of_get_parent(node);
2905 phandle *ibm_phandle;
2907 node->name = get_property(node, "name", NULL);
2908 node->type = get_property(node, "device_type", NULL);
2915 /* We don't support that function on PowerMac, at least
2918 if (systemcfg->platform == PLATFORM_POWERMAC)
2921 /* fix up new node's linux_phandle field */
2922 if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
2923 node->linux_phandle = *ibm_phandle;
2925 /* do the work of interpret_pci_props */
2926 if (parent->type && !strcmp(parent->type, "pci")) {
2927 struct address_range *adr;
2928 struct pci_reg_property *pci_addrs;
2931 pci_addrs = (struct pci_reg_property *)
2932 get_property(node, "assigned-addresses", &l);
2933 if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
2935 adr = kmalloc(sizeof(struct address_range) *
2936 (l / sizeof(struct pci_reg_property)),
2942 while ((l -= sizeof(struct pci_reg_property)) >= 0) {
2943 adr[i].space = pci_addrs[i].addr.a_hi;
2944 adr[i].address = pci_addrs[i].addr.a_lo;
2945 adr[i].size = pci_addrs[i].size_lo;
2953 /* now do the work of finish_node_interrupts */
2954 if (get_property(node, "interrupts", NULL)) {
2955 err = of_finish_dynamic_node_interrupts(node);
2959 /* now do the rough equivalent of update_dn_pci_info, this
2960 * probably is not correct for phb's, but should work for
2964 node->phb = parent->phb;
2966 regs = (u32 *)get_property(node, "reg", NULL);
2968 node->busno = (regs[0] >> 16) & 0xff;
2969 node->devfn = (regs[0] >> 8) & 0xff;
2972 /* fixing up iommu_table */
2974 if (strcmp(node->name, "pci") == 0 &&
2975 get_property(node, "ibm,dma-window", NULL)) {
2976 node->bussubno = node->busno;
2977 iommu_devnode_init(node);
2979 node->iommu_table = parent->iommu_table;
2982 of_node_put(parent);
2987 * Given a path and a property list, construct an OF device node, add
2988 * it to the device tree and global list, and place it in
2989 * /proc/device-tree. This function may sleep.
2991 int of_add_node(const char *path, struct property *proplist)
2993 struct device_node *np;
2996 np = kmalloc(sizeof(struct device_node), GFP_KERNEL);
3000 memset(np, 0, sizeof(*np));
3002 np->full_name = kmalloc(strlen(path) + 1, GFP_KERNEL);
3003 if (!np->full_name) {
3007 strcpy(np->full_name, path);
3009 np->properties = proplist;
3010 OF_MARK_DYNAMIC(np);
3012 np->parent = derive_parent(path);
3015 return -EINVAL; /* could also be ENOMEM, though */
3018 if (0 != (err = of_finish_dynamic_node(np))) {
3023 write_lock(&devtree_lock);
3024 np->sibling = np->parent->child;
3025 np->allnext = allnodes;
3026 np->parent->child = np;
3028 write_unlock(&devtree_lock);
3030 add_node_proc_entries(np);
3032 of_node_put(np->parent);
3038 * Remove an OF device node from the system.
3039 * Caller should have already "gotten" np.
3041 int of_remove_node(struct device_node *np)
3043 struct device_node *parent, *child;
3045 parent = of_get_parent(np);
3049 if ((child = of_get_next_child(np, NULL))) {
3054 write_lock(&devtree_lock);
3056 remove_node_proc_entries(np);
3058 allnodes = np->allnext;
3060 struct device_node *prev;
3061 for (prev = allnodes;
3062 prev->allnext != np;
3063 prev = prev->allnext)
3065 prev->allnext = np->allnext;
3068 if (parent->child == np)
3069 parent->child = np->sibling;
3071 struct device_node *prevsib;
3072 for (prevsib = np->parent->child;
3073 prevsib->sibling != np;
3074 prevsib = prevsib->sibling)
3076 prevsib->sibling = np->sibling;
3078 write_unlock(&devtree_lock);
3079 of_node_put(parent);
3084 * Find a property with a given name for a given node
3085 * and return the value.
3088 get_property(struct device_node *np, const char *name, int *lenp)
3090 struct property *pp;
3092 for (pp = np->properties; pp != 0; pp = pp->next)
3093 if (strcmp(pp->name, name) == 0) {
3102 * Add a property to a node
3105 prom_add_property(struct device_node* np, struct property* prop)
3107 struct property **next = &np->properties;
3111 next = &(*next)->next;
3117 print_properties(struct device_node *np)
3119 struct property *pp;
3123 for (pp = np->properties; pp != 0; pp = pp->next) {
3124 printk(KERN_INFO "%s", pp->name);
3125 for (i = strlen(pp->name); i < 16; ++i)
3127 cp = (char *) pp->value;
3128 for (i = pp->length; i > 0; --i, ++cp)
3129 if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
3130 || (i == 1 && *cp != 0))
3132 if (i == 0 && pp->length > 1) {
3133 /* looks like a string */
3134 printk(" %s\n", (char *) pp->value);
3136 /* dump it in hex */
3140 if (pp->length % 4 == 0) {
3141 unsigned int *p = (unsigned int *) pp->value;
3144 for (i = 0; i < n; ++i) {
3145 if (i != 0 && (i % 4) == 0)
3147 printk(" %08x", *p++);
3150 unsigned char *bp = pp->value;
3152 for (i = 0; i < n; ++i) {
3153 if (i != 0 && (i % 16) == 0)
3155 printk(" %02x", *bp++);
3159 if (pp->length > 64)
3160 printk(" ... (length = %d)\n",