2 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
3 * Copyright (C) 1995, 1996 TooLs GmbH.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by TooLs GmbH.
17 * 4. The name of TooLs GmbH may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Copyright (C) 2001 Benno Rice
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
44 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
45 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
49 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
50 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
51 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
52 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
53 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 * $NetBSD: machdep.c,v 1.74.2.1 2000/11/01 16:13:48 tv Exp $
57 #include <sys/cdefs.h>
58 __FBSDID("$FreeBSD$");
61 #include "opt_kstack_pages.h"
62 #include "opt_platform.h"
64 #include <sys/param.h>
66 #include <sys/systm.h>
72 #include <sys/eventhandler.h>
74 #include <sys/imgact.h>
76 #include <sys/kernel.h>
78 #include <sys/linker.h>
80 #include <sys/malloc.h>
82 #include <sys/msgbuf.h>
83 #include <sys/mutex.h>
84 #include <sys/ptrace.h>
85 #include <sys/reboot.h>
86 #include <sys/rwlock.h>
87 #include <sys/signalvar.h>
88 #include <sys/syscallsubr.h>
89 #include <sys/sysctl.h>
90 #include <sys/sysent.h>
91 #include <sys/sysproto.h>
92 #include <sys/ucontext.h>
94 #include <sys/vmmeter.h>
95 #include <sys/vnode.h>
97 #include <net/netisr.h>
100 #include <vm/vm_extern.h>
101 #include <vm/vm_kern.h>
102 #include <vm/vm_page.h>
103 #include <vm/vm_map.h>
104 #include <vm/vm_object.h>
105 #include <vm/vm_pager.h>
107 #include <machine/altivec.h>
108 #ifndef __powerpc64__
109 #include <machine/bat.h>
111 #include <machine/cpu.h>
112 #include <machine/elf.h>
113 #include <machine/fpu.h>
114 #include <machine/hid.h>
115 #include <machine/kdb.h>
116 #include <machine/md_var.h>
117 #include <machine/metadata.h>
118 #include <machine/mmuvar.h>
119 #include <machine/pcb.h>
120 #include <machine/reg.h>
121 #include <machine/sigframe.h>
122 #include <machine/spr.h>
123 #include <machine/trap.h>
124 #include <machine/vmparam.h>
125 #include <machine/ofw_machdep.h>
129 #include <dev/ofw/openfirm.h>
130 #include <dev/ofw/ofw_subr.h>
134 int cacheline_size = 128;
136 int cacheline_size = 32;
138 int hw_direct_map = 1;
141 extern vm_paddr_t kernload;
144 extern void *ap_pcpu;
146 struct pcpu __pcpu[MAXCPU];
147 static char init_kenv[2048];
149 static struct trapframe frame0;
151 char machine[] = "powerpc";
152 SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
154 static void cpu_startup(void *);
155 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
157 SYSCTL_INT(_machdep, CPU_CACHELINE, cacheline_size,
158 CTLFLAG_RD, &cacheline_size, 0, "");
160 uintptr_t powerpc_init(vm_offset_t, vm_offset_t, vm_offset_t, void *,
166 /* Default MSR values set in the AIM/Book-E early startup code */
167 register_t psl_kernset;
168 register_t psl_userset;
169 register_t psl_userstatic;
171 register_t psl_userset32;
174 struct kva_md_info kmi;
177 cpu_startup(void *dummy)
181 * Initialise the decrementer-based clock.
186 * Good {morning,afternoon,evening,night}.
188 cpu_setup(PCPU_GET(cpuid));
193 printf("real memory = %ju (%ju MB)\n", ptoa((uintmax_t)physmem),
194 ptoa((uintmax_t)physmem) / 1048576);
198 printf("available KVA = %zu (%zu MB)\n",
199 virtual_end - virtual_avail,
200 (virtual_end - virtual_avail) / 1048576);
203 * Display any holes after the first chunk of extended memory.
208 printf("Physical memory chunk(s):\n");
209 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
211 phys_avail[indx + 1] - phys_avail[indx];
214 printf("0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
216 printf("0x%09jx - 0x%09jx, %ju bytes (%ju pages)\n",
218 (uintmax_t)phys_avail[indx],
219 (uintmax_t)phys_avail[indx + 1] - 1,
220 (uintmax_t)size1, (uintmax_t)size1 / PAGE_SIZE);
224 vm_ksubmap_init(&kmi);
226 printf("avail memory = %ju (%ju MB)\n",
227 ptoa((uintmax_t)vm_free_count()),
228 ptoa((uintmax_t)vm_free_count()) / 1048576);
231 * Set up buffers, so they can be used to read disk labels.
234 vm_pager_bufferinit();
237 extern vm_offset_t __startkernel, __endkernel;
238 extern unsigned char __bss_start[];
239 extern unsigned char __sbss_start[];
240 extern unsigned char __sbss_end[];
241 extern unsigned char _end[];
243 void aim_early_init(vm_offset_t fdt, vm_offset_t toc, vm_offset_t ofentry,
244 void *mdp, uint32_t mdp_cookie);
245 void aim_cpu_init(vm_offset_t toc);
246 void booke_cpu_init(void);
249 powerpc_init(vm_offset_t fdt, vm_offset_t toc, vm_offset_t ofentry, void *mdp,
254 vm_offset_t startkernel, endkernel;
256 bool ofw_bootargs = false;
258 vm_offset_t ksym_start;
259 vm_offset_t ksym_end;
262 /* First guess at start/end kernel positions */
263 startkernel = __startkernel;
264 endkernel = __endkernel;
267 * If the metadata pointer cookie is not set to the magic value,
268 * the number in mdp should be treated as nonsense.
270 if (mdp_cookie != 0xfb5d104d)
275 * On BOOKE the BSS is already cleared and some variables
276 * initialized. Do not wipe them out.
278 bzero(__sbss_start, __sbss_end - __sbss_start);
279 bzero(__bss_start, _end - __bss_start);
285 aim_early_init(fdt, toc, ofentry, mdp, mdp_cookie);
289 * Parse metadata if present and fetch parameters. Must be done
290 * before console is inited so cninit gets the right value of
296 uintptr_t md_offset = 0;
297 vm_paddr_t kernelendphys;
300 if ((uintptr_t)&powerpc_init > DMAP_BASE_ADDRESS)
301 md_offset = DMAP_BASE_ADDRESS;
303 md_offset = VM_MIN_KERNEL_ADDRESS - kernload;
306 preload_metadata = mdp;
308 preload_metadata += md_offset;
309 preload_bootstrap_relocate(md_offset);
311 kmdp = preload_search_by_type("elf kernel");
313 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
314 envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
317 init_static_kenv(envp, 0);
319 fdt = MD_FETCH(kmdp, MODINFOMD_DTBP, uintptr_t);
323 kernelendphys = MD_FETCH(kmdp, MODINFOMD_KERNEND,
325 if (kernelendphys != 0)
326 kernelendphys += md_offset;
327 endkernel = ulmax(endkernel, kernelendphys);
329 ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
330 ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
331 db_fetch_ksymtab(ksym_start, ksym_end);
335 init_static_kenv(init_kenv, sizeof(init_kenv));
338 /* Store boot environment state */
339 OF_initial_setup((void *)fdt, NULL, (int (*)(void *))ofentry);
342 * Init params/tunables that can be overridden by the loader
347 * Start initializing proc0 and thread0.
349 proc_linkup0(&proc0, &thread0);
350 thread0.td_frame = &frame0;
352 __asm __volatile("mr 13,%0" :: "r"(&thread0));
354 __asm __volatile("mr 2,%0" :: "r"(&thread0));
358 * Init mutexes, which we use heavily in PMAP
363 * Install the OF client interface
368 ofw_parse_bootargs();
371 * Initialize the console before printing anything.
380 /* Make sure the kernel icache is valid before we go too much further */
381 __syncicache((caddr_t)startkernel, endkernel - startkernel);
385 * Choose a platform module so we can get the physical memory map.
388 platform_probe_and_attach();
391 * Set up per-cpu data for the BSP now that the platform can tell
394 if (platform_smp_get_bsp(&bsp) != 0)
396 pc = &__pcpu[bsp.cr_cpuid];
397 __asm __volatile("mtsprg 0, %0" :: "r"(pc));
398 pcpu_init(pc, bsp.cr_cpuid, sizeof(struct pcpu));
399 pc->pc_curthread = &thread0;
400 thread0.td_oncpu = bsp.cr_cpuid;
401 pc->pc_cpuid = bsp.cr_cpuid;
402 pc->pc_hwref = bsp.cr_hwref;
412 pmap_bootstrap(startkernel, endkernel);
413 mtmsr(psl_kernset & ~PSL_EE);
416 * Initialize params/tunables that are derived from memsize
418 init_param2(physmem);
421 * Grab booted kernel's name
423 env = kern_getenv("kernelname");
425 strlcpy(kernelname, env, sizeof(kernelname));
430 * Finish setting up thread0.
432 thread0.td_pcb = (struct pcb *)
433 ((thread0.td_kstack + thread0.td_kstack_pages * PAGE_SIZE -
434 sizeof(struct pcb)) & ~15UL);
435 bzero((void *)thread0.td_pcb, sizeof(struct pcb));
436 pc->pc_curpcb = thread0.td_pcb;
438 /* Initialise the message buffer. */
439 msgbufinit(msgbufp, msgbufsize);
442 if (boothowto & RB_KDB)
443 kdb_enter(KDB_WHY_BOOTFLAGS,
444 "Boot flags requested debugger");
447 return (((uintptr_t)thread0.td_pcb -
448 (sizeof(struct callframe) - 3*sizeof(register_t))) & ~15UL);
452 * Flush the D-cache for non-DMA I/O so that the I-cache can
453 * be made coherent later.
456 cpu_flush_dcache(void *ptr, size_t len)
458 register_t addr, off;
461 * Align the address to a cacheline and adjust the length
462 * accordingly. Then round the length to a multiple of the
463 * cacheline for easy looping.
465 addr = (uintptr_t)ptr;
466 off = addr & (cacheline_size - 1);
468 len = roundup2(len + off, cacheline_size);
471 __asm __volatile ("dcbf 0,%0" :: "r"(addr));
472 __asm __volatile ("sync");
473 addr += cacheline_size;
474 len -= cacheline_size;
479 ptrace_set_pc(struct thread *td, unsigned long addr)
481 struct trapframe *tf;
484 tf->srr0 = (register_t)addr;
496 if (td->td_md.md_spinlock_count == 0) {
497 __asm __volatile("or 2,2,2"); /* Set high thread priority */
498 msr = intr_disable();
499 td->td_md.md_spinlock_count = 1;
500 td->td_md.md_saved_msr = msr;
502 td->td_md.md_spinlock_count++;
514 msr = td->td_md.md_saved_msr;
515 td->td_md.md_spinlock_count--;
516 if (td->td_md.md_spinlock_count == 0) {
518 __asm __volatile("or 6,6,6"); /* Set normal thread priority */
523 * Simple ddb(4) command/hack to view any SPR on the running CPU.
524 * Uses a trivial asm function to perform the mfspr, and rewrites the mfspr
525 * instruction each time.
526 * XXX: Since it uses code modification, it won't work if the kernel code pages
529 extern register_t get_spr(int);
532 DB_SHOW_COMMAND(spr, db_show_spr)
535 volatile uint32_t *p;
536 int sprno, saved_sprno;
541 saved_sprno = sprno = (intptr_t) addr;
542 sprno = ((sprno & 0x3e0) >> 5) | ((sprno & 0x1f) << 5);
543 p = (uint32_t *)(void *)&get_spr;
545 #if defined(_CALL_ELF) && _CALL_ELF == 2
546 /* Account for ELFv2 function prologue. */
549 p = *(volatile uint32_t * volatile *)p;
552 *p = (*p & ~0x001ff800) | (sprno << 11);
553 __syncicache(__DEVOLATILE(uint32_t *, p), cacheline_size);
554 spr = get_spr(sprno);
556 db_printf("SPR %d(%x): %lx\n", saved_sprno, saved_sprno,
563 bzero(void *buf, size_t len)
569 while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
574 while (len >= sizeof(u_long) * 8) {
576 *((u_long*) p + 1) = 0;
577 *((u_long*) p + 2) = 0;
578 *((u_long*) p + 3) = 0;
579 len -= sizeof(u_long) * 8;
580 *((u_long*) p + 4) = 0;
581 *((u_long*) p + 5) = 0;
582 *((u_long*) p + 6) = 0;
583 *((u_long*) p + 7) = 0;
584 p += sizeof(u_long) * 8;
587 while (len >= sizeof(u_long)) {
589 len -= sizeof(u_long);