Home | History | Annotate | Line # | Download | only in oea
ofwoea_machdep.c revision 1.1.2.4
      1 /* $NetBSD: ofwoea_machdep.c,v 1.1.2.4 2007/06/18 03:31:33 macallan Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tim Rightnour
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: ofwoea_machdep.c,v 1.1.2.4 2007/06/18 03:31:33 macallan Exp $");
     41 
     42 
     43 #include "opt_compat_netbsd.h"
     44 #include "opt_ddb.h"
     45 #include "opt_kgdb.h"
     46 #include "opt_ipkdb.h"
     47 
     48 #include <sys/param.h>
     49 #include <sys/buf.h>
     50 #include <sys/boot_flag.h>
     51 #include <sys/extent.h>
     52 #include <sys/kernel.h>
     53 
     54 #include <uvm/uvm_extern.h>
     55 
     56 #include <dev/ofw/openfirm.h>
     57 #include <machine/pmap.h>
     58 #include <machine/powerpc.h>
     59 #include <machine/trap.h>
     60 #include <powerpc/bus.h>
     61 #include <powerpc/oea/bat.h>
     62 #include <powerpc/ofw_bus.h>
     63 #include <powerpc/ofw_cons.h>
     64 #include <powerpc/spr.h>
     65 #include <arch/powerpc/pic/picvar.h>
     66 
     67 #ifdef DDB
     68 #include <machine/db_machdep.h>
     69 #include <ddb/db_extern.h>
     70 #endif
     71 
     72 #ifdef KGDB
     73 #include <sys/kgdb.h>
     74 #endif
     75 
     76 #ifdef IPKDB
     77 #include <ipkdb/ipkdb.h>
     78 #endif
     79 
     80 
     81 typedef struct _rangemap {
     82 	u_int32_t addr;
     83 	u_int32_t size;
     84 	int type;
     85 } rangemap_t;
     86 
     87 struct ofw_translations {
     88 	vaddr_t va;
     89 	int len;
     90 #if defined (PMAC_G5)
     91 	register64_t pa;
     92 #else
     93 	register_t pa;
     94 #endif
     95 	int mode;
     96 }__attribute__((packed));
     97 
     98 struct pmap ofw_pmap;
     99 struct ofw_translations ofmap[32];
    100 char bootpath[256];
    101 #if NKSYMS || defined(DDB) || defined(LKM)
    102 void *startsym, *endsym;
    103 #endif
    104 #ifdef TIMEBASE_FREQ
    105 u_int timebase_freq = TIMEBASE_FREQ;
    106 #else
    107 u_int timebase_freq = 0;
    108 #endif
    109 
    110 extern int ofmsr;
    111 extern int chosen;
    112 extern uint32_t ticks_per_sec;
    113 extern uint32_t ns_per_tick;
    114 extern uint32_t ticks_per_intr;
    115 
    116 static int save_ofmap(struct ofw_translations *, int);
    117 static void restore_ofmap(struct ofw_translations *, int);
    118 static void set_timebase(void);
    119 
    120 void
    121 ofwoea_initppc(u_int startkernel, u_int endkernel, char *args)
    122 {
    123 	int ofmaplen;
    124 #if defined (PPC_OEA64_BRIDGE)
    125 	register_t scratch;
    126 #endif
    127 
    128 #if defined (PPC_OEA)
    129 	/* initialze bats */
    130 	ofwoea_batinit();
    131 #elif defined (PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
    132 #endif /* PPC_OEA */
    133 
    134 #if NKSYMS || defined(DDB) || defined(LKM)
    135 	/* get info of kernel symbol table from bootloader */
    136 	memcpy(&startsym, args + strlen(args) + 1, sizeof(startsym));
    137 	memcpy(&endsym, args + strlen(args) + 1 + sizeof(startsym),
    138 	    sizeof(endsym));
    139 	if (startsym == NULL || endsym == NULL)
    140 	    startsym = endsym = NULL;
    141 #endif
    142 
    143 	ofwoea_bus_space_init();
    144 
    145 	ofwoea_consinit();
    146 
    147 	oea_init(pic_ext_intr);
    148 
    149 	ofmaplen = save_ofmap(NULL, 0);
    150 	if (ofmaplen > 0)
    151 		save_ofmap(ofmap, ofmaplen);
    152 
    153 	ofmsr &= ~PSL_IP;
    154 
    155 	/* Parse the args string */
    156 	if (args) {
    157 		strcpy(bootpath, args);
    158 		args = bootpath;
    159 		while (*++args && *args != ' ');
    160 		if (*args) {
    161 			*args++ = 0;
    162 			while (*args)
    163 				BOOT_FLAG(*args++, boothowto);
    164 		}
    165 	}
    166 
    167 	uvm_setpagesize();
    168 	pmap_bootstrap(startkernel, endkernel);
    169 
    170 #if defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
    171 #if defined (PMAC_G5)
    172 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    173 	pmap_setup_segment0_map(0, 0xff800000, 0x3fc00000, 0x400000, 0x0);
    174 #elif defined (MAMBO)
    175 	/* Mapin 1st 256MB segment 1:1, also map in mem needed to access OFW*/
    176 	pmap_setup_segment0_map(0, 0xf4000000, 0xf4000000, 0x1000, 0x0);
    177 #endif /* PMAC_G5 */
    178 
    179 	/* Now enable translation (and machine checks/recoverable interrupts) */
    180 	__asm __volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
    181 	    : "=r"(scratch)
    182 	    : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
    183 #endif /* PPC_OEA64 || PPC_OEA64_BRIDGE */
    184 
    185 	restore_ofmap(ofmap, ofmaplen);
    186 
    187 	/* CPU clock stuff */
    188 	set_timebase();
    189 }
    190 
    191 void
    192 set_timebase(void)
    193 {
    194 	int qhandle, phandle, msr, scratch;
    195 	char type[32];
    196 
    197 	if (timebase_freq != 0) {
    198 		ticks_per_sec = timebase_freq;
    199 		goto found;
    200 	}
    201 
    202 	for (qhandle = OF_peer(0); qhandle; qhandle = phandle) {
    203 		if (OF_getprop(qhandle, "device_type", type, sizeof type) > 0
    204 		    && strcmp(type, "cpu") == 0
    205 		    && OF_getprop(qhandle, "timebase-frequency",
    206 			&ticks_per_sec, sizeof ticks_per_sec) > 0) {
    207 			goto found;
    208 		}
    209 		if ((phandle = OF_child(qhandle)))
    210 			continue;
    211 		while (qhandle) {
    212 			if ((phandle = OF_peer(qhandle)))
    213 				break;
    214 			qhandle = OF_parent(qhandle);
    215 		}
    216 	}
    217 	panic("no cpu node");
    218 
    219 found:
    220 	__asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
    221 		: "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE));
    222 	ns_per_tick = 1000000000 / ticks_per_sec;
    223 	ticks_per_intr = ticks_per_sec / hz;
    224 	cpu_timebase = ticks_per_sec;
    225 	curcpu()->ci_lasttb = mftbl();
    226 	mtspr(SPR_DEC, ticks_per_intr);
    227 	mtmsr(msr);
    228 }
    229 
    230 static int
    231 save_ofmap(struct ofw_translations *map, int maxlen)
    232 {
    233 	int mmui, mmu, len;
    234 
    235 	OF_getprop(chosen, "mmu", &mmui, sizeof mmui);
    236 	mmu = OF_instance_to_package(mmui);
    237 
    238 	if (map) {
    239 		memset(map, 0, maxlen); /* to be safe */
    240 		len = OF_getprop(mmu, "translations", map, maxlen);
    241 	} else
    242 		len = OF_getproplen(mmu, "translations");
    243 
    244 	if (len < 0)
    245 		len = 0;
    246 	return len;
    247 }
    248 
    249 void
    250 restore_ofmap(struct ofw_translations *map, int len)
    251 {
    252 	int n = len / sizeof(struct ofw_translations);
    253 	int i;
    254 
    255 	pmap_pinit(&ofw_pmap);
    256 
    257 #if defined(PPC_OEA64_BRIDGE)
    258 	ofw_pmap.pm_sr[0x0] = KERNELN_SEGMENT(0);
    259 #endif
    260 	ofw_pmap.pm_sr[KERNEL_SR] = KERNEL_SEGMENT;
    261 
    262 #ifdef KERNEL2_SR
    263 	ofw_pmap.pm_sr[KERNEL2_SR] = KERNEL2_SEGMENT;
    264 #endif
    265 
    266 	for (i = 0; i < n; i++) {
    267 #if defined (PMAC_G5)
    268 		register64_t pa = map[i].pa;
    269 #else
    270 		register_t pa = map[i].pa;
    271 #endif
    272 		vaddr_t va = map[i].va;
    273 		size_t length = map[i].len;
    274 
    275 		if (va < 0xf0000000) /* XXX */
    276 			continue;
    277 
    278 		while (length > 0) {
    279 			pmap_enter(&ofw_pmap, va, (paddr_t)pa, VM_PROT_ALL,
    280 			    VM_PROT_ALL|PMAP_WIRED);
    281 			pa += PAGE_SIZE;
    282 			va += PAGE_SIZE;
    283 			length -= PAGE_SIZE;
    284 		}
    285 	}
    286 	pmap_update(&ofw_pmap);
    287 }
    288 
    289 
    290 
    291 /*
    292  * Scan the device tree for ranges, and batmap them.
    293  */
    294 
    295 static u_int16_t
    296 ranges_bitmap(int node, u_int16_t bitmap)
    297 {
    298 	int child, mlen, len, acells, scells, reclen, i, addr, j;
    299 	u_int32_t map[160];
    300 
    301 	for (child = OF_child(node); child; child = OF_peer(child)) {
    302 		mlen = OF_getprop(child, "ranges", map, sizeof(map));
    303 		if (mlen == -1)
    304 			goto noranges;
    305 		len = OF_getprop(child, "#address-cells", &acells,
    306 		    sizeof(acells));
    307 		if (len == -1)
    308 			goto noranges;
    309 		len = OF_getprop(child, "#size-cells", &scells,
    310 		    sizeof(scells));
    311 		if (len == -1)
    312 			goto noranges;
    313 
    314 		reclen = acells + 1 + scells;
    315 
    316 		for (i=0; i < reclen/(mlen/4); i++) {
    317 			addr = map[reclen * i + acells + 1];
    318 			len = map[reclen * i + reclen - 1];
    319 			for (j = 0; j < len / 0x10000000; j++)
    320 				bitmap |= 1 << ((addr+j*0x10000000) >>28);
    321 			bitmap |= 1 << (addr >> 28);
    322 		}
    323 
    324 noranges:
    325 		bitmap |= ranges_bitmap(child, bitmap);
    326 		continue;
    327 	}
    328 	return bitmap;
    329 }
    330 
    331 void
    332 ofwoea_batinit(void)
    333 {
    334 #if 0
    335 	/* this is what macppc used to do */
    336         oea_batinit(0x80000000, BAT_BL_256M, 0xf0000000, BAT_BL_256M,
    337                     0x90000000, BAT_BL_256M, 0xa0000000, BAT_BL_256M,
    338                                 0xb0000000, BAT_BL_256M, 0);
    339 #else
    340         u_int16_t bitmap;
    341 	int node, i;
    342 
    343 	node = OF_finddevice("/");
    344 	bitmap = ranges_bitmap(node, 0);
    345 	oea_batinit(0);
    346 
    347 	/* XXX this is a macppc-specific hack */
    348 	bitmap = 0x8f00;
    349 	for (i=1; i < 0x10; i++)
    350 		if (bitmap & (1 << i))
    351 			oea_iobat_add(0x10000000 * i, BAT_BL_256M);
    352 #endif
    353 }
    354 
    355 
    356 /* we define these partially, as we will fill the rest in later */
    357 struct powerpc_bus_space genppc_isa_io_space_tag = {
    358 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_IO_TYPE,
    359 	.pbs_base = 0x00000000,
    360 };
    361 
    362 struct powerpc_bus_space genppc_isa_mem_space_tag = {
    363 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
    364 	.pbs_base = 0x00000000,
    365 };
    366 
    367 /* This gives us a maximum of 6 PCI busses, assuming both io/mem on each.
    368  * Increase if necc.
    369  */
    370 static char ex_storage[EXSTORAGE_MAX][EXTENT_FIXED_STORAGE_SIZE(8)]
    371 	__attribute__((aligned(8)));
    372 
    373 
    374 static void
    375 find_ranges(int base, rangemap_t *regions, int *cur, int type)
    376 {
    377 	int node, i, len, reclen;
    378 	u_int32_t acells, scells, map[160];
    379 	char tmp[32];
    380 
    381 	node = base;
    382 	if (OF_getprop(node, "device_type", tmp, sizeof(tmp)) == -1)
    383 		goto rec;
    384 	if ((type == RANGE_TYPE_PCI || type == RANGE_TYPE_FIRSTPCI) &&
    385 	    strcmp("pci", tmp) != 0)
    386 		goto rec;
    387 	if (type == RANGE_TYPE_ISA && strcmp("isa", tmp) != 0)
    388 		goto rec;
    389 	len = OF_getprop(node, "ranges", map, sizeof(map));
    390 	if (len == -1)
    391 		goto rec;
    392 	if (OF_getprop(node, "#address-cells", &acells,
    393 	    sizeof(acells)) != sizeof(acells))
    394 		acells = 1;
    395 	if (OF_getprop(node, "#size-cells", &scells,
    396 	    sizeof(scells)) != sizeof(scells))
    397 		scells = 1;
    398 	if (type == RANGE_TYPE_ISA)
    399 		reclen = 6;
    400 	else
    401 		reclen = acells + scells + 1;
    402 	printf("found a map reclen=%d cur=%d\n", reclen, *cur);
    403 	switch (type) {
    404 		case RANGE_TYPE_PCI:
    405 		case RANGE_TYPE_FIRSTPCI:
    406 			for (i=0; i < len/(4*reclen); i++) {
    407 				regions[*cur].type = map[i*reclen] >> 24;
    408 				regions[*cur].addr = map[i*reclen + acells];
    409 				regions[*cur].size =
    410 				    map[i*reclen + acells + scells];
    411 				(*cur)++;
    412 			}
    413 			break;
    414 		case RANGE_TYPE_ISA:
    415 			for (i=0; i < len/(4*reclen); i++) {
    416 				if (map[i*reclen] == 1)
    417 					regions[*cur].type = RANGE_IO;
    418 				else
    419 					regions[*cur].type = RANGE_MEM;
    420 				regions[*cur].size =
    421 				    map[i*reclen + acells + scells];
    422 				(*cur)++;
    423 			}
    424 			break;
    425 	}
    426 	return;
    427 rec:
    428 	for (node = OF_child(base); node; node = OF_peer(node))
    429 		find_ranges(node, regions, cur, type);
    430 }
    431 
    432 static int
    433 find_lowest_range(rangemap_t *ranges, int nrof, int type)
    434 {
    435 	int i, low = 0;
    436 	u_int32_t addr = 0xffffffff;
    437 
    438 	for (i=0; i < nrof; i++) {
    439 		if (ranges[i].type == type && ranges[i].addr != 0 &&
    440 		    ranges[i].addr < addr) {
    441 			low = i;
    442 			addr = ranges[i].addr;
    443 		}
    444 	}
    445 	if (addr == 0xffffffff)
    446 		return -1;
    447 	return low;
    448 }
    449 
    450 /*
    451  * Find a region of memory, and create a bus_space_tag for it.
    452  * Notes:
    453  * For ISA node is ignored.
    454  * node is the starting node.  if -1, we start at / and map everything.
    455  */
    456 
    457 int
    458 ofwoea_map_space(int rangetype, int iomem, int node,
    459     struct powerpc_bus_space *tag, const char *name)
    460 {
    461 	int i, cur, range, nrofholes, error;
    462 	static int exmap=0;
    463 	u_int32_t addr;
    464 	rangemap_t region, holes[32], list[32];
    465 
    466 	memset(list, 0, sizeof(list));
    467 	cur = 0;
    468 	if (rangetype == RANGE_TYPE_ISA || node == -1)
    469 		node = OF_finddevice("/");
    470 	if (rangetype == RANGE_TYPE_ISA) {
    471 		u_int32_t size = 0;
    472 		rangemap_t regions[32];
    473 
    474 		find_ranges(node, list, &cur, RANGE_TYPE_FIRSTPCI);
    475 		range = 0;
    476 		find_ranges(node, regions, &range, RANGE_TYPE_ISA);
    477 		if (range == 0 || cur == 0)
    478 			return -1; /* no isa stuff found */
    479 		for (i=0; i < range; i++)
    480 			if (regions[i].type == iomem)
    481 				size = regions[i].size;
    482 		if (iomem == RANGE_IO) {
    483 			/* the first io range is the one */
    484 			for (i=0; i < cur; i++)
    485 				if (list[i].type == RANGE_IO && size) {
    486 					tag->pbs_offset = list[i].addr;
    487 					tag->pbs_limit = size;
    488 					error = bus_space_init(tag, name, NULL, 0);
    489 					return error;
    490 				}
    491 		} else {
    492 			for (i=0; i < cur; i++)
    493 				if (list[i].type == RANGE_MEM &&
    494 				    list[i].size == size) {
    495 					tag->pbs_offset = list[i].addr;
    496 					tag->pbs_limit = size;
    497 					error = bus_space_init(tag, name, NULL, 0);
    498 					return error;
    499 				}
    500 		}
    501 		return -1; /* NO ISA FOUND */
    502 	}
    503 	find_ranges(node, list, &cur, rangetype);
    504 
    505 	printf("cur == %d\n", cur);
    506 	/* now list should contain a list of memory regions */
    507 	for (i=0; i < cur; i++)
    508 		printf("addr=0x%x size=0x%x type=%d\n", list[i].addr,
    509 		    list[i].size, list[i].type);
    510 
    511 	addr=0;
    512 	range = find_lowest_range(list, cur, iomem);
    513 	i = 0;
    514 	nrofholes = 0;
    515 	while (range != -1) {
    516 		printf("range==%d\n", range);
    517 		printf("i==%d\n", i);
    518 		if (i == 0) {
    519 			memcpy(&region, &list[range], sizeof(rangemap_t));
    520 			list[range].addr = 0;
    521 			i++;
    522 			range = find_lowest_range(list, cur, iomem);
    523 			continue;
    524 		}
    525 		if (region.addr + region.size < list[range].addr) {
    526 			/* allocate a hole */
    527 			holes[nrofholes].addr = region.size + region.addr;
    528 			holes[nrofholes].size = list[range].addr -
    529 			    holes[nrofholes].addr - 1;
    530 			nrofholes++;
    531 		}
    532 		region.size = list[range].size + list[range].addr -
    533 		    region.addr;
    534 		list[range].addr = 0;
    535 		range = find_lowest_range(list, cur, iomem);
    536 	}
    537 	printf("RANGE iomem=%d FOUND\n", iomem);
    538 	printf("addr=0x%x size=0x%x type=%d\n", region.addr,
    539 		    region.size, region.type);
    540 	printf("HOLES FOUND\n");
    541 	for (i=0; i < nrofholes; i++)
    542 		printf("addr=0x%x size=0x%x type=%d\n", holes[i].addr,
    543 		    holes[i].size, holes[i].type);
    544 	/* AT THIS POINT WE MAP IT */
    545 
    546 	if (rangetype == RANGE_TYPE_PCI) {
    547 		if (exmap == EXSTORAGE_MAX)
    548 			panic("Not enough ex_storage space. "
    549 			    "Increase EXSTORAGE_MAX");
    550 
    551 		/* XXX doing this in here might be wrong */
    552 		if (iomem == 1) {
    553 			/* we map an IO region */
    554 			tag->pbs_offset = region.addr;
    555 			tag->pbs_base = 0;
    556 			tag->pbs_limit = region.size;
    557 		} else {
    558 			/* ... or a memory region */
    559 			tag->pbs_offset = 0;
    560 			tag->pbs_base = region.addr;
    561 			tag->pbs_limit = region.size + region.addr;
    562 		}
    563 
    564 		error = bus_space_init(tag, name, ex_storage[exmap],
    565 		    sizeof(ex_storage[exmap]));
    566 		exmap++;
    567 		if (error)
    568 			panic("ofwoea_bus_space_init: can't init tag %s", name);
    569 		for (i=0; i < nrofholes; i++) {
    570 			error =
    571 			    extent_alloc_region(tag->pbs_extent,
    572 				holes[i].addr, holes[i].size, EX_NOWAIT);
    573 			if (error)
    574 				panic("ofwoea_bus_space_init: can't block out"
    575 				    " reserved space 0x%x-0x%x: error=%d",
    576 				    holes[i].addr, holes[i].addr+holes[i].size,
    577 				    error);
    578 		}
    579 		return error;
    580 	}
    581 	return -1;
    582 }
    583 
    584 void
    585 ofwoea_bus_space_init(void)
    586 {
    587 	int error;
    588 
    589 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_IO, -1,
    590 	    &genppc_isa_io_space_tag, "isa-ioport");
    591 	if (error > 0)
    592 		panic("Could not map ISA IO");
    593 	error = ofwoea_map_space(RANGE_TYPE_ISA, RANGE_MEM, -1,
    594 	    &genppc_isa_mem_space_tag, "isa-iomem");
    595 	if (error > 0)
    596 		panic("Could not map ISA MEM");
    597 }
    598