Home | History | Annotate | Line # | Download | only in ibm4xx
      1 /*	$NetBSD: ibm4xx_machdep.c,v 1.42 2026/09/28 20:41:00 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.42 2026/09/28 20:41:00 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 (*md_powerdown)(void);
    174 
    175 void
    176 cpu_reboot(int howto, char *what)
    177 {
    178 	static int syncing;
    179 	static char str[256];
    180 	char *ap = str, *ap1 = ap;
    181 
    182 	boothowto = howto;
    183 	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
    184 		syncing = 1;
    185 		vfs_shutdown();		/* sync */
    186 	}
    187 
    188 	splhigh();
    189 
    190 	if (!cold && (howto & RB_DUMP))
    191 		ibm4xx_dumpsys();
    192 
    193 	doshutdownhooks();
    194 
    195 	pmf_system_shutdown(boothowto);
    196 
    197 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN && md_powerdown != NULL) {
    198 		printf("Powering off...\n\n");
    199 		(*md_powerdown)();
    200 	}
    201 
    202 	if (howto & RB_HALT) {
    203 		printf("The operating system has halted.\n"
    204 		    "Press any key to reboot.\n\n");
    205 
    206 		cnpollc(true);
    207 		cngetc();
    208 		cnpollc(false);
    209 	}
    210 
    211 	printf("rebooting...\n\n");
    212 	if (what && *what) {
    213 		if (strlen(what) > sizeof(str) - 5)
    214 			printf("boot string too large, ignored\n");
    215 		else {
    216 			strcpy(str, what);
    217 			ap1 = ap = str + strlen(str);
    218 			*ap++ = ' ';
    219 		}
    220 	}
    221 	*ap++ = '-';
    222 	if (howto & RB_SINGLE)
    223 		*ap++ = 's';
    224 	if (howto & RB_KDB)
    225 		*ap++ = 'd';
    226 	*ap++ = '\0';
    227 	if (ap[-2] == '-')
    228 		*ap1 = '\0';
    229 
    230 	/* flush cache for msgbuf */
    231 	__syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
    232 
    233 	ppc4xx_reset();
    234 
    235 	printf("ppc4xx_reset() failed!\n");
    236 
    237 	for (;;) {
    238 #if defined(DDB)
    239 		Debugger();
    240 #elif defined(KGDB)
    241 		kgdb_connect(1);
    242 #else
    243 		continue;
    244 #endif
    245 	}
    246 }
    247 
    248 /*
    249  * Install a trap vector. We cannot use memcpy because the
    250  * destination may be zero.
    251  */
    252 static void
    253 trap_copy(const uint32_t *src, vaddr_t dest, size_t len)
    254 {
    255 	uint32_t *dest_p = (void *)dest;
    256 
    257 	while (len > 0) {
    258 		*dest_p++ = *src++;
    259 		len -= sizeof(uint32_t);
    260 	}
    261 }
    262 
    263 /*
    264  * ibm4xx_init:
    265  */
    266 void
    267 ibm4xx_init(vaddr_t startkernel, vaddr_t endkernel, void (*handler)(void))
    268 {
    269 	/* Initialize cache info for memcpy, etc. */
    270 	cpu_probe_cache();
    271 
    272 	/*
    273 	 * Initialize current pcb and pmap pointers.
    274 	 */
    275 	KASSERT(curcpu() == &cpu_info[0]);
    276 	KASSERT(lwp0.l_cpu == curcpu());
    277 	KASSERT(curlwp == &lwp0);
    278 
    279 	curpcb = lwp_getpcb(curlwp);
    280 	memset(curpcb, 0, sizeof(struct pcb));
    281 
    282 	curpcb->pcb_pm = pmap_kernel();
    283 
    284 	for (uintptr_t exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100) {
    285 		trap_copy(defaulttrap, exc, (uintptr_t)&defaultsize);
    286 	}
    287 
    288 	for (size_t i = 0; i < __arraycount(trap_table); i++) {
    289 		KASSERT(trap_table[i].exc_size <= 0x100);
    290 		trap_copy(trap_table[i].exc_addr, trap_table[i].exc_vector,
    291 		    trap_table[i].exc_size);
    292 	}
    293 
    294 	__syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
    295 
    296 #ifdef PPC_IBM440
    297 	/*
    298 	 * Book E: exception vectors are IVPR + IVORn.
    299 	 * Point the IVORs at the same memory offs the 40x uses, so the
    300 	 * trap_copy()ed handlers above are shared...
    301 	 */
    302 	mtspr(SPR_IVPR, 0);		/* Exception vector base */
    303 	mtspr(SPR_IVOR0, EXC_CII);	/* critical input */
    304 	mtspr(SPR_IVOR1, EXC_MCHK);	/* machine check */
    305 	mtspr(SPR_IVOR2, EXC_DSI);	/* data storage */
    306 	mtspr(SPR_IVOR3, EXC_ISI);	/* instruction storage */
    307 	mtspr(SPR_IVOR4, EXC_EXI);	/* external input */
    308 	mtspr(SPR_IVOR5, EXC_ALI);	/* alignment */
    309 	mtspr(SPR_IVOR6, EXC_PGM);	/* program */
    310 	mtspr(SPR_IVOR7, EXC_FPU);	/* FP unavailable */
    311 	mtspr(SPR_IVOR8, EXC_SC);	/* system call */
    312 	mtspr(SPR_IVOR9, EXC_FPA);	/* AP unavailable (default trap) */
    313 	mtspr(SPR_IVOR10, EXC_PIT);	/* decrementer -> PIT stub */
    314 	mtspr(SPR_IVOR11, EXC_FIT);	/* fixed interval timer */
    315 	mtspr(SPR_IVOR12, EXC_WDOG);	/* watchdog */
    316 	mtspr(SPR_IVOR13, EXC_DTMISS);	/* data TLB error */
    317 	mtspr(SPR_IVOR14, EXC_ITMISS);	/* instruction TLB error */
    318 	mtspr(SPR_IVOR15, EXC_DEBUG);	/* debug */
    319 #else
    320 	mtspr(SPR_EVPR, 0);		/* Set Exception vector base */
    321 #endif
    322 
    323 	/* Handle trap instruction as PGM exception */
    324 	mtspr(SPR_DBCR0, mfspr(SPR_DBCR0) & ~DBCR0_TDE);
    325 
    326 	/*
    327 	 * external interrupt handler install
    328 	 */
    329 	if (handler)
    330 		ibm4xx_install_extint(handler);
    331 
    332 	/*
    333 	 * Now enable translation (and machine checks/recoverable interrupts).
    334 	 */
    335 #ifdef PPC_IBM440
    336 	/*
    337 	 * The 440 returns from machine checks via MCSRR0/1 + rfmci, so
    338 	 * enabling ME is necessary - no stuck observed on 440
    339 	 */
    340 	__asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
    341 		      : : "r" (0), "K" (PSL_IR|PSL_DR|PSL_ME));
    342 #else
    343 	__asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
    344 		      : : "r" (0), "K" (PSL_IR|PSL_DR));
    345 	/* XXXX PSL_ME - With ME set kernel gets stuck... */
    346 #endif
    347 
    348 	/*
    349 	 * turn on console after enable translation
    350 	 */
    351 	consinit();
    352 
    353 	uvm_md_init();
    354 
    355 	/*
    356 	 * Initialize pmap module.
    357 	 */
    358 	pmap_bootstrap(startkernel, endkernel);
    359 
    360 	/*
    361 	 * Let's take all the indirect calls via our stubs and patch
    362 	 * them to be direct calls.
    363 	 */
    364 	cpu_fixup_stubs();
    365 
    366 #if NKSYMS || defined(DDB) || defined(MODULAR)
    367 	ksyms_addsyms_elf((uintptr_t)endsym - (uintptr_t)startsym,
    368 	    startsym, endsym);
    369 #endif
    370 }
    371 
    372 void
    373 ibm4xx_install_extint(void (*handler)(void))
    374 {
    375 	extern int extint, extsize;
    376 	extern u_long extint_call;
    377 	u_long offset = (u_long)handler - (u_long)&extint_call;
    378 	int msr;
    379 
    380 #ifdef	DIAGNOSTIC
    381 	if (offset > 0x1ffffff)
    382 		panic("install_extint: too far away");
    383 #endif
    384 	__asm volatile ("mfmsr %0; wrteei 0" : "=r" (msr));
    385 	extint_call = (extint_call & 0xfc000003) | offset;
    386 	memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
    387 	__syncicache((void *)&extint_call, sizeof extint_call);
    388 	__syncicache((void *)EXC_EXI, (int)&extsize);
    389 	__asm volatile ("mtmsr %0" :: "r" (msr));
    390 }
    391 
    392 /*
    393  * ibm4xx_cpu_startup:
    394  * Machine dependent startup code.
    395  */
    396 void
    397 ibm4xx_cpu_startup(const char *model)
    398 {
    399 	vaddr_t minaddr, maxaddr;
    400 	char pbuf[9];
    401 
    402 	KASSERT(curcpu() != NULL);
    403 	KASSERT(lwp0.l_cpu != NULL);
    404 	KASSERT(curcpu()->ci_intstk != 0);
    405 	KASSERT(curcpu()->ci_idepth == -1);
    406 
    407 	/*
    408 	 * Initialize error message buffer (at end of core).
    409 	 */
    410 #if 0	/* For some reason this fails... --Artem
    411 	 * Besides, do we really have to put it at the end of core?
    412 	 * Let's use static buffer for now
    413 	 */
    414 	if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE),
    415 	    0, UVM_KMF_VAONLY)))
    416 		panic("startup: no room for message buffer");
    417 	for (i = 0; i < btoc(MSGBUFSIZE); i++)
    418 		pmap_kenter_pa(msgbuf_vaddr + i * PAGE_SIZE,
    419 		    msgbuf_paddr + i * PAGE_SIZE,
    420 		    VM_PROT_READ|VM_PROT_WRITE, 0);
    421 	initmsgbuf((void *)msgbuf_vaddr, round_page(MSGBUFSIZE));
    422 #else
    423 	initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
    424 #endif
    425 
    426 	printf("%s%s", copyright, version);
    427 	if (model != NULL)
    428 		printf("Model: %s\n", model);
    429 
    430 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    431 	printf("total memory = %s\n", pbuf);
    432 
    433 	minaddr = 0;
    434 	/*
    435 	 * Allocate a submap for physio
    436 	 */
    437 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    438 				 VM_PHYS_SIZE, 0, false, NULL);
    439 
    440 	/*
    441 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    442 	 * are allocated via the pool allocator, and we use direct-mapped
    443 	 * pool pages.
    444 	 */
    445 
    446 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvm_availmem(false)));
    447 	printf("avail memory = %s\n", pbuf);
    448 
    449 #if defined(PPC_IBM440) && defined(BLKCPY_SELFTEST)
    450 	ibm4xx_blkcpy_selftest();
    451 #endif
    452 }
    453 
    454 /*
    455  * ibm4xx_dumpsys:
    456  * Crash dump handling.
    457  */
    458 void
    459 ibm4xx_dumpsys(void)
    460 {
    461 
    462 	printf("dumpsys: TBD\n");
    463 }
    464