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$");
60 #include "opt_compat.h"
62 #include "opt_kstack_pages.h"
63 #include "opt_platform.h"
65 #include <sys/param.h>
67 #include <sys/systm.h>
73 #include <sys/eventhandler.h>
75 #include <sys/imgact.h>
77 #include <sys/kernel.h>
79 #include <sys/linker.h>
81 #include <sys/malloc.h>
83 #include <sys/msgbuf.h>
84 #include <sys/mutex.h>
85 #include <sys/ptrace.h>
86 #include <sys/reboot.h>
87 #include <sys/rwlock.h>
88 #include <sys/signalvar.h>
89 #include <sys/syscallsubr.h>
90 #include <sys/sysctl.h>
91 #include <sys/sysent.h>
92 #include <sys/sysproto.h>
93 #include <sys/ucontext.h>
95 #include <sys/vmmeter.h>
96 #include <sys/vnode.h>
98 #include <net/netisr.h>
101 #include <vm/vm_extern.h>
102 #include <vm/vm_kern.h>
103 #include <vm/vm_page.h>
104 #include <vm/vm_map.h>
105 #include <vm/vm_object.h>
106 #include <vm/vm_pager.h>
108 #include <machine/altivec.h>
109 #ifndef __powerpc64__
110 #include <machine/bat.h>
112 #include <machine/cpu.h>
113 #include <machine/elf.h>
114 #include <machine/fpu.h>
115 #include <machine/hid.h>
116 #include <machine/kdb.h>
117 #include <machine/md_var.h>
118 #include <machine/metadata.h>
119 #include <machine/mmuvar.h>
120 #include <machine/pcb.h>
121 #include <machine/reg.h>
122 #include <machine/sigframe.h>
123 #include <machine/spr.h>
124 #include <machine/trap.h>
125 #include <machine/vmparam.h>
126 #include <machine/ofw_machdep.h>
130 #include <dev/ofw/openfirm.h>
134 int cacheline_size = 128;
136 int cacheline_size = 32;
138 int hw_direct_map = 1;
140 extern void *ap_pcpu;
142 struct pcpu __pcpu[MAXCPU];
144 static struct trapframe frame0;
146 char machine[] = "powerpc";
147 SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
149 static void cpu_startup(void *);
150 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
152 SYSCTL_INT(_machdep, CPU_CACHELINE, cacheline_size,
153 CTLFLAG_RD, &cacheline_size, 0, "");
155 uintptr_t powerpc_init(vm_offset_t, vm_offset_t, vm_offset_t, void *);
160 struct kva_md_info kmi;
163 cpu_startup(void *dummy)
167 * Initialise the decrementer-based clock.
172 * Good {morning,afternoon,evening,night}.
174 cpu_setup(PCPU_GET(cpuid));
179 printf("real memory = %ju (%ju MB)\n", ptoa((uintmax_t)physmem),
180 ptoa((uintmax_t)physmem) / 1048576);
184 printf("available KVA = %zu (%zu MB)\n",
185 virtual_end - virtual_avail,
186 (virtual_end - virtual_avail) / 1048576);
189 * Display any holes after the first chunk of extended memory.
194 printf("Physical memory chunk(s):\n");
195 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
197 phys_avail[indx + 1] - phys_avail[indx];
200 printf("0x%016jx - 0x%016jx, %jd bytes (%jd pages)\n",
202 printf("0x%09jx - 0x%09jx, %ju bytes (%ju pages)\n",
204 (uintmax_t)phys_avail[indx],
205 (uintmax_t)phys_avail[indx + 1] - 1,
206 (uintmax_t)size1, (uintmax_t)size1 / PAGE_SIZE);
210 vm_ksubmap_init(&kmi);
212 printf("avail memory = %ju (%ju MB)\n",
213 ptoa((uintmax_t)vm_cnt.v_free_count),
214 ptoa((uintmax_t)vm_cnt.v_free_count) / 1048576);
217 * Set up buffers, so they can be used to read disk labels.
220 vm_pager_bufferinit();
223 extern vm_offset_t __startkernel, __endkernel;
224 extern unsigned char __bss_start[];
225 extern unsigned char __sbss_start[];
226 extern unsigned char __sbss_end[];
227 extern unsigned char _end[];
229 void aim_cpu_init(vm_offset_t toc);
230 void booke_cpu_init(void);
233 powerpc_init(vm_offset_t fdt, vm_offset_t toc, vm_offset_t ofentry, void *mdp)
236 vm_offset_t startkernel, endkernel;
240 vm_offset_t ksym_start;
241 vm_offset_t ksym_end;
246 /* First guess at start/end kernel positions */
247 startkernel = __startkernel;
248 endkernel = __endkernel;
250 /* Check for ePAPR loader, which puts a magic value into r6 */
251 if (mdp == (void *)0x65504150)
256 * If running from an FDT, make sure we are in real mode to avoid
257 * tromping on firmware page tables. Everything in the kernel assumes
258 * 1:1 mappings out of firmware, so this won't break anything not
259 * already broken. This doesn't work if there is live OF, since OF
260 * may internally use non-1:1 mappings.
263 mtmsr(mfmsr() & ~(PSL_IR | PSL_DR));
267 * Parse metadata if present and fetch parameters. Must be done
268 * before console is inited so cninit gets the right value of
272 preload_metadata = mdp;
273 kmdp = preload_search_by_type("elf kernel");
275 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
276 init_static_kenv(MD_FETCH(kmdp, MODINFOMD_ENVP, char *),
278 endkernel = ulmax(endkernel, MD_FETCH(kmdp,
279 MODINFOMD_KERNEND, vm_offset_t));
281 ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
282 ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
283 db_fetch_ksymtab(ksym_start, ksym_end);
287 bzero(__sbss_start, __sbss_end - __sbss_start);
288 bzero(__bss_start, _end - __bss_start);
289 init_static_kenv(NULL, 0);
295 /* Store boot environment state */
296 OF_initial_setup((void *)fdt, NULL, (int (*)(void *))ofentry);
299 * Init params/tunables that can be overridden by the loader
304 * Start initializing proc0 and thread0.
306 proc_linkup0(&proc0, &thread0);
307 thread0.td_frame = &frame0;
310 * Set up per-cpu data.
313 pcpu_init(pc, 0, sizeof(struct pcpu));
314 pc->pc_curthread = &thread0;
316 __asm __volatile("mr 13,%0" :: "r"(pc->pc_curthread));
318 __asm __volatile("mr 2,%0" :: "r"(pc->pc_curthread));
322 __asm __volatile("mtsprg 0, %0" :: "r"(pc));
325 * Init mutexes, which we use heavily in PMAP
331 * Install the OF client interface
337 * Initialize the console before printing anything.
342 * Complain if there is no metadata.
344 if (mdp == NULL || kmdp == NULL) {
345 printf("powerpc_init: no loader metadata.\n");
359 /* Make sure the kernel icache is valid before we go too much further */
360 __syncicache((caddr_t)startkernel, endkernel - startkernel);
364 * Choose a platform module so we can get the physical memory map.
367 platform_probe_and_attach();
372 pmap_bootstrap(startkernel, endkernel);
373 mtmsr(PSL_KERNSET & ~PSL_EE);
376 * Initialize params/tunables that are derived from memsize
378 init_param2(physmem);
381 * Grab booted kernel's name
383 env = kern_getenv("kernelname");
385 strlcpy(kernelname, env, sizeof(kernelname));
390 * Finish setting up thread0.
392 thread0.td_pcb = (struct pcb *)
393 ((thread0.td_kstack + thread0.td_kstack_pages * PAGE_SIZE -
394 sizeof(struct pcb)) & ~15UL);
395 bzero((void *)thread0.td_pcb, sizeof(struct pcb));
396 pc->pc_curpcb = thread0.td_pcb;
398 /* Initialise the message buffer. */
399 msgbufinit(msgbufp, msgbufsize);
402 if (boothowto & RB_KDB)
403 kdb_enter(KDB_WHY_BOOTFLAGS,
404 "Boot flags requested debugger");
407 return (((uintptr_t)thread0.td_pcb -
408 (sizeof(struct callframe) - 3*sizeof(register_t))) & ~15UL);
412 bzero(void *buf, size_t len)
418 while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
423 while (len >= sizeof(u_long) * 8) {
425 *((u_long*) p + 1) = 0;
426 *((u_long*) p + 2) = 0;
427 *((u_long*) p + 3) = 0;
428 len -= sizeof(u_long) * 8;
429 *((u_long*) p + 4) = 0;
430 *((u_long*) p + 5) = 0;
431 *((u_long*) p + 6) = 0;
432 *((u_long*) p + 7) = 0;
433 p += sizeof(u_long) * 8;
436 while (len >= sizeof(u_long)) {
438 len -= sizeof(u_long);
449 * Flush the D-cache for non-DMA I/O so that the I-cache can
450 * be made coherent later.
453 cpu_flush_dcache(void *ptr, size_t len)
455 register_t addr, off;
458 * Align the address to a cacheline and adjust the length
459 * accordingly. Then round the length to a multiple of the
460 * cacheline for easy looping.
462 addr = (uintptr_t)ptr;
463 off = addr & (cacheline_size - 1);
465 len = roundup2(len + off, cacheline_size);
468 __asm __volatile ("dcbf 0,%0" :: "r"(addr));
469 __asm __volatile ("sync");
470 addr += cacheline_size;
471 len -= cacheline_size;
476 ptrace_set_pc(struct thread *td, unsigned long addr)
478 struct trapframe *tf;
481 tf->srr0 = (register_t)addr;
493 if (td->td_md.md_spinlock_count == 0) {
494 __asm __volatile("or 2,2,2"); /* Set high thread priority */
495 msr = intr_disable();
496 td->td_md.md_spinlock_count = 1;
497 td->td_md.md_saved_msr = msr;
499 td->td_md.md_spinlock_count++;
511 msr = td->td_md.md_saved_msr;
512 td->td_md.md_spinlock_count--;
513 if (td->td_md.md_spinlock_count == 0) {
515 __asm __volatile("or 6,6,6"); /* Set normal thread priority */