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