Home | History | Annotate | Line # | Download | only in ibm4xx
ibm4xx_machdep.c revision 1.30
      1 /*	$NetBSD: ibm4xx_machdep.c,v 1.30 2020/07/06 09:34:16 rin 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.30 2020/07/06 09:34:16 rin Exp $");
     72 
     73 #include "ksyms.h"
     74 
     75 #ifdef _KERNEL_OPT
     76 #include "opt_compat_netbsd.h"
     77 #include "opt_ddb.h"
     78 #include "opt_kgdb.h"
     79 #include "opt_modular.h"
     80 #endif
     81 
     82 #include <sys/param.h>
     83 #include <sys/msgbuf.h>
     84 #include <sys/proc.h>
     85 #include <sys/cpu.h>
     86 #include <sys/ksyms.h>
     87 
     88 #include <uvm/uvm_extern.h>
     89 
     90 #if defined(DDB)
     91 #include <machine/db_machdep.h>
     92 #include <ddb/db_extern.h>
     93 #endif
     94 
     95 #if defined(KGDB)
     96 #include <sys/kgdb.h>
     97 #endif
     98 
     99 #include <machine/powerpc.h>
    100 #include <powerpc/pcb.h>
    101 #include <machine/trap.h>
    102 
    103 #include <powerpc/spr.h>
    104 #include <powerpc/ibm4xx/spr.h>
    105 
    106 #include <powerpc/ibm4xx/cpu.h>
    107 
    108 /*
    109  * Global variables used here and there
    110  */
    111 paddr_t msgbuf_paddr;
    112 vaddr_t msgbuf_vaddr;
    113 char msgbuf[MSGBUFSIZE];
    114 
    115 #if NKSYMS || defined(DDB) || defined(MODULAR)
    116 void *startsym, *endsym;
    117 #endif
    118 
    119 /*
    120  * Trap vectors
    121  */
    122 extern const uint32_t defaulttrap[], defaultsize;
    123 extern const uint32_t sctrap[], scsize;
    124 extern const uint32_t accesstrap[], accesssize;
    125 extern const uint32_t criticaltrap[], criticalsize;
    126 extern const uint32_t tlbimiss4xx[], tlbim4size;
    127 extern const uint32_t tlbdmiss4xx[], tlbdm4size;
    128 extern const uint32_t pitfitwdog[], pitfitwdogsize;
    129 extern const uint32_t errata51handler[], errata51size;
    130 #if defined(DDB)
    131 extern const uint32_t ddblow[], ddbsize;
    132 #endif
    133 static const struct exc_info trap_table[] = {
    134 	{ EXC_SC,	sctrap,		(uintptr_t)&scsize },
    135 	{ EXC_ALI,	accesstrap,	(uintptr_t)&accesssize },
    136 	{ EXC_DSI,	accesstrap,	(uintptr_t)&accesssize },
    137 	{ EXC_MCHK,	criticaltrap,	(uintptr_t)&criticalsize },
    138 	{ EXC_ITMISS,	tlbimiss4xx,	(uintptr_t)&tlbim4size },
    139 	{ EXC_DTMISS,	tlbdmiss4xx,	(uintptr_t)&tlbdm4size },
    140 	{ EXC_PIT,	pitfitwdog,	(uintptr_t)&pitfitwdogsize },
    141 	{ EXC_DEBUG,	criticaltrap,	(uintptr_t)&criticalsize },
    142 	{ (EXC_DTMISS|EXC_ALI),
    143 			errata51handler, (uintptr_t)&errata51size },
    144 #if defined(DDB)
    145 	{ EXC_PGM,	ddblow,		(uintptr_t)&ddbsize },
    146 #endif
    147 };
    148 
    149 /*
    150  * Install a trap vector. We cannot use memcpy because the
    151  * destination may be zero.
    152  */
    153 static void
    154 trap_copy(const uint32_t *src, vaddr_t dest, size_t len)
    155 {
    156 	uint32_t *dest_p = (void *)dest;
    157 
    158 	while (len > 0) {
    159 		*dest_p++ = *src++;
    160 		len -= sizeof(uint32_t);
    161 	}
    162 }
    163 
    164 /*
    165  * ibm4xx_init:
    166  */
    167 void
    168 ibm4xx_init(vaddr_t startkernel, vaddr_t endkernel, void (*handler)(void))
    169 {
    170 	/* Initialize cache info for memcpy, etc. */
    171 	cpu_probe_cache();
    172 
    173 	/*
    174 	 * Initialize current pcb and pmap pointers.
    175 	 */
    176 	KASSERT(curcpu() == &cpu_info[0]);
    177 	KASSERT(lwp0.l_cpu == curcpu());
    178 	KASSERT(curlwp == &lwp0);
    179 
    180 	curpcb = lwp_getpcb(curlwp);
    181 	memset(curpcb, 0, sizeof(struct pcb));
    182 
    183 	curpcb->pcb_pm = pmap_kernel();
    184 
    185 	for (uintptr_t exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100) {
    186 		trap_copy(defaulttrap, exc, (uintptr_t)&defaultsize);
    187 	}
    188 
    189 	for (size_t i = 0; i < __arraycount(trap_table); i++) {
    190 		KASSERT(trap_table[i].exc_size <= 0x100);
    191 		trap_copy(trap_table[i].exc_addr, trap_table[i].exc_vector,
    192 		    trap_table[i].exc_size);
    193 	}
    194 
    195 	__syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
    196 
    197 	mtspr(SPR_EVPR, 0);		/* Set Exception vector base */
    198 
    199 	/* Handle trap instruction as PGM exception */
    200 	mtspr(SPR_DBCR0, mfspr(SPR_DBCR0) & ~DBCR0_TDE);
    201 
    202 	/*
    203 	 * external interrupt handler install
    204 	 */
    205 	if (handler)
    206 	    ibm4xx_install_extint(handler);
    207 
    208 	/*
    209 	 * Now enable translation (and machine checks/recoverable interrupts).
    210 	 */
    211 	__asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
    212 		      : : "r"(0), "K"(PSL_IR|PSL_DR));
    213 	/* XXXX PSL_ME - With ME set kernel gets stuck... */
    214 
    215 	/*
    216 	 * turn on console after enable translation
    217 	 */
    218 	consinit();
    219 
    220 	uvm_md_init();
    221 
    222 	/*
    223 	 * Initialize pmap module.
    224 	 */
    225 	pmap_bootstrap(startkernel, endkernel);
    226 
    227 	/*
    228 	 * Let's take all the indirect calls via our stubs and patch
    229 	 * them to be direct calls.
    230 	 */
    231 	cpu_fixup_stubs();
    232 
    233 #if NKSYMS || defined(DDB) || defined(MODULAR)
    234 	ksyms_addsyms_elf((uintptr_t)endsym - (uintptr_t)startsym,
    235 	    startsym, endsym);
    236 #endif
    237 }
    238 
    239 void
    240 ibm4xx_install_extint(void (*handler)(void))
    241 {
    242 	extern int extint, extsize;
    243 	extern u_long extint_call;
    244 	u_long offset = (u_long)handler - (u_long)&extint_call;
    245 	int msr;
    246 
    247 #ifdef	DIAGNOSTIC
    248 	if (offset > 0x1ffffff)
    249 		panic("install_extint: too far away");
    250 #endif
    251 	__asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr));
    252 	extint_call = (extint_call & 0xfc000003) | offset;
    253 	memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
    254 	__syncicache((void *)&extint_call, sizeof extint_call);
    255 	__syncicache((void *)EXC_EXI, (int)&extsize);
    256 	__asm volatile ("mtmsr %0" :: "r"(msr));
    257 }
    258 
    259 /*
    260  * ibm4xx_cpu_startup:
    261  * Machine dependent startup code.
    262  */
    263 void
    264 ibm4xx_cpu_startup(const char *model)
    265 {
    266 	vaddr_t minaddr, maxaddr;
    267 	char pbuf[9];
    268 
    269 	KASSERT(curcpu() != NULL);
    270 	KASSERT(lwp0.l_cpu != NULL);
    271 	KASSERT(curcpu()->ci_intstk != 0);
    272 	KASSERT(curcpu()->ci_idepth == -1);
    273 
    274 	/*
    275 	 * Initialize error message buffer (at end of core).
    276 	 */
    277 #if 0	/* For some reason this fails... --Artem
    278 	 * Besides, do we really have to put it at the end of core?
    279 	 * Let's use static buffer for now
    280 	 */
    281 	if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE),
    282 	    0, UVM_KMF_VAONLY)))
    283 		panic("startup: no room for message buffer");
    284 	for (i = 0; i < btoc(MSGBUFSIZE); i++)
    285 		pmap_kenter_pa(msgbuf_vaddr + i * PAGE_SIZE,
    286 		    msgbuf_paddr + i * PAGE_SIZE,
    287 		    VM_PROT_READ|VM_PROT_WRITE, 0);
    288 	initmsgbuf((void *)msgbuf_vaddr, round_page(MSGBUFSIZE));
    289 #else
    290 	initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
    291 #endif
    292 
    293 	printf("%s%s", copyright, version);
    294 	if (model != NULL)
    295 		printf("Model: %s\n", model);
    296 
    297 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    298 	printf("total memory = %s\n", pbuf);
    299 
    300 	minaddr = 0;
    301 	/*
    302 	 * Allocate a submap for physio
    303 	 */
    304 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    305 				 VM_PHYS_SIZE, 0, false, NULL);
    306 
    307 	/*
    308 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    309 	 * are allocated via the pool allocator, and we use direct-mapped
    310 	 * pool pages.
    311 	 */
    312 
    313 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvm_availmem(false)));
    314 	printf("avail memory = %s\n", pbuf);
    315 }
    316 
    317 /*
    318  * ibm4xx_dumpsys:
    319  * Crash dump handling.
    320  */
    321 void
    322 ibm4xx_dumpsys(void)
    323 {
    324 
    325 	printf("dumpsys: TBD\n");
    326 }
    327