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