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