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/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>
128 #include <dev/ofw/openfirm.h>
131 extern vm_offset_t ksym_start, ksym_end;
137 int cacheline_size = 128;
139 int cacheline_size = 32;
141 int hw_direct_map = 1;
143 struct pcpu __pcpu[MAXCPU];
145 static struct trapframe frame0;
147 char machine[] = "powerpc";
148 SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
150 static void cpu_startup(void *);
151 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
153 SYSCTL_INT(_machdep, CPU_CACHELINE, cacheline_size,
154 CTLFLAG_RD, &cacheline_size, 0, "");
156 uintptr_t powerpc_init(vm_offset_t, vm_offset_t, vm_offset_t, void *);
158 int setfault(faultbuf); /* defined in locore.S */
163 #ifndef __powerpc64__
164 struct bat battable[16];
167 struct kva_md_info kmi;
170 cpu_startup(void *dummy)
174 * Initialise the decrementer-based clock.
179 * Good {morning,afternoon,evening,night}.
181 cpu_setup(PCPU_GET(cpuid));
186 printf("real memory = %ld (%ld MB)\n", ptoa(physmem),
187 ptoa(physmem) / 1048576);
191 printf("available KVA = %zd (%zd MB)\n",
192 virtual_end - virtual_avail,
193 (virtual_end - virtual_avail) / 1048576);
196 * Display any holes after the first chunk of extended memory.
201 printf("Physical memory chunk(s):\n");
202 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
204 phys_avail[indx + 1] - phys_avail[indx];
207 printf("0x%016lx - 0x%016lx, %ld bytes (%ld pages)\n",
209 printf("0x%08x - 0x%08x, %d bytes (%ld pages)\n",
211 phys_avail[indx], phys_avail[indx + 1] - 1, size1,
216 vm_ksubmap_init(&kmi);
218 printf("avail memory = %ld (%ld MB)\n", ptoa(cnt.v_free_count),
219 ptoa(cnt.v_free_count) / 1048576);
222 * Set up buffers, so they can be used to read disk labels.
225 vm_pager_bufferinit();
228 extern char kernel_text[], _end[];
230 #ifndef __powerpc64__
231 /* Bits for running on 64-bit systems in 32-bit mode. */
232 extern void *testppc64, *testppc64size;
233 extern void *restorebridge, *restorebridgesize;
234 extern void *rfid_patch, *rfi_patch1, *rfi_patch2;
235 extern void *trapcode64;
239 extern void *rstcode, *rstsize;
241 extern void *trapcode, *trapsize;
242 extern void *slbtrap, *slbtrapsize;
243 extern void *alitrap, *alisize;
244 extern void *dsitrap, *dsisize;
245 extern void *decrint, *decrsize;
246 extern void *extint, *extsize;
247 extern void *dblow, *dbsize;
248 extern void *imisstrap, *imisssize;
249 extern void *dlmisstrap, *dlmisssize;
250 extern void *dsmisstrap, *dsmisssize;
253 powerpc_init(vm_offset_t startkernel, vm_offset_t endkernel,
254 vm_offset_t basekernel, void *mdp)
261 register_t msr, scratch;
265 uint8_t *cache_check;
267 #ifndef __powerpc64__
277 * The Wii loader doesn't pass us any environment so, mdp
278 * points to garbage at this point. The Wii CPU is a 750CL.
281 if ((vers & 0xfffff0e0) == (MPC750 << 16 | MPC750CL))
286 * Parse metadata if present and fetch parameters. Must be done
287 * before console is inited so cninit gets the right value of
291 preload_metadata = mdp;
292 kmdp = preload_search_by_type("elf kernel");
294 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
295 kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
296 endkernel = ulmax(endkernel, MD_FETCH(kmdp,
297 MODINFOMD_KERNEND, vm_offset_t));
299 ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
300 ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
306 * Init params/tunables that can be overridden by the loader
311 * Start initializing proc0 and thread0.
313 proc_linkup0(&proc0, &thread0);
314 thread0.td_frame = &frame0;
317 * Set up per-cpu data.
320 pcpu_init(pc, 0, sizeof(struct pcpu));
321 pc->pc_curthread = &thread0;
323 __asm __volatile("mr 13,%0" :: "r"(pc->pc_curthread));
325 __asm __volatile("mr 2,%0" :: "r"(pc->pc_curthread));
329 __asm __volatile("mtsprg 0, %0" :: "r"(pc));
332 * Init mutexes, which we use heavily in PMAP
338 * Install the OF client interface
344 * Initialize the console before printing anything.
349 * Complain if there is no metadata.
351 if (mdp == NULL || kmdp == NULL) {
352 printf("powerpc_init: no loader metadata.\n");
361 /* Various very early CPU fix ups */
362 switch (mfpvr() >> 16) {
364 * PowerPC 970 CPUs have a misfeature requested by Apple that
365 * makes them pretend they have a 32-byte cacheline. Turn this
366 * off before we measure the cacheline size.
372 scratch = mfspr(SPR_HID5);
373 scratch &= ~HID5_970_DCBZ_SIZE_HI;
374 mtspr(SPR_HID5, scratch);
378 /* XXX: get from ibm,slb-size in device tree */
385 * Initialize the interrupt tables and figure out our cache line
386 * size and whether or not we need the 64-bit bridge code.
390 * Disable translation in case the vector area hasn't been
391 * mapped (G5). Note that no OFW calls can be made until
392 * translation is re-enabled.
396 mtmsr((msr & ~(PSL_IR | PSL_DR)) | PSL_RI);
399 * Measure the cacheline size using dcbz
401 * Use EXC_PGM as a playground. We are about to overwrite it
402 * anyway, we know it exists, and we know it is cache-aligned.
405 cache_check = (void *)EXC_PGM;
407 for (cacheline_size = 0; cacheline_size < 0x100; cacheline_size++)
408 cache_check[cacheline_size] = 0xff;
410 __asm __volatile("dcbz 0,%0":: "r" (cache_check) : "memory");
412 /* Find the first byte dcbz did not zero to get the cache line size */
413 for (cacheline_size = 0; cacheline_size < 0x100 &&
414 cache_check[cacheline_size] == 0; cacheline_size++);
416 /* Work around psim bug */
417 if (cacheline_size == 0) {
422 /* Make sure the kernel icache is valid before we go too much further */
423 __syncicache((caddr_t)startkernel, endkernel - startkernel);
425 #ifndef __powerpc64__
427 * Figure out whether we need to use the 64 bit PMAP. This works by
428 * executing an instruction that is only legal on 64-bit PPC (mtmsrd),
429 * and setting ppc64 = 0 if that causes a trap.
434 bcopy(&testppc64, (void *)EXC_PGM, (size_t)&testppc64size);
435 __syncicache((void *)EXC_PGM, (size_t)&testppc64size);
443 : "=r"(scratch), "=r"(ppc64));
446 cpu_features |= PPC_FEATURE_64;
449 * Now copy restorebridge into all the handlers, if necessary,
450 * and set up the trap tables.
453 if (cpu_features & PPC_FEATURE_64) {
454 /* Patch the two instances of rfi -> rfid */
455 bcopy(&rfid_patch,&rfi_patch1,4);
457 /* rfi_patch2 is at the end of dbleave */
458 bcopy(&rfid_patch,&rfi_patch2,4);
462 * Copy a code snippet to restore 32-bit bridge mode
463 * to the top of every non-generic trap handler
466 trap_offset += (size_t)&restorebridgesize;
467 bcopy(&restorebridge, (void *)EXC_RST, trap_offset);
468 bcopy(&restorebridge, (void *)EXC_DSI, trap_offset);
469 bcopy(&restorebridge, (void *)EXC_ALI, trap_offset);
470 bcopy(&restorebridge, (void *)EXC_PGM, trap_offset);
471 bcopy(&restorebridge, (void *)EXC_MCHK, trap_offset);
472 bcopy(&restorebridge, (void *)EXC_TRC, trap_offset);
473 bcopy(&restorebridge, (void *)EXC_BPT, trap_offset);
476 * Set the common trap entry point to the one that
477 * knows to restore 32-bit operation on execution.
480 generictrap = &trapcode64;
482 generictrap = &trapcode;
485 #else /* powerpc64 */
486 cpu_features |= PPC_FEATURE_64;
487 generictrap = &trapcode;
491 bcopy(&rstcode, (void *)(EXC_RST + trap_offset), (size_t)&rstsize);
493 bcopy(generictrap, (void *)EXC_RST, (size_t)&trapsize);
497 bcopy(&dblow, (void *)(EXC_MCHK + trap_offset), (size_t)&dbsize);
498 bcopy(&dblow, (void *)(EXC_PGM + trap_offset), (size_t)&dbsize);
499 bcopy(&dblow, (void *)(EXC_TRC + trap_offset), (size_t)&dbsize);
500 bcopy(&dblow, (void *)(EXC_BPT + trap_offset), (size_t)&dbsize);
502 bcopy(generictrap, (void *)EXC_MCHK, (size_t)&trapsize);
503 bcopy(generictrap, (void *)EXC_PGM, (size_t)&trapsize);
504 bcopy(generictrap, (void *)EXC_TRC, (size_t)&trapsize);
505 bcopy(generictrap, (void *)EXC_BPT, (size_t)&trapsize);
507 bcopy(&alitrap, (void *)(EXC_ALI + trap_offset), (size_t)&alisize);
508 bcopy(&dsitrap, (void *)(EXC_DSI + trap_offset), (size_t)&dsisize);
509 bcopy(generictrap, (void *)EXC_ISI, (size_t)&trapsize);
511 bcopy(&slbtrap, (void *)EXC_DSE, (size_t)&slbtrapsize);
512 bcopy(&slbtrap, (void *)EXC_ISE, (size_t)&slbtrapsize);
514 bcopy(generictrap, (void *)EXC_EXI, (size_t)&trapsize);
515 bcopy(generictrap, (void *)EXC_FPU, (size_t)&trapsize);
516 bcopy(generictrap, (void *)EXC_DECR, (size_t)&trapsize);
517 bcopy(generictrap, (void *)EXC_SC, (size_t)&trapsize);
518 bcopy(generictrap, (void *)EXC_FPA, (size_t)&trapsize);
519 bcopy(generictrap, (void *)EXC_VEC, (size_t)&trapsize);
520 bcopy(generictrap, (void *)EXC_PERF, (size_t)&trapsize);
521 bcopy(generictrap, (void *)EXC_VECAST_G4, (size_t)&trapsize);
522 bcopy(generictrap, (void *)EXC_VECAST_G5, (size_t)&trapsize);
523 #ifndef __powerpc64__
524 /* G2-specific TLB miss helper handlers */
525 bcopy(&imisstrap, (void *)EXC_IMISS, (size_t)&imisssize);
526 bcopy(&dlmisstrap, (void *)EXC_DLMISS, (size_t)&dlmisssize);
527 bcopy(&dsmisstrap, (void *)EXC_DSMISS, (size_t)&dsmisssize);
529 __syncicache(EXC_RSVD, EXC_LAST - EXC_RSVD);
536 /* Warn if cachline size was not determined */
537 if (cacheline_warn == 1) {
538 printf("WARNING: cacheline size undetermined, setting to 32\n");
542 * Choose a platform module so we can get the physical memory map.
545 platform_probe_and_attach();
548 * Initialise virtual memory. Use BUS_PROBE_GENERIC priority
549 * in case the platform module had a better idea of what we
552 if (cpu_features & PPC_FEATURE_64)
553 pmap_mmu_install(MMU_TYPE_G5, BUS_PROBE_GENERIC);
555 pmap_mmu_install(MMU_TYPE_OEA, BUS_PROBE_GENERIC);
557 pmap_bootstrap(startkernel, endkernel);
558 mtmsr(PSL_KERNSET & ~PSL_EE);
561 * Initialize params/tunables that are derived from memsize
563 init_param2(physmem);
566 * Grab booted kernel's name
568 env = getenv("kernelname");
570 strlcpy(kernelname, env, sizeof(kernelname));
575 * Finish setting up thread0.
577 thread0.td_pcb = (struct pcb *)
578 ((thread0.td_kstack + thread0.td_kstack_pages * PAGE_SIZE -
579 sizeof(struct pcb)) & ~15UL);
580 bzero((void *)thread0.td_pcb, sizeof(struct pcb));
581 pc->pc_curpcb = thread0.td_pcb;
583 /* Initialise the message buffer. */
584 msgbufinit(msgbufp, msgbufsize);
587 if (boothowto & RB_KDB)
588 kdb_enter(KDB_WHY_BOOTFLAGS,
589 "Boot flags requested debugger");
592 return (((uintptr_t)thread0.td_pcb -
593 (sizeof(struct callframe) - 3*sizeof(register_t))) & ~15UL);
597 bzero(void *buf, size_t len)
603 while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
608 while (len >= sizeof(u_long) * 8) {
610 *((u_long*) p + 1) = 0;
611 *((u_long*) p + 2) = 0;
612 *((u_long*) p + 3) = 0;
613 len -= sizeof(u_long) * 8;
614 *((u_long*) p + 4) = 0;
615 *((u_long*) p + 5) = 0;
616 *((u_long*) p + 6) = 0;
617 *((u_long*) p + 7) = 0;
618 p += sizeof(u_long) * 8;
621 while (len >= sizeof(u_long)) {
623 len -= sizeof(u_long);
639 * Flush the D-cache for non-DMA I/O so that the I-cache can
640 * be made coherent later.
643 cpu_flush_dcache(void *ptr, size_t len)
653 cpu_initclocks_bsp();
657 * Shutdown the CPU as much as possible.
667 ptrace_set_pc(struct thread *td, unsigned long addr)
669 struct trapframe *tf;
672 tf->srr0 = (register_t)addr;
678 ptrace_single_step(struct thread *td)
680 struct trapframe *tf;
689 ptrace_clear_single_step(struct thread *td)
691 struct trapframe *tf;
700 kdb_cpu_clear_singlestep(void)
703 kdb_frame->srr1 &= ~PSL_SE;
707 kdb_cpu_set_singlestep(void)
710 kdb_frame->srr1 |= PSL_SE;
714 * Initialise a struct pcpu.
717 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t sz)
720 /* Copy the SLB contents from the current CPU */
721 memcpy(pcpu->pc_slb, PCPU_GET(slb), sizeof(pcpu->pc_slb));
732 if (td->td_md.md_spinlock_count == 0) {
733 msr = intr_disable();
734 td->td_md.md_spinlock_count = 1;
735 td->td_md.md_saved_msr = msr;
737 td->td_md.md_spinlock_count++;
749 msr = td->td_md.md_saved_msr;
750 td->td_md.md_spinlock_count--;
751 if (td->td_md.md_spinlock_count == 0)
755 int db_trap_glue(struct trapframe *); /* Called from trap_subr.S */
758 db_trap_glue(struct trapframe *frame)
760 if (!(frame->srr1 & PSL_PR)
761 && (frame->exc == EXC_TRC || frame->exc == EXC_RUNMODETRC
762 || (frame->exc == EXC_PGM
763 && (frame->srr1 & 0x20000))
764 || frame->exc == EXC_BPT
765 || frame->exc == EXC_DSI)) {
766 int type = frame->exc;
767 if (type == EXC_PGM && (frame->srr1 & 0x20000)) {
770 return (kdb_trap(type, 0, frame));
776 #ifndef __powerpc64__
779 va_to_vsid(pmap_t pm, vm_offset_t va)
781 return ((pm->pm_sr[(uintptr_t)va >> ADDR_SR_SHFT]) & SR_VSID_MASK);