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