Home | History | Annotate | Line # | Download | only in walnut
machdep.c revision 1.27
      1 /*	$NetBSD: machdep.c,v 1.27 2006/05/05 18:04:41 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright 2001, 2002 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
     40  * Copyright (C) 1995, 1996 TooLs GmbH.
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by TooLs GmbH.
     54  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     55  *    derived from this software without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     60  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     61  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     62  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     63  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     64  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     65  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     66  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.27 2006/05/05 18:04:41 thorpej Exp $");
     71 
     72 #include "opt_compat_netbsd.h"
     73 #include "opt_ddb.h"
     74 #include "opt_ipkdb.h"
     75 
     76 #include <sys/param.h>
     77 #include <sys/buf.h>
     78 #include <sys/exec.h>
     79 #include <sys/malloc.h>
     80 #include <sys/mbuf.h>
     81 #include <sys/mount.h>
     82 #include <sys/msgbuf.h>
     83 #include <sys/proc.h>
     84 #include <sys/reboot.h>
     85 #include <sys/sa.h>
     86 #include <sys/syscallargs.h>
     87 #include <sys/syslog.h>
     88 #include <sys/systm.h>
     89 #include <sys/kernel.h>
     90 #include <sys/user.h>
     91 #include <sys/boot_flag.h>
     92 #include <sys/ksyms.h>
     93 
     94 #include <uvm/uvm_extern.h>
     95 
     96 #include <net/netisr.h>
     97 
     98 #include <prop/proplib.h>
     99 
    100 #include <machine/bus.h>
    101 #include <machine/powerpc.h>
    102 #include <machine/trap.h>
    103 #include <machine/walnut.h>
    104 
    105 #include <powerpc/spr.h>
    106 #include <powerpc/ibm4xx/dcr405gp.h>
    107 #include <machine/bus.h>
    108 
    109 #include <dev/cons.h>
    110 
    111 #include "ksyms.h"
    112 
    113 #if defined(DDB)
    114 #include <machine/db_machdep.h>
    115 #include <ddb/db_extern.h>
    116 #endif
    117 
    118 /*
    119  * Global variables used here and there
    120  */
    121 struct vm_map *exec_map = NULL;
    122 struct vm_map *mb_map = NULL;
    123 struct vm_map *phys_map = NULL;
    124 
    125 /*
    126  * This should probably be in autoconf!				XXX
    127  */
    128 char cpu_model[80];
    129 char machine[] = MACHINE;		/* from <machine/param.h> */
    130 char machine_arch[] = MACHINE_ARCH;	/* from <machine/param.h> */
    131 
    132 extern struct user *proc0paddr;
    133 
    134 char bootpath[256];
    135 paddr_t msgbuf_paddr;
    136 vaddr_t msgbuf_vaddr;
    137 
    138 #if NKSYMS || defined(DDB) || defined(LKM)
    139 void *startsym, *endsym;
    140 #endif
    141 
    142 int lcsplx(int);
    143 void initppc(u_int, u_int, char *, void *);
    144 
    145 static void dumpsys(void);
    146 static void install_extint(void (*)(void));
    147 
    148 #define MEMREGIONS	8
    149 struct mem_region physmemr[MEMREGIONS];		/* Hard code memory */
    150 struct mem_region availmemr[MEMREGIONS];	/* Who's supposed to set these up? */
    151 
    152 struct board_cfg_data board_data;
    153 
    154 void
    155 initppc(u_int startkernel, u_int endkernel, char *args, void *info_block)
    156 {
    157 	extern int defaulttrap, defaultsize;
    158 	extern int sctrap, scsize;
    159 	extern int alitrap, alisize;
    160 	extern int dsitrap, dsisize;
    161 	extern int isitrap, isisize;
    162 	extern int mchktrap, mchksize;
    163 	extern int tlbimiss4xx, tlbim4size;
    164 	extern int tlbdmiss4xx, tlbdm4size;
    165 	extern int pitfitwdog, pitfitwdogsize;
    166 	extern int debugtrap, debugsize;
    167 	extern int errata51handler, errata51size;
    168 #ifdef DDB
    169 	extern int ddblow, ddbsize;
    170 #endif
    171 #ifdef IPKDB
    172 	extern int ipkdblow, ipkdbsize;
    173 #endif
    174 	int exc, dbcr0;
    175 	struct cpu_info * const ci = curcpu();
    176 
    177 	/* Disable all external interrupts */
    178 	mtdcr(DCR_UIC0_ER, 0);
    179 
    180         /* Initialize cache info for memcpy, etc. */
    181         cpu_probe_cache();
    182 
    183 	/* Save info block */
    184 	memcpy(&board_data, info_block, sizeof(board_data));
    185 
    186 	memset(physmemr, 0, sizeof physmemr);
    187 	memset(availmemr, 0, sizeof availmemr);
    188 	physmemr[0].start = 0;
    189 	physmemr[0].size = board_data.mem_size & ~PGOFSET;
    190 	/* Lower memory reserved by eval board BIOS */
    191 	availmemr[0].start = startkernel;
    192 	availmemr[0].size = board_data.mem_size - availmemr[0].start;
    193 
    194 	/*
    195 	 * Initialize lwp0 and current pcb and pmap pointers.
    196 	 */
    197 	lwp0.l_cpu = ci;
    198 	lwp0.l_addr = proc0paddr;
    199 	memset(lwp0.l_addr, 0, sizeof *lwp0.l_addr);
    200 
    201 	curpcb = &proc0paddr->u_pcb;
    202 	curpcb->pcb_pm = pmap_kernel();
    203 
    204 	/*
    205 	 * Set up trap vectors
    206 	 */
    207 	for (exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100)
    208 		switch (exc) {
    209 		default:
    210 			memcpy((void *)exc, &defaulttrap, (size_t)&defaultsize);
    211 			break;
    212 		case EXC_EXI:
    213 			/*
    214 			 * This one is (potentially) installed during autoconf
    215 			 */
    216 			break;
    217 		case EXC_SC:
    218 			memcpy((void *)EXC_SC, &sctrap, (size_t)&scsize);
    219 			break;
    220 		case EXC_ALI:
    221 			memcpy((void *)EXC_ALI, &alitrap, (size_t)&alisize);
    222 			break;
    223 		case EXC_DSI:
    224 			memcpy((void *)EXC_DSI, &dsitrap, (size_t)&dsisize);
    225 			break;
    226 		case EXC_ISI:
    227 			memcpy((void *)EXC_ISI, &isitrap, (size_t)&isisize);
    228 			break;
    229 		case EXC_MCHK:
    230 			memcpy((void *)EXC_MCHK, &mchktrap, (size_t)&mchksize);
    231 			break;
    232 		case EXC_ITMISS:
    233 			memcpy((void *)EXC_ITMISS, &tlbimiss4xx,
    234 				(size_t)&tlbim4size);
    235 			break;
    236 		case EXC_DTMISS:
    237 			memcpy((void *)EXC_DTMISS, &tlbdmiss4xx,
    238 				(size_t)&tlbdm4size);
    239 			break;
    240 		/*
    241 		 * EXC_PIT, EXC_FIT, EXC_WDOG handlers
    242 		 * are spaced by 0x10 bytes only..
    243 		 */
    244 		case EXC_PIT:
    245 			memcpy((void *)EXC_PIT, &pitfitwdog,
    246 				(size_t)&pitfitwdogsize);
    247 			break;
    248 		case EXC_DEBUG:
    249 			memcpy((void *)EXC_DEBUG, &debugtrap,
    250 				(size_t)&debugsize);
    251 			break;
    252 		case EXC_DTMISS|EXC_ALI:
    253                         /* PPC405GP Rev D errata item 51 */
    254 			memcpy((void *)(EXC_DTMISS|EXC_ALI), &errata51handler,
    255 				(size_t)&errata51size);
    256 			break;
    257 #if defined(DDB) || defined(IPKDB)
    258 		case EXC_PGM:
    259 #if defined(DDB)
    260 			memcpy((void *)exc, &ddblow, (size_t)&ddbsize);
    261 #elif defined(IPKDB)
    262 			memcpy((void *)exc, &ipkdblow, (size_t)&ipkdbsize);
    263 #endif
    264 #endif /* DDB | IPKDB */
    265 			break;
    266 		}
    267 
    268 	__syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
    269 	mtspr(SPR_EVPR, 0);		/* Set Exception vector base */
    270 	consinit();
    271 
    272 	/* Handle trap instruction as PGM exception */
    273 	__asm volatile("mfspr %0,%1":"=r"(dbcr0):"K"(SPR_DBCR0));
    274 	__asm volatile("mtspr %0,%1"::"K"(SPR_DBCR0),"r"(dbcr0 & ~DBCR0_TDE));
    275 
    276 	/*
    277 	 * external interrupt handler install
    278 	 */
    279 	install_extint(ext_intr);
    280 
    281 	/*
    282 	 * Now enable translation (and machine checks/recoverable interrupts).
    283 	 */
    284 	__asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
    285 		      : : "r"(0), "K"(PSL_IR|PSL_DR));
    286 	/* XXXX PSL_ME - With ME set kernel gets stuck... */
    287 
    288 	uvm_setpagesize();
    289 
    290 	/*
    291 	 * Initialize pmap module.
    292 	 */
    293 	pmap_bootstrap(startkernel, endkernel);
    294 
    295 #ifdef DEBUG
    296 	printf("Board config data:\n");
    297 	printf("  usr_config_ver = %s\n", board_data.usr_config_ver);
    298 	printf("  rom_sw_ver = %s\n", board_data.rom_sw_ver);
    299 	printf("  mem_size = %u\n", board_data.mem_size);
    300 	printf("  mac_address_local = %02x:%02x:%02x:%02x:%02x:%02x\n",
    301 	    board_data.mac_address_local[0], board_data.mac_address_local[1],
    302 	    board_data.mac_address_local[2], board_data.mac_address_local[3],
    303 	    board_data.mac_address_local[4], board_data.mac_address_local[5]);
    304 	printf("  mac_address_pci = %02x:%02x:%02x:%02x:%02x:%02x\n",
    305 	    board_data.mac_address_pci[0], board_data.mac_address_pci[1],
    306 	    board_data.mac_address_pci[2], board_data.mac_address_pci[3],
    307 	    board_data.mac_address_pci[4], board_data.mac_address_pci[5]);
    308 	printf("  processor_speed = %u\n", board_data.processor_speed);
    309 	printf("  plb_speed = %u\n", board_data.plb_speed);
    310 	printf("  pci_speed = %u\n", board_data.pci_speed);
    311 #endif
    312 
    313 #if NKSYMS || defined(DDB) || defined(LKM)
    314 	ksyms_init((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
    315 #endif
    316 #ifdef DDB
    317 	if (boothowto & RB_KDB)
    318 		Debugger();
    319 #endif
    320 #ifdef IPKDB
    321 	/*
    322 	 * Now trap to IPKDB
    323 	 */
    324 	ipkdb_init();
    325 	if (boothowto & RB_KDB)
    326 		ipkdb_connect(0);
    327 #endif
    328 }
    329 
    330 static void
    331 install_extint(void (*handler)(void))
    332 {
    333 	extern int extint, extsize;
    334 	extern u_long extint_call;
    335 	u_long offset = (u_long)handler - (u_long)&extint_call;
    336 	int msr;
    337 
    338 #ifdef	DIAGNOSTIC
    339 	if (offset > 0x1ffffff)
    340 		panic("install_extint: too far away");
    341 #endif
    342 	__asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr));
    343 	extint_call = (extint_call & 0xfc000003) | offset;
    344 	memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
    345 	__syncicache((void *)&extint_call, sizeof extint_call);
    346 	__syncicache((void *)EXC_EXI, (int)&extsize);
    347 	__asm volatile ("mtmsr %0" :: "r"(msr));
    348 }
    349 
    350 /*
    351  * Machine dependent startup code.
    352  */
    353 
    354 char msgbuf[MSGBUFSIZE];
    355 
    356 void
    357 cpu_startup(void)
    358 {
    359 	vaddr_t minaddr, maxaddr;
    360 	prop_number_t pn;
    361 	prop_data_t pd;
    362 	char pbuf[9];
    363 
    364 	/*
    365 	 * Initialize error message buffer (at end of core).
    366 	 */
    367 #if 0	/* For some reason this fails... --Artem
    368 	 * Besides, do we really have to put it at the end of core?
    369 	 * Let's use static buffer for now
    370 	 */
    371 	if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE), 0,
    372 	    UVM_KMF_VAONLY)))
    373 		panic("startup: no room for message buffer");
    374 	for (i = 0; i < btoc(MSGBUFSIZE); i++)
    375 		pmap_kenter_pa(msgbuf_vaddr + i * PAGE_SIZE,
    376 		    msgbuf_paddr + i * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE);
    377 	initmsgbuf((caddr_t)msgbuf_vaddr, round_page(MSGBUFSIZE));
    378 #else
    379 	initmsgbuf((caddr_t)msgbuf, round_page(MSGBUFSIZE));
    380 #endif
    381 
    382 	printf("%s%s", copyright, version);
    383 	printf("Walnut PowerPC 405GP Evaluation Board\n");
    384 
    385 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    386 	printf("total memory = %s\n", pbuf);
    387 
    388 	minaddr = 0;
    389 	/*
    390 	 * Allocate a submap for exec arguments.  This map effectively
    391 	 * limits the number of processes exec'ing at any time.
    392 	 */
    393 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    394 				 16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
    395 
    396 	/*
    397 	 * Allocate a submap for physio
    398 	 */
    399 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    400 				 VM_PHYS_SIZE, 0, FALSE, NULL);
    401 
    402 	/*
    403 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    404 	 * are allocated via the pool allocator, and we use direct-mapped
    405 	 * pool pages.
    406 	 */
    407 
    408 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    409 	printf("avail memory = %s\n", pbuf);
    410 
    411 	/*
    412 	 * Set up the board properties dictionary.
    413 	 */
    414 	board_properties = prop_dictionary_create();
    415 	KASSERT(board_properties != NULL);
    416 
    417 	pn = prop_number_create_integer(board_data.mem_size);
    418 	KASSERT(pn != NULL);
    419 	if (prop_dictionary_set(board_properties, "mem-size", pn) == FALSE)
    420 		panic("setting mem-size");
    421 	prop_object_release(pn);
    422 
    423 	pd = prop_data_create_data_nocopy(board_data.mac_address_local,
    424 					  sizeof(board_data.mac_address_local));
    425 	KASSERT(pd != NULL);
    426 	if (prop_dictionary_set(board_properties, "emac0-mac-addr",
    427 				pd) == FALSE)
    428 		panic("setting emac0-mac-addr");
    429 	prop_object_release(pd);
    430 
    431 	pd = prop_data_create_data_nocopy(board_data.mac_address_pci,
    432 					  sizeof(board_data.mac_address_pci));
    433 	KASSERT(pd != NULL);
    434 	if (prop_dictionary_set(board_properties, "sip0-mac-addr",
    435 				pd) == FALSE)
    436 		panic("setting sip0-mac-addr");
    437 	prop_object_release(pd);
    438 
    439 	pn = prop_number_create_integer(board_data.processor_speed);
    440 	KASSERT(pn != NULL);
    441 	if (prop_dictionary_set(board_properties, "processor-frequency",
    442 				pn) == FALSE)
    443 		panic("setting processor-frequency");
    444 	prop_object_release(pn);
    445 
    446 	/*
    447 	 * Now that we have VM, malloc()s are OK in bus_space.
    448 	 */
    449 	bus_space_mallocok();
    450 	fake_mapiodev = 0;
    451 }
    452 
    453 
    454 static void
    455 dumpsys(void)
    456 {
    457 
    458 	printf("dumpsys: TBD\n");
    459 }
    460 
    461 /*
    462  * Soft networking interrupts.
    463  */
    464 void
    465 softnet(void)
    466 {
    467 	int isr;
    468 
    469 	isr = netisr;
    470 	netisr = 0;
    471 
    472 #define DONETISR(bit, fn) do {		\
    473 	if (isr & (1 << bit))		\
    474 		fn();			\
    475 } while (0)
    476 
    477 #include <net/netisr_dispatch.h>
    478 
    479 #undef DONETISR
    480 
    481 }
    482 
    483 /*
    484  * Soft tty interrupts.
    485  */
    486 #include "com.h"
    487 void
    488 softserial(void)
    489 {
    490 #if NCOM > 0
    491 	void comsoft(void);	/* XXX from dev/ic/com.c */
    492 
    493 	comsoft();
    494 #endif
    495 }
    496 
    497 /*
    498  * Halt or reboot the machine after syncing/dumping according to howto.
    499  */
    500 void
    501 cpu_reboot(int howto, char *what)
    502 {
    503 	static int syncing;
    504 	static char str[256];
    505 	char *ap = str, *ap1 = ap;
    506 
    507 	boothowto = howto;
    508 	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
    509 		syncing = 1;
    510 		vfs_shutdown();		/* sync */
    511 		resettodr();		/* set wall clock */
    512 	}
    513 
    514 	splhigh();
    515 
    516 	if (!cold && (howto & RB_DUMP))
    517 		dumpsys();
    518 
    519 	doshutdownhooks();
    520 
    521 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    522 	  /* Power off here if we know how...*/
    523 	}
    524 
    525 	if (howto & RB_HALT) {
    526 		printf("halted\n\n");
    527 
    528 		goto reboot;	/* XXX for now... */
    529 
    530 #ifdef DDB
    531 		printf("dropping to debugger\n");
    532 		while(1)
    533 			Debugger();
    534 #endif
    535 	}
    536 
    537 	printf("rebooting\n\n");
    538 	if (what && *what) {
    539 		if (strlen(what) > sizeof str - 5)
    540 			printf("boot string too large, ignored\n");
    541 		else {
    542 			strcpy(str, what);
    543 			ap1 = ap = str + strlen(str);
    544 			*ap++ = ' ';
    545 		}
    546 	}
    547 	*ap++ = '-';
    548 	if (howto & RB_SINGLE)
    549 		*ap++ = 's';
    550 	if (howto & RB_KDB)
    551 		*ap++ = 'd';
    552 	*ap++ = 0;
    553 	if (ap[-2] == '-')
    554 		*ap1 = 0;
    555 
    556 	/* flush cache for msgbuf */
    557 	__syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
    558 
    559  reboot:
    560 	ppc4xx_reset();
    561 
    562 	printf("ppc4xx_reset() failed!\n");
    563 #ifdef DDB
    564 	while(1)
    565 		Debugger();
    566 #else
    567 	while (1)
    568 		/* nothing */;
    569 #endif
    570 }
    571 
    572 int
    573 lcsplx(int ipl)
    574 {
    575 
    576 	return spllower(ipl); 	/* XXX */
    577 }
    578 
    579 void
    580 mem_regions(struct mem_region **mem, struct mem_region **avail)
    581 {
    582 
    583 	*mem = physmemr;
    584 	*avail = availmemr;
    585 }
    586