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