1 /* $NetBSD: machdep.c,v 1.21 2012/07/27 22:13:58 matt Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Jachym Holecek 5 * All rights reserved. 6 * 7 * Written for DFC Design, s.r.o. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Based on Walnut and Explora. 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.21 2012/07/27 22:13:58 matt Exp $"); 38 39 #include "opt_compat_netbsd.h" 40 #include "opt_ddb.h" 41 #include "opt_ipkdb.h" 42 #include "opt_virtex.h" 43 #include "opt_kgdb.h" 44 45 #include <sys/param.h> 46 #include <sys/boot_flag.h> 47 #include <sys/buf.h> 48 #include <sys/bus.h> 49 #include <sys/device.h> 50 #include <sys/exec.h> 51 #include <sys/malloc.h> 52 #include <sys/mbuf.h> 53 #include <sys/module.h> 54 #include <sys/mount.h> 55 #include <sys/msgbuf.h> 56 #include <sys/kernel.h> 57 #include <sys/ksyms.h> 58 #include <sys/proc.h> 59 #include <sys/reboot.h> 60 #include <sys/syscallargs.h> 61 #include <sys/syslog.h> 62 #include <sys/systm.h> 63 64 #include <uvm/uvm_extern.h> 65 66 #include <dev/cons.h> 67 68 #include <machine/powerpc.h> 69 70 #include <powerpc/trap.h> 71 #include <powerpc/pcb.h> 72 73 #include <powerpc/spr.h> 74 #include <powerpc/ibm4xx/spr.h> 75 76 #include <powerpc/ibm4xx/cpu.h> 77 78 #include <evbppc/virtex/dcr.h> 79 #include <evbppc/virtex/virtex.h> 80 81 #include "ksyms.h" 82 83 #if defined(DDB) 84 #include <powerpc/db_machdep.h> 85 #include <ddb/db_extern.h> 86 #endif 87 88 #if defined(KGDB) 89 #include <sys/kgdb.h> 90 #endif 91 92 /* 93 * Global variables used here and there 94 */ 95 struct vm_map *phys_map = NULL; 96 97 /* 98 * This should probably be in autoconf! XXX 99 */ 100 char machine[] = MACHINE; /* from <machine/param.h> */ 101 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */ 102 103 char bootpath[256]; 104 vaddr_t msgbuf_vaddr; 105 106 void initppc(vaddr_t, vaddr_t); 107 108 static void dumpsys(void); 109 110 /* BSS segment start & end. */ 111 extern char edata[], end[]; 112 113 /* One region holds all memory, the other is terminator expected by 405 pmap. */ 114 #define MEMREGIONS 2 115 struct mem_region physmemr[MEMREGIONS]; 116 struct mem_region availmemr[MEMREGIONS]; 117 118 /* Maximum TLB page size. */ 119 #define TLB_PG_SIZE (16*1024*1024) 120 121 void 122 initppc(vaddr_t startkernel, vaddr_t endkernel) 123 { 124 paddr_t addr, memsize; 125 126 /* Initialize cache info for memcpy, memset, etc. */ 127 cpu_probe_cache(); 128 129 memset(physmemr, 0, sizeof(physmemr)); /* [1].size = 0 */ 130 memset(availmemr, 0, sizeof(availmemr)); /* [1].size = 0 */ 131 132 memsize = (PHYSMEM * 1024 * 1024) & ~PGOFSET; 133 134 physmemr[0].start = 0; 135 physmemr[0].size = memsize; 136 137 availmemr[0].start = startkernel; 138 availmemr[0].size = memsize - availmemr[0].start; 139 140 /* Map kernel memory linearly. */ 141 for (addr = 0; addr < endkernel; addr += TLB_PG_SIZE) 142 ppc4xx_tlb_reserve(addr, addr, TLB_PG_SIZE, TLB_EX); 143 144 /* Give design-specific code a hint for reserved mappings. */ 145 virtex_machdep_init(roundup(memsize, TLB_PG_SIZE), TLB_PG_SIZE, 146 physmemr, availmemr); 147 148 ibm4xx_init(startkernel, endkernel, pic_ext_intr); 149 150 #ifdef DDB 151 if (boothowto & RB_KDB) 152 Debugger(); 153 #endif 154 #ifdef IPKDB 155 /* 156 * Now trap to IPKDB 157 */ 158 ipkdb_init(); 159 if (boothowto & RB_KDB) 160 ipkdb_connect(0); 161 #endif 162 #ifdef KGDB 163 /* 164 * Now trap to KGDB 165 */ 166 kgdb_connect(1); 167 #endif /* KGDB */ 168 169 /* 170 * Look for the ibm4xx modules in the right place. 171 */ 172 module_machine = module_machine_ibm4xx; 173 } 174 175 /* 176 * Machine dependent startup code. 177 */ 178 179 void 180 cpu_startup(void) 181 { 182 /* For use by propdb. */ 183 static u_int memsize = PHYSMEM * 1024 * 1024; 184 static u_int cpuspeed = CPUFREQ * 1000 * 1000; 185 prop_number_t pn; 186 187 vaddr_t minaddr, maxaddr; 188 char pbuf[9]; 189 190 curcpu()->ci_khz = cpuspeed / 1000; 191 192 /* Initialize error message buffer. */ 193 initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE)); 194 195 printf("%s%s", copyright, version); 196 197 format_bytes(pbuf, sizeof(pbuf), ctob(physmem)); 198 printf("total memory = %s\n", pbuf); 199 200 minaddr = 0; 201 /* 202 * Allocate a submap for physio 203 */ 204 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 205 VM_PHYS_SIZE, 0, false, NULL); 206 207 /* 208 * No need to allocate an mbuf cluster submap. Mbuf clusters 209 * are allocated via the pool allocator, and we use direct-mapped 210 * pool pages. 211 */ 212 213 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free)); 214 printf("avail memory = %s\n", pbuf); 215 216 /* 217 * Set up the board properties database. 218 */ 219 board_info_init(); 220 221 pn = prop_number_create_integer(memsize); 222 KASSERT(pn != NULL); 223 if (prop_dictionary_set(board_properties, "mem-size", pn) == false) 224 panic("setting mem-size"); 225 prop_object_release(pn); 226 227 pn = prop_number_create_integer(cpuspeed); 228 KASSERT(pn != NULL); 229 if (prop_dictionary_set(board_properties, "processor-frequency", 230 pn) == false) 231 panic("setting processor-frequency"); 232 prop_object_release(pn); 233 234 /* 235 * Now that we have VM, malloc()s are OK in bus_space. 236 */ 237 bus_space_mallocok(); 238 fake_mapiodev = 0; 239 } 240 241 242 static void 243 dumpsys(void) 244 { 245 printf("dumpsys: TBD\n"); 246 } 247 248 /* Hook used by 405 pmap module. */ 249 void 250 mem_regions(struct mem_region **mem, struct mem_region **avail) 251 { 252 *mem = physmemr; 253 *avail = availmemr; 254 } 255 256 /* 257 * Halt or reboot the machine after syncing/dumping according to howto. 258 */ 259 void 260 cpu_reboot(int howto, char *what) 261 { 262 static int syncing; 263 static char str[256]; 264 char *ap = str, *ap1 = ap; 265 266 boothowto = howto; 267 if (!cold && !(howto & RB_NOSYNC) && !syncing) { 268 syncing = 1; 269 vfs_shutdown(); /* sync */ 270 resettodr(); /* set wall clock */ 271 } 272 273 splhigh(); 274 275 if (!cold && (howto & RB_DUMP)) 276 dumpsys(); 277 278 doshutdownhooks(); 279 280 pmf_system_shutdown(boothowto); 281 282 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) { 283 /* Power off here if we know how...*/ 284 } 285 286 if (howto & RB_HALT) { 287 printf("halted\n\n"); 288 289 goto reboot; /* XXX for now... */ 290 291 #ifdef DDB 292 printf("dropping to debugger\n"); 293 while(1) 294 Debugger(); 295 #endif 296 #ifdef KGDB 297 printf("dropping to kgdb\n"); 298 while(1) 299 kgdb_connect(1); 300 #endif 301 } 302 303 printf("rebooting\n\n"); 304 if (what && *what) { 305 if (strlen(what) > sizeof str - 5) 306 printf("boot string too large, ignored\n"); 307 else { 308 strcpy(str, what); 309 ap1 = ap = str + strlen(str); 310 *ap++ = ' '; 311 } 312 } 313 *ap++ = '-'; 314 if (howto & RB_SINGLE) 315 *ap++ = 's'; 316 if (howto & RB_KDB) 317 *ap++ = 'd'; 318 *ap++ = 0; 319 if (ap[-2] == '-') 320 *ap1 = 0; 321 322 /* flush cache for msgbuf */ 323 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE)); 324 325 reboot: 326 ppc4xx_reset(); 327 328 printf("ppc4xx_reset() failed!\n"); 329 #ifdef DDB 330 while(1) 331 Debugger(); 332 #endif 333 #ifdef KGDB 334 while(1) 335 kgdb_connect(1); 336 #else 337 while (1) 338 /* nothing */; 339 #endif 340 } 341