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