1 /* $NetBSD: machdep.c,v 1.32 2011/01/14 02:06:25 rmind Exp $ */ 2 3 /*- 4 * Copyright (c) 2003 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Juergen Hannken-Illjes. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.32 2011/01/14 02:06:25 rmind Exp $"); 34 35 #include "opt_explora.h" 36 #include "opt_modular.h" 37 #include "ksyms.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/buf.h> 42 #include <sys/msgbuf.h> 43 #include <sys/kernel.h> 44 #include <sys/mount.h> 45 #include <sys/proc.h> 46 #include <sys/reboot.h> 47 #include <sys/ksyms.h> 48 #include <sys/device.h> 49 50 #include <uvm/uvm_extern.h> 51 52 #include <prop/proplib.h> 53 54 #include <machine/explora.h> 55 #include <machine/bus.h> 56 #include <machine/powerpc.h> 57 #include <machine/tlb.h> 58 #include <machine/pcb.h> 59 #include <machine/trap.h> 60 61 #include <powerpc/spr.h> 62 #include <powerpc/ibm4xx/spr.h> 63 #include <powerpc/ibm4xx/dcr403cgx.h> 64 65 #if NKSYMS || defined(DDB) || defined(MODULAR) 66 #include <machine/db_machdep.h> 67 #include <ddb/db_extern.h> 68 #endif 69 70 #define MEMREGIONS 2 71 #define TLB_PG_SIZE (16*1024*1024) 72 73 char cpu_model[80]; 74 char machine[] = MACHINE; /* from <machine/param.h> */ 75 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */ 76 77 static const unsigned int cpuspeed = 66000000; 78 79 prop_dictionary_t board_properties; 80 struct vm_map *phys_map = NULL; 81 char msgbuf[MSGBUFSIZE]; 82 paddr_t msgbuf_paddr; 83 84 static struct mem_region phys_mem[MEMREGIONS]; 85 static struct mem_region avail_mem[MEMREGIONS]; 86 87 void bootstrap(u_int, u_int); 88 static void install_extint(void (*)(void)); 89 int lcsplx(int); 90 91 /* 92 * Trap vectors 93 */ 94 extern int defaulttrap, defaultsize; 95 extern int sctrap, scsize; 96 extern int alitrap, alisize; 97 extern int dsitrap, dsisize; 98 extern int isitrap, isisize; 99 extern int mchktrap, mchksize; 100 extern int tlbimiss4xx, tlbim4size; 101 extern int tlbdmiss4xx, tlbdm4size; 102 extern int pitfitwdog, pitfitwdogsize; 103 extern int debugtrap, debugsize; 104 extern int errata51handler, errata51size; 105 #ifdef DDB 106 extern int ddblow, ddbsize; 107 #endif 108 static struct { 109 int vector; 110 void *addr; 111 void *size; 112 } trap_table[] = { 113 { EXC_SC, &sctrap, &scsize }, 114 { EXC_ALI, &alitrap, &alisize }, 115 { EXC_DSI, &dsitrap, &dsisize }, 116 { EXC_ISI, &isitrap, &isisize }, 117 { EXC_MCHK, &mchktrap, &mchksize }, 118 { EXC_ITMISS, &tlbimiss4xx, &tlbim4size }, 119 { EXC_DTMISS, &tlbdmiss4xx, &tlbdm4size }, 120 { EXC_PIT, &pitfitwdog, &pitfitwdogsize }, 121 { EXC_DEBUG, &debugtrap, &debugsize }, 122 { (EXC_DTMISS|EXC_ALI), &errata51handler, &errata51size }, 123 #if defined(DDB) 124 { EXC_PGM, &ddblow, &ddbsize }, 125 #endif /* DDB */ 126 }; 127 128 /* 129 * Install a trap vector. We cannot use memcpy because the 130 * destination may be zero. 131 */ 132 static void 133 trap_copy(void *src, int dest, size_t len) 134 { 135 uint32_t *src_p = src; 136 uint32_t *dest_p = (void *)dest; 137 138 while (len > 0) { 139 *dest_p++ = *src_p++; 140 len -= sizeof(uint32_t); 141 } 142 } 143 144 void 145 bootstrap(u_int startkernel, u_int endkernel) 146 { 147 u_int i, j, t, br[4]; 148 u_int maddr, msize, size; 149 struct cpu_info * const ci = &cpu_info[0]; 150 151 br[0] = mfdcr(DCR_BR4); 152 br[1] = mfdcr(DCR_BR5); 153 br[2] = mfdcr(DCR_BR6); 154 br[3] = mfdcr(DCR_BR7); 155 156 for (i = 0; i < 4; i++) 157 for (j = i+1; j < 4; j++) 158 if (br[j] < br[i]) 159 t = br[j], br[j] = br[i], br[i] = t; 160 161 for (i = 0, size = 0; i < 4; i++) { 162 if (((br[i] >> 19) & 3) != 3) 163 continue; 164 maddr = ((br[i] >> 24) & 0xff) << 20; 165 msize = 1 << (20 + ((br[i] >> 21) & 7)); 166 if (maddr+msize > size) 167 size = maddr+msize; 168 } 169 170 phys_mem[0].start = 0; 171 phys_mem[0].size = size & ~PGOFSET; 172 avail_mem[0].start = startkernel; 173 avail_mem[0].size = size-startkernel; 174 175 __asm volatile( 176 " mtpid %0 \n" 177 " sync \n" 178 : : "r" (KERNEL_PID) ); 179 180 /* 181 * Setup initial tlbs. 182 * Kernel memory and console device are 183 * mapped into the first (reserved) tlbs. 184 */ 185 186 for (maddr = 0; maddr < endkernel; maddr += TLB_PG_SIZE) 187 ppc4xx_tlb_reserve(maddr, maddr, TLB_PG_SIZE, TLB_EX); 188 189 /* Map PCKBC, PCKBC2, COM, LPT. This is far beyond physmem. */ 190 ppc4xx_tlb_reserve(BASE_ISA, BASE_ISA, TLB_PG_SIZE, TLB_I | TLB_G); 191 192 #ifndef COM_IS_CONSOLE 193 ppc4xx_tlb_reserve(BASE_FB, BASE_FB, TLB_PG_SIZE, TLB_I | TLB_G); 194 ppc4xx_tlb_reserve(BASE_FB2, BASE_FB2, TLB_PG_SIZE, TLB_I | TLB_G); 195 #endif 196 197 consinit(); 198 199 /* Disable all external interrupts */ 200 mtdcr(DCR_EXIER, 0); 201 202 /* Disable all timer interrupts */ 203 mtspr(SPR_TCR, 0); 204 205 /* Initialize cache info for memcpy, etc. */ 206 cpu_probe_cache(); 207 208 /* 209 * Initialize lwp0 and current pcb and pmap pointers. 210 */ 211 lwp0.l_cpu = ci; 212 213 curpcb = lwp_getpcb(&lwp0); 214 memset(curpcb, 0, sizeof(struct pcb)); /* XXX why? */ 215 curpcb->pcb_pm = pmap_kernel(); 216 217 /* 218 * Install trap vectors. 219 */ 220 221 for (i = EXC_RSVD; i <= EXC_LAST; i += 0x100) 222 trap_copy(&defaulttrap, i, (size_t)&defaultsize); 223 224 for (i = 0; i < sizeof(trap_table)/sizeof(trap_table[0]); i++) { 225 trap_copy(trap_table[i].addr, trap_table[i].vector, 226 (size_t)trap_table[i].size); 227 } 228 229 __syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100); 230 231 /* 232 * Set Exception vector base. 233 * Handle trap instruction as PGM exception. 234 */ 235 236 mtspr(SPR_EVPR, 0); 237 238 t = mfspr(SPR_DBCR0); 239 t &= ~DBCR0_TDE; 240 mtspr(SPR_DBCR0, t); 241 242 /* 243 * External interrupt handler install. 244 */ 245 246 install_extint(ext_intr); 247 248 /* 249 * Now enable translation (and machine checks/recoverable interrupts). 250 */ 251 __asm volatile ( 252 " mfmsr %0 \n" 253 " ori %0,%0,%1 \n" 254 " mtmsr %0 \n" 255 " sync \n" 256 : : "r" (0), "K" (PSL_IR|PSL_DR|PSL_ME) ); 257 258 uvm_setpagesize(); 259 260 /* 261 * Initialize pmap module. 262 */ 263 pmap_bootstrap(startkernel, endkernel); 264 fake_mapiodev = 0; 265 } 266 267 static void 268 install_extint(void (*handler)(void)) 269 { 270 extern int extint, extsize; 271 extern u_long extint_call; 272 u_long offset = (u_long)handler - (u_long)&extint_call; 273 int omsr, msr; 274 275 #ifdef DIAGNOSTIC 276 if (offset > 0x1ffffff) 277 panic("install_extint: too far away"); 278 #endif 279 __asm volatile ( 280 " mfmsr %0 \n" 281 " andi. %1,%0,%2 \n" 282 " mtmsr %1 \n" 283 : "=r" (omsr), "=r" (msr) : "K" ((u_short)~PSL_EE) ); 284 extint_call = (extint_call & 0xfc000003) | offset; 285 memcpy((void *)EXC_EXI, &extint, (size_t)&extsize); 286 __syncicache((void *)&extint_call, sizeof extint_call); 287 __syncicache((void *)EXC_EXI, (int)&extsize); 288 __asm volatile ( 289 " mtmsr %0 \n" 290 : : "r" (omsr) ); 291 } 292 293 void 294 cpu_startup(void) 295 { 296 vaddr_t minaddr, maxaddr; 297 prop_number_t pn; 298 char pbuf[9]; 299 300 /* 301 * Initialize error message buffer (before start of kernel) 302 */ 303 initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE)); 304 305 printf("%s%s", copyright, version); 306 printf("NCD Explora451\n"); 307 308 format_bytes(pbuf, sizeof(pbuf), ctob(physmem)); 309 printf("total memory = %s\n", pbuf); 310 311 minaddr = 0; 312 /* 313 * Allocate a submap for physio 314 */ 315 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 316 VM_PHYS_SIZE, 0, false, NULL); 317 318 /* 319 * No need to allocate an mbuf cluster submap. Mbuf clusters 320 * are allocated via the pool allocator, and we use direct-mapped 321 * pool pages. 322 */ 323 324 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free)); 325 printf("avail memory = %s\n", pbuf); 326 327 /* 328 * Set up the board properties database. 329 */ 330 board_properties = prop_dictionary_create(); 331 KASSERT(board_properties != NULL); 332 333 pn = prop_number_create_integer(ctob(physmem)); 334 KASSERT(pn != NULL); 335 if (prop_dictionary_set(board_properties, "mem-size", pn) == false) 336 panic("setting mem-size"); 337 prop_object_release(pn); 338 339 pn = prop_number_create_integer(cpuspeed); 340 KASSERT(pn != NULL); 341 if (prop_dictionary_set(board_properties, "processor-frequency", 342 pn) == false) 343 panic("setting processor-frequency"); 344 prop_object_release(pn); 345 346 intr_init(); 347 } 348 349 int 350 lcsplx(int ipl) 351 { 352 return spllower(ipl); /*XXX*/ 353 } 354 355 void 356 cpu_reboot(int howto, char *what) 357 { 358 static int syncing = 0; 359 360 boothowto = howto; 361 if (!cold && !(howto & RB_NOSYNC) && !syncing) { 362 syncing = 1; 363 vfs_shutdown(); 364 resettodr(); 365 } 366 367 splhigh(); 368 369 if (!cold && (howto & RB_DUMP)) 370 /*XXX dumpsys()*/; 371 372 doshutdownhooks(); 373 374 pmf_system_shutdown(boothowto); 375 376 if (howto & RB_HALT) { 377 printf("halted\n\n"); 378 379 while (1) 380 ; 381 } 382 383 printf("rebooting\n\n"); 384 385 /* flush cache for msgbuf */ 386 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE)); 387 388 ppc4xx_reset(); 389 390 #ifdef DDB 391 while (1) 392 Debugger(); 393 #else 394 while (1) 395 ; 396 #endif 397 } 398 399 void 400 mem_regions(struct mem_region **mem, struct mem_region **avail) 401 { 402 *mem = phys_mem; 403 *avail = avail_mem; 404 } 405