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