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