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"
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/signalvar.h>
87 #include <sys/syscallsubr.h>
88 #include <sys/sysctl.h>
89 #include <sys/sysent.h>
90 #include <sys/sysproto.h>
91 #include <sys/ucontext.h>
93 #include <sys/vmmeter.h>
94 #include <sys/vnode.h>
96 #include <net/netisr.h>
99 #include <vm/vm_extern.h>
100 #include <vm/vm_kern.h>
101 #include <vm/vm_page.h>
102 #include <vm/vm_map.h>
103 #include <vm/vm_object.h>
104 #include <vm/vm_pager.h>
106 #include <machine/altivec.h>
107 #ifndef __powerpc64__
108 #include <machine/bat.h>
110 #include <machine/cpu.h>
111 #include <machine/elf.h>
112 #include <machine/fpu.h>
113 #include <machine/hid.h>
114 #include <machine/kdb.h>
115 #include <machine/md_var.h>
116 #include <machine/metadata.h>
117 #include <machine/mmuvar.h>
118 #include <machine/pcb.h>
119 #include <machine/reg.h>
120 #include <machine/sigframe.h>
121 #include <machine/spr.h>
122 #include <machine/trap.h>
123 #include <machine/vmparam.h>
127 #include <dev/ofw/openfirm.h>
130 extern vm_offset_t ksym_start, ksym_end;
136 int cacheline_size = 128;
138 int cacheline_size = 32;
140 int hw_direct_map = 1;
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 *);
157 int setfault(faultbuf); /* defined in locore.S */
162 #ifndef __powerpc64__
163 struct bat battable[16];
166 struct kva_md_info kmi;
169 cpu_startup(void *dummy)
173 * Initialise the decrementer-based clock.
178 * Good {morning,afternoon,evening,night}.
180 cpu_setup(PCPU_GET(cpuid));
185 printf("real memory = %ld (%ld MB)\n", ptoa(physmem),
186 ptoa(physmem) / 1048576);
190 printf("available KVA = %zd (%zd MB)\n",
191 virtual_end - virtual_avail,
192 (virtual_end - virtual_avail) / 1048576);
195 * Display any holes after the first chunk of extended memory.
200 printf("Physical memory chunk(s):\n");
201 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
203 phys_avail[indx + 1] - phys_avail[indx];
206 printf("0x%016lx - 0x%016lx, %ld bytes (%ld pages)\n",
208 printf("0x%08x - 0x%08x, %d bytes (%ld pages)\n",
210 phys_avail[indx], phys_avail[indx + 1] - 1, size1,
215 vm_ksubmap_init(&kmi);
217 printf("avail memory = %ld (%ld MB)\n", ptoa(cnt.v_free_count),
218 ptoa(cnt.v_free_count) / 1048576);
221 * Set up buffers, so they can be used to read disk labels.
224 vm_pager_bufferinit();
227 extern char kernel_text[], _end[];
229 #ifndef __powerpc64__
230 /* Bits for running on 64-bit systems in 32-bit mode. */
231 extern void *testppc64, *testppc64size;
232 extern void *restorebridge, *restorebridgesize;
233 extern void *rfid_patch, *rfi_patch1, *rfi_patch2;
234 extern void *trapcode64;
238 extern void *rstcode, *rstsize;
240 extern void *trapcode, *trapsize;
241 extern void *alitrap, *alisize;
242 extern void *dsitrap, *dsisize;
243 extern void *decrint, *decrsize;
244 extern void *extint, *extsize;
245 extern void *dblow, *dbsize;
246 extern void *imisstrap, *imisssize;
247 extern void *dlmisstrap, *dlmisssize;
248 extern void *dsmisstrap, *dsmisssize;
251 powerpc_init(vm_offset_t startkernel, vm_offset_t endkernel,
252 vm_offset_t basekernel, void *mdp)
259 register_t msr, scratch;
260 uint8_t *cache_check;
262 #ifndef __powerpc64__
271 * Parse metadata if present and fetch parameters. Must be done
272 * before console is inited so cninit gets the right value of
276 preload_metadata = mdp;
277 kmdp = preload_search_by_type("elf kernel");
279 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
280 kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
281 endkernel = ulmax(endkernel, MD_FETCH(kmdp,
282 MODINFOMD_KERNEND, vm_offset_t));
284 ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
285 ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
291 * Init params/tunables that can be overridden by the loader
296 * Start initializing proc0 and thread0.
298 proc_linkup0(&proc0, &thread0);
299 thread0.td_frame = &frame0;
302 * Set up per-cpu data.
305 pcpu_init(pc, 0, sizeof(struct pcpu));
306 curthread_reg = pc->pc_curthread = &thread0;
309 __asm __volatile("mtsprg 0, %0" :: "r"(pc));
312 * Init mutexes, which we use heavily in PMAP
318 * Install the OF client interface
324 * Initialize the console before printing anything.
329 * Complain if there is no metadata.
331 if (mdp == NULL || kmdp == NULL) {
332 printf("powerpc_init: no loader metadata.\n");
341 /* Various very early CPU fix ups */
342 switch (mfpvr() >> 16) {
344 * PowerPC 970 CPUs have a misfeature requested by Apple that
345 * makes them pretend they have a 32-byte cacheline. Turn this
346 * off before we measure the cacheline size.
352 scratch = mfspr(SPR_HID5);
353 scratch &= ~HID5_970_DCBZ_SIZE_HI;
354 mtspr(SPR_HID5, scratch);
358 /* XXX: get from ibm,slb-size in device tree */
365 * Initialize the interrupt tables and figure out our cache line
366 * size and whether or not we need the 64-bit bridge code.
370 * Disable translation in case the vector area hasn't been
371 * mapped (G5). Note that no OFW calls can be made until
372 * translation is re-enabled.
376 mtmsr((msr & ~(PSL_IR | PSL_DR)) | PSL_RI);
379 * Measure the cacheline size using dcbz
381 * Use EXC_PGM as a playground. We are about to overwrite it
382 * anyway, we know it exists, and we know it is cache-aligned.
385 cache_check = (void *)EXC_PGM;
387 for (cacheline_size = 0; cacheline_size < 0x100; cacheline_size++)
388 cache_check[cacheline_size] = 0xff;
390 __asm __volatile("dcbz 0,%0":: "r" (cache_check) : "memory");
392 /* Find the first byte dcbz did not zero to get the cache line size */
393 for (cacheline_size = 0; cacheline_size < 0x100 &&
394 cache_check[cacheline_size] == 0; cacheline_size++);
396 /* Work around psim bug */
397 if (cacheline_size == 0) {
402 #ifndef __powerpc64__
404 * Figure out whether we need to use the 64 bit PMAP. This works by
405 * executing an instruction that is only legal on 64-bit PPC (mtmsrd),
406 * and setting ppc64 = 0 if that causes a trap.
411 bcopy(&testppc64, (void *)EXC_PGM, (size_t)&testppc64size);
412 __syncicache((void *)EXC_PGM, (size_t)&testppc64size);
420 : "=r"(scratch), "=r"(ppc64));
423 cpu_features |= PPC_FEATURE_64;
426 * Now copy restorebridge into all the handlers, if necessary,
427 * and set up the trap tables.
430 if (cpu_features & PPC_FEATURE_64) {
431 /* Patch the two instances of rfi -> rfid */
432 bcopy(&rfid_patch,&rfi_patch1,4);
434 /* rfi_patch2 is at the end of dbleave */
435 bcopy(&rfid_patch,&rfi_patch2,4);
439 * Copy a code snippet to restore 32-bit bridge mode
440 * to the top of every non-generic trap handler
443 trap_offset += (size_t)&restorebridgesize;
444 bcopy(&restorebridge, (void *)EXC_RST, trap_offset);
445 bcopy(&restorebridge, (void *)EXC_DSI, trap_offset);
446 bcopy(&restorebridge, (void *)EXC_ALI, trap_offset);
447 bcopy(&restorebridge, (void *)EXC_PGM, trap_offset);
448 bcopy(&restorebridge, (void *)EXC_MCHK, trap_offset);
449 bcopy(&restorebridge, (void *)EXC_TRC, trap_offset);
450 bcopy(&restorebridge, (void *)EXC_BPT, trap_offset);
453 * Set the common trap entry point to the one that
454 * knows to restore 32-bit operation on execution.
457 generictrap = &trapcode64;
459 generictrap = &trapcode;
462 #else /* powerpc64 */
463 cpu_features |= PPC_FEATURE_64;
464 generictrap = &trapcode;
468 bcopy(&rstcode, (void *)(EXC_RST + trap_offset), (size_t)&rstsize);
470 bcopy(generictrap, (void *)EXC_RST, (size_t)&trapsize);
474 bcopy(&dblow, (void *)(EXC_MCHK + trap_offset), (size_t)&dbsize);
475 bcopy(&dblow, (void *)(EXC_PGM + trap_offset), (size_t)&dbsize);
476 bcopy(&dblow, (void *)(EXC_TRC + trap_offset), (size_t)&dbsize);
477 bcopy(&dblow, (void *)(EXC_BPT + trap_offset), (size_t)&dbsize);
479 bcopy(generictrap, (void *)EXC_MCHK, (size_t)&trapsize);
480 bcopy(generictrap, (void *)EXC_PGM, (size_t)&trapsize);
481 bcopy(generictrap, (void *)EXC_TRC, (size_t)&trapsize);
482 bcopy(generictrap, (void *)EXC_BPT, (size_t)&trapsize);
484 bcopy(&alitrap, (void *)(EXC_ALI + trap_offset), (size_t)&alisize);
485 bcopy(&dsitrap, (void *)(EXC_DSI + trap_offset), (size_t)&dsisize);
486 bcopy(generictrap, (void *)EXC_ISI, (size_t)&trapsize);
488 bcopy(generictrap, (void *)EXC_DSE, (size_t)&trapsize);
489 bcopy(generictrap, (void *)EXC_ISE, (size_t)&trapsize);
491 bcopy(generictrap, (void *)EXC_EXI, (size_t)&trapsize);
492 bcopy(generictrap, (void *)EXC_FPU, (size_t)&trapsize);
493 bcopy(generictrap, (void *)EXC_DECR, (size_t)&trapsize);
494 bcopy(generictrap, (void *)EXC_SC, (size_t)&trapsize);
495 bcopy(generictrap, (void *)EXC_FPA, (size_t)&trapsize);
496 bcopy(generictrap, (void *)EXC_VEC, (size_t)&trapsize);
497 bcopy(generictrap, (void *)EXC_VECAST_G4, (size_t)&trapsize);
498 bcopy(generictrap, (void *)EXC_VECAST_G5, (size_t)&trapsize);
499 #ifndef __powerpc64__
500 /* G2-specific TLB miss helper handlers */
501 bcopy(&imisstrap, (void *)EXC_IMISS, (size_t)&imisssize);
502 bcopy(&dlmisstrap, (void *)EXC_DLMISS, (size_t)&dlmisssize);
503 bcopy(&dsmisstrap, (void *)EXC_DSMISS, (size_t)&dsmisssize);
505 __syncicache(EXC_RSVD, EXC_LAST - EXC_RSVD);
512 /* Warn if cachline size was not determined */
513 if (cacheline_warn == 1) {
514 printf("WARNING: cacheline size undetermined, setting to 32\n");
518 * Choose a platform module so we can get the physical memory map.
521 platform_probe_and_attach();
524 * Initialise virtual memory. Use BUS_PROBE_GENERIC priority
525 * in case the platform module had a better idea of what we
528 if (cpu_features & PPC_FEATURE_64)
529 pmap_mmu_install(MMU_TYPE_G5, BUS_PROBE_GENERIC);
531 pmap_mmu_install(MMU_TYPE_OEA, BUS_PROBE_GENERIC);
533 pmap_bootstrap(startkernel, endkernel);
534 mtmsr(PSL_KERNSET & ~PSL_EE);
537 * Initialize params/tunables that are derived from memsize
539 init_param2(physmem);
542 * Grab booted kernel's name
544 env = getenv("kernelname");
546 strlcpy(kernelname, env, sizeof(kernelname));
551 * Finish setting up thread0.
553 thread0.td_pcb = (struct pcb *)
554 ((thread0.td_kstack + thread0.td_kstack_pages * PAGE_SIZE -
555 sizeof(struct pcb)) & ~15UL);
556 bzero((void *)thread0.td_pcb, sizeof(struct pcb));
557 pc->pc_curpcb = thread0.td_pcb;
559 /* Initialise the message buffer. */
560 msgbufinit(msgbufp, msgbufsize);
563 if (boothowto & RB_KDB)
564 kdb_enter(KDB_WHY_BOOTFLAGS,
565 "Boot flags requested debugger");
568 return (((uintptr_t)thread0.td_pcb -
569 (sizeof(struct callframe) - 3*sizeof(register_t))) & ~15UL);
573 bzero(void *buf, size_t len)
579 while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
584 while (len >= sizeof(u_long) * 8) {
586 *((u_long*) p + 1) = 0;
587 *((u_long*) p + 2) = 0;
588 *((u_long*) p + 3) = 0;
589 len -= sizeof(u_long) * 8;
590 *((u_long*) p + 4) = 0;
591 *((u_long*) p + 5) = 0;
592 *((u_long*) p + 6) = 0;
593 *((u_long*) p + 7) = 0;
594 p += sizeof(u_long) * 8;
597 while (len >= sizeof(u_long)) {
599 len -= sizeof(u_long);
615 * Flush the D-cache for non-DMA I/O so that the I-cache can
616 * be made coherent later.
619 cpu_flush_dcache(void *ptr, size_t len)
629 cpu_initclocks_bsp();
633 * Shutdown the CPU as much as possible.
643 ptrace_set_pc(struct thread *td, unsigned long addr)
645 struct trapframe *tf;
648 tf->srr0 = (register_t)addr;
654 ptrace_single_step(struct thread *td)
656 struct trapframe *tf;
665 ptrace_clear_single_step(struct thread *td)
667 struct trapframe *tf;
676 kdb_cpu_clear_singlestep(void)
679 kdb_frame->srr1 &= ~PSL_SE;
683 kdb_cpu_set_singlestep(void)
686 kdb_frame->srr1 |= PSL_SE;
690 * Initialise a struct pcpu.
693 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t sz)
696 /* Copy the SLB contents from the current CPU */
697 memcpy(pcpu->pc_slb, PCPU_GET(slb), sizeof(pcpu->pc_slb));
708 if (td->td_md.md_spinlock_count == 0) {
709 msr = intr_disable();
710 td->td_md.md_spinlock_count = 1;
711 td->td_md.md_saved_msr = msr;
713 td->td_md.md_spinlock_count++;
725 msr = td->td_md.md_saved_msr;
726 td->td_md.md_spinlock_count--;
727 if (td->td_md.md_spinlock_count == 0)
732 * kcopy(const void *src, void *dst, size_t len);
734 * Copy len bytes from src to dst, aborting if we encounter a fatal
737 * kcopy() _must_ save and restore the old fault handler since it is
738 * called by uiomove(), which may be in the path of servicing a non-fatal
742 kcopy(const void *src, void *dst, size_t len)
745 faultbuf env, *oldfault;
749 oldfault = td->td_pcb->pcb_onfault;
750 if ((rv = setfault(env)) != 0) {
751 td->td_pcb->pcb_onfault = oldfault;
755 memcpy(dst, src, len);
757 td->td_pcb->pcb_onfault = oldfault;
761 int db_trap_glue(struct trapframe *); /* Called from trap_subr.S */
764 db_trap_glue(struct trapframe *frame)
766 if (!(frame->srr1 & PSL_PR)
767 && (frame->exc == EXC_TRC || frame->exc == EXC_RUNMODETRC
768 || (frame->exc == EXC_PGM
769 && (frame->srr1 & 0x20000))
770 || frame->exc == EXC_BPT
771 || frame->exc == EXC_DSI)) {
772 int type = frame->exc;
773 if (type == EXC_PGM && (frame->srr1 & 0x20000)) {
776 return (kdb_trap(type, 0, frame));
782 #ifndef __powerpc64__
785 va_to_vsid(pmap_t pm, vm_offset_t va)
787 return ((pm->pm_sr[(uintptr_t)va >> ADDR_SR_SHFT]) & SR_VSID_MASK);