Home | History | Annotate | Line # | Download | only in walnut
machdep.c revision 1.31
      1 /*	$NetBSD: machdep.c,v 1.31 2006/11/29 19:56:47 freza 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.31 2006/11/29 19:56:47 freza 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 #define TLB_PG_SIZE 	(16*1024*1024)
    120 
    121 /*
    122  * Global variables used here and there
    123  */
    124 struct vm_map *exec_map = NULL;
    125 struct vm_map *mb_map = NULL;
    126 struct vm_map *phys_map = NULL;
    127 
    128 /*
    129  * This should probably be in autoconf!				XXX
    130  */
    131 char cpu_model[80];
    132 char machine[] = MACHINE;		/* from <machine/param.h> */
    133 char machine_arch[] = MACHINE_ARCH;	/* from <machine/param.h> */
    134 
    135 extern struct user *proc0paddr;
    136 
    137 char bootpath[256];
    138 paddr_t msgbuf_paddr;
    139 vaddr_t msgbuf_vaddr;
    140 
    141 #if NKSYMS || defined(DDB) || defined(LKM)
    142 void *startsym, *endsym;
    143 #endif
    144 
    145 int lcsplx(int);
    146 void initppc(u_int, u_int, char *, void *);
    147 
    148 static void dumpsys(void);
    149 static void install_extint(void (*)(void));
    150 
    151 #define MEMREGIONS	8
    152 struct mem_region physmemr[MEMREGIONS];		/* Hard code memory */
    153 struct mem_region availmemr[MEMREGIONS];	/* Who's supposed to set these up? */
    154 
    155 struct board_cfg_data board_data;
    156 
    157 void
    158 initppc(u_int startkernel, u_int endkernel, char *args, void *info_block)
    159 {
    160 	extern int defaulttrap, defaultsize;
    161 	extern int sctrap, scsize;
    162 	extern int alitrap, alisize;
    163 	extern int dsitrap, dsisize;
    164 	extern int isitrap, isisize;
    165 	extern int mchktrap, mchksize;
    166 	extern int tlbimiss4xx, tlbim4size;
    167 	extern int tlbdmiss4xx, tlbdm4size;
    168 	extern int pitfitwdog, pitfitwdogsize;
    169 	extern int debugtrap, debugsize;
    170 	extern int errata51handler, errata51size;
    171 #ifdef DDB
    172 	extern int ddblow, ddbsize;
    173 #endif
    174 #ifdef IPKDB
    175 	extern int ipkdblow, ipkdbsize;
    176 #endif
    177 	vaddr_t va;
    178 	int exc, dbcr0;
    179 	struct cpu_info * const ci = curcpu();
    180 
    181 	/* Disable all external interrupts */
    182 	mtdcr(DCR_UIC0_ER, 0);
    183 
    184         /* Initialize cache info for memcpy, etc. */
    185         cpu_probe_cache();
    186 
    187 	/* Save info block */
    188 	memcpy(&board_data, info_block, sizeof(board_data));
    189 
    190 	memset(physmemr, 0, sizeof physmemr);
    191 	memset(availmemr, 0, sizeof availmemr);
    192 	physmemr[0].start = 0;
    193 	physmemr[0].size = board_data.mem_size & ~PGOFSET;
    194 	/* Lower memory reserved by eval board BIOS */
    195 	availmemr[0].start = startkernel;
    196 	availmemr[0].size = board_data.mem_size - availmemr[0].start;
    197 
    198 	/* Linear map kernel memory */
    199 	for (va = 0; va < endkernel; va += TLB_PG_SIZE)
    200 		ppc4xx_tlb_reserve(va, va, TLB_PG_SIZE, TLB_EX);
    201 
    202 	/* Map console after physmem (see pmap_tlbmiss()) */
    203 	ppc4xx_tlb_reserve(0xef000000, roundup(physmemr[0].size, TLB_PG_SIZE),
    204 	    TLB_PG_SIZE, TLB_I | TLB_G);
    205 
    206 	/*
    207 	 * Initialize lwp0 and current pcb and pmap pointers.
    208 	 */
    209 	lwp0.l_cpu = ci;
    210 	lwp0.l_addr = proc0paddr;
    211 	memset(lwp0.l_addr, 0, sizeof *lwp0.l_addr);
    212 
    213 	curpcb = &proc0paddr->u_pcb;
    214 	curpcb->pcb_pm = pmap_kernel();
    215 
    216 	/*
    217 	 * Set up trap vectors
    218 	 */
    219 	for (exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100)
    220 		switch (exc) {
    221 		default:
    222 			memcpy((void *)exc, &defaulttrap, (size_t)&defaultsize);
    223 			break;
    224 		case EXC_EXI:
    225 			/*
    226 			 * This one is (potentially) installed during autoconf
    227 			 */
    228 			break;
    229 		case EXC_SC:
    230 			memcpy((void *)EXC_SC, &sctrap, (size_t)&scsize);
    231 			break;
    232 		case EXC_ALI:
    233 			memcpy((void *)EXC_ALI, &alitrap, (size_t)&alisize);
    234 			break;
    235 		case EXC_DSI:
    236 			memcpy((void *)EXC_DSI, &dsitrap, (size_t)&dsisize);
    237 			break;
    238 		case EXC_ISI:
    239 			memcpy((void *)EXC_ISI, &isitrap, (size_t)&isisize);
    240 			break;
    241 		case EXC_MCHK:
    242 			memcpy((void *)EXC_MCHK, &mchktrap, (size_t)&mchksize);
    243 			break;
    244 		case EXC_ITMISS:
    245 			memcpy((void *)EXC_ITMISS, &tlbimiss4xx,
    246 				(size_t)&tlbim4size);
    247 			break;
    248 		case EXC_DTMISS:
    249 			memcpy((void *)EXC_DTMISS, &tlbdmiss4xx,
    250 				(size_t)&tlbdm4size);
    251 			break;
    252 		/*
    253 		 * EXC_PIT, EXC_FIT, EXC_WDOG handlers
    254 		 * are spaced by 0x10 bytes only..
    255 		 */
    256 		case EXC_PIT:
    257 			memcpy((void *)EXC_PIT, &pitfitwdog,
    258 				(size_t)&pitfitwdogsize);
    259 			break;
    260 		case EXC_DEBUG:
    261 			memcpy((void *)EXC_DEBUG, &debugtrap,
    262 				(size_t)&debugsize);
    263 			break;
    264 		case EXC_DTMISS|EXC_ALI:
    265                         /* PPC405GP Rev D errata item 51 */
    266 			memcpy((void *)(EXC_DTMISS|EXC_ALI), &errata51handler,
    267 				(size_t)&errata51size);
    268 			break;
    269 #if defined(DDB) || defined(IPKDB)
    270 		case EXC_PGM:
    271 #if defined(DDB)
    272 			memcpy((void *)exc, &ddblow, (size_t)&ddbsize);
    273 #elif defined(IPKDB)
    274 			memcpy((void *)exc, &ipkdblow, (size_t)&ipkdbsize);
    275 #endif
    276 #endif /* DDB | IPKDB */
    277 			break;
    278 		}
    279 
    280 	__syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
    281 	mtspr(SPR_EVPR, 0);		/* Set Exception vector base */
    282 
    283 	consinit();
    284 
    285 	/* Handle trap instruction as PGM exception */
    286 	__asm volatile("mfspr %0,%1":"=r"(dbcr0):"K"(SPR_DBCR0));
    287 	__asm volatile("mtspr %0,%1"::"K"(SPR_DBCR0),"r"(dbcr0 & ~DBCR0_TDE));
    288 
    289 	/*
    290 	 * external interrupt handler install
    291 	 */
    292 	install_extint(ext_intr);
    293 
    294 	/*
    295 	 * Now enable translation (and machine checks/recoverable interrupts).
    296 	 */
    297 	__asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
    298 		      : : "r"(0), "K"(PSL_IR|PSL_DR));
    299 	/* XXXX PSL_ME - With ME set kernel gets stuck... */
    300 
    301 	uvm_setpagesize();
    302 
    303 	/*
    304 	 * Initialize pmap module.
    305 	 */
    306 	pmap_bootstrap(startkernel, endkernel);
    307 
    308 #ifdef DEBUG
    309 	printf("Board config data:\n");
    310 	printf("  usr_config_ver = %s\n", board_data.usr_config_ver);
    311 	printf("  rom_sw_ver = %s\n", board_data.rom_sw_ver);
    312 	printf("  mem_size = %u\n", board_data.mem_size);
    313 	printf("  mac_address_local = %02x:%02x:%02x:%02x:%02x:%02x\n",
    314 	    board_data.mac_address_local[0], board_data.mac_address_local[1],
    315 	    board_data.mac_address_local[2], board_data.mac_address_local[3],
    316 	    board_data.mac_address_local[4], board_data.mac_address_local[5]);
    317 	printf("  mac_address_pci = %02x:%02x:%02x:%02x:%02x:%02x\n",
    318 	    board_data.mac_address_pci[0], board_data.mac_address_pci[1],
    319 	    board_data.mac_address_pci[2], board_data.mac_address_pci[3],
    320 	    board_data.mac_address_pci[4], board_data.mac_address_pci[5]);
    321 	printf("  processor_speed = %u\n", board_data.processor_speed);
    322 	printf("  plb_speed = %u\n", board_data.plb_speed);
    323 	printf("  pci_speed = %u\n", board_data.pci_speed);
    324 #endif
    325 
    326 #if NKSYMS || defined(DDB) || defined(LKM)
    327 	ksyms_init((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
    328 #endif
    329 #ifdef DDB
    330 	if (boothowto & RB_KDB)
    331 		Debugger();
    332 #endif
    333 #ifdef IPKDB
    334 	/*
    335 	 * Now trap to IPKDB
    336 	 */
    337 	ipkdb_init();
    338 	if (boothowto & RB_KDB)
    339 		ipkdb_connect(0);
    340 #endif
    341 }
    342 
    343 static void
    344 install_extint(void (*handler)(void))
    345 {
    346 	extern int extint, extsize;
    347 	extern u_long extint_call;
    348 	u_long offset = (u_long)handler - (u_long)&extint_call;
    349 	int msr;
    350 
    351 #ifdef	DIAGNOSTIC
    352 	if (offset > 0x1ffffff)
    353 		panic("install_extint: too far away");
    354 #endif
    355 	__asm volatile ("mfmsr %0; wrteei 0" : "=r"(msr));
    356 	extint_call = (extint_call & 0xfc000003) | offset;
    357 	memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
    358 	__syncicache((void *)&extint_call, sizeof extint_call);
    359 	__syncicache((void *)EXC_EXI, (int)&extsize);
    360 	__asm volatile ("mtmsr %0" :: "r"(msr));
    361 }
    362 
    363 /*
    364  * Machine dependent startup code.
    365  */
    366 
    367 char msgbuf[MSGBUFSIZE];
    368 
    369 void
    370 cpu_startup(void)
    371 {
    372 	vaddr_t minaddr, maxaddr;
    373 	prop_number_t pn;
    374 	prop_data_t pd;
    375 	char pbuf[9];
    376 
    377 	/*
    378 	 * Initialize error message buffer (at end of core).
    379 	 */
    380 #if 0	/* For some reason this fails... --Artem
    381 	 * Besides, do we really have to put it at the end of core?
    382 	 * Let's use static buffer for now
    383 	 */
    384 	if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE), 0,
    385 	    UVM_KMF_VAONLY)))
    386 		panic("startup: no room for message buffer");
    387 	for (i = 0; i < btoc(MSGBUFSIZE); i++)
    388 		pmap_kenter_pa(msgbuf_vaddr + i * PAGE_SIZE,
    389 		    msgbuf_paddr + i * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE);
    390 	initmsgbuf((caddr_t)msgbuf_vaddr, round_page(MSGBUFSIZE));
    391 #else
    392 	initmsgbuf((caddr_t)msgbuf, round_page(MSGBUFSIZE));
    393 #endif
    394 
    395 	printf("%s%s", copyright, version);
    396 	printf("Walnut PowerPC 405GP Evaluation Board\n");
    397 
    398 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    399 	printf("total memory = %s\n", pbuf);
    400 
    401 	minaddr = 0;
    402 	/*
    403 	 * Allocate a submap for exec arguments.  This map effectively
    404 	 * limits the number of processes exec'ing at any time.
    405 	 */
    406 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    407 				 16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
    408 
    409 	/*
    410 	 * Allocate a submap for physio
    411 	 */
    412 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    413 				 VM_PHYS_SIZE, 0, FALSE, NULL);
    414 
    415 	/*
    416 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    417 	 * are allocated via the pool allocator, and we use direct-mapped
    418 	 * pool pages.
    419 	 */
    420 
    421 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    422 	printf("avail memory = %s\n", pbuf);
    423 
    424 	/*
    425 	 * Set up the board properties dictionary.
    426 	 */
    427 	board_properties = prop_dictionary_create();
    428 	KASSERT(board_properties != NULL);
    429 
    430 	pn = prop_number_create_integer(board_data.mem_size);
    431 	KASSERT(pn != NULL);
    432 	if (prop_dictionary_set(board_properties, "mem-size", pn) == FALSE)
    433 		panic("setting mem-size");
    434 	prop_object_release(pn);
    435 
    436 	pd = prop_data_create_data_nocopy(board_data.mac_address_local,
    437 					  sizeof(board_data.mac_address_local));
    438 	KASSERT(pd != NULL);
    439 	if (prop_dictionary_set(board_properties, "emac0-mac-addr",
    440 				pd) == FALSE)
    441 		panic("setting emac0-mac-addr");
    442 	prop_object_release(pd);
    443 
    444 	pd = prop_data_create_data_nocopy(board_data.mac_address_pci,
    445 					  sizeof(board_data.mac_address_pci));
    446 	KASSERT(pd != NULL);
    447 	if (prop_dictionary_set(board_properties, "sip0-mac-addr",
    448 				pd) == FALSE)
    449 		panic("setting sip0-mac-addr");
    450 	prop_object_release(pd);
    451 
    452 	pn = prop_number_create_integer(board_data.processor_speed);
    453 	KASSERT(pn != NULL);
    454 	if (prop_dictionary_set(board_properties, "processor-frequency",
    455 				pn) == FALSE)
    456 		panic("setting processor-frequency");
    457 	prop_object_release(pn);
    458 
    459 	/*
    460 	 * Now that we have VM, malloc()s are OK in bus_space.
    461 	 */
    462 	bus_space_mallocok();
    463 	fake_mapiodev = 0;
    464 }
    465 
    466 
    467 static void
    468 dumpsys(void)
    469 {
    470 
    471 	printf("dumpsys: TBD\n");
    472 }
    473 
    474 /*
    475  * Halt or reboot the machine after syncing/dumping according to howto.
    476  */
    477 void
    478 cpu_reboot(int howto, char *what)
    479 {
    480 	static int syncing;
    481 	static char str[256];
    482 	char *ap = str, *ap1 = ap;
    483 
    484 	boothowto = howto;
    485 	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
    486 		syncing = 1;
    487 		vfs_shutdown();		/* sync */
    488 		resettodr();		/* set wall clock */
    489 	}
    490 
    491 	splhigh();
    492 
    493 	if (!cold && (howto & RB_DUMP))
    494 		dumpsys();
    495 
    496 	doshutdownhooks();
    497 
    498 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    499 	  /* Power off here if we know how...*/
    500 	}
    501 
    502 	if (howto & RB_HALT) {
    503 		printf("halted\n\n");
    504 
    505 		goto reboot;	/* XXX for now... */
    506 
    507 #ifdef DDB
    508 		printf("dropping to debugger\n");
    509 		while(1)
    510 			Debugger();
    511 #endif
    512 	}
    513 
    514 	printf("rebooting\n\n");
    515 	if (what && *what) {
    516 		if (strlen(what) > sizeof str - 5)
    517 			printf("boot string too large, ignored\n");
    518 		else {
    519 			strcpy(str, what);
    520 			ap1 = ap = str + strlen(str);
    521 			*ap++ = ' ';
    522 		}
    523 	}
    524 	*ap++ = '-';
    525 	if (howto & RB_SINGLE)
    526 		*ap++ = 's';
    527 	if (howto & RB_KDB)
    528 		*ap++ = 'd';
    529 	*ap++ = 0;
    530 	if (ap[-2] == '-')
    531 		*ap1 = 0;
    532 
    533 	/* flush cache for msgbuf */
    534 	__syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
    535 
    536  reboot:
    537 	ppc4xx_reset();
    538 
    539 	printf("ppc4xx_reset() failed!\n");
    540 #ifdef DDB
    541 	while(1)
    542 		Debugger();
    543 #else
    544 	while (1)
    545 		/* nothing */;
    546 #endif
    547 }
    548 
    549 int
    550 lcsplx(int ipl)
    551 {
    552 
    553 	return spllower(ipl); 	/* XXX */
    554 }
    555 
    556 void
    557 mem_regions(struct mem_region **mem, struct mem_region **avail)
    558 {
    559 
    560 	*mem = physmemr;
    561 	*avail = availmemr;
    562 }
    563