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