Home | History | Annotate | Line # | Download | only in virtex
machdep.c revision 1.5.24.1
      1 /*	$NetBSD: machdep.c,v 1.5.24.1 2008/07/03 18:37:53 simonb Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2006 Jachym Holecek
      5  * All rights reserved.
      6  *
      7  * Written for DFC Design, s.r.o.
      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  *
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  *
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Based on Walnut and Explora.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.5.24.1 2008/07/03 18:37:53 simonb Exp $");
     38 
     39 #include "opt_compat_netbsd.h"
     40 #include "opt_ddb.h"
     41 #include "opt_ipkdb.h"
     42 #include "opt_virtex.h"
     43 #include "opt_kgdb.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/buf.h>
     47 #include <sys/exec.h>
     48 #include <sys/malloc.h>
     49 #include <sys/mbuf.h>
     50 #include <sys/mount.h>
     51 #include <sys/msgbuf.h>
     52 #include <sys/proc.h>
     53 #include <sys/reboot.h>
     54 #include <sys/syscallargs.h>
     55 #include <sys/syslog.h>
     56 #include <sys/systm.h>
     57 #include <sys/kernel.h>
     58 #include <sys/user.h>
     59 #include <sys/boot_flag.h>
     60 #include <sys/ksyms.h>
     61 
     62 #include <uvm/uvm_extern.h>
     63 
     64 #include <net/netisr.h>
     65 
     66 #include <dev/cons.h>
     67 
     68 #include <machine/bus.h>
     69 #include <machine/powerpc.h>
     70 #include <machine/trap.h>
     71 
     72 #include <powerpc/spr.h>
     73 
     74 #include <evbppc/virtex/dcr.h>
     75 #include <evbppc/virtex/virtex.h>
     76 
     77 #include "ksyms.h"
     78 
     79 #if defined(DDB)
     80 #include <machine/db_machdep.h>
     81 #include <ddb/db_extern.h>
     82 #endif
     83 
     84 #if defined(KGDB)
     85 #include <sys/kgdb.h>
     86 #endif
     87 
     88 /*
     89  * Global variables used here and there
     90  */
     91 struct vm_map *mb_map = NULL;
     92 struct vm_map *phys_map = NULL;
     93 
     94 /*
     95  * This should probably be in autoconf!				XXX
     96  */
     97 char cpu_model[80];
     98 char machine[] = MACHINE;		/* from <machine/param.h> */
     99 char machine_arch[] = MACHINE_ARCH;	/* from <machine/param.h> */
    100 
    101 extern struct user *proc0paddr;
    102 
    103 char bootpath[256];
    104 paddr_t msgbuf_paddr;
    105 vaddr_t msgbuf_vaddr;
    106 
    107 #if NKSYMS || defined(DDB) || defined(LKM)
    108 void *startsym, *endsym;
    109 #endif
    110 
    111 int lcsplx(int);
    112 void initppc(u_int, u_int);
    113 
    114 static void dumpsys(void);
    115 static void install_extint(void (*)(void));
    116 static void trapcpy(int, void *, size_t);
    117 
    118 
    119 /* These two live in powerpc/powerpc/trap_subr.S */
    120 extern int 		extint, extsize;
    121 extern u_long 		extint_call;
    122 
    123 extern int defaulttrap, defaultsize;
    124 extern int sctrap, scsize;
    125 extern int alitrap, alisize;
    126 extern int dsitrap, dsisize;
    127 extern int isitrap, isisize;
    128 extern int mchktrap, mchksize;
    129 extern int tlbimiss4xx, tlbim4size;
    130 extern int tlbdmiss4xx, tlbdm4size;
    131 extern int pitfitwdog, pitfitwdogsize;
    132 extern int debugtrap, debugsize;
    133 extern int errata51handler, errata51size;
    134 #ifdef DDB
    135 extern int ddblow, ddbsize;
    136 #endif
    137 #ifdef IPKDB
    138 extern int ipkdblow, ipkdbsize;
    139 #endif
    140 
    141 /* BSS segment start & end. */
    142 extern char 		edata[], end[];
    143 
    144 /* One region holds all memory, the other is terminator expected by 405 pmap. */
    145 #define MEMREGIONS 	2
    146 struct mem_region 	physmemr[MEMREGIONS];
    147 struct mem_region 	availmemr[MEMREGIONS];
    148 
    149 static struct {
    150 	int 	vector;
    151 	void 	*addr;
    152 	void 	*size;
    153 } traps[] = {
    154 	/* EXC_EXI handled by install_extint(). */
    155 	{ EXC_SC, 	&sctrap, 	&scsize 	},
    156 	{ EXC_ALI, 	&alitrap, 	&alisize 	},
    157 	{ EXC_DSI, 	&dsitrap, 	&dsisize 	},
    158 	{ EXC_ISI, 	&isitrap, 	&isisize 	},
    159 	{ EXC_MCHK, 	&mchktrap, 	&mchksize 	},
    160 	{ EXC_ITMISS, 	&tlbimiss4xx, 	&tlbim4size 	},
    161 	{ EXC_DTMISS, 	&tlbdmiss4xx, 	&tlbdm4size 	},
    162 
    163 	/* EXC_{PIT, FIT, WDOG} handlers are spaced by 0x10 bytes only.. */
    164 	{ EXC_PIT, 	&pitfitwdog, 	&pitfitwdogsize },
    165 	{ EXC_DEBUG, 	&debugtrap, 	&debugsize	},
    166 
    167 	/* PPC405GP Rev D errata item 51 */
    168 	{ EXC_DTMISS|EXC_ALI, &errata51handler, &errata51size },
    169 
    170 #if defined(DDB)
    171 	{ EXC_PGM, 	&ddblow, 	&ddbsize 	},
    172 #elif defined(IPKDB)
    173 	{ EXC_PGM, 	&ipkdblow, 	&ipkdbsize 	},
    174 #endif
    175 };
    176 
    177 /* Maximum TLB page size. */
    178 #define TLB_PG_SIZE 	(16*1024*1024)
    179 
    180 void
    181 initppc(u_int startkernel, u_int endkernel)
    182 {
    183 	struct cpu_info * const ci = curcpu();
    184 	u_int 			addr, memsize;
    185 	int 			exc, dbcr0, i;
    186 
    187         /* Initialize cache info for memcpy, memset, etc. */
    188         cpu_probe_cache();
    189 
    190 	memset(physmemr, 0, sizeof(physmemr)); 		/* [1].size = 0 */
    191 	memset(availmemr, 0, sizeof(availmemr)); 	/* [1].size = 0 */
    192 
    193 	memsize = (PHYSMEM * 1024 * 1024) & ~PGOFSET;
    194 
    195 	physmemr[0].start 	= 0;
    196 	physmemr[0].size 	= memsize;
    197 
    198 	availmemr[0].start 	= startkernel;
    199 	availmemr[0].size 	= memsize - availmemr[0].start;
    200 
    201 	/* Map kernel memory linearly. */
    202 	for (addr = 0; addr < endkernel; addr += TLB_PG_SIZE)
    203 		ppc4xx_tlb_reserve(addr, addr, TLB_PG_SIZE, TLB_EX);
    204 
    205 	/* Give design-specific code a hint for reserved mappings. */
    206 	virtex_machdep_init(roundup(memsize, TLB_PG_SIZE), TLB_PG_SIZE,
    207 	    physmemr, availmemr);
    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 	for (exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100)
    217 		trapcpy(exc, &defaulttrap, (size_t)&defaultsize);
    218 
    219 	for (i = 0; i < __arraycount(traps); i++)
    220 		trapcpy(traps[i].vector, traps[i].addr, (size_t)traps[i].size);
    221 
    222 	__syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
    223 
    224 	/* set exception vector base */
    225 	mtspr(SPR_EVPR, 0);
    226 
    227 	/* handle trap instruction as PGM exception */
    228 	dbcr0 = mfspr(SPR_DBCR0) & ~DBCR0_TDE;
    229 	mtspr(SPR_DBCR0, dbcr0);
    230 
    231 	install_extint(ext_intr);
    232 
    233 	/* enable translation */
    234 	__asm volatile (
    235 	    "	mfmsr %0 	\n"
    236 	    "	ori %0,%0,%1 	\n"
    237 	    "	mtmsr %0 	\n"
    238 	    "   sync 		\n"
    239 	    "	isync		\n"
    240 	    : : "r"(0), "K"(PSL_IR | PSL_DR | PSL_ME));
    241 
    242 	/* now that we enabled MMU, we can map console */
    243 	consinit();
    244 
    245 	uvm_setpagesize();
    246 	pmap_bootstrap(startkernel, endkernel);
    247 
    248 #if NKSYMS || defined(DDB) || defined(LKM)
    249 	ksyms_init((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
    250 #endif
    251 #ifdef DDB
    252 	if (boothowto & RB_KDB)
    253 		Debugger();
    254 #endif
    255 #ifdef IPKDB
    256 	/*
    257 	 * Now trap to IPKDB
    258 	 */
    259 	ipkdb_init();
    260 	if (boothowto & RB_KDB)
    261 		ipkdb_connect(0);
    262 #endif
    263 #ifdef KGDB
    264 	/*
    265 	 * Now trap to KGDB
    266 	 */
    267 	kgdb_connect(1);
    268 #endif /* KGDB */
    269 }
    270 
    271 /*
    272  * trapcpy:
    273  *
    274  * 	Install a trap vector. We cannot use memcpy because the
    275  * 	destination may be zero. Borrowed from Explora.
    276  */
    277 static void
    278 trapcpy(int dest, void *src, size_t len)
    279 {
    280 	uint32_t 		*dest_p = (void *)dest;
    281 	uint32_t 		*src_p = src;
    282 
    283 	while (len > 0) {
    284 		*dest_p++ = *src_p++;
    285 		len -= sizeof(uint32_t);
    286 	}
    287 }
    288 
    289 static void
    290 install_extint(void (*handler)(void))
    291 {
    292 	u_long 			offset = (u_long)handler - (u_long)&extint_call;
    293 
    294 #ifdef DIAGNOSTIC
    295 	if (offset > 0x1ffffff)
    296 		panic("install_extint: too far away");
    297 #endif
    298 
    299 	/* Patch branch target in EXC_EXI handler. */
    300 	extint_call = (extint_call & 0xfc000003) | offset;
    301 
    302 	memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
    303 	__syncicache((void *)&extint_call, sizeof(extint_call));
    304 	__syncicache((void *)EXC_EXI, (int)&extsize);
    305 }
    306 
    307 /*
    308  * Machine dependent startup code.
    309  */
    310 
    311 char msgbuf[MSGBUFSIZE];
    312 
    313 void
    314 cpu_startup(void)
    315 {
    316 	/* For use by propdb. */
    317 	static u_int 	memsize = PHYSMEM * 1024 * 1024;
    318 	static u_int 	cpuspeed = CPUFREQ * 1000 * 1000;
    319 	prop_number_t 	pn;
    320 
    321 	vaddr_t 	minaddr, maxaddr;
    322 	char 		pbuf[9];
    323 
    324 	curcpu()->ci_khz = cpuspeed / 1000;
    325 
    326 	/* Initialize error message buffer. */
    327 	initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
    328 
    329 	printf("%s%s", copyright, version);
    330 
    331 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    332 	printf("total memory = %s\n", pbuf);
    333 
    334 	minaddr = 0;
    335 	/*
    336 	 * Allocate a submap for physio
    337 	 */
    338 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    339 				 VM_PHYS_SIZE, 0, false, NULL);
    340 
    341 	/*
    342 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    343 	 * are allocated via the pool allocator, and we use direct-mapped
    344 	 * pool pages.
    345 	 */
    346 
    347 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    348 	printf("avail memory = %s\n", pbuf);
    349 
    350 	/*
    351 	 * Set up the board properties database.
    352 	 */
    353 	board_info_init();
    354 
    355 	pn = prop_number_create_integer(memsize);
    356 	KASSERT(pn != NULL);
    357 	if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
    358 		panic("setting mem-size");
    359 	prop_object_release(pn);
    360 
    361 	pn = prop_number_create_integer(cpuspeed);
    362 	KASSERT(pn != NULL);
    363 	if (prop_dictionary_set(board_properties, "processor-frequency",
    364 	    pn) == false)
    365 		panic("setting processor-frequency");
    366 	prop_object_release(pn);
    367 
    368 	/*
    369 	 * Now that we have VM, malloc()s are OK in bus_space.
    370 	 */
    371 	bus_space_mallocok();
    372 	fake_mapiodev = 0;
    373 }
    374 
    375 
    376 static void
    377 dumpsys(void)
    378 {
    379 	printf("dumpsys: TBD\n");
    380 }
    381 
    382 /* Hook used by 405 pmap module. */
    383 void
    384 mem_regions(struct mem_region **mem, struct mem_region **avail)
    385 {
    386 	*mem 	= physmemr;
    387 	*avail 	= availmemr;
    388 }
    389 
    390 /*
    391  * Halt or reboot the machine after syncing/dumping according to howto.
    392  */
    393 void
    394 cpu_reboot(int howto, char *what)
    395 {
    396 	static int syncing;
    397 	static char str[256];
    398 	char *ap = str, *ap1 = ap;
    399 
    400 	boothowto = howto;
    401 	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
    402 		syncing = 1;
    403 		vfs_shutdown();		/* sync */
    404 		resettodr();		/* set wall clock */
    405 	}
    406 
    407 	splhigh();
    408 
    409 	if (!cold && (howto & RB_DUMP))
    410 		dumpsys();
    411 
    412 	doshutdownhooks();
    413 
    414 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    415 		/* Power off here if we know how...*/
    416 	}
    417 
    418 	if (howto & RB_HALT) {
    419 		printf("halted\n\n");
    420 
    421 		goto reboot;	/* XXX for now... */
    422 
    423 #ifdef DDB
    424 		printf("dropping to debugger\n");
    425 		while(1)
    426 			Debugger();
    427 #endif
    428 #ifdef KGDB
    429 		printf("dropping to kgdb\n");
    430 		while(1)
    431 			kgdb_connect(1);
    432 #endif
    433 	}
    434 
    435 	printf("rebooting\n\n");
    436 	if (what && *what) {
    437 		if (strlen(what) > sizeof str - 5)
    438 			printf("boot string too large, ignored\n");
    439 		else {
    440 			strcpy(str, what);
    441 			ap1 = ap = str + strlen(str);
    442 			*ap++ = ' ';
    443 		}
    444 	}
    445 	*ap++ = '-';
    446 	if (howto & RB_SINGLE)
    447 		*ap++ = 's';
    448 	if (howto & RB_KDB)
    449 		*ap++ = 'd';
    450 	*ap++ = 0;
    451 	if (ap[-2] == '-')
    452 		*ap1 = 0;
    453 
    454 	/* flush cache for msgbuf */
    455 	__syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
    456 
    457  reboot:
    458 	ppc4xx_reset();
    459 
    460 	printf("ppc4xx_reset() failed!\n");
    461 #ifdef DDB
    462 	while(1)
    463 		Debugger();
    464 #endif
    465 #ifdef KGDB
    466 	while(1)
    467 		kgdb_connect(1);
    468 #else
    469 	while (1)
    470 		/* nothing */;
    471 #endif
    472 }
    473 
    474 int
    475 lcsplx(int ipl)
    476 {
    477 	return spllower(ipl); 	/* XXX */
    478 }
    479