1 /* $NetBSD: machdep.c,v 1.33 2017/11/06 03:47:47 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.33 2017/11/06 03:47:47 christos Exp $"); 31 32 #include "opt_ddb.h" 33 #include "opt_kloader.h" 34 #include "opt_kloader_kernel_path.h" 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/buf.h> 40 #include <sys/reboot.h> 41 #include <sys/mount.h> 42 #include <sys/kcore.h> 43 #include <sys/boot_flag.h> 44 #include <sys/device.h> 45 46 #include <uvm/uvm_extern.h> 47 48 #ifdef DDB 49 #include <machine/db_machdep.h> 50 #include <ddb/db_sym.h> 51 #include <ddb/db_extern.h> 52 #include <sys/exec_elf.h> 53 #endif 54 55 #include <dev/cons.h> /* cntab access (cpu_reboot) */ 56 #include <machine/bootinfo.h> 57 #include <machine/psl.h> 58 #include <machine/intr.h>/* hardintr_init */ 59 #include <playstation2/playstation2/sifbios.h> 60 #include <playstation2/playstation2/interrupt.h> 61 62 #if defined KLOADER_KERNEL_PATH && !defined KLOADER 63 #error "define KLOADER" 64 #endif 65 #ifdef KLOADER 66 #include <machine/kloader.h> 67 #endif 68 69 struct cpu_info cpu_info_store; 70 71 struct vm_map *mb_map; 72 struct vm_map *phys_map; 73 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX]; 74 int mem_cluster_cnt; 75 76 #ifdef DEBUG 77 static void bootinfo_dump(void); 78 #endif 79 80 void mach_init(void); 81 /* 82 * Do all the stuff that locore normally does before calling main(). 83 */ 84 void 85 mach_init(void) 86 { 87 extern char kernel_text[], edata[], end[]; 88 void *kernend; 89 struct pcb *pcb0; 90 vaddr_t v; 91 paddr_t start; 92 size_t size; 93 94 /* 95 * Clear the BSS segment. 96 */ 97 kernend = (void *)mips_round_page(end); 98 memset(edata, 0, kernend - edata); 99 100 /* disable all interrupt */ 101 interrupt_init_bootstrap(); 102 103 /* enable SIF BIOS */ 104 sifbios_init(); 105 106 consinit(); 107 108 printf("kernel_text=%p edata=%p end=%p\n", kernel_text, edata, end); 109 110 #ifdef DEBUG 111 bootinfo_dump(); 112 #endif 113 uvm_md_init(); 114 115 physmem = atop(PS2_MEMORY_SIZE); 116 117 /* 118 * Copy exception-dispatch code down to exception vector. 119 * Initialize locore-function vector. 120 * Clear out the I and D caches. 121 */ 122 mips_vector_init(); 123 124 /* 125 * Load the rest of the available pages into the VM system. 126 */ 127 start = (paddr_t)round_page(MIPS_KSEG0_TO_PHYS(kernend)); 128 size = PS2_MEMORY_SIZE - start - BOOTINFO_BLOCK_SIZE; 129 memset((void *)MIPS_PHYS_TO_KSEG1(start), 0, size); 130 131 /* kernel itself */ 132 mem_clusters[0].start = trunc_page(MIPS_KSEG0_TO_PHYS(kernel_text)); 133 mem_clusters[0].size = start - mem_clusters[0].start; 134 /* heap */ 135 mem_clusters[1].start = start; 136 mem_clusters[1].size = size; 137 /* load */ 138 printf("load memory %#lx, %#x\n", start, size); 139 uvm_page_physload(atop(start), atop(start + size), 140 atop(start), atop(start + size), VM_FREELIST_DEFAULT); 141 142 strcpy(cpu_model, "SONY PlayStation 2"); 143 144 /* 145 * Initialize error message buffer (at end of core). 146 */ 147 mips_init_msgbuf(); 148 149 pmap_bootstrap(); 150 151 /* 152 * Allocate uarea page for lwp0 and set it. 153 */ 154 v = uvm_pageboot_alloc(USPACE); 155 156 pcb0 = lwp_getpcb(&lwp0); 157 pcb0->pcb_context[11] = PSL_LOWIPL; /* SR */ 158 #ifdef IPL_ICU_MASK 159 pcb0->pcb_ppl = 0; 160 #endif 161 162 lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1 163 } 164 165 /* 166 * Allocate memory for variable-sized tables, 167 */ 168 void 169 cpu_startup(void) 170 { 171 cpu_startup_common(); 172 } 173 174 void 175 cpu_reboot(int howto, char *bootstr) 176 { 177 #ifdef KLOADER 178 struct kloader_bootinfo kbi; 179 #endif 180 static int waittime = -1; 181 182 /* Take a snapshot before clobbering any registers. */ 183 if (curlwp) 184 savectx(curpcb); 185 186 if (cold) { 187 howto |= RB_HALT; 188 goto haltsys; 189 } 190 191 /* If "always halt" was specified as a boot flag, obey. */ 192 if (boothowto & RB_HALT) { 193 howto |= RB_HALT; 194 } 195 196 #ifdef KLOADER 197 /* No bootinfo is required. */ 198 kloader_bootinfo_set(&kbi, 0, NULL, NULL, true); 199 #ifndef KLOADER_KERNEL_PATH 200 #define KLOADER_KERNEL_PATH "/netbsd" 201 #endif 202 if ((howto & RB_HALT) == 0) 203 kloader_reboot_setup(KLOADER_KERNEL_PATH); 204 #endif 205 206 boothowto = howto; 207 if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) { 208 waittime = 0; 209 vfs_shutdown(); 210 211 /* 212 * If we've been adjusting the clock, the todr 213 * will be out of synch; adjust it now. 214 */ 215 resettodr(); 216 } 217 218 splhigh(); 219 220 if (howto & RB_DUMP) 221 dumpsys(); 222 223 haltsys: 224 doshutdownhooks(); 225 226 pmf_system_shutdown(boothowto); 227 228 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) 229 sifbios_halt(0); /* power down */ 230 else if (howto & RB_HALT) 231 sifbios_halt(1); /* halt */ 232 else { 233 #ifdef KLOADER 234 kloader_reboot(); 235 /* NOTREACHED */ 236 #endif 237 sifbios_halt(2); /* reset */ 238 } 239 240 while (1) 241 ; 242 /* NOTREACHED */ 243 } 244 245 #ifdef DEBUG 246 void 247 bootinfo_dump(void) 248 { 249 printf("devconf=%#x, option=%#x, rtc=%#x, pcmcia_type=%#x," 250 "sysconf=%#x\n", 251 BOOTINFO_REF(BOOTINFO_DEVCONF), 252 BOOTINFO_REF(BOOTINFO_OPTION_PTR), 253 BOOTINFO_REF(BOOTINFO_RTC), 254 BOOTINFO_REF(BOOTINFO_PCMCIA_TYPE), 255 BOOTINFO_REF(BOOTINFO_SYSCONF)); 256 } 257 #endif /* DEBUG */ 258