2 * Procedures for interfacing to Open Firmware.
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
19 #include <linux/config.h>
20 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/init.h>
23 #include <linux/threads.h>
24 #include <linux/spinlock.h>
25 #include <linux/types.h>
26 #include <linux/pci.h>
27 #include <linux/proc_fs.h>
28 #include <linux/stringify.h>
29 #include <linux/delay.h>
30 #include <linux/initrd.h>
31 #include <linux/bitops.h>
35 #include <asm/processor.h>
39 #include <asm/system.h>
41 #include <asm/pgtable.h>
43 #include <asm/iommu.h>
44 #include <asm/bootinfo.h>
45 #include <asm/btext.h>
46 #include <asm/sections.h>
47 #include <asm/machdep.h>
49 #ifdef CONFIG_LOGO_LINUX_CLUT224
50 #include <linux/linux_logo.h>
51 extern const struct linux_logo logo_linux_clut224
;
55 * Properties whose value is longer than this get excluded from our
56 * copy of the device tree. This value does need to be big enough to
57 * ensure that we don't lose things like the interrupt-map property
58 * on a PCI-PCI bridge.
60 #define MAX_PROPERTY_LENGTH (1UL * 1024 * 1024)
63 * Eventually bump that one up
65 #define DEVTREE_CHUNK_SIZE 0x100000
68 * This is the size of the local memory reserve map that gets copied
69 * into the boot params passed to the kernel. That size is totally
70 * flexible as the kernel just reads the list until it encounters an
71 * entry with size 0, so it can be changed without breaking binary
74 #define MEM_RESERVE_MAP_SIZE 8
77 * prom_init() is called very early on, before the kernel text
78 * and data have been mapped to KERNELBASE. At this point the code
79 * is running at whatever address it has been loaded at.
80 * On ppc32 we compile with -mrelocatable, which means that references
81 * to extern and static variables get relocated automatically.
82 * On ppc64 we have to relocate the references explicitly with
83 * RELOC. (Note that strings count as static variables.)
85 * Because OF may have mapped I/O devices into the area starting at
86 * KERNELBASE, particularly on CHRP machines, we can't safely call
87 * OF once the kernel has been mapped to KERNELBASE. Therefore all
88 * OF calls must be done within prom_init().
90 * ADDR is used in calls to call_prom. The 4th and following
91 * arguments to call_prom should be 32-bit values.
92 * On ppc64, 64 bit values are truncated to 32 bits (and
93 * fortunately don't get interpreted as two arguments).
96 #define RELOC(x) (*PTRRELOC(&(x)))
97 #define ADDR(x) (u32) add_reloc_offset((unsigned long)(x))
100 #define ADDR(x) (u32) (x)
103 #define PROM_BUG() do { \
104 prom_printf("kernel BUG at %s line 0x%x!\n", \
105 RELOC(__FILE__), __LINE__); \
106 __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR); \
110 #define prom_debug(x...) prom_printf(x)
112 #define prom_debug(x...)
116 #define PLATFORM_POWERMAC _MACH_Pmac
117 #define PLATFORM_CHRP _MACH_chrp
121 typedef u32 prom_arg_t
;
137 struct mem_map_entry
{
144 extern void __start(unsigned long r3
, unsigned long r4
, unsigned long r5
);
147 extern void enter_prom(struct prom_args
*args
, unsigned long entry
);
149 static inline void enter_prom(struct prom_args
*args
, unsigned long entry
)
151 ((void (*)(struct prom_args
*))entry
)(args
);
155 extern void copy_and_flush(unsigned long dest
, unsigned long src
,
156 unsigned long size
, unsigned long offset
);
159 static struct prom_t __initdata prom
;
161 static unsigned long prom_entry __initdata
;
163 #define PROM_SCRATCH_SIZE 256
165 static char __initdata of_stdout_device
[256];
166 static char __initdata prom_scratch
[PROM_SCRATCH_SIZE
];
168 static unsigned long __initdata dt_header_start
;
169 static unsigned long __initdata dt_struct_start
, dt_struct_end
;
170 static unsigned long __initdata dt_string_start
, dt_string_end
;
172 static unsigned long __initdata prom_initrd_start
, prom_initrd_end
;
175 static int __initdata iommu_force_on
;
176 static int __initdata ppc64_iommu_off
;
177 static unsigned long __initdata prom_tce_alloc_start
;
178 static unsigned long __initdata prom_tce_alloc_end
;
181 static int __initdata of_platform
;
183 static char __initdata prom_cmd_line
[COMMAND_LINE_SIZE
];
185 static unsigned long __initdata prom_memory_limit
;
187 static unsigned long __initdata alloc_top
;
188 static unsigned long __initdata alloc_top_high
;
189 static unsigned long __initdata alloc_bottom
;
190 static unsigned long __initdata rmo_top
;
191 static unsigned long __initdata ram_top
;
193 static struct mem_map_entry __initdata mem_reserve_map
[MEM_RESERVE_MAP_SIZE
];
194 static int __initdata mem_reserve_cnt
;
196 static cell_t __initdata regbuf
[1024];
199 #define MAX_CPU_THREADS 2
205 unsigned int threadid
;
206 } hmt_thread_data
[NR_CPUS
];
207 #endif /* CONFIG_HMT */
210 * Error results ... some OF calls will return "-1" on error, some
211 * will return 0, some will return either. To simplify, here are
212 * macros to use with any ihandle or phandle return value to check if
216 #define PROM_ERROR (-1u)
217 #define PHANDLE_VALID(p) ((p) != 0 && (p) != PROM_ERROR)
218 #define IHANDLE_VALID(i) ((i) != 0 && (i) != PROM_ERROR)
221 /* This is the one and *ONLY* place where we actually call open
225 static int __init
call_prom(const char *service
, int nargs
, int nret
, ...)
228 struct prom_args args
;
231 args
.service
= ADDR(service
);
235 va_start(list
, nret
);
236 for (i
= 0; i
< nargs
; i
++)
237 args
.args
[i
] = va_arg(list
, prom_arg_t
);
240 for (i
= 0; i
< nret
; i
++)
241 args
.args
[nargs
+i
] = 0;
243 enter_prom(&args
, RELOC(prom_entry
));
245 return (nret
> 0) ? args
.args
[nargs
] : 0;
248 static int __init
call_prom_ret(const char *service
, int nargs
, int nret
,
249 prom_arg_t
*rets
, ...)
252 struct prom_args args
;
255 args
.service
= ADDR(service
);
259 va_start(list
, rets
);
260 for (i
= 0; i
< nargs
; i
++)
261 args
.args
[i
] = va_arg(list
, prom_arg_t
);
264 for (i
= 0; i
< nret
; i
++)
267 enter_prom(&args
, RELOC(prom_entry
));
270 for (i
= 1; i
< nret
; ++i
)
271 rets
[i
-1] = args
.args
[nargs
+i
];
273 return (nret
> 0) ? args
.args
[nargs
] : 0;
277 static unsigned int __init
prom_claim(unsigned long virt
, unsigned long size
,
280 return (unsigned int)call_prom("claim", 3, 1,
281 (prom_arg_t
)virt
, (prom_arg_t
)size
,
285 static void __init
prom_print(const char *msg
)
288 struct prom_t
*_prom
= &RELOC(prom
);
290 if (_prom
->stdout
== 0)
293 for (p
= msg
; *p
!= 0; p
= q
) {
294 for (q
= p
; *q
!= 0 && *q
!= '\n'; ++q
)
297 call_prom("write", 3, 1, _prom
->stdout
, p
, q
- p
);
301 call_prom("write", 3, 1, _prom
->stdout
, ADDR("\r\n"), 2);
306 static void __init
prom_print_hex(unsigned long val
)
308 int i
, nibbles
= sizeof(val
)*2;
309 char buf
[sizeof(val
)*2+1];
310 struct prom_t
*_prom
= &RELOC(prom
);
312 for (i
= nibbles
-1; i
>= 0; i
--) {
313 buf
[i
] = (val
& 0xf) + '0';
315 buf
[i
] += ('a'-'0'-10);
319 call_prom("write", 3, 1, _prom
->stdout
, buf
, nibbles
);
323 static void __init
prom_printf(const char *format
, ...)
325 const char *p
, *q
, *s
;
328 struct prom_t
*_prom
= &RELOC(prom
);
330 va_start(args
, format
);
332 format
= PTRRELOC(format
);
334 for (p
= format
; *p
!= 0; p
= q
) {
335 for (q
= p
; *q
!= 0 && *q
!= '\n' && *q
!= '%'; ++q
)
338 call_prom("write", 3, 1, _prom
->stdout
, p
, q
- p
);
343 call_prom("write", 3, 1, _prom
->stdout
,
353 s
= va_arg(args
, const char *);
358 v
= va_arg(args
, unsigned long);
366 static void __init
__attribute__((noreturn
)) prom_panic(const char *reason
)
369 reason
= PTRRELOC(reason
);
372 /* ToDo: should put up an SRC here on p/iSeries */
373 call_prom("exit", 0, 0);
375 for (;;) /* should never get here */
380 static int __init
prom_next_node(phandle
*nodep
)
384 if ((node
= *nodep
) != 0
385 && (*nodep
= call_prom("child", 1, 1, node
)) != 0)
387 if ((*nodep
= call_prom("peer", 1, 1, node
)) != 0)
390 if ((node
= call_prom("parent", 1, 1, node
)) == 0)
392 if ((*nodep
= call_prom("peer", 1, 1, node
)) != 0)
397 static int __init
prom_getprop(phandle node
, const char *pname
,
398 void *value
, size_t valuelen
)
400 return call_prom("getprop", 4, 1, node
, ADDR(pname
),
401 (u32
)(unsigned long) value
, (u32
) valuelen
);
404 static int __init
prom_getproplen(phandle node
, const char *pname
)
406 return call_prom("getproplen", 2, 1, node
, ADDR(pname
));
409 static int __init
prom_setprop(phandle node
, const char *pname
,
410 void *value
, size_t valuelen
)
412 return call_prom("setprop", 4, 1, node
, ADDR(pname
),
413 (u32
)(unsigned long) value
, (u32
) valuelen
);
416 /* We can't use the standard versions because of RELOC headaches. */
417 #define isxdigit(c) (('0' <= (c) && (c) <= '9') \
418 || ('a' <= (c) && (c) <= 'f') \
419 || ('A' <= (c) && (c) <= 'F'))
421 #define isdigit(c) ('0' <= (c) && (c) <= '9')
422 #define islower(c) ('a' <= (c) && (c) <= 'z')
423 #define toupper(c) (islower(c) ? ((c) - 'a' + 'A') : (c))
425 unsigned long prom_strtoul(const char *cp
, const char **endp
)
427 unsigned long result
= 0, base
= 10, value
;
432 if (toupper(*cp
) == 'X') {
438 while (isxdigit(*cp
) &&
439 (value
= isdigit(*cp
) ? *cp
- '0' : toupper(*cp
) - 'A' + 10) < base
) {
440 result
= result
* base
+ value
;
450 unsigned long prom_memparse(const char *ptr
, const char **retptr
)
452 unsigned long ret
= prom_strtoul(ptr
, retptr
);
456 * We can't use a switch here because GCC *may* generate a
457 * jump table which won't work, because we're not running at
458 * the address we're linked at.
460 if ('G' == **retptr
|| 'g' == **retptr
)
463 if ('M' == **retptr
|| 'm' == **retptr
)
466 if ('K' == **retptr
|| 'k' == **retptr
)
478 * Early parsing of the command line passed to the kernel, used for
479 * "mem=x" and the options that affect the iommu
481 static void __init
early_cmdline_parse(void)
483 struct prom_t
*_prom
= &RELOC(prom
);
487 RELOC(prom_cmd_line
[0]) = 0;
488 p
= RELOC(prom_cmd_line
);
489 if ((long)_prom
->chosen
> 0)
490 l
= prom_getprop(_prom
->chosen
, "bootargs", p
, COMMAND_LINE_SIZE
-1);
491 #ifdef CONFIG_CMDLINE
492 if (l
== 0) /* dbl check */
493 strlcpy(RELOC(prom_cmd_line
),
494 RELOC(CONFIG_CMDLINE
), sizeof(prom_cmd_line
));
495 #endif /* CONFIG_CMDLINE */
496 prom_printf("command line: %s\n", RELOC(prom_cmd_line
));
499 opt
= strstr(RELOC(prom_cmd_line
), RELOC("iommu="));
501 prom_printf("iommu opt is: %s\n", opt
);
503 while (*opt
&& *opt
== ' ')
505 if (!strncmp(opt
, RELOC("off"), 3))
506 RELOC(ppc64_iommu_off
) = 1;
507 else if (!strncmp(opt
, RELOC("force"), 5))
508 RELOC(iommu_force_on
) = 1;
512 opt
= strstr(RELOC(prom_cmd_line
), RELOC("mem="));
515 RELOC(prom_memory_limit
) = prom_memparse(opt
, (const char **)&opt
);
517 /* Align to 16 MB == size of ppc64 large page */
518 RELOC(prom_memory_limit
) = ALIGN(RELOC(prom_memory_limit
), 0x1000000);
523 #ifdef CONFIG_PPC_PSERIES
525 * To tell the firmware what our capabilities are, we have to pass
526 * it a fake 32-bit ELF header containing a couple of PT_NOTE sections
527 * that contain structures that contain the actual values.
529 static struct fake_elf
{
536 char name
[8]; /* "PowerPC" */
550 char name
[24]; /* "IBM,RPA-Client-Config" */
564 .e_ident
= { 0x7f, 'E', 'L', 'F',
565 ELFCLASS32
, ELFDATA2MSB
, EV_CURRENT
},
566 .e_type
= ET_EXEC
, /* yeah right */
568 .e_version
= EV_CURRENT
,
569 .e_phoff
= offsetof(struct fake_elf
, phdr
),
570 .e_phentsize
= sizeof(Elf32_Phdr
),
576 .p_offset
= offsetof(struct fake_elf
, chrpnote
),
577 .p_filesz
= sizeof(struct chrpnote
)
580 .p_offset
= offsetof(struct fake_elf
, rpanote
),
581 .p_filesz
= sizeof(struct rpanote
)
585 .namesz
= sizeof("PowerPC"),
586 .descsz
= sizeof(struct chrpdesc
),
590 .real_mode
= ~0U, /* ~0 means "don't care" */
599 .namesz
= sizeof("IBM,RPA-Client-Config"),
600 .descsz
= sizeof(struct rpadesc
),
602 .name
= "IBM,RPA-Client-Config",
605 .min_rmo_size
= 64, /* in megabytes */
606 .min_rmo_percent
= 0,
607 .max_pft_size
= 48, /* 2^48 bytes max PFT size */
615 static void __init
prom_send_capabilities(void)
619 elfloader
= call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
620 if (elfloader
== 0) {
621 prom_printf("couldn't open /packages/elf-loader\n");
624 call_prom("call-method", 3, 1, ADDR("process-elf-header"),
625 elfloader
, ADDR(&fake_elf
));
626 call_prom("close", 1, 0, elfloader
);
631 * Memory allocation strategy... our layout is normally:
633 * at 14Mb or more we have vmlinux, then a gap and initrd. In some
634 * rare cases, initrd might end up being before the kernel though.
635 * We assume this won't override the final kernel at 0, we have no
636 * provision to handle that in this version, but it should hopefully
639 * alloc_top is set to the top of RMO, eventually shrink down if the
642 * alloc_bottom is set to the top of kernel/initrd
644 * from there, allocations are done this way : rtas is allocated
645 * topmost, and the device-tree is allocated from the bottom. We try
646 * to grow the device-tree allocation as we progress. If we can't,
647 * then we fail, we don't currently have a facility to restart
648 * elsewhere, but that shouldn't be necessary.
650 * Note that calls to reserve_mem have to be done explicitly, memory
651 * allocated with either alloc_up or alloc_down isn't automatically
657 * Allocates memory in the RMO upward from the kernel/initrd
659 * When align is 0, this is a special case, it means to allocate in place
660 * at the current location of alloc_bottom or fail (that is basically
661 * extending the previous allocation). Used for the device-tree flattening
663 static unsigned long __init
alloc_up(unsigned long size
, unsigned long align
)
665 unsigned long base
= _ALIGN_UP(RELOC(alloc_bottom
), align
);
666 unsigned long addr
= 0;
668 prom_debug("alloc_up(%x, %x)\n", size
, align
);
669 if (RELOC(ram_top
) == 0)
670 prom_panic("alloc_up() called with mem not initialized\n");
673 base
= _ALIGN_UP(RELOC(alloc_bottom
), align
);
675 base
= RELOC(alloc_bottom
);
677 for(; (base
+ size
) <= RELOC(alloc_top
);
678 base
= _ALIGN_UP(base
+ 0x100000, align
)) {
679 prom_debug(" trying: 0x%x\n\r", base
);
680 addr
= (unsigned long)prom_claim(base
, size
, 0);
681 if (addr
!= PROM_ERROR
)
689 RELOC(alloc_bottom
) = addr
;
691 prom_debug(" -> %x\n", addr
);
692 prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom
));
693 prom_debug(" alloc_top : %x\n", RELOC(alloc_top
));
694 prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high
));
695 prom_debug(" rmo_top : %x\n", RELOC(rmo_top
));
696 prom_debug(" ram_top : %x\n", RELOC(ram_top
));
702 * Allocates memory downward, either from top of RMO, or if highmem
703 * is set, from the top of RAM. Note that this one doesn't handle
704 * failures. It does claim memory if highmem is not set.
706 static unsigned long __init
alloc_down(unsigned long size
, unsigned long align
,
709 unsigned long base
, addr
= 0;
711 prom_debug("alloc_down(%x, %x, %s)\n", size
, align
,
712 highmem
? RELOC("(high)") : RELOC("(low)"));
713 if (RELOC(ram_top
) == 0)
714 prom_panic("alloc_down() called with mem not initialized\n");
717 /* Carve out storage for the TCE table. */
718 addr
= _ALIGN_DOWN(RELOC(alloc_top_high
) - size
, align
);
719 if (addr
<= RELOC(alloc_bottom
))
721 /* Will we bump into the RMO ? If yes, check out that we
722 * didn't overlap existing allocations there, if we did,
723 * we are dead, we must be the first in town !
725 if (addr
< RELOC(rmo_top
)) {
726 /* Good, we are first */
727 if (RELOC(alloc_top
) == RELOC(rmo_top
))
728 RELOC(alloc_top
) = RELOC(rmo_top
) = addr
;
732 RELOC(alloc_top_high
) = addr
;
736 base
= _ALIGN_DOWN(RELOC(alloc_top
) - size
, align
);
737 for (; base
> RELOC(alloc_bottom
);
738 base
= _ALIGN_DOWN(base
- 0x100000, align
)) {
739 prom_debug(" trying: 0x%x\n\r", base
);
740 addr
= (unsigned long)prom_claim(base
, size
, 0);
741 if (addr
!= PROM_ERROR
)
747 RELOC(alloc_top
) = addr
;
750 prom_debug(" -> %x\n", addr
);
751 prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom
));
752 prom_debug(" alloc_top : %x\n", RELOC(alloc_top
));
753 prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high
));
754 prom_debug(" rmo_top : %x\n", RELOC(rmo_top
));
755 prom_debug(" ram_top : %x\n", RELOC(ram_top
));
763 static unsigned long __init
prom_next_cell(int s
, cell_t
**cellp
)
768 /* Ignore more than 2 cells */
769 while (s
> sizeof(unsigned long) / 4) {
785 * Very dumb function for adding to the memory reserve list, but
786 * we don't need anything smarter at this point
788 * XXX Eventually check for collisions. They should NEVER happen.
789 * If problems seem to show up, it would be a good start to track
792 static void reserve_mem(unsigned long base
, unsigned long size
)
794 unsigned long top
= base
+ size
;
795 unsigned long cnt
= RELOC(mem_reserve_cnt
);
800 /* We need to always keep one empty entry so that we
801 * have our terminator with "size" set to 0 since we are
802 * dumb and just copy this entire array to the boot params
804 base
= _ALIGN_DOWN(base
, PAGE_SIZE
);
805 top
= _ALIGN_UP(top
, PAGE_SIZE
);
808 if (cnt
>= (MEM_RESERVE_MAP_SIZE
- 1))
809 prom_panic("Memory reserve map exhausted !\n");
810 RELOC(mem_reserve_map
)[cnt
].base
= base
;
811 RELOC(mem_reserve_map
)[cnt
].size
= size
;
812 RELOC(mem_reserve_cnt
) = cnt
+ 1;
816 * Initialize memory allocation mecanism, parse "memory" nodes and
817 * obtain that way the top of memory and RMO to setup out local allocator
819 static void __init
prom_init_mem(void)
822 char *path
, type
[64];
825 struct prom_t
*_prom
= &RELOC(prom
);
829 * We iterate the memory nodes to find
830 * 1) top of RMO (first node)
834 prom_getprop(_prom
->root
, "#address-cells", &rac
, sizeof(rac
));
836 prom_getprop(_prom
->root
, "#size-cells", &rsc
, sizeof(rsc
));
837 prom_debug("root_addr_cells: %x\n", (unsigned long) rac
);
838 prom_debug("root_size_cells: %x\n", (unsigned long) rsc
);
840 prom_debug("scanning memory:\n");
841 path
= RELOC(prom_scratch
);
843 for (node
= 0; prom_next_node(&node
); ) {
845 prom_getprop(node
, "device_type", type
, sizeof(type
));
847 if (strcmp(type
, RELOC("memory")))
850 plen
= prom_getprop(node
, "reg", RELOC(regbuf
), sizeof(regbuf
));
851 if (plen
> sizeof(regbuf
)) {
852 prom_printf("memory node too large for buffer !\n");
853 plen
= sizeof(regbuf
);
856 endp
= p
+ (plen
/ sizeof(cell_t
));
859 memset(path
, 0, PROM_SCRATCH_SIZE
);
860 call_prom("package-to-path", 3, 1, node
, path
, PROM_SCRATCH_SIZE
-1);
861 prom_debug(" node %s :\n", path
);
862 #endif /* DEBUG_PROM */
864 while ((endp
- p
) >= (rac
+ rsc
)) {
865 unsigned long base
, size
;
867 base
= prom_next_cell(rac
, &p
);
868 size
= prom_next_cell(rsc
, &p
);
872 prom_debug(" %x %x\n", base
, size
);
874 RELOC(rmo_top
) = size
;
875 if ((base
+ size
) > RELOC(ram_top
))
876 RELOC(ram_top
) = base
+ size
;
880 RELOC(alloc_bottom
) = PAGE_ALIGN((unsigned long)&RELOC(_end
) + 0x4000);
882 /* Check if we have an initrd after the kernel, if we do move our bottom
885 if (RELOC(prom_initrd_start
)) {
886 if (RELOC(prom_initrd_end
) > RELOC(alloc_bottom
))
887 RELOC(alloc_bottom
) = PAGE_ALIGN(RELOC(prom_initrd_end
));
891 * If prom_memory_limit is set we reduce the upper limits *except* for
892 * alloc_top_high. This must be the real top of RAM so we can put
896 RELOC(alloc_top_high
) = RELOC(ram_top
);
898 if (RELOC(prom_memory_limit
)) {
899 if (RELOC(prom_memory_limit
) <= RELOC(alloc_bottom
)) {
900 prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
901 RELOC(prom_memory_limit
));
902 RELOC(prom_memory_limit
) = 0;
903 } else if (RELOC(prom_memory_limit
) >= RELOC(ram_top
)) {
904 prom_printf("Ignoring mem=%x >= ram_top.\n",
905 RELOC(prom_memory_limit
));
906 RELOC(prom_memory_limit
) = 0;
908 RELOC(ram_top
) = RELOC(prom_memory_limit
);
909 RELOC(rmo_top
) = min(RELOC(rmo_top
), RELOC(prom_memory_limit
));
914 * Setup our top alloc point, that is top of RMO or top of
915 * segment 0 when running non-LPAR.
916 * Some RS64 machines have buggy firmware where claims up at
917 * 1GB fail. Cap at 768MB as a workaround.
918 * Since 768MB is plenty of room, and we need to cap to something
919 * reasonable on 32-bit, cap at 768MB on all machines.
922 RELOC(rmo_top
) = RELOC(ram_top
);
923 RELOC(rmo_top
) = min(0x30000000ul
, RELOC(rmo_top
));
924 RELOC(alloc_top
) = RELOC(rmo_top
);
926 prom_printf("memory layout at init:\n");
927 prom_printf(" memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit
));
928 prom_printf(" alloc_bottom : %x\n", RELOC(alloc_bottom
));
929 prom_printf(" alloc_top : %x\n", RELOC(alloc_top
));
930 prom_printf(" alloc_top_hi : %x\n", RELOC(alloc_top_high
));
931 prom_printf(" rmo_top : %x\n", RELOC(rmo_top
));
932 prom_printf(" ram_top : %x\n", RELOC(ram_top
));
937 * Allocate room for and instantiate RTAS
939 static void __init
prom_instantiate_rtas(void)
946 prom_debug("prom_instantiate_rtas: start...\n");
948 rtas_node
= call_prom("finddevice", 1, 1, ADDR("/rtas"));
949 prom_debug("rtas_node: %x\n", rtas_node
);
950 if (!PHANDLE_VALID(rtas_node
))
953 prom_getprop(rtas_node
, "rtas-size", &size
, sizeof(size
));
957 base
= alloc_down(size
, PAGE_SIZE
, 0);
959 prom_printf("RTAS allocation failed !\n");
963 rtas_inst
= call_prom("open", 1, 1, ADDR("/rtas"));
964 if (!IHANDLE_VALID(rtas_inst
)) {
965 prom_printf("opening rtas package failed");
969 prom_printf("instantiating rtas at 0x%x ...", base
);
971 if (call_prom_ret("call-method", 3, 2, &entry
,
972 ADDR("instantiate-rtas"),
973 rtas_inst
, base
) == PROM_ERROR
975 prom_printf(" failed\n");
978 prom_printf(" done\n");
980 reserve_mem(base
, size
);
982 prom_setprop(rtas_node
, "linux,rtas-base", &base
, sizeof(base
));
983 prom_setprop(rtas_node
, "linux,rtas-entry", &entry
, sizeof(entry
));
985 prom_debug("rtas base = 0x%x\n", base
);
986 prom_debug("rtas entry = 0x%x\n", entry
);
987 prom_debug("rtas size = 0x%x\n", (long)size
);
989 prom_debug("prom_instantiate_rtas: end...\n");
994 * Allocate room for and initialize TCE tables
996 static void __init
prom_initialize_tce_table(void)
1000 char compatible
[64], type
[64], model
[64];
1001 char *path
= RELOC(prom_scratch
);
1003 u32 minalign
, minsize
;
1004 u64 tce_entry
, *tce_entryp
;
1005 u64 local_alloc_top
, local_alloc_bottom
;
1008 if (RELOC(ppc64_iommu_off
))
1011 prom_debug("starting prom_initialize_tce_table\n");
1013 /* Cache current top of allocs so we reserve a single block */
1014 local_alloc_top
= RELOC(alloc_top_high
);
1015 local_alloc_bottom
= local_alloc_top
;
1017 /* Search all nodes looking for PHBs. */
1018 for (node
= 0; prom_next_node(&node
); ) {
1022 prom_getprop(node
, "compatible",
1023 compatible
, sizeof(compatible
));
1024 prom_getprop(node
, "device_type", type
, sizeof(type
));
1025 prom_getprop(node
, "model", model
, sizeof(model
));
1027 if ((type
[0] == 0) || (strstr(type
, RELOC("pci")) == NULL
))
1030 /* Keep the old logic in tack to avoid regression. */
1031 if (compatible
[0] != 0) {
1032 if ((strstr(compatible
, RELOC("python")) == NULL
) &&
1033 (strstr(compatible
, RELOC("Speedwagon")) == NULL
) &&
1034 (strstr(compatible
, RELOC("Winnipeg")) == NULL
))
1036 } else if (model
[0] != 0) {
1037 if ((strstr(model
, RELOC("ython")) == NULL
) &&
1038 (strstr(model
, RELOC("peedwagon")) == NULL
) &&
1039 (strstr(model
, RELOC("innipeg")) == NULL
))
1043 if (prom_getprop(node
, "tce-table-minalign", &minalign
,
1044 sizeof(minalign
)) == PROM_ERROR
)
1046 if (prom_getprop(node
, "tce-table-minsize", &minsize
,
1047 sizeof(minsize
)) == PROM_ERROR
)
1048 minsize
= 4UL << 20;
1051 * Even though we read what OF wants, we just set the table
1052 * size to 4 MB. This is enough to map 2GB of PCI DMA space.
1053 * By doing this, we avoid the pitfalls of trying to DMA to
1054 * MMIO space and the DMA alias hole.
1056 * On POWER4, firmware sets the TCE region by assuming
1057 * each TCE table is 8MB. Using this memory for anything
1058 * else will impact performance, so we always allocate 8MB.
1061 if (__is_processor(PV_POWER4
) || __is_processor(PV_POWER4p
))
1062 minsize
= 8UL << 20;
1064 minsize
= 4UL << 20;
1066 /* Align to the greater of the align or size */
1067 align
= max(minalign
, minsize
);
1068 base
= alloc_down(minsize
, align
, 1);
1070 prom_panic("ERROR, cannot find space for TCE table.\n");
1071 if (base
< local_alloc_bottom
)
1072 local_alloc_bottom
= base
;
1074 /* Save away the TCE table attributes for later use. */
1075 prom_setprop(node
, "linux,tce-base", &base
, sizeof(base
));
1076 prom_setprop(node
, "linux,tce-size", &minsize
, sizeof(minsize
));
1078 /* It seems OF doesn't null-terminate the path :-( */
1079 memset(path
, 0, sizeof(path
));
1080 /* Call OF to setup the TCE hardware */
1081 if (call_prom("package-to-path", 3, 1, node
,
1082 path
, PROM_SCRATCH_SIZE
-1) == PROM_ERROR
) {
1083 prom_printf("package-to-path failed\n");
1086 prom_debug("TCE table: %s\n", path
);
1087 prom_debug("\tnode = 0x%x\n", node
);
1088 prom_debug("\tbase = 0x%x\n", base
);
1089 prom_debug("\tsize = 0x%x\n", minsize
);
1091 /* Initialize the table to have a one-to-one mapping
1092 * over the allocated size.
1094 tce_entryp
= (unsigned long *)base
;
1095 for (i
= 0; i
< (minsize
>> 3) ;tce_entryp
++, i
++) {
1096 tce_entry
= (i
<< PAGE_SHIFT
);
1098 *tce_entryp
= tce_entry
;
1101 prom_printf("opening PHB %s", path
);
1102 phb_node
= call_prom("open", 1, 1, path
);
1104 prom_printf("... failed\n");
1106 prom_printf("... done\n");
1108 call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
1109 phb_node
, -1, minsize
,
1110 (u32
) base
, (u32
) (base
>> 32));
1111 call_prom("close", 1, 0, phb_node
);
1114 reserve_mem(local_alloc_bottom
, local_alloc_top
- local_alloc_bottom
);
1116 if (RELOC(prom_memory_limit
)) {
1118 * We align the start to a 16MB boundary so we can map
1119 * the TCE area using large pages if possible.
1120 * The end should be the top of RAM so no need to align it.
1122 RELOC(prom_tce_alloc_start
) = _ALIGN_DOWN(local_alloc_bottom
,
1124 RELOC(prom_tce_alloc_end
) = local_alloc_top
;
1127 /* Flag the first invalid entry */
1128 prom_debug("ending prom_initialize_tce_table\n");
1133 * With CHRP SMP we need to use the OF to start the other processors.
1134 * We can't wait until smp_boot_cpus (the OF is trashed by then)
1135 * so we have to put the processors into a holding pattern controlled
1136 * by the kernel (not OF) before we destroy the OF.
1138 * This uses a chunk of low memory, puts some holding pattern
1139 * code there and sends the other processors off to there until
1140 * smp_boot_cpus tells them to do something. The holding pattern
1141 * checks that address until its cpu # is there, when it is that
1142 * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
1143 * of setting those values.
1145 * We also use physical address 0x4 here to tell when a cpu
1146 * is in its holding pattern code.
1150 static void __init
prom_hold_cpus(void)
1158 unsigned int interrupt_server
[MAX_CPU_THREADS
];
1159 unsigned int cpu_threads
, hw_cpu_num
;
1161 extern void __secondary_hold(void);
1162 extern unsigned long __secondary_hold_spinloop
;
1163 extern unsigned long __secondary_hold_acknowledge
;
1164 unsigned long *spinloop
1165 = (void *) __pa(&__secondary_hold_spinloop
);
1166 unsigned long *acknowledge
1167 = (void *) __pa(&__secondary_hold_acknowledge
);
1169 unsigned long secondary_hold
1170 = __pa(*PTRRELOC((unsigned long *)__secondary_hold
));
1172 unsigned long secondary_hold
= __pa(&__secondary_hold
);
1174 struct prom_t
*_prom
= &RELOC(prom
);
1176 prom_debug("prom_hold_cpus: start...\n");
1177 prom_debug(" 1) spinloop = 0x%x\n", (unsigned long)spinloop
);
1178 prom_debug(" 1) *spinloop = 0x%x\n", *spinloop
);
1179 prom_debug(" 1) acknowledge = 0x%x\n",
1180 (unsigned long)acknowledge
);
1181 prom_debug(" 1) *acknowledge = 0x%x\n", *acknowledge
);
1182 prom_debug(" 1) secondary_hold = 0x%x\n", secondary_hold
);
1184 /* Set the common spinloop variable, so all of the secondary cpus
1185 * will block when they are awakened from their OF spinloop.
1186 * This must occur for both SMP and non SMP kernels, since OF will
1187 * be trashed when we move the kernel.
1192 for (i
= 0; i
< NR_CPUS
; i
++) {
1193 RELOC(hmt_thread_data
)[i
].pir
= 0xdeadbeef;
1197 for (node
= 0; prom_next_node(&node
); ) {
1199 prom_getprop(node
, "device_type", type
, sizeof(type
));
1200 if (strcmp(type
, RELOC("cpu")) != 0)
1203 /* Skip non-configured cpus. */
1204 if (prom_getprop(node
, "status", type
, sizeof(type
)) > 0)
1205 if (strcmp(type
, RELOC("okay")) != 0)
1209 prom_getprop(node
, "reg", ®
, sizeof(reg
));
1211 prom_debug("\ncpuid = 0x%x\n", cpuid
);
1212 prom_debug("cpu hw idx = 0x%x\n", reg
);
1214 /* Init the acknowledge var which will be reset by
1215 * the secondary cpu when it awakens from its OF
1218 *acknowledge
= (unsigned long)-1;
1220 propsize
= prom_getprop(node
, "ibm,ppc-interrupt-server#s",
1222 sizeof(interrupt_server
));
1224 /* no property. old hardware has no SMT */
1226 interrupt_server
[0] = reg
; /* fake it with phys id */
1228 /* We have a threaded processor */
1229 cpu_threads
= propsize
/ sizeof(u32
);
1230 if (cpu_threads
> MAX_CPU_THREADS
) {
1231 prom_printf("SMT: too many threads!\n"
1232 "SMT: found %x, max is %x\n",
1233 cpu_threads
, MAX_CPU_THREADS
);
1234 cpu_threads
= 1; /* ToDo: panic? */
1238 hw_cpu_num
= interrupt_server
[0];
1239 if (hw_cpu_num
!= _prom
->cpu
) {
1240 /* Primary Thread of non-boot cpu */
1241 prom_printf("%x : starting cpu hw idx %x... ", cpuid
, reg
);
1242 call_prom("start-cpu", 3, 0, node
,
1243 secondary_hold
, reg
);
1245 for ( i
= 0 ; (i
< 100000000) &&
1246 (*acknowledge
== ((unsigned long)-1)); i
++ )
1249 if (*acknowledge
== reg
) {
1250 prom_printf("done\n");
1251 /* We have to get every CPU out of OF,
1252 * even if we never start it. */
1253 if (cpuid
>= NR_CPUS
)
1256 prom_printf("failed: %x\n", *acknowledge
);
1261 prom_printf("%x : boot cpu %x\n", cpuid
, reg
);
1265 /* Init paca for secondary threads. They start later. */
1266 for (i
=1; i
< cpu_threads
; i
++) {
1268 if (cpuid
>= NR_CPUS
)
1271 #endif /* CONFIG_SMP */
1275 /* Only enable HMT on processors that provide support. */
1276 if (__is_processor(PV_PULSAR
) ||
1277 __is_processor(PV_ICESTAR
) ||
1278 __is_processor(PV_SSTAR
)) {
1279 prom_printf(" starting secondary threads\n");
1281 for (i
= 0; i
< NR_CPUS
; i
+= 2) {
1286 unsigned long pir
= mfspr(SPRN_PIR
);
1287 if (__is_processor(PV_PULSAR
)) {
1288 RELOC(hmt_thread_data
)[i
].pir
=
1291 RELOC(hmt_thread_data
)[i
].pir
=
1297 prom_printf("Processor is not HMT capable\n");
1301 if (cpuid
> NR_CPUS
)
1302 prom_printf("WARNING: maximum CPUs (" __stringify(NR_CPUS
)
1303 ") exceeded: ignoring extras\n");
1305 prom_debug("prom_hold_cpus: end...\n");
1310 static void __init
prom_init_client_services(unsigned long pp
)
1312 struct prom_t
*_prom
= &RELOC(prom
);
1314 /* Get a handle to the prom entry point before anything else */
1315 RELOC(prom_entry
) = pp
;
1317 /* get a handle for the stdout device */
1318 _prom
->chosen
= call_prom("finddevice", 1, 1, ADDR("/chosen"));
1319 if (!PHANDLE_VALID(_prom
->chosen
))
1320 prom_panic("cannot find chosen"); /* msg won't be printed :( */
1322 /* get device tree root */
1323 _prom
->root
= call_prom("finddevice", 1, 1, ADDR("/"));
1324 if (!PHANDLE_VALID(_prom
->root
))
1325 prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1328 static void __init
prom_init_stdout(void)
1330 struct prom_t
*_prom
= &RELOC(prom
);
1331 char *path
= RELOC(of_stdout_device
);
1335 if (prom_getprop(_prom
->chosen
, "stdout", &val
, sizeof(val
)) <= 0)
1336 prom_panic("cannot find stdout");
1338 _prom
->stdout
= val
;
1340 /* Get the full OF pathname of the stdout device */
1341 memset(path
, 0, 256);
1342 call_prom("instance-to-path", 3, 1, _prom
->stdout
, path
, 255);
1343 val
= call_prom("instance-to-package", 1, 1, _prom
->stdout
);
1344 prom_setprop(_prom
->chosen
, "linux,stdout-package", &val
, sizeof(val
));
1345 prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device
));
1346 prom_setprop(_prom
->chosen
, "linux,stdout-path",
1347 RELOC(of_stdout_device
), strlen(RELOC(of_stdout_device
))+1);
1349 /* If it's a display, note it */
1350 memset(type
, 0, sizeof(type
));
1351 prom_getprop(val
, "device_type", type
, sizeof(type
));
1352 if (strcmp(type
, RELOC("display")) == 0)
1353 prom_setprop(val
, "linux,boot-display", NULL
, 0);
1356 static void __init
prom_close_stdin(void)
1358 struct prom_t
*_prom
= &RELOC(prom
);
1361 if (prom_getprop(_prom
->chosen
, "stdin", &val
, sizeof(val
)) > 0)
1362 call_prom("close", 1, 0, val
);
1365 static int __init
prom_find_machine_type(void)
1367 struct prom_t
*_prom
= &RELOC(prom
);
1372 len
= prom_getprop(_prom
->root
, "compatible",
1373 compat
, sizeof(compat
)-1);
1377 char *p
= &compat
[i
];
1381 if (strstr(p
, RELOC("Power Macintosh")) ||
1382 strstr(p
, RELOC("MacRISC4")))
1383 return PLATFORM_POWERMAC
;
1385 if (strstr(p
, RELOC("Momentum,Maple")))
1386 return PLATFORM_MAPLE
;
1392 /* Default to pSeries. We need to know if we are running LPAR */
1393 rtas
= call_prom("finddevice", 1, 1, ADDR("/rtas"));
1394 if (PHANDLE_VALID(rtas
)) {
1395 int x
= prom_getproplen(rtas
, "ibm,hypertas-functions");
1396 if (x
!= PROM_ERROR
) {
1397 prom_printf("Hypertas detected, assuming LPAR !\n");
1398 return PLATFORM_PSERIES_LPAR
;
1401 return PLATFORM_PSERIES
;
1403 return PLATFORM_CHRP
;
1407 static int __init
prom_set_color(ihandle ih
, int i
, int r
, int g
, int b
)
1409 return call_prom("call-method", 6, 1, ADDR("color!"), ih
, i
, b
, g
, r
);
1413 * If we have a display that we don't know how to drive,
1414 * we will want to try to execute OF's open method for it
1415 * later. However, OF will probably fall over if we do that
1416 * we've taken over the MMU.
1417 * So we check whether we will need to open the display,
1418 * and if so, open it now.
1420 static void __init
prom_check_displays(void)
1422 char type
[16], *path
;
1427 static unsigned char default_colors
[] = {
1445 const unsigned char *clut
;
1447 prom_printf("Looking for displays\n");
1448 for (node
= 0; prom_next_node(&node
); ) {
1449 memset(type
, 0, sizeof(type
));
1450 prom_getprop(node
, "device_type", type
, sizeof(type
));
1451 if (strcmp(type
, RELOC("display")) != 0)
1454 /* It seems OF doesn't null-terminate the path :-( */
1455 path
= RELOC(prom_scratch
);
1456 memset(path
, 0, PROM_SCRATCH_SIZE
);
1459 * leave some room at the end of the path for appending extra
1462 if (call_prom("package-to-path", 3, 1, node
, path
,
1463 PROM_SCRATCH_SIZE
-10) == PROM_ERROR
)
1465 prom_printf("found display : %s, opening ... ", path
);
1467 ih
= call_prom("open", 1, 1, path
);
1469 prom_printf("failed\n");
1474 prom_printf("done\n");
1475 prom_setprop(node
, "linux,opened", NULL
, 0);
1477 /* Setup a usable color table when the appropriate
1478 * method is available. Should update this to set-colors */
1479 clut
= RELOC(default_colors
);
1480 for (i
= 0; i
< 32; i
++, clut
+= 3)
1481 if (prom_set_color(ih
, i
, clut
[0], clut
[1],
1485 #ifdef CONFIG_LOGO_LINUX_CLUT224
1486 clut
= PTRRELOC(RELOC(logo_linux_clut224
.clut
));
1487 for (i
= 0; i
< RELOC(logo_linux_clut224
.clutsize
); i
++, clut
+= 3)
1488 if (prom_set_color(ih
, i
+ 32, clut
[0], clut
[1],
1491 #endif /* CONFIG_LOGO_LINUX_CLUT224 */
1496 /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
1497 static void __init
*make_room(unsigned long *mem_start
, unsigned long *mem_end
,
1498 unsigned long needed
, unsigned long align
)
1502 *mem_start
= _ALIGN(*mem_start
, align
);
1503 while ((*mem_start
+ needed
) > *mem_end
) {
1504 unsigned long room
, chunk
;
1506 prom_debug("Chunk exhausted, claiming more at %x...\n",
1507 RELOC(alloc_bottom
));
1508 room
= RELOC(alloc_top
) - RELOC(alloc_bottom
);
1509 if (room
> DEVTREE_CHUNK_SIZE
)
1510 room
= DEVTREE_CHUNK_SIZE
;
1511 if (room
< PAGE_SIZE
)
1512 prom_panic("No memory for flatten_device_tree (no room)");
1513 chunk
= alloc_up(room
, 0);
1515 prom_panic("No memory for flatten_device_tree (claim failed)");
1516 *mem_end
= RELOC(alloc_top
);
1519 ret
= (void *)*mem_start
;
1520 *mem_start
+= needed
;
1525 #define dt_push_token(token, mem_start, mem_end) \
1526 do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
1528 static unsigned long __init
dt_find_string(char *str
)
1532 s
= os
= (char *)RELOC(dt_string_start
);
1534 while (s
< (char *)RELOC(dt_string_end
)) {
1535 if (strcmp(s
, str
) == 0)
1543 * The Open Firmware 1275 specification states properties must be 31 bytes or
1544 * less, however not all firmwares obey this. Make it 64 bytes to be safe.
1546 #define MAX_PROPERTY_NAME 64
1548 static void __init
scan_dt_build_strings(phandle node
,
1549 unsigned long *mem_start
,
1550 unsigned long *mem_end
)
1552 char *prev_name
, *namep
, *sstart
;
1556 sstart
= (char *)RELOC(dt_string_start
);
1558 /* get and store all property names */
1559 prev_name
= RELOC("");
1561 /* 64 is max len of name including nul. */
1562 namep
= make_room(mem_start
, mem_end
, MAX_PROPERTY_NAME
, 1);
1563 if (call_prom("nextprop", 3, 1, node
, prev_name
, namep
) != 1) {
1564 /* No more nodes: unwind alloc */
1565 *mem_start
= (unsigned long)namep
;
1570 if (strcmp(namep
, RELOC("name")) == 0) {
1571 *mem_start
= (unsigned long)namep
;
1572 prev_name
= RELOC("name");
1575 /* get/create string entry */
1576 soff
= dt_find_string(namep
);
1578 *mem_start
= (unsigned long)namep
;
1579 namep
= sstart
+ soff
;
1581 /* Trim off some if we can */
1582 *mem_start
= (unsigned long)namep
+ strlen(namep
) + 1;
1583 RELOC(dt_string_end
) = *mem_start
;
1588 /* do all our children */
1589 child
= call_prom("child", 1, 1, node
);
1590 while (child
!= 0) {
1591 scan_dt_build_strings(child
, mem_start
, mem_end
);
1592 child
= call_prom("peer", 1, 1, child
);
1596 static void __init
scan_dt_build_struct(phandle node
, unsigned long *mem_start
,
1597 unsigned long *mem_end
)
1600 char *namep
, *prev_name
, *sstart
, *p
, *ep
, *lp
, *path
;
1602 unsigned char *valp
;
1603 static char pname
[MAX_PROPERTY_NAME
];
1606 dt_push_token(OF_DT_BEGIN_NODE
, mem_start
, mem_end
);
1608 /* get the node's full name */
1609 namep
= (char *)*mem_start
;
1610 l
= call_prom("package-to-path", 3, 1, node
,
1611 namep
, *mem_end
- *mem_start
);
1613 /* Didn't fit? Get more room. */
1614 if ((l
+1) > (*mem_end
- *mem_start
)) {
1615 namep
= make_room(mem_start
, mem_end
, l
+1, 1);
1616 call_prom("package-to-path", 3, 1, node
, namep
, l
);
1620 /* Fixup an Apple bug where they have bogus \0 chars in the
1621 * middle of the path in some properties
1623 for (p
= namep
, ep
= namep
+ l
; p
< ep
; p
++)
1625 memmove(p
, p
+1, ep
- p
);
1629 /* now try to extract the unit name in that mess */
1630 for (p
= namep
, lp
= NULL
; *p
; p
++)
1634 memmove(namep
, lp
, strlen(lp
) + 1);
1635 *mem_start
= _ALIGN(((unsigned long) namep
) +
1636 strlen(namep
) + 1, 4);
1639 /* get it again for debugging */
1640 path
= RELOC(prom_scratch
);
1641 memset(path
, 0, PROM_SCRATCH_SIZE
);
1642 call_prom("package-to-path", 3, 1, node
, path
, PROM_SCRATCH_SIZE
-1);
1644 /* get and store all properties */
1645 prev_name
= RELOC("");
1646 sstart
= (char *)RELOC(dt_string_start
);
1648 if (call_prom("nextprop", 3, 1, node
, prev_name
,
1653 if (strcmp(RELOC(pname
), RELOC("name")) == 0) {
1654 prev_name
= RELOC("name");
1658 /* find string offset */
1659 soff
= dt_find_string(RELOC(pname
));
1661 prom_printf("WARNING: Can't find string index for"
1662 " <%s>, node %s\n", RELOC(pname
), path
);
1665 prev_name
= sstart
+ soff
;
1668 l
= call_prom("getproplen", 2, 1, node
, RELOC(pname
));
1671 if (l
== PROM_ERROR
)
1673 if (l
> MAX_PROPERTY_LENGTH
) {
1674 prom_printf("WARNING: ignoring large property ");
1675 /* It seems OF doesn't null-terminate the path :-( */
1676 prom_printf("[%s] ", path
);
1677 prom_printf("%s length 0x%x\n", RELOC(pname
), l
);
1681 /* push property head */
1682 dt_push_token(OF_DT_PROP
, mem_start
, mem_end
);
1683 dt_push_token(l
, mem_start
, mem_end
);
1684 dt_push_token(soff
, mem_start
, mem_end
);
1686 /* push property content */
1687 valp
= make_room(mem_start
, mem_end
, l
, 4);
1688 call_prom("getprop", 4, 1, node
, RELOC(pname
), valp
, l
);
1689 *mem_start
= _ALIGN(*mem_start
, 4);
1692 /* Add a "linux,phandle" property. */
1693 soff
= dt_find_string(RELOC("linux,phandle"));
1695 prom_printf("WARNING: Can't find string index for"
1696 " <linux-phandle> node %s\n", path
);
1698 dt_push_token(OF_DT_PROP
, mem_start
, mem_end
);
1699 dt_push_token(4, mem_start
, mem_end
);
1700 dt_push_token(soff
, mem_start
, mem_end
);
1701 valp
= make_room(mem_start
, mem_end
, 4, 4);
1702 *(u32
*)valp
= node
;
1705 /* do all our children */
1706 child
= call_prom("child", 1, 1, node
);
1707 while (child
!= 0) {
1708 scan_dt_build_struct(child
, mem_start
, mem_end
);
1709 child
= call_prom("peer", 1, 1, child
);
1712 dt_push_token(OF_DT_END_NODE
, mem_start
, mem_end
);
1715 static void __init
flatten_device_tree(void)
1718 unsigned long mem_start
, mem_end
, room
;
1719 struct boot_param_header
*hdr
;
1720 struct prom_t
*_prom
= &RELOC(prom
);
1725 * Check how much room we have between alloc top & bottom (+/- a
1726 * few pages), crop to 4Mb, as this is our "chuck" size
1728 room
= RELOC(alloc_top
) - RELOC(alloc_bottom
) - 0x4000;
1729 if (room
> DEVTREE_CHUNK_SIZE
)
1730 room
= DEVTREE_CHUNK_SIZE
;
1731 prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom
));
1733 /* Now try to claim that */
1734 mem_start
= (unsigned long)alloc_up(room
, PAGE_SIZE
);
1736 prom_panic("Can't allocate initial device-tree chunk\n");
1737 mem_end
= RELOC(alloc_top
);
1739 /* Get root of tree */
1740 root
= call_prom("peer", 1, 1, (phandle
)0);
1741 if (root
== (phandle
)0)
1742 prom_panic ("couldn't get device tree root\n");
1744 /* Build header and make room for mem rsv map */
1745 mem_start
= _ALIGN(mem_start
, 4);
1746 hdr
= make_room(&mem_start
, &mem_end
,
1747 sizeof(struct boot_param_header
), 4);
1748 RELOC(dt_header_start
) = (unsigned long)hdr
;
1749 rsvmap
= make_room(&mem_start
, &mem_end
, sizeof(mem_reserve_map
), 8);
1751 /* Start of strings */
1752 mem_start
= PAGE_ALIGN(mem_start
);
1753 RELOC(dt_string_start
) = mem_start
;
1754 mem_start
+= 4; /* hole */
1756 /* Add "linux,phandle" in there, we'll need it */
1757 namep
= make_room(&mem_start
, &mem_end
, 16, 1);
1758 strcpy(namep
, RELOC("linux,phandle"));
1759 mem_start
= (unsigned long)namep
+ strlen(namep
) + 1;
1761 /* Build string array */
1762 prom_printf("Building dt strings...\n");
1763 scan_dt_build_strings(root
, &mem_start
, &mem_end
);
1764 RELOC(dt_string_end
) = mem_start
;
1766 /* Build structure */
1767 mem_start
= PAGE_ALIGN(mem_start
);
1768 RELOC(dt_struct_start
) = mem_start
;
1769 prom_printf("Building dt structure...\n");
1770 scan_dt_build_struct(root
, &mem_start
, &mem_end
);
1771 dt_push_token(OF_DT_END
, &mem_start
, &mem_end
);
1772 RELOC(dt_struct_end
) = PAGE_ALIGN(mem_start
);
1775 hdr
->boot_cpuid_phys
= _prom
->cpu
;
1776 hdr
->magic
= OF_DT_HEADER
;
1777 hdr
->totalsize
= RELOC(dt_struct_end
) - RELOC(dt_header_start
);
1778 hdr
->off_dt_struct
= RELOC(dt_struct_start
) - RELOC(dt_header_start
);
1779 hdr
->off_dt_strings
= RELOC(dt_string_start
) - RELOC(dt_header_start
);
1780 hdr
->dt_strings_size
= RELOC(dt_string_end
) - RELOC(dt_string_start
);
1781 hdr
->off_mem_rsvmap
= ((unsigned long)rsvmap
) - RELOC(dt_header_start
);
1782 hdr
->version
= OF_DT_VERSION
;
1783 /* Version 16 is not backward compatible */
1784 hdr
->last_comp_version
= 0x10;
1786 /* Reserve the whole thing and copy the reserve map in, we
1787 * also bump mem_reserve_cnt to cause further reservations to
1788 * fail since it's too late.
1790 reserve_mem(RELOC(dt_header_start
), hdr
->totalsize
);
1791 memcpy(rsvmap
, RELOC(mem_reserve_map
), sizeof(mem_reserve_map
));
1796 prom_printf("reserved memory map:\n");
1797 for (i
= 0; i
< RELOC(mem_reserve_cnt
); i
++)
1798 prom_printf(" %x - %x\n",
1799 RELOC(mem_reserve_map
)[i
].base
,
1800 RELOC(mem_reserve_map
)[i
].size
);
1803 RELOC(mem_reserve_cnt
) = MEM_RESERVE_MAP_SIZE
;
1805 prom_printf("Device tree strings 0x%x -> 0x%x\n",
1806 RELOC(dt_string_start
), RELOC(dt_string_end
));
1807 prom_printf("Device tree struct 0x%x -> 0x%x\n",
1808 RELOC(dt_struct_start
), RELOC(dt_struct_end
));
1813 static void __init
fixup_device_tree(void)
1815 #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
1816 phandle u3
, i2c
, mpic
;
1821 /* Some G5s have a missing interrupt definition, fix it up here */
1822 u3
= call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
1823 if (!PHANDLE_VALID(u3
))
1825 i2c
= call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
1826 if (!PHANDLE_VALID(i2c
))
1828 mpic
= call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
1829 if (!PHANDLE_VALID(mpic
))
1832 /* check if proper rev of u3 */
1833 if (prom_getprop(u3
, "device-rev", &u3_rev
, sizeof(u3_rev
))
1836 if (u3_rev
!= 0x35 && u3_rev
!= 0x37)
1838 /* does it need fixup ? */
1839 if (prom_getproplen(i2c
, "interrupts") > 0)
1842 prom_printf("fixing up bogus interrupts for u3 i2c...\n");
1844 /* interrupt on this revision of u3 is number 0 and level */
1847 prom_setprop(i2c
, "interrupts", &interrupts
, sizeof(interrupts
));
1849 prom_setprop(i2c
, "interrupt-parent", &parent
, sizeof(parent
));
1854 static void __init
prom_find_boot_cpu(void)
1856 struct prom_t
*_prom
= &RELOC(prom
);
1861 if (prom_getprop(_prom
->chosen
, "cpu", &prom_cpu
, sizeof(prom_cpu
)) <= 0)
1862 prom_panic("cannot find boot cpu");
1864 cpu_pkg
= call_prom("instance-to-package", 1, 1, prom_cpu
);
1866 prom_getprop(cpu_pkg
, "reg", &getprop_rval
, sizeof(getprop_rval
));
1867 _prom
->cpu
= getprop_rval
;
1869 prom_debug("Booting CPU hw index = 0x%x\n", _prom
->cpu
);
1872 static void __init
prom_check_initrd(unsigned long r3
, unsigned long r4
)
1874 #ifdef CONFIG_BLK_DEV_INITRD
1875 struct prom_t
*_prom
= &RELOC(prom
);
1877 if (r3
&& r4
&& r4
!= 0xdeadbeef) {
1880 RELOC(prom_initrd_start
) = (r3
>= KERNELBASE
) ? __pa(r3
) : r3
;
1881 RELOC(prom_initrd_end
) = RELOC(prom_initrd_start
) + r4
;
1883 val
= RELOC(prom_initrd_start
);
1884 prom_setprop(_prom
->chosen
, "linux,initrd-start", &val
,
1886 val
= RELOC(prom_initrd_end
);
1887 prom_setprop(_prom
->chosen
, "linux,initrd-end", &val
,
1890 reserve_mem(RELOC(prom_initrd_start
),
1891 RELOC(prom_initrd_end
) - RELOC(prom_initrd_start
));
1893 prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start
));
1894 prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end
));
1896 #endif /* CONFIG_BLK_DEV_INITRD */
1900 * We enter here early on, when the Open Firmware prom is still
1901 * handling exceptions and the MMU hash table for us.
1904 unsigned long __init
prom_init(unsigned long r3
, unsigned long r4
,
1906 unsigned long r6
, unsigned long r7
)
1908 struct prom_t
*_prom
;
1909 extern char _stext
[];
1912 unsigned long offset
= reloc_offset();
1918 _prom
= &RELOC(prom
);
1921 * First zero the BSS
1923 memset(&RELOC(__bss_start
), 0, __bss_stop
- __bss_start
);
1926 * Init interface to Open Firmware, get some node references,
1929 prom_init_client_services(pp
);
1932 * Init prom stdout device
1937 * Check for an initrd
1939 prom_check_initrd(r3
, r4
);
1942 * Get default machine type. At this point, we do not differentiate
1943 * between pSeries SMP and pSeries LPAR
1945 RELOC(of_platform
) = prom_find_machine_type();
1946 getprop_rval
= RELOC(of_platform
);
1947 prom_setprop(_prom
->chosen
, "linux,platform",
1948 &getprop_rval
, sizeof(getprop_rval
));
1950 #ifdef CONFIG_PPC_PSERIES
1952 * On pSeries, inform the firmware about our capabilities
1954 if (RELOC(of_platform
) & PLATFORM_PSERIES
)
1955 prom_send_capabilities();
1959 * On pSeries and BPA, copy the CPU hold code
1961 if (RELOC(of_platform
) != PLATFORM_POWERMAC
)
1962 copy_and_flush(0, KERNELBASE
+ offset
, 0x100, 0);
1965 * Do early parsing of command line
1967 early_cmdline_parse();
1970 * Initialize memory management within prom_init
1975 * Determine which cpu is actually running right _now_
1977 prom_find_boot_cpu();
1980 * Initialize display devices
1982 prom_check_displays();
1986 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
1987 * that uses the allocator, we need to make sure we get the top of memory
1988 * available for us here...
1990 if (RELOC(of_platform
) == PLATFORM_PSERIES
)
1991 prom_initialize_tce_table();
1995 * On non-powermacs, try to instantiate RTAS and puts all CPUs
1996 * in spin-loops. PowerMacs don't have a working RTAS and use
1997 * a different way to spin CPUs
1999 if (RELOC(of_platform
) != PLATFORM_POWERMAC
) {
2000 prom_instantiate_rtas();
2005 * Fill in some infos for use by the kernel later on
2007 if (RELOC(prom_memory_limit
))
2008 prom_setprop(_prom
->chosen
, "linux,memory-limit",
2009 &RELOC(prom_memory_limit
),
2010 sizeof(prom_memory_limit
));
2012 if (RELOC(ppc64_iommu_off
))
2013 prom_setprop(_prom
->chosen
, "linux,iommu-off", NULL
, 0);
2015 if (RELOC(iommu_force_on
))
2016 prom_setprop(_prom
->chosen
, "linux,iommu-force-on", NULL
, 0);
2018 if (RELOC(prom_tce_alloc_start
)) {
2019 prom_setprop(_prom
->chosen
, "linux,tce-alloc-start",
2020 &RELOC(prom_tce_alloc_start
),
2021 sizeof(prom_tce_alloc_start
));
2022 prom_setprop(_prom
->chosen
, "linux,tce-alloc-end",
2023 &RELOC(prom_tce_alloc_end
),
2024 sizeof(prom_tce_alloc_end
));
2029 * Fixup any known bugs in the device-tree
2031 fixup_device_tree();
2034 * Now finally create the flattened device-tree
2036 prom_printf("copying OF device tree ...\n");
2037 flatten_device_tree();
2039 /* in case stdin is USB and still active on IBM machines... */
2043 * Call OF "quiesce" method to shut down pending DMA's from
2046 prom_printf("Calling quiesce ...\n");
2047 call_prom("quiesce", 0, 0);
2050 * And finally, call the kernel passing it the flattened device
2051 * tree and NULL as r5, thus triggering the new entry point which
2052 * is common to us and kexec
2054 hdr
= RELOC(dt_header_start
);
2055 prom_printf("returning from prom_init\n");
2056 prom_debug("->dt_header_start=0x%x\n", hdr
);
2059 reloc_got2(-offset
);