Home | History | Annotate | Line # | Download | only in ofw
ofw.c revision 1.43
      1 /*	$NetBSD: ofw.c,v 1.43 2008/04/27 18:58:47 matt Exp $	*/
      2 
      3 /*
      4  * Copyright 1997
      5  * Digital Equipment Corporation. All rights reserved.
      6  *
      7  * This software is furnished under license and may be used and
      8  * copied only in accordance with the following terms and conditions.
      9  * Subject to these conditions, you may download, copy, install,
     10  * use, modify and distribute this software in source and/or binary
     11  * form. No title or ownership is transferred hereby.
     12  *
     13  * 1) Any source code used, modified or distributed must reproduce
     14  *    and retain this copyright notice and list of conditions as
     15  *    they appear in the source file.
     16  *
     17  * 2) No right is granted to use any trade name, trademark, or logo of
     18  *    Digital Equipment Corporation. Neither the "Digital Equipment
     19  *    Corporation" name nor any trademark or logo of Digital Equipment
     20  *    Corporation may be used to endorse or promote products derived
     21  *    from this software without the prior written permission of
     22  *    Digital Equipment Corporation.
     23  *
     24  * 3) This software is provided "AS-IS" and any express or implied
     25  *    warranties, including but not limited to, any implied warranties
     26  *    of merchantability, fitness for a particular purpose, or
     27  *    non-infringement are disclaimed. In no event shall DIGITAL be
     28  *    liable for any damages whatsoever, and in particular, DIGITAL
     29  *    shall not be liable for special, indirect, consequential, or
     30  *    incidental damages or damages for lost profits, loss of
     31  *    revenue or loss of use, whether such damages arise in contract,
     32  *    negligence, tort, under statute, in equity, at law or otherwise,
     33  *    even if advised of the possibility of such damage.
     34  */
     35 
     36 /*
     37  *  Routines for interfacing between NetBSD and OFW.
     38  *
     39  *  Parts of this could be moved to an MI file in time. -JJK
     40  *
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: ofw.c,v 1.43 2008/04/27 18:58:47 matt Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/reboot.h>
     50 #include <sys/mbuf.h>
     51 
     52 #include <uvm/uvm_extern.h>
     53 
     54 #include <dev/cons.h>
     55 
     56 #define	_ARM32_BUS_DMA_PRIVATE
     57 #include <machine/bus.h>
     58 #include <machine/frame.h>
     59 #include <machine/bootconfig.h>
     60 #include <machine/cpu.h>
     61 #include <machine/intr.h>
     62 #include <machine/irqhandler.h>
     63 
     64 #include <dev/ofw/openfirm.h>
     65 #include <machine/ofw.h>
     66 
     67 #include <netinet/in.h>
     68 
     69 #if	BOOT_FW_DHCP
     70 #include <nfs/bootdata.h>
     71 #endif
     72 
     73 #ifdef SHARK
     74 #include "machine/pio.h"
     75 #include "machine/isa_machdep.h"
     76 #endif
     77 
     78 #include "isadma.h"
     79 #include "igsfb_ofbus.h"
     80 #include "vga_ofbus.h"
     81 
     82 #define IO_VIRT_BASE (OFW_VIRT_BASE + OFW_VIRT_SIZE)
     83 #define IO_VIRT_SIZE 0x01000000
     84 
     85 #define	KERNEL_IMG_PTS		2
     86 #define	KERNEL_VMDATA_PTS	(KERNEL_VM_SIZE >> (L1_S_SHIFT + 2))
     87 #define	KERNEL_OFW_PTS		4
     88 #define	KERNEL_IO_PTS		4
     89 
     90 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
     91 /*
     92  * The range 0xf1000000 - 0xf6ffffff is available for kernel VM space
     93  * OFW sits at 0xf7000000
     94  */
     95 #define	KERNEL_VM_SIZE		0x06000000
     96 
     97 /*
     98  *  Imported variables
     99  */
    100 extern BootConfig bootconfig;	/* temporary, I hope */
    101 
    102 #ifdef	DIAGNOSTIC
    103 /* NOTE: These variables will be removed, well some of them */
    104 extern u_int current_mask;
    105 #endif
    106 
    107 extern int ofw_handleticks;
    108 
    109 
    110 /*
    111  *  Imported routines
    112  */
    113 extern void dump_spl_masks  __P((void));
    114 extern void dumpsys	    __P((void));
    115 extern void dotickgrovelling __P((vaddr_t));
    116 
    117 #define WriteWord(a, b) \
    118 *((volatile unsigned int *)(a)) = (b)
    119 
    120 #define ReadWord(a) \
    121 (*((volatile unsigned int *)(a)))
    122 
    123 
    124 /*
    125  *  Exported variables
    126  */
    127 /* These should all be in a meminfo structure. */
    128 paddr_t physical_start;
    129 paddr_t physical_freestart;
    130 paddr_t physical_freeend;
    131 paddr_t physical_end;
    132 u_int free_pages;
    133 int physmem;
    134 #ifndef	OFWGENCFG
    135 pv_addr_t irqstack;
    136 #endif
    137 pv_addr_t undstack;
    138 pv_addr_t abtstack;
    139 pv_addr_t kernelstack;
    140 
    141 paddr_t msgbufphys;
    142 
    143 /* for storage allocation, used to be local to ofw_construct_proc0_addrspace */
    144 static vaddr_t  virt_freeptr;
    145 
    146 int ofw_callbacks = 0;		/* debugging counter */
    147 
    148 #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
    149 int console_ihandle = 0;
    150 static void reset_screen(void);
    151 #endif
    152 
    153 /**************************************************************/
    154 
    155 
    156 /*
    157  *  Declarations and definitions private to this module
    158  *
    159  */
    160 
    161 struct mem_region {
    162 	paddr_t start;
    163 	psize_t size;
    164 };
    165 
    166 struct mem_translation {
    167 	vaddr_t virt;
    168 	vsize_t size;
    169 	paddr_t phys;
    170 	unsigned int mode;
    171 };
    172 
    173 struct isa_range {
    174 	paddr_t isa_phys_hi;
    175 	paddr_t isa_phys_lo;
    176 	paddr_t parent_phys_start;
    177 	psize_t isa_size;
    178 };
    179 
    180 struct vl_range {
    181 	paddr_t vl_phys_hi;
    182 	paddr_t vl_phys_lo;
    183 	paddr_t parent_phys_start;
    184 	psize_t vl_size;
    185 };
    186 
    187 struct vl_isa_range {
    188 	paddr_t isa_phys_hi;
    189 	paddr_t isa_phys_lo;
    190 	paddr_t parent_phys_hi;
    191 	paddr_t parent_phys_lo;
    192 	psize_t isa_size;
    193 };
    194 
    195 struct dma_range {
    196 	paddr_t start;
    197 	psize_t   size;
    198 };
    199 
    200 struct ofw_cbargs {
    201 	char *name;
    202 	int nargs;
    203 	int nreturns;
    204 	int args_n_results[12];
    205 };
    206 
    207 
    208 /* Memory info */
    209 static int nOFphysmem;
    210 static struct mem_region *OFphysmem;
    211 static int nOFphysavail;
    212 static struct mem_region *OFphysavail;
    213 static int nOFtranslations;
    214 static struct mem_translation *OFtranslations;
    215 static int nOFdmaranges;
    216 static struct dma_range *OFdmaranges;
    217 
    218 /* The OFW client services handle. */
    219 /* Initialized by ofw_init(). */
    220 static ofw_handle_t ofw_client_services_handle;
    221 
    222 
    223 static void ofw_callbackhandler __P((void *));
    224 static void ofw_construct_proc0_addrspace __P((void));
    225 static void ofw_getphysmeminfo __P((void));
    226 static void ofw_getvirttranslations __P((void));
    227 static void *ofw_malloc(vsize_t size);
    228 static void ofw_claimpages __P((vaddr_t *, pv_addr_t *, vsize_t));
    229 static void ofw_discardmappings __P ((vaddr_t, vaddr_t, vsize_t));
    230 static int ofw_mem_ihandle  __P((void));
    231 static int ofw_mmu_ihandle  __P((void));
    232 static paddr_t ofw_claimphys __P((paddr_t, psize_t, paddr_t));
    233 #if 0
    234 static paddr_t ofw_releasephys __P((paddr_t, psize_t));
    235 #endif
    236 static vaddr_t ofw_claimvirt __P((vaddr_t, vsize_t, vaddr_t));
    237 static void ofw_settranslation __P ((vaddr_t, paddr_t, vsize_t, int));
    238 static void ofw_initallocator __P((void));
    239 static void ofw_configisaonly __P((paddr_t *, paddr_t *));
    240 static void ofw_configvl __P((int, paddr_t *, paddr_t *));
    241 static vaddr_t ofw_valloc __P((vsize_t, vaddr_t));
    242 
    243 
    244 /*
    245  * DHCP hooks.  For a first cut, we look to see if there is a DHCP
    246  * packet that was saved by the firmware.  If not, we proceed as before,
    247  * getting hand-configured data from NVRAM.  If there is one, we get the
    248  * packet, and extract the data from it.  For now, we hand that data up
    249  * in the boot_args string as before.
    250  */
    251 
    252 
    253 /**************************************************************/
    254 
    255 
    256 /*
    257  *
    258  *  Support routines for xxx_machdep.c
    259  *
    260  *  The intent is that all OFW-based configurations use the
    261  *  exported routines in this file to do their business.  If
    262  *  they need to override some function they are free to do so.
    263  *
    264  *  The exported routines are:
    265  *
    266  *    openfirmware
    267  *    ofw_init
    268  *    ofw_boot
    269  *    ofw_getbootinfo
    270  *    ofw_configmem
    271  *    ofw_configisa
    272  *    ofw_configisadma
    273  *    ofw_gettranslation
    274  *    ofw_map
    275  *    ofw_getcleaninfo
    276  */
    277 
    278 
    279 int
    280 openfirmware(args)
    281 	void *args;
    282 {
    283 	int ofw_result;
    284 	u_int saved_irq_state;
    285 
    286 	/* OFW is not re-entrant, so we wrap a mutex around the call. */
    287 	saved_irq_state = disable_interrupts(I32_bit);
    288 	ofw_result = ofw_client_services_handle(args);
    289 	(void)restore_interrupts(saved_irq_state);
    290 
    291 	return(ofw_result);
    292 }
    293 
    294 
    295 void
    296 ofw_init(ofw_handle)
    297 	ofw_handle_t ofw_handle;
    298 {
    299 	ofw_client_services_handle = ofw_handle;
    300 
    301 	/*  Everything we allocate in the remainder of this block is
    302 	 *  constrained to be in the "kernel-static" portion of the
    303 	 *  virtual address space (i.e., 0xF0000000 - 0xF1000000).
    304 	 *  This is because all such objects are expected to be in
    305 	 *  that range by NetBSD, or the objects will be re-mapped
    306 	 *  after the page-table-switch to other specific locations.
    307 	 *  In the latter case, it's simplest if our pre-switch handles
    308 	 *  on those objects are in regions that are already "well-
    309 	 *  known."  (Otherwise, the cloning of the OFW-managed address-
    310 	 *  space becomes more awkward.)  To minimize the number of L2
    311 	 *  page tables that we use, we are further restricting the
    312 	 *  remaining allocations in this block to the bottom quarter of
    313 	 *  the legal range.  OFW will have loaded the kernel text+data+bss
    314 	 *  starting at the bottom of the range, and we will allocate
    315 	 *  objects from the top, moving downwards.  The two sub-regions
    316 	 *  will collide if their total sizes hit 8MB.  The current total
    317 	 *  is <1.5MB, but INSTALL kernels are > 4MB, so hence the 8MB
    318 	 *  limit.  The variable virt-freeptr represents the next free va
    319 	 *  (moving downwards).
    320 	 */
    321 	virt_freeptr = KERNEL_BASE + (0x00400000 * KERNEL_IMG_PTS);
    322 }
    323 
    324 
    325 void
    326 ofw_boot(howto, bootstr)
    327 	int howto;
    328 	char *bootstr;
    329 {
    330 
    331 #ifdef DIAGNOSTIC
    332 	printf("boot: howto=%08x curlwp=%p\n", howto, curlwp);
    333 	printf("current_mask=%08x\n", current_mask);
    334 
    335 	printf("ipl_bio=%08x ipl_net=%08x ipl_tty=%08x ipl_vm=%08x\n",
    336 	    irqmasks[IPL_BIO], irqmasks[IPL_NET], irqmasks[IPL_TTY],
    337 	    irqmasks[IPL_VM]);
    338 	printf("ipl_audio=%08x ipl_clock=%08x ipl_none=%08x\n",
    339 	    irqmasks[IPL_AUDIO], irqmasks[IPL_CLOCK], irqmasks[IPL_NONE]);
    340 
    341 	dump_spl_masks();
    342 #endif
    343 
    344 	/*
    345 	 * If we are still cold then hit the air brakes
    346 	 * and crash to earth fast
    347 	 */
    348 	if (cold) {
    349 		doshutdownhooks();
    350 		printf("Halted while still in the ICE age.\n");
    351 		printf("The operating system has halted.\n");
    352 		goto ofw_exit;
    353 		/*NOTREACHED*/
    354 	}
    355 
    356 	/*
    357 	 * If RB_NOSYNC was not specified sync the discs.
    358 	 * Note: Unless cold is set to 1 here, syslogd will die during the unmount.
    359 	 * It looks like syslogd is getting woken up only to find that it cannot
    360 	 * page part of the binary in as the filesystem has been unmounted.
    361 	 */
    362 	if (!(howto & RB_NOSYNC))
    363 		bootsync();
    364 
    365 	/* Say NO to interrupts */
    366 	splhigh();
    367 
    368 	/* Do a dump if requested. */
    369 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    370 		dumpsys();
    371 
    372 	/* Run any shutdown hooks */
    373 	doshutdownhooks();
    374 
    375 	/* Make sure IRQ's are disabled */
    376 	IRQdisable;
    377 
    378 	if (howto & RB_HALT) {
    379 		printf("The operating system has halted.\n");
    380 		goto ofw_exit;
    381 	}
    382 
    383 	/* Tell the user we are booting */
    384 	printf("rebooting...\n");
    385 
    386 	/* Jump into the OFW boot routine. */
    387 	{
    388 		static char str[256];
    389 		char *ap = str, *ap1 = ap;
    390 
    391 		if (bootstr && *bootstr) {
    392 			if (strlen(bootstr) > sizeof str - 5)
    393 				printf("boot string too large, ignored\n");
    394 			else {
    395 				strcpy(str, bootstr);
    396 				ap1 = ap = str + strlen(str);
    397 				*ap++ = ' ';
    398 			}
    399 		}
    400 		*ap++ = '-';
    401 		if (howto & RB_SINGLE)
    402 			*ap++ = 's';
    403 		if (howto & RB_KDB)
    404 			*ap++ = 'd';
    405 		*ap++ = 0;
    406 		if (ap[-2] == '-')
    407 			*ap1 = 0;
    408 #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
    409 		reset_screen();
    410 #endif
    411 		OF_boot(str);
    412 		/*NOTREACHED*/
    413 	}
    414 
    415 ofw_exit:
    416 	printf("Calling OF_exit...\n");
    417 #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
    418 	reset_screen();
    419 #endif
    420 	OF_exit();
    421 	/*NOTREACHED*/
    422 }
    423 
    424 
    425 #if	BOOT_FW_DHCP
    426 
    427 extern	char	*ip2dotted	__P((struct in_addr));
    428 
    429 /*
    430  * Get DHCP data from OFW
    431  */
    432 
    433 void
    434 get_fw_dhcp_data(bdp)
    435 	struct bootdata *bdp;
    436 {
    437 	int chosen;
    438 	int dhcplen;
    439 
    440 	bzero((char *)bdp, sizeof(*bdp));
    441 	if ((chosen = OF_finddevice("/chosen")) == -1)
    442 		panic("no /chosen from OFW");
    443 	if ((dhcplen = OF_getproplen(chosen, "bootp-response")) > 0) {
    444 		u_char *cp;
    445 		int dhcp_type = 0;
    446 		char *ip;
    447 
    448 		/*
    449 		 * OFW saved a DHCP (or BOOTP) packet for us.
    450 		 */
    451 		if (dhcplen > sizeof(bdp->dhcp_packet))
    452 			panic("DHCP packet too large");
    453 		OF_getprop(chosen, "bootp-response", &bdp->dhcp_packet,
    454 		    sizeof(bdp->dhcp_packet));
    455 		SANITY(bdp->dhcp_packet.op == BOOTREPLY, "bogus DHCP packet");
    456 		/*
    457 		 * Collect the interesting data from DHCP into
    458 		 * the bootdata structure.
    459 		 */
    460 		bdp->ip_address = bdp->dhcp_packet.yiaddr;
    461 		ip = ip2dotted(bdp->ip_address);
    462 		if (bcmp(bdp->dhcp_packet.options, DHCP_OPTIONS_COOKIE, 4) == 0)
    463 			parse_dhcp_options(&bdp->dhcp_packet,
    464 			    bdp->dhcp_packet.options + 4,
    465 			    &bdp->dhcp_packet.options[dhcplen
    466 			    - DHCP_FIXED_NON_UDP], bdp, ip);
    467 		if (bdp->root_ip.s_addr == 0)
    468 			bdp->root_ip = bdp->dhcp_packet.siaddr;
    469 		if (bdp->swap_ip.s_addr == 0)
    470 			bdp->swap_ip = bdp->dhcp_packet.siaddr;
    471 	}
    472 	/*
    473 	 * If the DHCP packet did not contain all the necessary data,
    474 	 * look in NVRAM for the missing parts.
    475 	 */
    476 	{
    477 		int options;
    478 		int proplen;
    479 #define BOOTJUNKV_SIZE	256
    480 		char bootjunkv[BOOTJUNKV_SIZE];	/* minimize stack usage */
    481 
    482 
    483 		if ((options = OF_finddevice("/options")) == -1)
    484 			panic("can't find /options");
    485 		if (bdp->ip_address.s_addr == 0 &&
    486 		    (proplen = OF_getprop(options, "ipaddr",
    487 		    bootjunkv, BOOTJUNKV_SIZE - 1)) > 0) {
    488 			bootjunkv[proplen] = '\0';
    489 			if (dotted2ip(bootjunkv, &bdp->ip_address.s_addr) == 0)
    490 				bdp->ip_address.s_addr = 0;
    491 		}
    492 		if (bdp->ip_mask.s_addr == 0 &&
    493 		    (proplen = OF_getprop(options, "netmask",
    494 		    bootjunkv, BOOTJUNKV_SIZE - 1)) > 0) {
    495 			bootjunkv[proplen] = '\0';
    496 			if (dotted2ip(bootjunkv, &bdp->ip_mask.s_addr) == 0)
    497 				bdp->ip_mask.s_addr = 0;
    498 		}
    499 		if (bdp->hostname[0] == '\0' &&
    500 		    (proplen = OF_getprop(options, "hostname",
    501 		    bdp->hostname, sizeof(bdp->hostname) - 1)) > 0) {
    502 			bdp->hostname[proplen] = '\0';
    503 		}
    504 		if (bdp->root[0] == '\0' &&
    505 		    (proplen = OF_getprop(options, "rootfs",
    506 		    bootjunkv, BOOTJUNKV_SIZE - 1)) > 0) {
    507 			bootjunkv[proplen] = '\0';
    508 			parse_server_path(bootjunkv, &bdp->root_ip, bdp->root);
    509 		}
    510 		if (bdp->swap[0] == '\0' &&
    511 		    (proplen = OF_getprop(options, "swapfs",
    512 		    bootjunkv, BOOTJUNKV_SIZE - 1)) > 0) {
    513 			bootjunkv[proplen] = '\0';
    514 			parse_server_path(bootjunkv, &bdp->swap_ip, bdp->swap);
    515 		}
    516 	}
    517 }
    518 
    519 #endif	/* BOOT_FW_DHCP */
    520 
    521 void
    522 ofw_getbootinfo(bp_pp, ba_pp)
    523 	char **bp_pp;
    524 	char **ba_pp;
    525 {
    526 	int chosen;
    527 	int bp_len;
    528 	int ba_len;
    529 	char *bootpathv;
    530 	char *bootargsv;
    531 
    532 	/* Read the bootpath and bootargs out of OFW. */
    533 	/* XXX is bootpath still interesting?  --emg */
    534 	if ((chosen = OF_finddevice("/chosen")) == -1)
    535 		panic("no /chosen from OFW");
    536 	bp_len = OF_getproplen(chosen, "bootpath");
    537 	ba_len = OF_getproplen(chosen, "bootargs");
    538 	if (bp_len < 0 || ba_len < 0)
    539 		panic("can't get boot data from OFW");
    540 
    541 	bootpathv = (char *)ofw_malloc(bp_len);
    542 	bootargsv = (char *)ofw_malloc(ba_len);
    543 
    544 	if (bp_len)
    545 		OF_getprop(chosen, "bootpath", bootpathv, bp_len);
    546 	else
    547 		bootpathv[0] = '\0';
    548 
    549 	if (ba_len)
    550 		OF_getprop(chosen, "bootargs", bootargsv, ba_len);
    551 	else
    552 		bootargsv[0] = '\0';
    553 
    554 	*bp_pp = bootpathv;
    555 	*ba_pp = bootargsv;
    556 #ifdef DIAGNOSTIC
    557 	printf("bootpath=<%s>, bootargs=<%s>\n", bootpathv, bootargsv);
    558 #endif
    559 }
    560 
    561 paddr_t
    562 ofw_getcleaninfo(void)
    563 {
    564 	int cpu;
    565 	vaddr_t vclean;
    566 	paddr_t pclean;
    567 
    568 	if ((cpu = OF_finddevice("/cpu")) == -1)
    569 		panic("no /cpu from OFW");
    570 
    571 	if ((OF_getprop(cpu, "d-cache-flush-address", &vclean,
    572 	    sizeof(vclean))) != sizeof(vclean)) {
    573 #ifdef DEBUG
    574 		printf("no OFW d-cache-flush-address property\n");
    575 #endif
    576 		return -1;
    577 	}
    578 
    579 	if ((pclean = ofw_gettranslation(
    580 	    of_decode_int((unsigned char *)&vclean))) == -1)
    581 	panic("OFW failed to translate cache flush address");
    582 
    583 	return pclean;
    584 }
    585 
    586 void
    587 ofw_configisa(pio, pmem)
    588 	paddr_t *pio;
    589 	paddr_t *pmem;
    590 {
    591 	int vl;
    592 
    593 	if ((vl = OF_finddevice("/vlbus")) == -1) /* old style OFW dev info tree */
    594 		ofw_configisaonly(pio, pmem);
    595 	else /* old style OFW dev info tree */
    596 		ofw_configvl(vl, pio, pmem);
    597 }
    598 
    599 static void
    600 ofw_configisaonly(pio, pmem)
    601 	paddr_t *pio;
    602 	paddr_t *pmem;
    603 {
    604 	int isa;
    605 	int rangeidx;
    606 	int size;
    607 	paddr_t hi, start;
    608 	struct isa_range ranges[2];
    609 
    610 	if ((isa = OF_finddevice("/isa")) == -1)
    611 	panic("OFW has no /isa device node");
    612 
    613 	/* expect to find two isa ranges: IO/data and memory/data */
    614 	if ((size = OF_getprop(isa, "ranges", ranges, sizeof(ranges)))
    615 	    != sizeof(ranges))
    616 		panic("unexpected size of OFW /isa ranges property: %d", size);
    617 
    618 	*pio = *pmem = -1;
    619 
    620 	for (rangeidx = 0; rangeidx < 2; ++rangeidx) {
    621 		hi    = of_decode_int((unsigned char *)
    622 		    &ranges[rangeidx].isa_phys_hi);
    623 		start = of_decode_int((unsigned char *)
    624 		    &ranges[rangeidx].parent_phys_start);
    625 
    626 	if (hi & 1) { /* then I/O space */
    627 		*pio = start;
    628 	} else {
    629 		*pmem = start;
    630 	}
    631 	} /* END for */
    632 
    633 	if ((*pio == -1) || (*pmem == -1))
    634 		panic("bad OFW /isa ranges property");
    635 
    636 }
    637 
    638 static void
    639 ofw_configvl(vl, pio, pmem)
    640 	int vl;
    641 	paddr_t *pio;
    642 	paddr_t *pmem;
    643 {
    644 	int isa;
    645 	int ir, vr;
    646 	int size;
    647 	paddr_t hi, start;
    648 	struct vl_isa_range isa_ranges[2];
    649 	struct vl_range     vl_ranges[2];
    650 
    651 	if ((isa = OF_finddevice("/vlbus/isa")) == -1)
    652 		panic("OFW has no /vlbus/isa device node");
    653 
    654 	/* expect to find two isa ranges: IO/data and memory/data */
    655 	if ((size = OF_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges)))
    656 	    != sizeof(isa_ranges))
    657 		panic("unexpected size of OFW /vlbus/isa ranges property: %d",
    658 		     size);
    659 
    660 	/* expect to find two vl ranges: IO/data and memory/data */
    661 	if ((size = OF_getprop(vl, "ranges", vl_ranges, sizeof(vl_ranges)))
    662 	    != sizeof(vl_ranges))
    663 		panic("unexpected size of OFW /vlbus ranges property: %d", size);
    664 
    665 	*pio = -1;
    666 	*pmem = -1;
    667 
    668 	for (ir = 0; ir < 2; ++ir) {
    669 		for (vr = 0; vr < 2; ++vr) {
    670 			if ((isa_ranges[ir].parent_phys_hi
    671 			    == vl_ranges[vr].vl_phys_hi) &&
    672 			    (isa_ranges[ir].parent_phys_lo
    673 			    == vl_ranges[vr].vl_phys_lo)) {
    674 				hi    = of_decode_int((unsigned char *)
    675 				    &isa_ranges[ir].isa_phys_hi);
    676 				start = of_decode_int((unsigned char *)
    677 				    &vl_ranges[vr].parent_phys_start);
    678 
    679 				if (hi & 1) { /* then I/O space */
    680 					*pio = start;
    681 				} else {
    682 					*pmem = start;
    683 				}
    684 			} /* END if */
    685 		} /* END for */
    686 	} /* END for */
    687 
    688 	if ((*pio == -1) || (*pmem == -1))
    689 		panic("bad OFW /isa ranges property");
    690 }
    691 
    692 #if NISADMA > 0
    693 struct arm32_dma_range *shark_isa_dma_ranges;
    694 int shark_isa_dma_nranges;
    695 #endif
    696 
    697 void
    698 ofw_configisadma(pdma)
    699 	paddr_t *pdma;
    700 {
    701 	int root;
    702 	int rangeidx;
    703 	int size;
    704 	struct dma_range *dr;
    705 
    706 	if ((root = OF_finddevice("/")) == -1 ||
    707 	    (size = OF_getproplen(root, "dma-ranges")) <= 0 ||
    708 	    (OFdmaranges = (struct dma_range *)ofw_malloc(size)) == 0 ||
    709  	    OF_getprop(root, "dma-ranges", OFdmaranges, size) != size)
    710 		panic("bad / dma-ranges property");
    711 
    712 	nOFdmaranges = size / sizeof(struct dma_range);
    713 
    714 #if NISADMA > 0
    715 	/* Allocate storage for non-OFW representation of the range. */
    716 	shark_isa_dma_ranges = ofw_malloc(nOFdmaranges *
    717 	    sizeof(*shark_isa_dma_ranges));
    718 	if (shark_isa_dma_ranges == NULL)
    719 		panic("unable to allocate shark_isa_dma_ranges");
    720 	shark_isa_dma_nranges = nOFdmaranges;
    721 #endif
    722 
    723 	for (rangeidx = 0, dr = OFdmaranges; rangeidx < nOFdmaranges;
    724 	    ++rangeidx, ++dr) {
    725 		dr->start = of_decode_int((unsigned char *)&dr->start);
    726 		dr->size = of_decode_int((unsigned char *)&dr->size);
    727 #if NISADMA > 0
    728 		shark_isa_dma_ranges[rangeidx].dr_sysbase = dr->start;
    729 		shark_isa_dma_ranges[rangeidx].dr_busbase = dr->start;
    730 		shark_isa_dma_ranges[rangeidx].dr_len  = dr->size;
    731 #endif
    732 	}
    733 
    734 #ifdef DEBUG
    735 	printf("DMA ranges size = %d\n", size);
    736 
    737 	for (rangeidx = 0; rangeidx < nOFdmaranges; ++rangeidx) {
    738 		printf("%08lx %08lx\n",
    739 		(u_long)OFdmaranges[rangeidx].start,
    740 		(u_long)OFdmaranges[rangeidx].size);
    741 	}
    742 #endif
    743 }
    744 
    745 /*
    746  *  Memory configuration:
    747  *
    748  *  We start off running in the environment provided by OFW.
    749  *  This has the MMU turned on, the kernel code and data
    750  *  mapped-in at KERNEL_BASE (0xF0000000), OFW's text and
    751  *  data mapped-in at OFW_VIRT_BASE (0xF7000000), and (possibly)
    752  *  page0 mapped-in at 0x0.
    753  *
    754  *  The strategy is to set-up the address space for proc0 --
    755  *  including the allocation of space for new page tables -- while
    756  *  memory is still managed by OFW.  We then effectively create a
    757  *  copy of the address space by dumping all of OFW's translations
    758  *  and poking them into the new page tables.  We then notify OFW
    759  *  that we are assuming control of memory-management by installing
    760  *  our callback-handler, and switch to the NetBSD-managed page
    761  *  tables with the setttb() call.
    762  *
    763  *  This scheme may cause some amount of memory to be wasted within
    764  *  OFW as dead page tables, but it shouldn't be more than about
    765  *  20-30KB.  (It's also possible that OFW will re-use the space.)
    766  */
    767 void
    768 ofw_configmem(void)
    769 {
    770 	int i;
    771 
    772 	/* Set-up proc0 address space. */
    773 	ofw_construct_proc0_addrspace();
    774 
    775 	/*
    776 	 * Get a dump of OFW's picture of physical memory.
    777 	 * This is used below to initialize a load of variables used by pmap.
    778 	 * We get it now rather than later because we are about to
    779 	 * tell OFW to stop managing memory.
    780 	 */
    781 	ofw_getphysmeminfo();
    782 
    783 	/* We are about to take control of memory-management from OFW.
    784 	 * Establish callbacks for OFW to use for its future memory needs.
    785 	 * This is required for us to keep using OFW services.
    786 	 */
    787 
    788 	/* First initialize our callback memory allocator. */
    789 	ofw_initallocator();
    790 
    791 	OF_set_callback(ofw_callbackhandler);
    792 
    793 	/* Switch to the proc0 pagetables. */
    794 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
    795 	setttb(kernel_l1pt.pv_pa);
    796 	cpu_tlb_flushID();
    797 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
    798 
    799 	/*
    800 	 * Moved from cpu_startup() as data_abort_handler() references
    801 	 * this during uvm init
    802 	 */
    803 	{
    804 		extern struct user *proc0paddr;
    805 		proc0paddr = (struct user *)kernelstack.pv_va;
    806 		lwp0.l_addr = proc0paddr;
    807 	}
    808 
    809 	/* Aaaaaaaah, running in the proc0 address space! */
    810 	/* I feel good... */
    811 
    812 	/* Set-up the various globals which describe physical memory for pmap. */
    813 	{
    814 		struct mem_region *mp;
    815 		int totalcnt;
    816 		int availcnt;
    817 
    818 		/* physmem, physical_start, physical_end */
    819 		physmem = 0;
    820 		for (totalcnt = 0, mp = OFphysmem; totalcnt < nOFphysmem;
    821 		    totalcnt++, mp++) {
    822 #ifdef	OLDPRINTFS
    823 			printf("physmem: %x, %x\n", mp->start, mp->size);
    824 #endif
    825 			physmem += btoc(mp->size);
    826 		}
    827 		physical_start = OFphysmem[0].start;
    828 		mp--;
    829 		physical_end = mp->start + mp->size;
    830 
    831 		/* free_pages, physical_freestart, physical_freeend */
    832 		free_pages = 0;
    833 		for (availcnt = 0, mp = OFphysavail; availcnt < nOFphysavail;
    834 		    availcnt++, mp++) {
    835 #ifdef	OLDPRINTFS
    836 			printf("physavail: %x, %x\n", mp->start, mp->size);
    837 #endif
    838 			free_pages += btoc(mp->size);
    839 		}
    840 		physical_freestart = OFphysavail[0].start;
    841 		mp--;
    842 		physical_freeend = mp->start + mp->size;
    843 #ifdef	OLDPRINTFS
    844 		printf("pmap_bootstrap:  physmem = %x, free_pages = %x\n",
    845 		    physmem, free_pages);
    846 #endif
    847 
    848 		/*
    849 		 *  This is a hack to work with the existing pmap code.
    850 		 *  That code depends on a RiscPC BootConfig structure
    851 		 *  containing, among other things, an array describing
    852 		 *  the regions of physical memory.  So, for now, we need
    853 		 *  to stuff our OFW-derived physical memory info into a
    854 		 *  "fake" BootConfig structure.
    855 		 *
    856 		 *  An added twist is that we initialize the BootConfig
    857 		 *  structure with our "available" physical memory regions
    858 		 *  rather than the "total" physical memory regions.  Why?
    859 		 *  Because:
    860 		 *
    861 		 *   (a) the VM code requires that the "free" pages it is
    862 		 *       initialized with have consecutive indices.  This
    863 		 *       allows it to use more efficient data structures
    864 		 *       (presumably).
    865 		 *   (b) the current pmap routines which report the initial
    866 		 *       set of free page indices (pmap_next_page) and
    867 		 *       which map addresses to indices (pmap_page_index)
    868 		 *       assume that the free pages are consecutive across
    869 		 *       memory region boundaries.
    870 		 *
    871 		 *  This means that memory which is "stolen" at startup time
    872 		 *  (say, for page descriptors) MUST come from either the
    873 		 *  bottom of the first region or the top of the last.
    874 		 *
    875 		 *  This requirement doesn't mesh well with OFW (or at least
    876 		 *  our use of it).  We can get around it for the time being
    877 		 *  by pretending that our "available" region array describes
    878 		 *  all of our physical memory.  This may cause some important
    879 		 *  information to be excluded from a dump file, but so far
    880 		 *  I haven't come across any other negative effects.
    881 		 *
    882 		 *  In the long-run we should fix the index
    883 		 *  generation/translation code in the pmap module.
    884 		 */
    885 
    886 		if (DRAM_BLOCKS < (availcnt + 1))
    887 			panic("more ofw memory regions than bootconfig blocks");
    888 
    889 		for (i = 0, mp = OFphysavail; i < nOFphysavail; i++, mp++) {
    890 			bootconfig.dram[i].address = mp->start;
    891 			bootconfig.dram[i].pages = btoc(mp->size);
    892 		}
    893 		bootconfig.dramblocks = availcnt;
    894 	}
    895 
    896 	/* Load memory into UVM. */
    897 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
    898 
    899 	/* XXX Please kill this code dead. */
    900 	for (i = 0; i < bootconfig.dramblocks; i++) {
    901 		paddr_t start = (paddr_t)bootconfig.dram[i].address;
    902 		paddr_t end = start + (bootconfig.dram[i].pages * PAGE_SIZE);
    903 #if NISADMA > 0
    904 		paddr_t istart, isize;
    905 #endif
    906 
    907 		if (start < physical_freestart)
    908 			start = physical_freestart;
    909 		if (end > physical_freeend)
    910 			end = physical_freeend;
    911 
    912 #if 0
    913 		printf("%d: %lx -> %lx\n", loop, start, end - 1);
    914 #endif
    915 
    916 #if NISADMA > 0
    917 		if (arm32_dma_range_intersect(shark_isa_dma_ranges,
    918 					      shark_isa_dma_nranges,
    919 					      start, end - start,
    920 					      &istart, &isize)) {
    921 			/*
    922 			 * Place the pages that intersect with the
    923 			 * ISA DMA range onto the ISA DMA free list.
    924 			 */
    925 #if 0
    926 			printf("    ISADMA 0x%lx -> 0x%lx\n", istart,
    927 			    istart + isize - 1);
    928 #endif
    929 			uvm_page_physload(atop(istart),
    930 			    atop(istart + isize), atop(istart),
    931 			    atop(istart + isize), VM_FREELIST_ISADMA);
    932 
    933 			/*
    934 			 * Load the pieces that come before the
    935 			 * intersection onto the default free list.
    936 			 */
    937 			if (start < istart) {
    938 #if 0
    939 				printf("    BEFORE 0x%lx -> 0x%lx\n",
    940 				    start, istart - 1);
    941 #endif
    942 				uvm_page_physload(atop(start),
    943 				    atop(istart), atop(start),
    944 				    atop(istart), VM_FREELIST_DEFAULT);
    945 			}
    946 
    947 			/*
    948 			 * Load the pieces that come after the
    949 			 * intersection onto the default free list.
    950 			 */
    951 			if ((istart + isize) < end) {
    952 #if 0
    953 				printf("     AFTER 0x%lx -> 0x%lx\n",
    954 				    (istart + isize), end - 1);
    955 #endif
    956 				uvm_page_physload(atop(istart + isize),
    957 				    atop(end), atop(istart + isize),
    958 				    atop(end), VM_FREELIST_DEFAULT);
    959 			}
    960 		} else {
    961 			uvm_page_physload(atop(start), atop(end),
    962 			    atop(start), atop(end), VM_FREELIST_DEFAULT);
    963 		}
    964 #else /* NISADMA > 0 */
    965 		uvm_page_physload(atop(start), atop(end),
    966 		    atop(start), atop(end), VM_FREELIST_DEFAULT);
    967 #endif /* NISADMA > 0 */
    968 	}
    969 
    970 	/* Initialize pmap module. */
    971 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
    972 }
    973 
    974 
    975 /*
    976  ************************************************************
    977 
    978   Routines private to this module
    979 
    980  ************************************************************
    981  */
    982 
    983 /* N.B.  Not supposed to call printf in callback-handler!  Could deadlock! */
    984 static void
    985 ofw_callbackhandler(v)
    986 	void *v;
    987 {
    988 	struct ofw_cbargs *args = v;
    989 	char *name = args->name;
    990 	int nargs = args->nargs;
    991 	int nreturns = args->nreturns;
    992 	int *args_n_results = args->args_n_results;
    993 
    994 	ofw_callbacks++;
    995 
    996 #if defined(OFWGENCFG)
    997 	/* Check this first, so that we don't waste IRQ time parsing. */
    998 	if (strcmp(name, "tick") == 0) {
    999 		vaddr_t frame;
   1000 
   1001 		/* Check format. */
   1002 		if (nargs != 1 || nreturns < 1) {
   1003 			args_n_results[nargs] = -1;
   1004 			args->nreturns = 1;
   1005 			return;
   1006 		}
   1007 		args_n_results[nargs] =	0;	/* properly formatted request */
   1008 
   1009 		/*
   1010 		 *  Note that we are running in the IRQ frame, with interrupts
   1011 		 *  disabled.
   1012 		 *
   1013 		 *  We need to do two things here:
   1014 		 *    - copy a few words out of the input frame into a global
   1015 		 *      area, for later use by our real tick-handling code
   1016 		 *    - patch a few words in the frame so that when OFW returns
   1017 		 *      from the interrupt it will resume with our handler
   1018 		 *      rather than the code that was actually interrupted.
   1019 		 *      Our handler will resume when it finishes with the code
   1020 		 *      that was actually interrupted.
   1021 		 *
   1022 		 *  It's simplest to do this in assembler, since it requires
   1023 		 *  switching frames and grovelling about with registers.
   1024 		 */
   1025 		frame = (vaddr_t)args_n_results[0];
   1026 		if (ofw_handleticks)
   1027 			dotickgrovelling(frame);
   1028 		args_n_results[nargs + 1] = frame;
   1029 		args->nreturns = 1;
   1030 	} else
   1031 #endif
   1032 
   1033 	if (strcmp(name, "map") == 0) {
   1034 		vaddr_t va;
   1035 		paddr_t pa;
   1036 		vsize_t size;
   1037 		int mode;
   1038 		int ap_bits;
   1039 		int dom_bits;
   1040 		int cb_bits;
   1041 
   1042 		/* Check format. */
   1043 		if (nargs != 4 || nreturns < 2) {
   1044 			args_n_results[nargs] = -1;
   1045 			args->nreturns = 1;
   1046 			return;
   1047 		}
   1048 		args_n_results[nargs] =	0;	/* properly formatted request */
   1049 
   1050 		pa = (paddr_t)args_n_results[0];
   1051 		va = (vaddr_t)args_n_results[1];
   1052 		size = (vsize_t)args_n_results[2];
   1053 		mode = args_n_results[3];
   1054 		ap_bits =  (mode & 0x00000C00);
   1055 		dom_bits = (mode & 0x000001E0);
   1056 		cb_bits =  (mode & 0x000000C0);
   1057 
   1058 		/* Sanity checks. */
   1059 		if ((va & PGOFSET) != 0 || va < OFW_VIRT_BASE ||
   1060 		    (va + size) > (OFW_VIRT_BASE + OFW_VIRT_SIZE) ||
   1061 		    (pa & PGOFSET) != 0 || (size & PGOFSET) != 0 ||
   1062 		    size == 0 || (dom_bits >> 5) != 0) {
   1063 			args_n_results[nargs + 1] = -1;
   1064 			args->nreturns = 1;
   1065 			return;
   1066 		}
   1067 
   1068 		/* Write-back anything stuck in the cache. */
   1069 		cpu_idcache_wbinv_all();
   1070 
   1071 		/* Install new mappings. */
   1072 		{
   1073 			pt_entry_t *pte = vtopte(va);
   1074 			int npages = size >> PGSHIFT;
   1075 
   1076 			ap_bits >>= 10;
   1077 			for (; npages > 0; pte++, pa += PAGE_SIZE, npages--)
   1078 				*pte = (pa | L2_AP(ap_bits) | L2_TYPE_S |
   1079 				    cb_bits);
   1080 			PTE_SYNC_RANGE(vtopte(va), size >> PGSHIFT);
   1081 		}
   1082 
   1083 		/* Clean out tlb. */
   1084 		tlb_flush();
   1085 
   1086 		args_n_results[nargs + 1] = 0;
   1087 		args->nreturns = 2;
   1088 	} else if (strcmp(name, "unmap") == 0) {
   1089 		vaddr_t va;
   1090 		vsize_t size;
   1091 
   1092 		/* Check format. */
   1093 		if (nargs != 2 || nreturns < 1) {
   1094 			args_n_results[nargs] = -1;
   1095 			args->nreturns = 1;
   1096 			return;
   1097 		}
   1098 		args_n_results[nargs] =	0;	/* properly formatted request */
   1099 
   1100 		va = (vaddr_t)args_n_results[0];
   1101 		size = (vsize_t)args_n_results[1];
   1102 
   1103 		/* Sanity checks. */
   1104 		if ((va & PGOFSET) != 0 || va < OFW_VIRT_BASE ||
   1105 		    (va + size) > (OFW_VIRT_BASE + OFW_VIRT_SIZE) ||
   1106 		    (size & PGOFSET) != 0 || size == 0) {
   1107 			args_n_results[nargs + 1] = -1;
   1108 			args->nreturns = 1;
   1109 			return;
   1110 		}
   1111 
   1112 		/* Write-back anything stuck in the cache. */
   1113 		cpu_idcache_wbinv_all();
   1114 
   1115 		/* Zero the mappings. */
   1116 		{
   1117 			pt_entry_t *pte = vtopte(va);
   1118 			int npages = size >> PGSHIFT;
   1119 
   1120 			for (; npages > 0; pte++, npages--)
   1121 				*pte = 0;
   1122 			PTE_SYNC_RANGE(vtopte(va), size >> PGSHIFT);
   1123 		}
   1124 
   1125 		/* Clean out tlb. */
   1126 		tlb_flush();
   1127 
   1128 		args->nreturns = 1;
   1129 	} else if (strcmp(name, "translate") == 0) {
   1130 		vaddr_t va;
   1131 		paddr_t pa;
   1132 		int mode;
   1133 		pt_entry_t pte;
   1134 
   1135 		/* Check format. */
   1136 		if (nargs != 1 || nreturns < 4) {
   1137 			args_n_results[nargs] = -1;
   1138 			args->nreturns = 1;
   1139 			return;
   1140 		}
   1141 		args_n_results[nargs] =	0;	/* properly formatted request */
   1142 
   1143 		va = (vaddr_t)args_n_results[0];
   1144 
   1145 		/* Sanity checks.
   1146 		 * For now, I am only willing to translate va's in the
   1147 		 * "ofw range." Eventually, I may be more generous. -JJK
   1148 		 */
   1149 		if ((va & PGOFSET) != 0 ||  va < OFW_VIRT_BASE ||
   1150 		    va >= (OFW_VIRT_BASE + OFW_VIRT_SIZE)) {
   1151 			args_n_results[nargs + 1] = -1;
   1152 			args->nreturns = 1;
   1153 			return;
   1154 		}
   1155 
   1156 		/* Lookup mapping. */
   1157 		pte = *vtopte(va);
   1158 		if (pte == 0) {
   1159 			/* No mapping. */
   1160 			args_n_results[nargs + 1] = -1;
   1161 			args->nreturns = 2;
   1162 		} else {
   1163 			/* Existing mapping. */
   1164 			pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
   1165 			mode = (pte & 0x0C00) | (0 << 5) | (pte & 0x000C);	/* AP | DOM | CB */
   1166 
   1167 			args_n_results[nargs + 1] = 0;
   1168 			args_n_results[nargs + 2] = pa;
   1169 			args_n_results[nargs + 3] =	mode;
   1170 			args->nreturns = 4;
   1171 		}
   1172 	} else if (strcmp(name, "claim-phys") == 0) {
   1173 		struct pglist alloclist;
   1174 		paddr_t low, high, align;
   1175 		psize_t size;
   1176 
   1177 		/*
   1178 		 * XXX
   1179 		 * XXX THIS IS A GROSS HACK AND NEEDS TO BE REWRITTEN. -- cgd
   1180 		 * XXX
   1181 		 */
   1182 
   1183 		/* Check format. */
   1184 		if (nargs != 4 || nreturns < 3) {
   1185 			args_n_results[nargs] = -1;
   1186 			args->nreturns = 1;
   1187 			return;
   1188 		}
   1189 		args_n_results[nargs] =	0;	/* properly formatted request */
   1190 
   1191 		low = args_n_results[0];
   1192 		size = args_n_results[2];
   1193 		align = args_n_results[3];
   1194 		high = args_n_results[1] + size;
   1195 
   1196 #if 0
   1197 		printf("claim-phys: low = 0x%x, size = 0x%x, align = 0x%x, high = 0x%x\n",
   1198 		    low, size, align, high);
   1199 		align = size;
   1200 		printf("forcing align to be 0x%x\n", align);
   1201 #endif
   1202 
   1203 		args_n_results[nargs + 1] =
   1204 		uvm_pglistalloc(size, low, high, align, 0, &alloclist, 1, 0);
   1205 #if 0
   1206 		printf(" -> 0x%lx", args_n_results[nargs + 1]);
   1207 #endif
   1208 		if (args_n_results[nargs + 1] != 0) {
   1209 #if 0
   1210 			printf("(failed)\n");
   1211 #endif
   1212 			args_n_results[nargs + 1] = -1;
   1213 			args->nreturns = 2;
   1214 			return;
   1215 		}
   1216 		args_n_results[nargs + 2] = VM_PAGE_TO_PHYS(alloclist.tqh_first);
   1217 #if 0
   1218 		printf("(succeeded: pa = 0x%lx)\n", args_n_results[nargs + 2]);
   1219 #endif
   1220 		args->nreturns = 3;
   1221 
   1222 	} else if (strcmp(name, "release-phys") == 0) {
   1223 		printf("unimplemented ofw callback - %s\n", name);
   1224 		args_n_results[nargs] = -1;
   1225 		args->nreturns = 1;
   1226 	} else if (strcmp(name, "claim-virt") == 0) {
   1227 		vaddr_t va;
   1228 		vsize_t size;
   1229 		vaddr_t align;
   1230 
   1231 		/* XXX - notyet */
   1232 /*		printf("unimplemented ofw callback - %s\n", name);*/
   1233 		args_n_results[nargs] = -1;
   1234 		args->nreturns = 1;
   1235 		return;
   1236 
   1237 		/* Check format. */
   1238 		if (nargs != 2 || nreturns < 3) {
   1239 		    args_n_results[nargs] = -1;
   1240 		    args->nreturns = 1;
   1241 		    return;
   1242 		}
   1243 		args_n_results[nargs] =	0;	/* properly formatted request */
   1244 
   1245 		/* Allocate size bytes with specified alignment. */
   1246 		size = (vsize_t)args_n_results[0];
   1247 		align = (vaddr_t)args_n_results[1];
   1248 		if (align % PAGE_SIZE != 0) {
   1249 			args_n_results[nargs + 1] = -1;
   1250 			args->nreturns = 2;
   1251 			return;
   1252 		}
   1253 
   1254 		if (va == 0) {
   1255 			/* Couldn't allocate. */
   1256 			args_n_results[nargs + 1] = -1;
   1257 			args->nreturns = 2;
   1258 		} else {
   1259 			/* Successful allocation. */
   1260 			args_n_results[nargs + 1] = 0;
   1261 			args_n_results[nargs + 2] = va;
   1262 			args->nreturns = 3;
   1263 		}
   1264 	} else if (strcmp(name, "release-virt") == 0) {
   1265 		vaddr_t va;
   1266 		vsize_t size;
   1267 
   1268 		/* XXX - notyet */
   1269 		printf("unimplemented ofw callback - %s\n", name);
   1270 		args_n_results[nargs] = -1;
   1271 		args->nreturns = 1;
   1272 		return;
   1273 
   1274 		/* Check format. */
   1275 		if (nargs != 2 || nreturns < 1) {
   1276 			args_n_results[nargs] = -1;
   1277 			args->nreturns = 1;
   1278 			return;
   1279 		}
   1280 		args_n_results[nargs] =	0;	/* properly formatted request */
   1281 
   1282 		/* Release bytes. */
   1283 		va = (vaddr_t)args_n_results[0];
   1284 		size = (vsize_t)args_n_results[1];
   1285 
   1286 		args->nreturns = 1;
   1287 	} else {
   1288 		args_n_results[nargs] = -1;
   1289 		args->nreturns = 1;
   1290 	}
   1291 }
   1292 
   1293 static void
   1294 ofw_construct_proc0_addrspace(void)
   1295 {
   1296 	int i, oft;
   1297 	static pv_addr_t proc0_pt_sys;
   1298 	static pv_addr_t proc0_pt_kernel[KERNEL_IMG_PTS];
   1299 	static pv_addr_t proc0_pt_vmdata[KERNEL_VMDATA_PTS];
   1300 	static pv_addr_t proc0_pt_ofw[KERNEL_OFW_PTS];
   1301 	static pv_addr_t proc0_pt_io[KERNEL_IO_PTS];
   1302 	static pv_addr_t msgbuf;
   1303 	vaddr_t L1pagetable;
   1304 	struct mem_translation *tp;
   1305 
   1306 	/* Set-up the system page. */
   1307 	KASSERT(vector_page == 0);	/* XXX for now */
   1308 	systempage.pv_va = ofw_claimvirt(vector_page, PAGE_SIZE, 0);
   1309 	if (systempage.pv_va == -1) {
   1310 		/* Something was already mapped to vector_page's VA. */
   1311 		systempage.pv_va = vector_page;
   1312 		systempage.pv_pa = ofw_gettranslation(vector_page);
   1313 		if (systempage.pv_pa == -1)
   1314 			panic("bogus result from gettranslation(vector_page)");
   1315 	} else {
   1316 		/* We were just allocated the page-length range at VA 0. */
   1317 		if (systempage.pv_va != vector_page)
   1318 			panic("bogus result from claimvirt(vector_page, PAGE_SIZE, 0)");
   1319 
   1320 		/* Now allocate a physical page, and establish the mapping. */
   1321 		systempage.pv_pa = ofw_claimphys(0, PAGE_SIZE, PAGE_SIZE);
   1322 		if (systempage.pv_pa == -1)
   1323 			panic("bogus result from claimphys(0, PAGE_SIZE, PAGE_SIZE)");
   1324 		ofw_settranslation(systempage.pv_va, systempage.pv_pa,
   1325 		    PAGE_SIZE, -1);	/* XXX - mode? -JJK */
   1326 
   1327 		/* Zero the memory. */
   1328 		bzero((char *)systempage.pv_va, PAGE_SIZE);
   1329 	}
   1330 
   1331 	/* Allocate/initialize space for the proc0, NetBSD-managed */
   1332 	/* page tables that we will be switching to soon. */
   1333 	ofw_claimpages(&virt_freeptr, &kernel_l1pt, L1_TABLE_SIZE);
   1334 	ofw_claimpages(&virt_freeptr, &proc0_pt_sys, L2_TABLE_SIZE);
   1335 	for (i = 0; i < KERNEL_IMG_PTS; i++)
   1336 		ofw_claimpages(&virt_freeptr, &proc0_pt_kernel[i], L2_TABLE_SIZE);
   1337 	for (i = 0; i < KERNEL_VMDATA_PTS; i++)
   1338 		ofw_claimpages(&virt_freeptr, &proc0_pt_vmdata[i], L2_TABLE_SIZE);
   1339 	for (i = 0; i < KERNEL_OFW_PTS; i++)
   1340 		ofw_claimpages(&virt_freeptr, &proc0_pt_ofw[i], L2_TABLE_SIZE);
   1341 	for (i = 0; i < KERNEL_IO_PTS; i++)
   1342 		ofw_claimpages(&virt_freeptr, &proc0_pt_io[i], L2_TABLE_SIZE);
   1343 
   1344 	/* Allocate/initialize space for stacks. */
   1345 #ifndef	OFWGENCFG
   1346 	ofw_claimpages(&virt_freeptr, &irqstack, PAGE_SIZE);
   1347 #endif
   1348 	ofw_claimpages(&virt_freeptr, &undstack, PAGE_SIZE);
   1349 	ofw_claimpages(&virt_freeptr, &abtstack, PAGE_SIZE);
   1350 	ofw_claimpages(&virt_freeptr, &kernelstack, UPAGES * PAGE_SIZE);
   1351 
   1352 	/* Allocate/initialize space for msgbuf area. */
   1353 	ofw_claimpages(&virt_freeptr, &msgbuf, MSGBUFSIZE);
   1354 	msgbufphys = msgbuf.pv_pa;
   1355 
   1356 	/* Construct the proc0 L1 pagetable. */
   1357 	L1pagetable = kernel_l1pt.pv_va;
   1358 
   1359 	pmap_link_l2pt(L1pagetable, 0x0, &proc0_pt_sys);
   1360 	for (i = 0; i < KERNEL_IMG_PTS; i++)
   1361 		pmap_link_l2pt(L1pagetable, KERNEL_BASE + i * 0x00400000,
   1362 		    &proc0_pt_kernel[i]);
   1363 	for (i = 0; i < KERNEL_VMDATA_PTS; i++)
   1364 		pmap_link_l2pt(L1pagetable, KERNEL_VM_BASE + i * 0x00400000,
   1365 		    &proc0_pt_vmdata[i]);
   1366 	for (i = 0; i < KERNEL_OFW_PTS; i++)
   1367 		pmap_link_l2pt(L1pagetable, OFW_VIRT_BASE + i * 0x00400000,
   1368 		    &proc0_pt_ofw[i]);
   1369 	for (i = 0; i < KERNEL_IO_PTS; i++)
   1370 		pmap_link_l2pt(L1pagetable, IO_VIRT_BASE + i * 0x00400000,
   1371 		    &proc0_pt_io[i]);
   1372 
   1373 	/*
   1374 	 * OK, we're done allocating.
   1375 	 * Get a dump of OFW's translations, and make the appropriate
   1376 	 * entries in the L2 pagetables that we just allocated.
   1377 	 */
   1378 
   1379 	ofw_getvirttranslations();
   1380 
   1381 	for (oft = 0,  tp = OFtranslations; oft < nOFtranslations;
   1382 	    oft++, tp++) {
   1383 
   1384 		vaddr_t va;
   1385 		paddr_t pa;
   1386 		int npages = tp->size / PAGE_SIZE;
   1387 
   1388 		/* Size must be an integral number of pages. */
   1389 		if (npages == 0 || tp->size % PAGE_SIZE != 0)
   1390 			panic("illegal ofw translation (size)");
   1391 
   1392 		/* Make an entry for each page in the appropriate table. */
   1393 		for (va = tp->virt, pa = tp->phys; npages > 0;
   1394 		    va += PAGE_SIZE, pa += PAGE_SIZE, npages--) {
   1395 			/*
   1396 			 * Map the top bits to the appropriate L2 pagetable.
   1397 			 * The only allowable regions are page0, the
   1398 			 * kernel-static area, and the ofw area.
   1399 			 */
   1400 			switch (va >> (L1_S_SHIFT + 2)) {
   1401 			case 0:
   1402 				/* page0 */
   1403 				break;
   1404 
   1405 #if KERNEL_IMG_PTS != 2
   1406 #error "Update ofw translation range list"
   1407 #endif
   1408 			case ( KERNEL_BASE                 >> (L1_S_SHIFT + 2)):
   1409 			case ((KERNEL_BASE   + 0x00400000) >> (L1_S_SHIFT + 2)):
   1410 				/* kernel static area */
   1411 				break;
   1412 
   1413 			case ( OFW_VIRT_BASE               >> (L1_S_SHIFT + 2)):
   1414 			case ((OFW_VIRT_BASE + 0x00400000) >> (L1_S_SHIFT + 2)):
   1415 			case ((OFW_VIRT_BASE + 0x00800000) >> (L1_S_SHIFT + 2)):
   1416 			case ((OFW_VIRT_BASE + 0x00C00000) >> (L1_S_SHIFT + 2)):
   1417 				/* ofw area */
   1418 				break;
   1419 
   1420 			case ( IO_VIRT_BASE               >> (L1_S_SHIFT + 2)):
   1421 			case ((IO_VIRT_BASE + 0x00400000) >> (L1_S_SHIFT + 2)):
   1422 			case ((IO_VIRT_BASE + 0x00800000) >> (L1_S_SHIFT + 2)):
   1423 			case ((IO_VIRT_BASE + 0x00C00000) >> (L1_S_SHIFT + 2)):
   1424 				/* io area */
   1425 				break;
   1426 
   1427 			default:
   1428 				/* illegal */
   1429 				panic("illegal ofw translation (addr) %#lx",
   1430 				    va);
   1431 			}
   1432 
   1433 			/* Make the entry. */
   1434 			pmap_map_entry(L1pagetable, va, pa,
   1435 			    VM_PROT_READ|VM_PROT_WRITE,
   1436 			    (tp->mode & 0xC) == 0xC ? PTE_CACHE
   1437 						    : PTE_NOCACHE);
   1438 		}
   1439 	}
   1440 
   1441 	/*
   1442 	 * We don't actually want some of the mappings that we just
   1443 	 * set up to appear in proc0's address space.  In particular,
   1444 	 * we don't want aliases to physical addresses that the kernel
   1445 	 * has-mapped/will-map elsewhere.
   1446 	 */
   1447 	ofw_discardmappings(proc0_pt_kernel[KERNEL_IMG_PTS - 1].pv_va,
   1448 	    msgbuf.pv_va, MSGBUFSIZE);
   1449 
   1450 	/* update the top of the kernel VM */
   1451 	pmap_curmaxkvaddr =
   1452 	    KERNEL_VM_BASE + (KERNEL_VMDATA_PTS * 0x00400000);
   1453 
   1454 	/*
   1455          * gross hack for the sake of not thrashing the TLB and making
   1456 	 * cache flush more efficient: blast l1 ptes for sections.
   1457          */
   1458 	for (oft = 0, tp = OFtranslations; oft < nOFtranslations; oft++, tp++) {
   1459 		vaddr_t va = tp->virt;
   1460 		paddr_t pa = tp->phys;
   1461 
   1462 		if (((va | pa) & L1_S_OFFSET) == 0) {
   1463 			int nsections = tp->size / L1_S_SIZE;
   1464 
   1465 			while (nsections--) {
   1466 				/* XXXJRT prot?? */
   1467 				pmap_map_section(L1pagetable, va, pa,
   1468 				    VM_PROT_READ|VM_PROT_WRITE,
   1469 				    (tp->mode & 0xC) == 0xC ? PTE_CACHE
   1470 							    : PTE_NOCACHE);
   1471 				va += L1_S_SIZE;
   1472 				pa += L1_S_SIZE;
   1473 			}
   1474 		}
   1475 	}
   1476 }
   1477 
   1478 
   1479 static void
   1480 ofw_getphysmeminfo()
   1481 {
   1482 	int phandle;
   1483 	int mem_len;
   1484 	int avail_len;
   1485 	int i;
   1486 
   1487 	if ((phandle = OF_finddevice("/memory")) == -1 ||
   1488 	    (mem_len = OF_getproplen(phandle, "reg")) <= 0 ||
   1489 	    (OFphysmem = (struct mem_region *)ofw_malloc(mem_len)) == 0 ||
   1490 	    OF_getprop(phandle, "reg", OFphysmem, mem_len) != mem_len ||
   1491 	    (avail_len = OF_getproplen(phandle, "available")) <= 0 ||
   1492  	    (OFphysavail = (struct mem_region *)ofw_malloc(avail_len)) == 0 ||
   1493 	    OF_getprop(phandle, "available", OFphysavail, avail_len)
   1494 	    != avail_len)
   1495 		panic("can't get physmeminfo from OFW");
   1496 
   1497 	nOFphysmem = mem_len / sizeof(struct mem_region);
   1498 	nOFphysavail = avail_len / sizeof(struct mem_region);
   1499 
   1500 	/*
   1501 	 * Sort the blocks in each array into ascending address order.
   1502 	 * Also, page-align all blocks.
   1503 	 */
   1504 	for (i = 0; i < 2; i++) {
   1505 		struct mem_region *tmp = (i == 0) ? OFphysmem : OFphysavail;
   1506 		struct mem_region *mp;
   1507 		int cnt =  (i == 0) ? nOFphysmem : nOFphysavail;
   1508 		int j;
   1509 
   1510 #ifdef	OLDPRINTFS
   1511 		printf("ofw_getphysmeminfo:  %d blocks\n", cnt);
   1512 #endif
   1513 
   1514 		/* XXX - Convert all the values to host order. -JJK */
   1515 		for (j = 0, mp = tmp; j < cnt; j++, mp++) {
   1516 			mp->start = of_decode_int((unsigned char *)&mp->start);
   1517 			mp->size = of_decode_int((unsigned char *)&mp->size);
   1518 		}
   1519 
   1520 		for (j = 0, mp = tmp; j < cnt; j++, mp++) {
   1521 			u_int s, sz;
   1522 			struct mem_region *mp1;
   1523 
   1524 			/* Page-align start of the block. */
   1525 			s = mp->start % PAGE_SIZE;
   1526 			if (s != 0) {
   1527 				s = (PAGE_SIZE - s);
   1528 
   1529 				if (mp->size >= s) {
   1530 					mp->start += s;
   1531 					mp->size -= s;
   1532 				}
   1533 			}
   1534 
   1535 			/* Page-align the size. */
   1536 			mp->size -= mp->size % PAGE_SIZE;
   1537 
   1538 			/* Handle empty block. */
   1539 			if (mp->size == 0) {
   1540 				memmove(mp, mp + 1, (cnt - (mp - tmp))
   1541 				    * sizeof(struct mem_region));
   1542 				cnt--;
   1543 				mp--;
   1544 				continue;
   1545 			}
   1546 
   1547 			/* Bubble sort. */
   1548 			s = mp->start;
   1549 			sz = mp->size;
   1550 			for (mp1 = tmp; mp1 < mp; mp1++)
   1551 				if (s < mp1->start)
   1552 					break;
   1553 			if (mp1 < mp) {
   1554 				memmove(mp1 + 1, mp1, (char *)mp - (char *)mp1);
   1555 				mp1->start = s;
   1556 				mp1->size = sz;
   1557 			}
   1558 		}
   1559 
   1560 #ifdef	OLDPRINTFS
   1561 		for (mp = tmp; mp->size; mp++) {
   1562 			printf("%x, %x\n", mp->start, mp->size);
   1563 		}
   1564 #endif
   1565 	}
   1566 }
   1567 
   1568 
   1569 static void
   1570 ofw_getvirttranslations(void)
   1571 {
   1572 	int mmu_phandle;
   1573 	int mmu_ihandle;
   1574 	int trans_len;
   1575 	int over, len;
   1576 	int i;
   1577 	struct mem_translation *tp;
   1578 
   1579 	mmu_ihandle = ofw_mmu_ihandle();
   1580 
   1581 	/* overallocate to avoid increases during allocation */
   1582 	over = 4 * sizeof(struct mem_translation);
   1583 	if ((mmu_phandle = OF_instance_to_package(mmu_ihandle)) == -1 ||
   1584 	    (len = OF_getproplen(mmu_phandle, "translations")) <= 0 ||
   1585 	    (OFtranslations = ofw_malloc(len + over)) == 0 ||
   1586 	    (trans_len = OF_getprop(mmu_phandle, "translations",
   1587 	    OFtranslations, len + over)) > (len + over))
   1588 		panic("can't get virttranslations from OFW");
   1589 
   1590 	/* XXX - Convert all the values to host order. -JJK */
   1591 	nOFtranslations = trans_len / sizeof(struct mem_translation);
   1592 #ifdef	OLDPRINTFS
   1593 	printf("ofw_getvirtmeminfo:  %d blocks\n", nOFtranslations);
   1594 #endif
   1595 	for (i = 0, tp = OFtranslations; i < nOFtranslations; i++, tp++) {
   1596 		tp->virt = of_decode_int((unsigned char *)&tp->virt);
   1597 		tp->size = of_decode_int((unsigned char *)&tp->size);
   1598 		tp->phys = of_decode_int((unsigned char *)&tp->phys);
   1599 		tp->mode = of_decode_int((unsigned char *)&tp->mode);
   1600 	}
   1601 }
   1602 
   1603 /*
   1604  * ofw_valloc: allocate blocks of VM for IO and other special purposes
   1605  */
   1606 typedef struct _vfree {
   1607 	struct _vfree *pNext;
   1608 	vaddr_t start;
   1609 	vsize_t size;
   1610 } VFREE, *PVFREE;
   1611 
   1612 static VFREE vfinitial = { NULL, IO_VIRT_BASE, IO_VIRT_SIZE };
   1613 
   1614 static PVFREE vflist = &vfinitial;
   1615 
   1616 static vaddr_t
   1617 ofw_valloc(size, align)
   1618 	vsize_t size;
   1619 	vaddr_t align;
   1620 {
   1621 	PVFREE        *ppvf;
   1622 	PVFREE        pNew;
   1623 	vaddr_t       new;
   1624 	vaddr_t       lead;
   1625 
   1626 	for (ppvf = &vflist; *ppvf; ppvf = &((*ppvf)->pNext)) {
   1627 		if (align == 0) {
   1628 			new = (*ppvf)->start;
   1629 			lead = 0;
   1630 		} else {
   1631 			new  = ((*ppvf)->start + (align - 1)) & ~(align - 1);
   1632 			lead = new - (*ppvf)->start;
   1633 		}
   1634 
   1635 		if (((*ppvf)->size - lead) >= size) {
   1636  			if (lead == 0) {
   1637 				/* using whole block */
   1638 				if (size == (*ppvf)->size) {
   1639 					/* splice out of list */
   1640 					(*ppvf) = (*ppvf)->pNext;
   1641 				} else { /* tail of block is free */
   1642 					(*ppvf)->start = new + size;
   1643 					(*ppvf)->size -= size;
   1644 				}
   1645 			} else {
   1646 				vsize_t tail = ((*ppvf)->start
   1647 				    + (*ppvf)->size) - (new + size);
   1648 				/* free space at beginning */
   1649 				(*ppvf)->size = lead;
   1650 
   1651 				if (tail != 0) {
   1652 					/* free space at tail */
   1653 					pNew = ofw_malloc(sizeof(VFREE));
   1654 					pNew->pNext  = (*ppvf)->pNext;
   1655 					(*ppvf)->pNext = pNew;
   1656 					pNew->start  = new + size;
   1657 					pNew->size   = tail;
   1658 				}
   1659 			}
   1660 			return new;
   1661 		} /* END if */
   1662 	} /* END for */
   1663 
   1664 	return -1;
   1665 }
   1666 
   1667 vaddr_t
   1668 ofw_map(pa, size, cb_bits)
   1669 	paddr_t pa;
   1670 	vsize_t size;
   1671 	int cb_bits;
   1672 {
   1673 	vaddr_t va;
   1674 
   1675 	if ((va = ofw_valloc(size, size)) == -1)
   1676 		panic("cannot alloc virtual memory for %#lx", pa);
   1677 
   1678 	ofw_claimvirt(va, size, 0); /* make sure OFW knows about the memory */
   1679 
   1680 	ofw_settranslation(va, pa, size, L2_AP(AP_KRW) | cb_bits);
   1681 
   1682 	return va;
   1683 }
   1684 
   1685 static int
   1686 ofw_mem_ihandle(void)
   1687 {
   1688 	static int mem_ihandle = 0;
   1689 	int chosen;
   1690 
   1691 	if (mem_ihandle != 0)
   1692 		return(mem_ihandle);
   1693 
   1694 	if ((chosen = OF_finddevice("/chosen")) == -1 ||
   1695 	    OF_getprop(chosen, "memory", &mem_ihandle, sizeof(int)) < 0)
   1696 		panic("ofw_mem_ihandle");
   1697 
   1698 	mem_ihandle = of_decode_int((unsigned char *)&mem_ihandle);
   1699 
   1700 	return(mem_ihandle);
   1701 }
   1702 
   1703 
   1704 static int
   1705 ofw_mmu_ihandle(void)
   1706 {
   1707 	static int mmu_ihandle = 0;
   1708 	int chosen;
   1709 
   1710 	if (mmu_ihandle != 0)
   1711 		return(mmu_ihandle);
   1712 
   1713 	if ((chosen = OF_finddevice("/chosen")) == -1 ||
   1714 	    OF_getprop(chosen, "mmu", &mmu_ihandle, sizeof(int)) < 0)
   1715 		panic("ofw_mmu_ihandle");
   1716 
   1717 	mmu_ihandle = of_decode_int((unsigned char *)&mmu_ihandle);
   1718 
   1719 	return(mmu_ihandle);
   1720 }
   1721 
   1722 
   1723 /* Return -1 on failure. */
   1724 static paddr_t
   1725 ofw_claimphys(pa, size, align)
   1726 	paddr_t pa;
   1727 	psize_t size;
   1728 	paddr_t align;
   1729 {
   1730 	int mem_ihandle = ofw_mem_ihandle();
   1731 
   1732 /*	printf("ofw_claimphys (%x, %x, %x) --> ", pa, size, align);*/
   1733 	if (align == 0) {
   1734 		/* Allocate at specified base; alignment is ignored. */
   1735 		pa = OF_call_method_1("claim", mem_ihandle, 3, pa, size, align);
   1736 	} else {
   1737 		/* Allocate anywhere, with specified alignment. */
   1738 		pa = OF_call_method_1("claim", mem_ihandle, 2, size, align);
   1739 	}
   1740 
   1741 /*	printf("%x\n", pa);*/
   1742 	return(pa);
   1743 }
   1744 
   1745 
   1746 #if 0
   1747 /* Return -1 on failure. */
   1748 static paddr_t
   1749 ofw_releasephys(pa, size)
   1750 	paddr_t pa;
   1751 	psize_t size;
   1752 {
   1753 	int mem_ihandle = ofw_mem_ihandle();
   1754 
   1755 /*	printf("ofw_releasephys (%x, %x)\n", pa, size);*/
   1756 
   1757 	return (OF_call_method_1("release", mem_ihandle, 2, pa, size));
   1758 }
   1759 #endif
   1760 
   1761 /* Return -1 on failure. */
   1762 static vaddr_t
   1763 ofw_claimvirt(va, size, align)
   1764 	vaddr_t va;
   1765 	vsize_t size;
   1766 	vaddr_t align;
   1767 {
   1768 	int mmu_ihandle = ofw_mmu_ihandle();
   1769 
   1770 	/*printf("ofw_claimvirt (%x, %x, %x) --> ", va, size, align);*/
   1771 	if (align == 0) {
   1772 		/* Allocate at specified base; alignment is ignored. */
   1773 		va = OF_call_method_1("claim", mmu_ihandle, 3, va, size, align);
   1774 	} else {
   1775 		/* Allocate anywhere, with specified alignment. */
   1776 		va = OF_call_method_1("claim", mmu_ihandle, 2, size, align);
   1777 	}
   1778 
   1779 	/*printf("%x\n", va);*/
   1780 	return(va);
   1781 }
   1782 
   1783 /* Return -1 if no mapping. */
   1784 paddr_t
   1785 ofw_gettranslation(va)
   1786 	vaddr_t va;
   1787 {
   1788 	int mmu_ihandle = ofw_mmu_ihandle();
   1789 	paddr_t pa;
   1790 	int mode;
   1791 	int exists;
   1792 
   1793 #ifdef OFW_DEBUG
   1794 	printf("ofw_gettranslation (%x) --> ", (uint32_t)va);
   1795 #endif
   1796 	exists = 0;	    /* gets set to true if translation exists */
   1797 	if (OF_call_method("translate", mmu_ihandle, 1, 3, va, &pa, &mode,
   1798 	    &exists) != 0)
   1799 		return(-1);
   1800 
   1801 #ifdef OFW_DEBUG
   1802 	printf("%d %x\n", exists, (uint32_t)pa);
   1803 #endif
   1804 	return(exists ? pa : -1);
   1805 }
   1806 
   1807 
   1808 static void
   1809 ofw_settranslation(va, pa, size, mode)
   1810 	vaddr_t va;
   1811 	paddr_t pa;
   1812 	vsize_t size;
   1813 	int mode;
   1814 {
   1815 	int mmu_ihandle = ofw_mmu_ihandle();
   1816 
   1817 #ifdef OFW_DEBUG
   1818 	printf("ofw_settranslation (%x, %x, %x, %x) --> void", (uint32_t)va,
   1819 	    (uint32_t)pa, (uint32_t)size, (uint32_t)mode);
   1820 #endif
   1821 	if (OF_call_method("map", mmu_ihandle, 4, 0, pa, va, size, mode) != 0)
   1822 		panic("ofw_settranslation failed");
   1823 }
   1824 
   1825 /*
   1826  *  Allocation routine used before the kernel takes over memory.
   1827  *  Use this for efficient storage for things that aren't rounded to
   1828  *  page size.
   1829  *
   1830  *  The point here is not necessarily to be very efficient (even though
   1831  *  that's sort of nice), but to do proper dynamic allocation to avoid
   1832  *  size-limitation errors.
   1833  *
   1834  */
   1835 
   1836 typedef struct _leftover {
   1837 	struct _leftover *pNext;
   1838 	vsize_t size;
   1839 } LEFTOVER, *PLEFTOVER;
   1840 
   1841 /* leftover bits of pages.  first word is pointer to next.
   1842    second word is size of leftover */
   1843 static PLEFTOVER leftovers = NULL;
   1844 
   1845 static void *
   1846 ofw_malloc(size)
   1847 	vsize_t size;
   1848 {
   1849 	PLEFTOVER   *ppLeftover;
   1850 	PLEFTOVER   pLeft;
   1851 	pv_addr_t   new;
   1852 	vsize_t   newSize, claim_size;
   1853 
   1854 	/* round and set minimum size */
   1855 	size = max(sizeof(LEFTOVER),
   1856 	    ((size + (sizeof(LEFTOVER) - 1)) & ~(sizeof(LEFTOVER) - 1)));
   1857 
   1858 	for (ppLeftover = &leftovers; *ppLeftover;
   1859 	    ppLeftover = &((*ppLeftover)->pNext))
   1860 		if ((*ppLeftover)->size >= size)
   1861 			break;
   1862 
   1863 	if (*ppLeftover) { /* have a leftover of the right size */
   1864 		/* remember the leftover */
   1865 		new.pv_va = (vaddr_t)*ppLeftover;
   1866 		if ((*ppLeftover)->size < (size + sizeof(LEFTOVER))) {
   1867 			/* splice out of chain */
   1868 			*ppLeftover = (*ppLeftover)->pNext;
   1869 		} else {
   1870 			/* remember the next pointer */
   1871 			pLeft = (*ppLeftover)->pNext;
   1872 			newSize = (*ppLeftover)->size - size; /* reduce size */
   1873 			/* move pointer */
   1874 			*ppLeftover = (PLEFTOVER)(((vaddr_t)*ppLeftover)
   1875 			    + size);
   1876 			(*ppLeftover)->pNext = pLeft;
   1877 			(*ppLeftover)->size  = newSize;
   1878 		}
   1879 	} else {
   1880 		claim_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
   1881 		ofw_claimpages(&virt_freeptr, &new, claim_size);
   1882 		if ((size + sizeof(LEFTOVER)) <= claim_size) {
   1883 			pLeft = (PLEFTOVER)(new.pv_va + size);
   1884 			pLeft->pNext = leftovers;
   1885 			pLeft->size = claim_size - size;
   1886 			leftovers = pLeft;
   1887 		}
   1888 	}
   1889 
   1890 	return (void *)(new.pv_va);
   1891 }
   1892 
   1893 /*
   1894  *  Here is a really, really sleazy free.  It's not used right now,
   1895  *  because it's not worth the extra complexity for just a few bytes.
   1896  *
   1897  */
   1898 #if 0
   1899 static void
   1900 ofw_free(addr, size)
   1901 	vaddr_t addr;
   1902 	vsize_t size;
   1903 {
   1904 	PLEFTOVER pLeftover = (PLEFTOVER)addr;
   1905 
   1906 	/* splice right into list without checks or compaction */
   1907 	pLeftover->pNext = leftovers;
   1908 	pLeftover->size  = size;
   1909 	leftovers        = pLeftover;
   1910 }
   1911 #endif
   1912 
   1913 /*
   1914  *  Allocate and zero round(size)/PAGE_SIZE pages of memory.
   1915  *  We guarantee that the allocated memory will be
   1916  *  aligned to a boundary equal to the smallest power of
   1917  *  2 greater than or equal to size.
   1918  *  free_pp is an IN/OUT parameter which points to the
   1919  *  last allocated virtual address in an allocate-downwards
   1920  *  stack.  pv_p is an OUT parameter which contains the
   1921  *  virtual and physical base addresses of the allocated
   1922  *  memory.
   1923  */
   1924 static void
   1925 ofw_claimpages(free_pp, pv_p, size)
   1926 	vaddr_t *free_pp;
   1927 	pv_addr_t *pv_p;
   1928 	vsize_t size;
   1929 {
   1930 	/* round-up to page boundary */
   1931 	vsize_t alloc_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
   1932 	vsize_t aligned_size;
   1933 	vaddr_t va;
   1934 	paddr_t pa;
   1935 
   1936 	if (alloc_size == 0)
   1937 		panic("ofw_claimpages zero");
   1938 
   1939 	for (aligned_size = 1; aligned_size < alloc_size; aligned_size <<= 1)
   1940 		;
   1941 
   1942 	/*  The only way to provide the alignment guarantees is to
   1943 	 *  allocate the virtual and physical ranges separately,
   1944 	 *  then do an explicit map call.
   1945 	 */
   1946 	va = (*free_pp & ~(aligned_size - 1)) - aligned_size;
   1947 	if (ofw_claimvirt(va, alloc_size, 0) != va)
   1948 		panic("ofw_claimpages va alloc");
   1949 	pa = ofw_claimphys(0, alloc_size, aligned_size);
   1950 	if (pa == -1)
   1951 		panic("ofw_claimpages pa alloc");
   1952 	/* XXX - what mode? -JJK */
   1953 	ofw_settranslation(va, pa, alloc_size, -1);
   1954 
   1955 	/* The memory's mapped-in now, so we can zero it. */
   1956 	bzero((char *)va, alloc_size);
   1957 
   1958 	/* Set OUT parameters. */
   1959 	*free_pp = va;
   1960 	pv_p->pv_va = va;
   1961 	pv_p->pv_pa = pa;
   1962 }
   1963 
   1964 
   1965 static void
   1966 ofw_discardmappings(L2pagetable, va, size)
   1967 	vaddr_t L2pagetable;
   1968 	vaddr_t va;
   1969 	vsize_t size;
   1970 {
   1971 	/* round-up to page boundary */
   1972 	vsize_t alloc_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
   1973 	int npages = alloc_size / PAGE_SIZE;
   1974 
   1975 	if (npages == 0)
   1976 		panic("ofw_discardmappings zero");
   1977 
   1978 	/* Discard each mapping. */
   1979 	for (; npages > 0; va += PAGE_SIZE, npages--) {
   1980 		/* Sanity. The current entry should be non-null. */
   1981 		if (ReadWord(L2pagetable + ((va >> 10) & 0x00000FFC)) == 0)
   1982 			panic("ofw_discardmappings zero entry");
   1983 
   1984 		/* Clear the entry. */
   1985 		WriteWord(L2pagetable + ((va >> 10) & 0x00000FFC), 0);
   1986 	}
   1987 }
   1988 
   1989 
   1990 static void
   1991 ofw_initallocator(void)
   1992 {
   1993 
   1994 }
   1995 
   1996 #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
   1997 static void
   1998 reset_screen()
   1999 {
   2000 
   2001 	if ((console_ihandle == 0) || (console_ihandle == -1))
   2002 		return;
   2003 
   2004 	OF_call_method("install", console_ihandle, 0, 0);
   2005 }
   2006 #endif /* (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0) */
   2007