1 /* $NetBSD: ibm4xx_machdep.c,v 1.41 2026/06/13 20:16:23 rkujawa Exp $ */ 2 /* Original: ibm40x_machdep.c,v 1.3 2005/01/17 17:19:36 shige Exp $ */ 3 4 /* 5 * Copyright 2001, 2002 Wasabi Systems, Inc. 6 * All rights reserved. 7 * 8 * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc. 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 for the NetBSD Project by 21 * Wasabi Systems, Inc. 22 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 23 * or promote products derived from this software without specific prior 24 * written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 27 * 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 WASABI SYSTEMS, INC 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 /* 40 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 41 * Copyright (C) 1995, 1996 TooLs GmbH. 42 * All rights reserved. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * This product includes software developed by TooLs GmbH. 55 * 4. The name of TooLs GmbH may not be used to endorse or promote products 56 * derived from this software without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 59 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 60 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 61 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 62 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 63 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 64 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 65 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 66 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 67 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 68 */ 69 70 #include <sys/cdefs.h> 71 __KERNEL_RCSID(0, "$NetBSD: ibm4xx_machdep.c,v 1.41 2026/06/13 20:16:23 rkujawa Exp $"); 72 73 #include "ksyms.h" 74 75 #ifdef _KERNEL_OPT 76 #include "opt_ddb.h" 77 #include "opt_kgdb.h" 78 #include "opt_modular.h" 79 #include "opt_ppcarch.h" 80 #endif 81 82 #include <sys/param.h> 83 #include <sys/cpu.h> 84 #include <sys/ksyms.h> 85 #include <sys/mount.h> 86 #include <sys/msgbuf.h> 87 #include <sys/pmf.h> 88 #include <sys/proc.h> 89 #include <sys/reboot.h> 90 #include <sys/systm.h> 91 92 #include <dev/cons.h> 93 94 #include <uvm/uvm_extern.h> 95 96 #if defined(DDB) 97 #include <machine/db_machdep.h> 98 #include <ddb/db_extern.h> 99 #endif 100 101 #if defined(KGDB) 102 #include <sys/kgdb.h> 103 #endif 104 105 #include <machine/powerpc.h> 106 #include <machine/trap.h> 107 108 #include <powerpc/pcb.h> 109 #include <powerpc/spr.h> 110 111 #include <powerpc/ibm4xx/cpu.h> 112 #include <powerpc/ibm4xx/spr.h> 113 114 /* 115 * Global variables used here and there 116 */ 117 paddr_t msgbuf_paddr; 118 vaddr_t msgbuf_vaddr; 119 char msgbuf[MSGBUFSIZE]; 120 121 #if NKSYMS || defined(DDB) || defined(MODULAR) 122 void *startsym, *endsym; 123 #endif 124 125 #ifdef MODULAR 126 register_t cpu_psluserset = PSL_USERSET; 127 register_t cpu_pslusermod = PSL_USERMOD; 128 register_t cpu_pslusermask = PSL_USERMASK; 129 #endif 130 131 /* 132 * Trap vectors 133 */ 134 extern const uint32_t defaulttrap[], defaultsize; 135 extern const uint32_t sctrap[], scsize; 136 extern const uint32_t accesstrap[], accesssize; 137 extern const uint32_t criticaltrap[], criticalsize; 138 #ifdef PPC_IBM440 139 extern const uint32_t mchktrap[], mchksize; 140 #endif 141 extern const uint32_t tlbimiss4xx[], tlbim4size; 142 extern const uint32_t tlbdmiss4xx[], tlbdm4size; 143 extern const uint32_t pitfitwdog[], pitfitwdogsize; 144 extern const uint32_t errata51handler[], errata51size; 145 #if defined(DDB) 146 extern const uint32_t ddblow[], ddbsize; 147 #endif 148 static const struct exc_info trap_table[] = { 149 { EXC_SC, sctrap, (uintptr_t)&scsize }, 150 { EXC_ALI, accesstrap, (uintptr_t)&accesssize }, 151 { EXC_DSI, accesstrap, (uintptr_t)&accesssize }, 152 #ifdef PPC_IBM440 153 { EXC_MCHK, mchktrap, (uintptr_t)&mchksize }, 154 { EXC_ISI, accesstrap, (uintptr_t)&accesssize }, 155 #else 156 { EXC_MCHK, criticaltrap, (uintptr_t)&criticalsize }, 157 #endif 158 { EXC_ITMISS, tlbimiss4xx, (uintptr_t)&tlbim4size }, 159 { EXC_DTMISS, tlbdmiss4xx, (uintptr_t)&tlbdm4size }, 160 { EXC_PIT, pitfitwdog, (uintptr_t)&pitfitwdogsize }, 161 { EXC_DEBUG, criticaltrap, (uintptr_t)&criticalsize }, 162 #ifndef PPC_IBM440 163 { (EXC_DTMISS|EXC_ALI), 164 errata51handler, (uintptr_t)&errata51size }, 165 #endif 166 #if defined(DDB) 167 { EXC_PGM, ddblow, (uintptr_t)&ddbsize }, 168 #else 169 { EXC_PGM, accesstrap, (uintptr_t)&accesssize }, 170 #endif 171 }; 172 173 void 174 cpu_reboot(int howto, char *what) 175 { 176 static int syncing; 177 static char str[256]; 178 char *ap = str, *ap1 = ap; 179 180 boothowto = howto; 181 if (!cold && !(howto & RB_NOSYNC) && !syncing) { 182 syncing = 1; 183 vfs_shutdown(); /* sync */ 184 } 185 186 splhigh(); 187 188 if (!cold && (howto & RB_DUMP)) 189 ibm4xx_dumpsys(); 190 191 doshutdownhooks(); 192 193 pmf_system_shutdown(boothowto); 194 195 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) { 196 /* Power off here if we know how... */ 197 } 198 199 if (howto & RB_HALT) { 200 printf("The operating system has halted.\n" 201 "Press any key to reboot.\n\n"); 202 203 cnpollc(true); 204 cngetc(); 205 cnpollc(false); 206 } 207 208 printf("rebooting...\n\n"); 209 if (what && *what) { 210 if (strlen(what) > sizeof(str) - 5) 211 printf("boot string too large, ignored\n"); 212 else { 213 strcpy(str, what); 214 ap1 = ap = str + strlen(str); 215 *ap++ = ' '; 216 } 217 } 218 *ap++ = '-'; 219 if (howto & RB_SINGLE) 220 *ap++ = 's'; 221 if (howto & RB_KDB) 222 *ap++ = 'd'; 223 *ap++ = '\0'; 224 if (ap[-2] == '-') 225 *ap1 = '\0'; 226 227 /* flush cache for msgbuf */ 228 __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE)); 229 230 ppc4xx_reset(); 231 232 printf("ppc4xx_reset() failed!\n"); 233 234 for (;;) { 235 #if defined(DDB) 236 Debugger(); 237 #elif defined(KGDB) 238 kgdb_connect(1); 239 #else 240 continue; 241 #endif 242 } 243 } 244 245 /* 246 * Install a trap vector. We cannot use memcpy because the 247 * destination may be zero. 248 */ 249 static void 250 trap_copy(const uint32_t *src, vaddr_t dest, size_t len) 251 { 252 uint32_t *dest_p = (void *)dest; 253 254 while (len > 0) { 255 *dest_p++ = *src++; 256 len -= sizeof(uint32_t); 257 } 258 } 259 260 /* 261 * ibm4xx_init: 262 */ 263 void 264 ibm4xx_init(vaddr_t startkernel, vaddr_t endkernel, void (*handler)(void)) 265 { 266 /* Initialize cache info for memcpy, etc. */ 267 cpu_probe_cache(); 268 269 /* 270 * Initialize current pcb and pmap pointers. 271 */ 272 KASSERT(curcpu() == &cpu_info[0]); 273 KASSERT(lwp0.l_cpu == curcpu()); 274 KASSERT(curlwp == &lwp0); 275 276 curpcb = lwp_getpcb(curlwp); 277 memset(curpcb, 0, sizeof(struct pcb)); 278 279 curpcb->pcb_pm = pmap_kernel(); 280 281 for (uintptr_t exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100) { 282 trap_copy(defaulttrap, exc, (uintptr_t)&defaultsize); 283 } 284 285 for (size_t i = 0; i < __arraycount(trap_table); i++) { 286 KASSERT(trap_table[i].exc_size <= 0x100); 287 trap_copy(trap_table[i].exc_addr, trap_table[i].exc_vector, 288 trap_table[i].exc_size); 289 } 290 291 __syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100); 292 293 #ifdef PPC_IBM440 294 /* 295 * Book E: exception vectors are IVPR + IVORn. 296 * Point the IVORs at the same memory offs the 40x uses, so the 297 * trap_copy()ed handlers above are shared... 298 */ 299 mtspr(SPR_IVPR, 0); /* Exception vector base */ 300 mtspr(SPR_IVOR0, EXC_CII); /* critical input */ 301 mtspr(SPR_IVOR1, EXC_MCHK); /* machine check */ 302 mtspr(SPR_IVOR2, EXC_DSI); /* data storage */ 303 mtspr(SPR_IVOR3, EXC_ISI); /* instruction storage */ 304 mtspr(SPR_IVOR4, EXC_EXI); /* external input */ 305 mtspr(SPR_IVOR5, EXC_ALI); /* alignment */ 306 mtspr(SPR_IVOR6, EXC_PGM); /* program */ 307 mtspr(SPR_IVOR7, EXC_FPU); /* FP unavailable */ 308 mtspr(SPR_IVOR8, EXC_SC); /* system call */ 309 mtspr(SPR_IVOR9, EXC_FPA); /* AP unavailable (default trap) */ 310 mtspr(SPR_IVOR10, EXC_PIT); /* decrementer -> PIT stub */ 311 mtspr(SPR_IVOR11, EXC_FIT); /* fixed interval timer */ 312 mtspr(SPR_IVOR12, EXC_WDOG); /* watchdog */ 313 mtspr(SPR_IVOR13, EXC_DTMISS); /* data TLB error */ 314 mtspr(SPR_IVOR14, EXC_ITMISS); /* instruction TLB error */ 315 mtspr(SPR_IVOR15, EXC_DEBUG); /* debug */ 316 #else 317 mtspr(SPR_EVPR, 0); /* Set Exception vector base */ 318 #endif 319 320 /* Handle trap instruction as PGM exception */ 321 mtspr(SPR_DBCR0, mfspr(SPR_DBCR0) & ~DBCR0_TDE); 322 323 /* 324 * external interrupt handler install 325 */ 326 if (handler) 327 ibm4xx_install_extint(handler); 328 329 /* 330 * Now enable translation (and machine checks/recoverable interrupts). 331 */ 332 #ifdef PPC_IBM440 333 /* 334 * The 440 returns from machine checks via MCSRR0/1 + rfmci, so 335 * enabling ME is necessary - no stuck observed on 440 336 */ 337 __asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync" 338 : : "r" (0), "K" (PSL_IR|PSL_DR|PSL_ME)); 339 #else 340 __asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync" 341 : : "r" (0), "K" (PSL_IR|PSL_DR)); 342 /* XXXX PSL_ME - With ME set kernel gets stuck... */ 343 #endif 344 345 /* 346 * turn on console after enable translation 347 */ 348 consinit(); 349 350 uvm_md_init(); 351 352 /* 353 * Initialize pmap module. 354 */ 355 pmap_bootstrap(startkernel, endkernel); 356 357 /* 358 * Let's take all the indirect calls via our stubs and patch 359 * them to be direct calls. 360 */ 361 cpu_fixup_stubs(); 362 363 #if NKSYMS || defined(DDB) || defined(MODULAR) 364 ksyms_addsyms_elf((uintptr_t)endsym - (uintptr_t)startsym, 365 startsym, endsym); 366 #endif 367 } 368 369 void 370 ibm4xx_install_extint(void (*handler)(void)) 371 { 372 extern int extint, extsize; 373 extern u_long extint_call; 374 u_long offset = (u_long)handler - (u_long)&extint_call; 375 int msr; 376 377 #ifdef DIAGNOSTIC 378 if (offset > 0x1ffffff) 379 panic("install_extint: too far away"); 380 #endif 381 __asm volatile ("mfmsr %0; wrteei 0" : "=r" (msr)); 382 extint_call = (extint_call & 0xfc000003) | offset; 383 memcpy((void *)EXC_EXI, &extint, (size_t)&extsize); 384 __syncicache((void *)&extint_call, sizeof extint_call); 385 __syncicache((void *)EXC_EXI, (int)&extsize); 386 __asm volatile ("mtmsr %0" :: "r" (msr)); 387 } 388 389 /* 390 * ibm4xx_cpu_startup: 391 * Machine dependent startup code. 392 */ 393 void 394 ibm4xx_cpu_startup(const char *model) 395 { 396 vaddr_t minaddr, maxaddr; 397 char pbuf[9]; 398 399 KASSERT(curcpu() != NULL); 400 KASSERT(lwp0.l_cpu != NULL); 401 KASSERT(curcpu()->ci_intstk != 0); 402 KASSERT(curcpu()->ci_idepth == -1); 403 404 /* 405 * Initialize error message buffer (at end of core). 406 */ 407 #if 0 /* For some reason this fails... --Artem 408 * Besides, do we really have to put it at the end of core? 409 * Let's use static buffer for now 410 */ 411 if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE), 412 0, UVM_KMF_VAONLY))) 413 panic("startup: no room for message buffer"); 414 for (i = 0; i < btoc(MSGBUFSIZE); i++) 415 pmap_kenter_pa(msgbuf_vaddr + i * PAGE_SIZE, 416 msgbuf_paddr + i * PAGE_SIZE, 417 VM_PROT_READ|VM_PROT_WRITE, 0); 418 initmsgbuf((void *)msgbuf_vaddr, round_page(MSGBUFSIZE)); 419 #else 420 initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE)); 421 #endif 422 423 printf("%s%s", copyright, version); 424 if (model != NULL) 425 printf("Model: %s\n", model); 426 427 format_bytes(pbuf, sizeof(pbuf), ctob(physmem)); 428 printf("total memory = %s\n", pbuf); 429 430 minaddr = 0; 431 /* 432 * Allocate a submap for physio 433 */ 434 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 435 VM_PHYS_SIZE, 0, false, NULL); 436 437 /* 438 * No need to allocate an mbuf cluster submap. Mbuf clusters 439 * are allocated via the pool allocator, and we use direct-mapped 440 * pool pages. 441 */ 442 443 format_bytes(pbuf, sizeof(pbuf), ptoa(uvm_availmem(false))); 444 printf("avail memory = %s\n", pbuf); 445 446 #if defined(PPC_IBM440) && defined(BLKCPY_SELFTEST) 447 ibm4xx_blkcpy_selftest(); 448 #endif 449 } 450 451 /* 452 * ibm4xx_dumpsys: 453 * Crash dump handling. 454 */ 455 void 456 ibm4xx_dumpsys(void) 457 { 458 459 printf("dumpsys: TBD\n"); 460 } 461