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