Home | History | Annotate | Line # | Download | only in virtex
machdep.c revision 1.18
      1 /*	$NetBSD: machdep.c,v 1.18 2011/06/18 06:44:27 matt 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.18 2011/06/18 06:44:27 matt 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 #include <sys/bus.h>
     64 
     65 #include <uvm/uvm_extern.h>
     66 
     67 #include <net/netisr.h>
     68 
     69 #include <dev/cons.h>
     70 
     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 <powerpc/ibm4xx/cpu.h>
     79 
     80 #include <evbppc/virtex/dcr.h>
     81 #include <evbppc/virtex/virtex.h>
     82 
     83 #include "ksyms.h"
     84 
     85 #if defined(DDB)
     86 #include <machine/db_machdep.h>
     87 #include <ddb/db_extern.h>
     88 #endif
     89 
     90 #if defined(KGDB)
     91 #include <sys/kgdb.h>
     92 #endif
     93 
     94 /*
     95  * Global variables used here and there
     96  */
     97 struct vm_map *phys_map = NULL;
     98 
     99 /*
    100  * This should probably be in autoconf!				XXX
    101  */
    102 char cpu_model[80];
    103 char machine[] = MACHINE;		/* from <machine/param.h> */
    104 char machine_arch[] = MACHINE_ARCH;	/* from <machine/param.h> */
    105 
    106 char bootpath[256];
    107 paddr_t msgbuf_paddr;
    108 vaddr_t msgbuf_vaddr;
    109 
    110 #if NKSYMS || defined(DDB) || defined(MODULAR)
    111 void *startsym, *endsym;
    112 #endif
    113 
    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 
    213 	curpcb = lwp_getpcb(&lwp0);
    214 	memset(curpcb, 0, sizeof(struct pcb));
    215 	curpcb->pcb_pm = pmap_kernel();
    216 
    217 	for (exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100)
    218 		trapcpy(exc, &defaulttrap, (size_t)&defaultsize);
    219 
    220 	for (i = 0; i < __arraycount(traps); i++)
    221 		trapcpy(traps[i].vector, traps[i].addr, (size_t)traps[i].size);
    222 
    223 	__syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
    224 
    225 	/* set exception vector base */
    226 	mtspr(SPR_EVPR, 0);
    227 
    228 	/* handle trap instruction as PGM exception */
    229 	dbcr0 = mfspr(SPR_DBCR0) & ~DBCR0_TDE;
    230 	mtspr(SPR_DBCR0, dbcr0);
    231 
    232 	install_extint(ext_intr);
    233 
    234 	/* enable translation */
    235 	__asm volatile (
    236 	    "	mfmsr %0 	\n"
    237 	    "	ori %0,%0,%1 	\n"
    238 	    "	mtmsr %0 	\n"
    239 	    "   sync 		\n"
    240 	    "	isync		\n"
    241 	    : : "r"(0), "K"(PSL_IR | PSL_DR | PSL_ME));
    242 
    243 	/* now that we enabled MMU, we can map console */
    244 	consinit();
    245 
    246 	uvm_setpagesize();
    247 	pmap_bootstrap(startkernel, endkernel);
    248 
    249 #if NKSYMS || defined(DDB) || defined(MODULAR)
    250 	ksyms_addsyms_elf((int)((u_int)endsym - (u_int)startsym), startsym, endsym);
    251 #endif
    252 #ifdef DDB
    253 	if (boothowto & RB_KDB)
    254 		Debugger();
    255 #endif
    256 #ifdef IPKDB
    257 	/*
    258 	 * Now trap to IPKDB
    259 	 */
    260 	ipkdb_init();
    261 	if (boothowto & RB_KDB)
    262 		ipkdb_connect(0);
    263 #endif
    264 #ifdef KGDB
    265 	/*
    266 	 * Now trap to KGDB
    267 	 */
    268 	kgdb_connect(1);
    269 #endif /* KGDB */
    270 
    271 	/*
    272 	 * Look for the ibm4xx modules in the right place.
    273 	 */
    274 	module_machine = module_machine_ibm4xx;
    275 }
    276 
    277 /*
    278  * trapcpy:
    279  *
    280  * 	Install a trap vector. We cannot use memcpy because the
    281  * 	destination may be zero. Borrowed from Explora.
    282  */
    283 static void
    284 trapcpy(int dest, void *src, size_t len)
    285 {
    286 	uint32_t 		*dest_p = (void *)dest;
    287 	uint32_t 		*src_p = src;
    288 
    289 	while (len > 0) {
    290 		*dest_p++ = *src_p++;
    291 		len -= sizeof(uint32_t);
    292 	}
    293 }
    294 
    295 static void
    296 install_extint(void (*handler)(void))
    297 {
    298 	u_long 			offset = (u_long)handler - (u_long)&extint_call;
    299 
    300 #ifdef DIAGNOSTIC
    301 	if (offset > 0x1ffffff)
    302 		panic("install_extint: too far away");
    303 #endif
    304 
    305 	/* Patch branch target in EXC_EXI handler. */
    306 	extint_call = (extint_call & 0xfc000003) | offset;
    307 
    308 	memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
    309 	__syncicache((void *)&extint_call, sizeof(extint_call));
    310 	__syncicache((void *)EXC_EXI, (int)&extsize);
    311 }
    312 
    313 /*
    314  * Machine dependent startup code.
    315  */
    316 
    317 char msgbuf[MSGBUFSIZE];
    318 
    319 void
    320 cpu_startup(void)
    321 {
    322 	/* For use by propdb. */
    323 	static u_int 	memsize = PHYSMEM * 1024 * 1024;
    324 	static u_int 	cpuspeed = CPUFREQ * 1000 * 1000;
    325 	prop_number_t 	pn;
    326 
    327 	vaddr_t 	minaddr, maxaddr;
    328 	char 		pbuf[9];
    329 
    330 	curcpu()->ci_khz = cpuspeed / 1000;
    331 
    332 	/* Initialize error message buffer. */
    333 	initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
    334 
    335 	printf("%s%s", copyright, version);
    336 
    337 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    338 	printf("total memory = %s\n", pbuf);
    339 
    340 	minaddr = 0;
    341 	/*
    342 	 * Allocate a submap for physio
    343 	 */
    344 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    345 				 VM_PHYS_SIZE, 0, false, NULL);
    346 
    347 	/*
    348 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    349 	 * are allocated via the pool allocator, and we use direct-mapped
    350 	 * pool pages.
    351 	 */
    352 
    353 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    354 	printf("avail memory = %s\n", pbuf);
    355 
    356 	/*
    357 	 * Set up the board properties database.
    358 	 */
    359 	board_info_init();
    360 
    361 	pn = prop_number_create_integer(memsize);
    362 	KASSERT(pn != NULL);
    363 	if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
    364 		panic("setting mem-size");
    365 	prop_object_release(pn);
    366 
    367 	pn = prop_number_create_integer(cpuspeed);
    368 	KASSERT(pn != NULL);
    369 	if (prop_dictionary_set(board_properties, "processor-frequency",
    370 	    pn) == false)
    371 		panic("setting processor-frequency");
    372 	prop_object_release(pn);
    373 
    374 	/*
    375 	 * Now that we have VM, malloc()s are OK in bus_space.
    376 	 */
    377 	bus_space_mallocok();
    378 	fake_mapiodev = 0;
    379 }
    380 
    381 
    382 static void
    383 dumpsys(void)
    384 {
    385 	printf("dumpsys: TBD\n");
    386 }
    387 
    388 /* Hook used by 405 pmap module. */
    389 void
    390 mem_regions(struct mem_region **mem, struct mem_region **avail)
    391 {
    392 	*mem 	= physmemr;
    393 	*avail 	= availmemr;
    394 }
    395 
    396 /*
    397  * Halt or reboot the machine after syncing/dumping according to howto.
    398  */
    399 void
    400 cpu_reboot(int howto, char *what)
    401 {
    402 	static int syncing;
    403 	static char str[256];
    404 	char *ap = str, *ap1 = ap;
    405 
    406 	boothowto = howto;
    407 	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
    408 		syncing = 1;
    409 		vfs_shutdown();		/* sync */
    410 		resettodr();		/* set wall clock */
    411 	}
    412 
    413 	splhigh();
    414 
    415 	if (!cold && (howto & RB_DUMP))
    416 		dumpsys();
    417 
    418 	doshutdownhooks();
    419 
    420 	pmf_system_shutdown(boothowto);
    421 
    422 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    423 		/* Power off here if we know how...*/
    424 	}
    425 
    426 	if (howto & RB_HALT) {
    427 		printf("halted\n\n");
    428 
    429 		goto reboot;	/* XXX for now... */
    430 
    431 #ifdef DDB
    432 		printf("dropping to debugger\n");
    433 		while(1)
    434 			Debugger();
    435 #endif
    436 #ifdef KGDB
    437 		printf("dropping to kgdb\n");
    438 		while(1)
    439 			kgdb_connect(1);
    440 #endif
    441 	}
    442 
    443 	printf("rebooting\n\n");
    444 	if (what && *what) {
    445 		if (strlen(what) > sizeof str - 5)
    446 			printf("boot string too large, ignored\n");
    447 		else {
    448 			strcpy(str, what);
    449 			ap1 = ap = str + strlen(str);
    450 			*ap++ = ' ';
    451 		}
    452 	}
    453 	*ap++ = '-';
    454 	if (howto & RB_SINGLE)
    455 		*ap++ = 's';
    456 	if (howto & RB_KDB)
    457 		*ap++ = 'd';
    458 	*ap++ = 0;
    459 	if (ap[-2] == '-')
    460 		*ap1 = 0;
    461 
    462 	/* flush cache for msgbuf */
    463 	__syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
    464 
    465  reboot:
    466 	ppc4xx_reset();
    467 
    468 	printf("ppc4xx_reset() failed!\n");
    469 #ifdef DDB
    470 	while(1)
    471 		Debugger();
    472 #endif
    473 #ifdef KGDB
    474 	while(1)
    475 		kgdb_connect(1);
    476 #else
    477 	while (1)
    478 		/* nothing */;
    479 #endif
    480 }
    481