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