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>
129 #include <dev/ofw/openfirm.h>
132 extern vm_offset_t ksym_start, ksym_end;
138 int cacheline_size = 128;
140 int cacheline_size = 32;
142 int hw_direct_map = 1;
144 struct pcpu __pcpu[MAXCPU];
146 static struct trapframe frame0;
148 char machine[] = "powerpc";
149 SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
151 static void cpu_startup(void *);
152 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
154 SYSCTL_INT(_machdep, CPU_CACHELINE, cacheline_size,
155 CTLFLAG_RD, &cacheline_size, 0, "");
157 uintptr_t powerpc_init(vm_offset_t, vm_offset_t, vm_offset_t, void *);
159 int setfault(faultbuf); /* defined in locore.S */
164 #ifndef __powerpc64__
165 struct bat battable[16];
168 struct kva_md_info kmi;
171 cpu_startup(void *dummy)
175 * Initialise the decrementer-based clock.
180 * Good {morning,afternoon,evening,night}.
182 cpu_setup(PCPU_GET(cpuid));
187 printf("real memory = %ld (%ld MB)\n", ptoa(physmem),
188 ptoa(physmem) / 1048576);
192 printf("available KVA = %zd (%zd MB)\n",
193 virtual_end - virtual_avail,
194 (virtual_end - virtual_avail) / 1048576);
197 * Display any holes after the first chunk of extended memory.
202 printf("Physical memory chunk(s):\n");
203 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
205 phys_avail[indx + 1] - phys_avail[indx];
208 printf("0x%016lx - 0x%016lx, %ld bytes (%ld pages)\n",
210 printf("0x%08x - 0x%08x, %d bytes (%ld pages)\n",
212 phys_avail[indx], phys_avail[indx + 1] - 1, size1,
217 vm_ksubmap_init(&kmi);
219 printf("avail memory = %ld (%ld MB)\n", ptoa(cnt.v_free_count),
220 ptoa(cnt.v_free_count) / 1048576);
223 * Set up buffers, so they can be used to read disk labels.
226 vm_pager_bufferinit();
229 extern char kernel_text[], _end[];
231 #ifndef __powerpc64__
232 /* Bits for running on 64-bit systems in 32-bit mode. */
233 extern void *testppc64, *testppc64size;
234 extern void *restorebridge, *restorebridgesize;
235 extern void *rfid_patch, *rfi_patch1, *rfi_patch2;
236 extern void *trapcode64;
240 extern void *rstcode, *rstsize;
242 extern void *trapcode, *trapsize;
243 extern void *slbtrap, *slbtrapsize;
244 extern void *alitrap, *alisize;
245 extern void *dsitrap, *dsisize;
246 extern void *decrint, *decrsize;
247 extern void *extint, *extsize;
248 extern void *dblow, *dbsize;
249 extern void *imisstrap, *imisssize;
250 extern void *dlmisstrap, *dlmisssize;
251 extern void *dsmisstrap, *dsmisssize;
254 powerpc_init(vm_offset_t startkernel, vm_offset_t endkernel,
255 vm_offset_t basekernel, void *mdp)
262 register_t msr, scratch;
266 uint8_t *cache_check;
268 #ifndef __powerpc64__
278 * The Wii loader doesn't pass us any environment so, mdp
279 * points to garbage at this point. The Wii CPU is a 750CL.
282 if ((vers & 0xfffff0e0) == (MPC750 << 16 | MPC750CL))
287 * Parse metadata if present and fetch parameters. Must be done
288 * before console is inited so cninit gets the right value of
292 preload_metadata = mdp;
293 kmdp = preload_search_by_type("elf kernel");
295 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
296 kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
297 endkernel = ulmax(endkernel, MD_FETCH(kmdp,
298 MODINFOMD_KERNEND, vm_offset_t));
300 ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
301 ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
307 * Init params/tunables that can be overridden by the loader
312 * Start initializing proc0 and thread0.
314 proc_linkup0(&proc0, &thread0);
315 thread0.td_frame = &frame0;
318 * Set up per-cpu data.
321 pcpu_init(pc, 0, sizeof(struct pcpu));
322 pc->pc_curthread = &thread0;
324 __asm __volatile("mr 13,%0" :: "r"(pc->pc_curthread));
326 __asm __volatile("mr 2,%0" :: "r"(pc->pc_curthread));
330 __asm __volatile("mtsprg 0, %0" :: "r"(pc));
333 * Init mutexes, which we use heavily in PMAP
339 * Install the OF client interface
345 * Initialize the console before printing anything.
350 * Complain if there is no metadata.
352 if (mdp == NULL || kmdp == NULL) {
353 printf("powerpc_init: no loader metadata.\n");
362 /* Various very early CPU fix ups */
363 switch (mfpvr() >> 16) {
365 * PowerPC 970 CPUs have a misfeature requested by Apple that
366 * makes them pretend they have a 32-byte cacheline. Turn this
367 * off before we measure the cacheline size.
373 scratch = mfspr(SPR_HID5);
374 scratch &= ~HID5_970_DCBZ_SIZE_HI;
375 mtspr(SPR_HID5, scratch);
379 /* XXX: get from ibm,slb-size in device tree */
386 * Initialize the interrupt tables and figure out our cache line
387 * size and whether or not we need the 64-bit bridge code.
391 * Disable translation in case the vector area hasn't been
392 * mapped (G5). Note that no OFW calls can be made until
393 * translation is re-enabled.
397 mtmsr((msr & ~(PSL_IR | PSL_DR)) | PSL_RI);
400 * Measure the cacheline size using dcbz
402 * Use EXC_PGM as a playground. We are about to overwrite it
403 * anyway, we know it exists, and we know it is cache-aligned.
406 cache_check = (void *)EXC_PGM;
408 for (cacheline_size = 0; cacheline_size < 0x100; cacheline_size++)
409 cache_check[cacheline_size] = 0xff;
411 __asm __volatile("dcbz 0,%0":: "r" (cache_check) : "memory");
413 /* Find the first byte dcbz did not zero to get the cache line size */
414 for (cacheline_size = 0; cacheline_size < 0x100 &&
415 cache_check[cacheline_size] == 0; cacheline_size++);
417 /* Work around psim bug */
418 if (cacheline_size == 0) {
423 /* Make sure the kernel icache is valid before we go too much further */
424 __syncicache((caddr_t)startkernel, endkernel - startkernel);
426 #ifndef __powerpc64__
428 * Figure out whether we need to use the 64 bit PMAP. This works by
429 * executing an instruction that is only legal on 64-bit PPC (mtmsrd),
430 * and setting ppc64 = 0 if that causes a trap.
435 bcopy(&testppc64, (void *)EXC_PGM, (size_t)&testppc64size);
436 __syncicache((void *)EXC_PGM, (size_t)&testppc64size);
444 : "=r"(scratch), "=r"(ppc64));
447 cpu_features |= PPC_FEATURE_64;
450 * Now copy restorebridge into all the handlers, if necessary,
451 * and set up the trap tables.
454 if (cpu_features & PPC_FEATURE_64) {
455 /* Patch the two instances of rfi -> rfid */
456 bcopy(&rfid_patch,&rfi_patch1,4);
458 /* rfi_patch2 is at the end of dbleave */
459 bcopy(&rfid_patch,&rfi_patch2,4);
463 * Copy a code snippet to restore 32-bit bridge mode
464 * to the top of every non-generic trap handler
467 trap_offset += (size_t)&restorebridgesize;
468 bcopy(&restorebridge, (void *)EXC_RST, trap_offset);
469 bcopy(&restorebridge, (void *)EXC_DSI, trap_offset);
470 bcopy(&restorebridge, (void *)EXC_ALI, trap_offset);
471 bcopy(&restorebridge, (void *)EXC_PGM, trap_offset);
472 bcopy(&restorebridge, (void *)EXC_MCHK, trap_offset);
473 bcopy(&restorebridge, (void *)EXC_TRC, trap_offset);
474 bcopy(&restorebridge, (void *)EXC_BPT, trap_offset);
477 * Set the common trap entry point to the one that
478 * knows to restore 32-bit operation on execution.
481 generictrap = &trapcode64;
483 generictrap = &trapcode;
486 #else /* powerpc64 */
487 cpu_features |= PPC_FEATURE_64;
488 generictrap = &trapcode;
492 bcopy(&rstcode, (void *)(EXC_RST + trap_offset), (size_t)&rstsize);
494 bcopy(generictrap, (void *)EXC_RST, (size_t)&trapsize);
498 bcopy(&dblow, (void *)(EXC_MCHK + trap_offset), (size_t)&dbsize);
499 bcopy(&dblow, (void *)(EXC_PGM + trap_offset), (size_t)&dbsize);
500 bcopy(&dblow, (void *)(EXC_TRC + trap_offset), (size_t)&dbsize);
501 bcopy(&dblow, (void *)(EXC_BPT + trap_offset), (size_t)&dbsize);
503 bcopy(generictrap, (void *)EXC_MCHK, (size_t)&trapsize);
504 bcopy(generictrap, (void *)EXC_PGM, (size_t)&trapsize);
505 bcopy(generictrap, (void *)EXC_TRC, (size_t)&trapsize);
506 bcopy(generictrap, (void *)EXC_BPT, (size_t)&trapsize);
508 bcopy(&alitrap, (void *)(EXC_ALI + trap_offset), (size_t)&alisize);
509 bcopy(&dsitrap, (void *)(EXC_DSI + trap_offset), (size_t)&dsisize);
510 bcopy(generictrap, (void *)EXC_ISI, (size_t)&trapsize);
512 bcopy(&slbtrap, (void *)EXC_DSE, (size_t)&slbtrapsize);
513 bcopy(&slbtrap, (void *)EXC_ISE, (size_t)&slbtrapsize);
515 bcopy(generictrap, (void *)EXC_EXI, (size_t)&trapsize);
516 bcopy(generictrap, (void *)EXC_FPU, (size_t)&trapsize);
517 bcopy(generictrap, (void *)EXC_DECR, (size_t)&trapsize);
518 bcopy(generictrap, (void *)EXC_SC, (size_t)&trapsize);
519 bcopy(generictrap, (void *)EXC_FPA, (size_t)&trapsize);
520 bcopy(generictrap, (void *)EXC_VEC, (size_t)&trapsize);
521 bcopy(generictrap, (void *)EXC_PERF, (size_t)&trapsize);
522 bcopy(generictrap, (void *)EXC_VECAST_G4, (size_t)&trapsize);
523 bcopy(generictrap, (void *)EXC_VECAST_G5, (size_t)&trapsize);
524 #ifndef __powerpc64__
525 /* G2-specific TLB miss helper handlers */
526 bcopy(&imisstrap, (void *)EXC_IMISS, (size_t)&imisssize);
527 bcopy(&dlmisstrap, (void *)EXC_DLMISS, (size_t)&dlmisssize);
528 bcopy(&dsmisstrap, (void *)EXC_DSMISS, (size_t)&dsmisssize);
530 __syncicache(EXC_RSVD, EXC_LAST - EXC_RSVD);
537 /* Warn if cachline size was not determined */
538 if (cacheline_warn == 1) {
539 printf("WARNING: cacheline size undetermined, setting to 32\n");
543 * Choose a platform module so we can get the physical memory map.
546 platform_probe_and_attach();
549 * Initialise virtual memory. Use BUS_PROBE_GENERIC priority
550 * in case the platform module had a better idea of what we
553 if (cpu_features & PPC_FEATURE_64)
554 pmap_mmu_install(MMU_TYPE_G5, BUS_PROBE_GENERIC);
556 pmap_mmu_install(MMU_TYPE_OEA, BUS_PROBE_GENERIC);
558 pmap_bootstrap(startkernel, endkernel);
559 mtmsr(PSL_KERNSET & ~PSL_EE);
562 * Initialize params/tunables that are derived from memsize
564 init_param2(physmem);
567 * Grab booted kernel's name
569 env = getenv("kernelname");
571 strlcpy(kernelname, env, sizeof(kernelname));
576 * Finish setting up thread0.
578 thread0.td_pcb = (struct pcb *)
579 ((thread0.td_kstack + thread0.td_kstack_pages * PAGE_SIZE -
580 sizeof(struct pcb)) & ~15UL);
581 bzero((void *)thread0.td_pcb, sizeof(struct pcb));
582 pc->pc_curpcb = thread0.td_pcb;
584 /* Initialise the message buffer. */
585 msgbufinit(msgbufp, msgbufsize);
588 if (boothowto & RB_KDB)
589 kdb_enter(KDB_WHY_BOOTFLAGS,
590 "Boot flags requested debugger");
593 return (((uintptr_t)thread0.td_pcb -
594 (sizeof(struct callframe) - 3*sizeof(register_t))) & ~15UL);
598 bzero(void *buf, size_t len)
604 while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
609 while (len >= sizeof(u_long) * 8) {
611 *((u_long*) p + 1) = 0;
612 *((u_long*) p + 2) = 0;
613 *((u_long*) p + 3) = 0;
614 len -= sizeof(u_long) * 8;
615 *((u_long*) p + 4) = 0;
616 *((u_long*) p + 5) = 0;
617 *((u_long*) p + 6) = 0;
618 *((u_long*) p + 7) = 0;
619 p += sizeof(u_long) * 8;
622 while (len >= sizeof(u_long)) {
624 len -= sizeof(u_long);
640 * Flush the D-cache for non-DMA I/O so that the I-cache can
641 * be made coherent later.
644 cpu_flush_dcache(void *ptr, size_t len)
654 cpu_initclocks_bsp();
658 * Shutdown the CPU as much as possible.
668 ptrace_set_pc(struct thread *td, unsigned long addr)
670 struct trapframe *tf;
673 tf->srr0 = (register_t)addr;
679 ptrace_single_step(struct thread *td)
681 struct trapframe *tf;
690 ptrace_clear_single_step(struct thread *td)
692 struct trapframe *tf;
701 kdb_cpu_clear_singlestep(void)
704 kdb_frame->srr1 &= ~PSL_SE;
708 kdb_cpu_set_singlestep(void)
711 kdb_frame->srr1 |= PSL_SE;
715 * Initialise a struct pcpu.
718 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t sz)
721 /* Copy the SLB contents from the current CPU */
722 memcpy(pcpu->pc_slb, PCPU_GET(slb), sizeof(pcpu->pc_slb));
733 if (td->td_md.md_spinlock_count == 0) {
734 msr = intr_disable();
735 td->td_md.md_spinlock_count = 1;
736 td->td_md.md_saved_msr = msr;
738 td->td_md.md_spinlock_count++;
750 msr = td->td_md.md_saved_msr;
751 td->td_md.md_spinlock_count--;
752 if (td->td_md.md_spinlock_count == 0)
756 int db_trap_glue(struct trapframe *); /* Called from trap_subr.S */
759 db_trap_glue(struct trapframe *frame)
761 if (!(frame->srr1 & PSL_PR)
762 && (frame->exc == EXC_TRC || frame->exc == EXC_RUNMODETRC
763 || (frame->exc == EXC_PGM
764 && (frame->srr1 & 0x20000))
765 || frame->exc == EXC_BPT
766 || frame->exc == EXC_DSI)) {
767 int type = frame->exc;
768 if (type == EXC_PGM && (frame->srr1 & 0x20000)) {
771 return (kdb_trap(type, 0, frame));
777 #ifndef __powerpc64__
780 va_to_vsid(pmap_t pm, vm_offset_t va)
782 return ((pm->pm_sr[(uintptr_t)va >> ADDR_SR_SHFT]) & SR_VSID_MASK);