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