1 /* $NetBSD: machdep.c,v 1.31 2010/02/25 23:33:44 matt 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.31 2010/02/25 23:33:44 matt 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/trap.h> 59 60 #include <powerpc/spr.h> 61 #include <powerpc/ibm4xx/spr.h> 62 #include <powerpc/ibm4xx/dcr403cgx.h> 63 64 #if NKSYMS || defined(DDB) || defined(MODULAR) 65 #include <machine/db_machdep.h> 66 #include <ddb/db_extern.h> 67 #endif 68 69 #define MEMREGIONS 2 70 #define TLB_PG_SIZE (16*1024*1024) 71 72 char cpu_model[80]; 73 char machine[] = MACHINE; /* from <machine/param.h> */ 74 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */ 75 76 static const unsigned int cpuspeed = 66000000; 77 78 prop_dictionary_t board_properties; 79 struct vm_map *phys_map = NULL; 80 char msgbuf[MSGBUFSIZE]; 81 paddr_t msgbuf_paddr; 82 83 static struct mem_region phys_mem[MEMREGIONS]; 84 static struct mem_region avail_mem[MEMREGIONS]; 85 86 void bootstrap(u_int, u_int); 87 static void install_extint(void (*)(void)); 88 int lcsplx(int); 89 90 /* 91 * Trap vectors 92 */ 93 extern int defaulttrap, defaultsize; 94 extern int sctrap, scsize; 95 extern int alitrap, alisize; 96 extern int dsitrap, dsisize; 97 extern int isitrap, isisize; 98 extern int mchktrap, mchksize; 99 extern int tlbimiss4xx, tlbim4size; 100 extern int tlbdmiss4xx, tlbdm4size; 101 extern int pitfitwdog, pitfitwdogsize; 102 extern int debugtrap, debugsize; 103 extern int errata51handler, errata51size; 104 #ifdef DDB 105 extern int ddblow, ddbsize; 106 #endif 107 static struct { 108 int vector; 109 void *addr; 110 void *size; 111 } trap_table[] = { 112 { EXC_SC, &sctrap, &scsize }, 113 { EXC_ALI, &alitrap, &alisize }, 114 { EXC_DSI, &dsitrap, &dsisize }, 115 { EXC_ISI, &isitrap, &isisize }, 116 { EXC_MCHK, &mchktrap, &mchksize }, 117 { EXC_ITMISS, &tlbimiss4xx, &tlbim4size }, 118 { EXC_DTMISS, &tlbdmiss4xx, &tlbdm4size }, 119 { EXC_PIT, &pitfitwdog, &pitfitwdogsize }, 120 { EXC_DEBUG, &debugtrap, &debugsize }, 121 { (EXC_DTMISS|EXC_ALI), &errata51handler, &errata51size }, 122 #if defined(DDB) 123 { EXC_PGM, &ddblow, &ddbsize }, 124 #endif /* DDB */ 125 }; 126 127 /* 128 * Install a trap vector. We cannot use memcpy because the 129 * destination may be zero. 130 */ 131 static void 132 trap_copy(void *src, int dest, size_t len) 133 { 134 uint32_t *src_p = src; 135 uint32_t *dest_p = (void *)dest; 136 137 while (len > 0) { 138 *dest_p++ = *src_p++; 139 len -= sizeof(uint32_t); 140 } 141 } 142 143 void 144 bootstrap(u_int startkernel, u_int endkernel) 145 { 146 u_int i, j, t, br[4]; 147 u_int maddr, msize, size; 148 struct cpu_info * const ci = &cpu_info[0]; 149 150 br[0] = mfdcr(DCR_BR4); 151 br[1] = mfdcr(DCR_BR5); 152 br[2] = mfdcr(DCR_BR6); 153 br[3] = mfdcr(DCR_BR7); 154 155 for (i = 0; i < 4; i++) 156 for (j = i+1; j < 4; j++) 157 if (br[j] < br[i]) 158 t = br[j], br[j] = br[i], br[i] = t; 159 160 for (i = 0, size = 0; i < 4; i++) { 161 if (((br[i] >> 19) & 3) != 3) 162 continue; 163 maddr = ((br[i] >> 24) & 0xff) << 20; 164 msize = 1 << (20 + ((br[i] >> 21) & 7)); 165 if (maddr+msize > size) 166 size = maddr+msize; 167 } 168 169 phys_mem[0].start = 0; 170 phys_mem[0].size = size & ~PGOFSET; 171 avail_mem[0].start = startkernel; 172 avail_mem[0].size = size-startkernel; 173 174 __asm volatile( 175 " mtpid %0 \n" 176 " sync \n" 177 : : "r" (KERNEL_PID) ); 178 179 /* 180 * Setup initial tlbs. 181 * Kernel memory and console device are 182 * mapped into the first (reserved) tlbs. 183 */ 184 185 for (maddr = 0; maddr < endkernel; maddr += TLB_PG_SIZE) 186 ppc4xx_tlb_reserve(maddr, maddr, TLB_PG_SIZE, TLB_EX); 187 188 /* Map PCKBC, PCKBC2, COM, LPT. This is far beyond physmem. */ 189 ppc4xx_tlb_reserve(BASE_ISA, BASE_ISA, TLB_PG_SIZE, TLB_I | TLB_G); 190 191 #ifndef COM_IS_CONSOLE 192 ppc4xx_tlb_reserve(BASE_FB, BASE_FB, TLB_PG_SIZE, TLB_I | TLB_G); 193 ppc4xx_tlb_reserve(BASE_FB2, BASE_FB2, TLB_PG_SIZE, TLB_I | TLB_G); 194 #endif 195 196 consinit(); 197 198 /* Disable all external interrupts */ 199 mtdcr(DCR_EXIER, 0); 200 201 /* Disable all timer interrupts */ 202 mtspr(SPR_TCR, 0); 203 204 /* Initialize cache info for memcpy, etc. */ 205 cpu_probe_cache(); 206 207 /* 208 * Initialize lwp0 and current pcb and pmap pointers. 209 */ 210 lwp0.l_cpu = ci; 211 212 curpcb = lwp_getpcb(&lwp0); 213 memset(curpcb, 0, sizeof(struct pcb)); /* XXX why? */ 214 curpcb->pcb_pm = pmap_kernel(); 215 216 /* 217 * Install trap vectors. 218 */ 219 220 for (i = EXC_RSVD; i <= EXC_LAST; i += 0x100) 221 trap_copy(&defaulttrap, i, (size_t)&defaultsize); 222 223 for (i = 0; i < sizeof(trap_table)/sizeof(trap_table[0]); i++) { 224 trap_copy(trap_table[i].addr, trap_table[i].vector, 225 (size_t)trap_table[i].size); 226 } 227 228 __syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100); 229 230 /* 231 * Set Exception vector base. 232 * Handle trap instruction as PGM exception. 233 */ 234 235 mtspr(SPR_EVPR, 0); 236 237 t = mfspr(SPR_DBCR0); 238 t &= ~DBCR0_TDE; 239 mtspr(SPR_DBCR0, t); 240 241 /* 242 * External interrupt handler install. 243 */ 244 245 install_extint(ext_intr); 246 247 /* 248 * Now enable translation (and machine checks/recoverable interrupts). 249 */ 250 __asm volatile ( 251 " mfmsr %0 \n" 252 " ori %0,%0,%1 \n" 253 " mtmsr %0 \n" 254 " sync \n" 255 : : "r" (0), "K" (PSL_IR|PSL_DR|PSL_ME) ); 256 257 uvm_setpagesize(); 258 259 /* 260 * Initialize pmap module. 261 */ 262 pmap_bootstrap(startkernel, endkernel); 263 fake_mapiodev = 0; 264 } 265 266 static void 267 install_extint(void (*handler)(void)) 268 { 269 extern int extint, extsize; 270 extern u_long extint_call; 271 u_long offset = (u_long)handler - (u_long)&extint_call; 272 int omsr, msr; 273 274 #ifdef DIAGNOSTIC 275 if (offset > 0x1ffffff) 276 panic("install_extint: too far away"); 277 #endif 278 __asm volatile ( 279 " mfmsr %0 \n" 280 " andi. %1,%0,%2 \n" 281 " mtmsr %1 \n" 282 : "=r" (omsr), "=r" (msr) : "K" ((u_short)~PSL_EE) ); 283 extint_call = (extint_call & 0xfc000003) | offset; 284 memcpy((void *)EXC_EXI, &extint, (size_t)&extsize); 285 __syncicache((void *)&extint_call, sizeof extint_call); 286 __syncicache((void *)EXC_EXI, (int)&extsize); 287 __asm volatile ( 288 " mtmsr %0 \n" 289 : : "r" (omsr) ); 290 } 291 292 void 293 cpu_startup(void) 294 { 295 vaddr_t minaddr, maxaddr; 296 prop_number_t pn; 297 char pbuf[9]; 298 299 /* 300 * Initialize error message buffer (before start of kernel) 301 */ 302 initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE)); 303 304 printf("%s%s", copyright, version); 305 printf("NCD Explora451\n"); 306 307 format_bytes(pbuf, sizeof(pbuf), ctob(physmem)); 308 printf("total memory = %s\n", pbuf); 309 310 minaddr = 0; 311 /* 312 * Allocate a submap for physio 313 */ 314 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 315 VM_PHYS_SIZE, 0, false, NULL); 316 317 /* 318 * No need to allocate an mbuf cluster submap. Mbuf clusters 319 * are allocated via the pool allocator, and we use direct-mapped 320 * pool pages. 321 */ 322 323 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free)); 324 printf("avail memory = %s\n", pbuf); 325 326 /* 327 * Set up the board properties database. 328 */ 329 board_properties = prop_dictionary_create(); 330 KASSERT(board_properties != NULL); 331 332 pn = prop_number_create_integer(ctob(physmem)); 333 KASSERT(pn != NULL); 334 if (prop_dictionary_set(board_properties, "mem-size", pn) == false) 335 panic("setting mem-size"); 336 prop_object_release(pn); 337 338 pn = prop_number_create_integer(cpuspeed); 339 KASSERT(pn != NULL); 340 if (prop_dictionary_set(board_properties, "processor-frequency", 341 pn) == false) 342 panic("setting processor-frequency"); 343 prop_object_release(pn); 344 345 intr_init(); 346 } 347 348 int 349 lcsplx(int ipl) 350 { 351 return spllower(ipl); /*XXX*/ 352 } 353 354 void 355 cpu_reboot(int howto, char *what) 356 { 357 static int syncing = 0; 358 359 boothowto = howto; 360 if (!cold && !(howto & RB_NOSYNC) && !syncing) { 361 syncing = 1; 362 vfs_shutdown(); 363 resettodr(); 364 } 365 366 splhigh(); 367 368 if (!cold && (howto & RB_DUMP)) 369 /*XXX dumpsys()*/; 370 371 doshutdownhooks(); 372 373 pmf_system_shutdown(boothowto); 374 375 if (howto & RB_HALT) { 376 printf("halted\n\n"); 377 378 while (1) 379 ; 380 } 381 382 printf("rebooting\n\n"); 383 384 /* flush cache for msgbuf */ 385 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE)); 386 387 ppc4xx_reset(); 388 389 #ifdef DDB 390 while (1) 391 Debugger(); 392 #else 393 while (1) 394 ; 395 #endif 396 } 397 398 void 399 mem_regions(struct mem_region **mem, struct mem_region **avail) 400 { 401 *mem = phys_mem; 402 *avail = avail_mem; 403 } 404