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