xref: /netbsd-src/sys/arch/shark/ofw/ofw.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*	$NetBSD: ofw.c,v 1.54 2010/11/12 13:40:10 uebayasi 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.54 2010/11/12 13:40:10 uebayasi 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.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 cpu_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 	cpu_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 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
792 
793 	/* Set-up the various globals which describe physical memory for pmap. */
794 	{
795 		struct mem_region *mp;
796 		int totalcnt;
797 		int availcnt;
798 
799 		/* physmem, physical_start, physical_end */
800 		physmem = 0;
801 		for (totalcnt = 0, mp = OFphysmem; totalcnt < nOFphysmem;
802 		    totalcnt++, mp++) {
803 #ifdef	OLDPRINTFS
804 			printf("physmem: %x, %x\n", mp->start, mp->size);
805 #endif
806 			physmem += btoc(mp->size);
807 		}
808 		physical_start = OFphysmem[0].start;
809 		mp--;
810 		physical_end = mp->start + mp->size;
811 
812 		/* free_pages, physical_freestart, physical_freeend */
813 		free_pages = 0;
814 		for (availcnt = 0, mp = OFphysavail; availcnt < nOFphysavail;
815 		    availcnt++, mp++) {
816 #ifdef	OLDPRINTFS
817 			printf("physavail: %x, %x\n", mp->start, mp->size);
818 #endif
819 			free_pages += btoc(mp->size);
820 		}
821 		physical_freestart = OFphysavail[0].start;
822 		mp--;
823 		physical_freeend = mp->start + mp->size;
824 #ifdef	OLDPRINTFS
825 		printf("pmap_bootstrap:  physmem = %x, free_pages = %x\n",
826 		    physmem, free_pages);
827 #endif
828 
829 		/*
830 		 *  This is a hack to work with the existing pmap code.
831 		 *  That code depends on a RiscPC BootConfig structure
832 		 *  containing, among other things, an array describing
833 		 *  the regions of physical memory.  So, for now, we need
834 		 *  to stuff our OFW-derived physical memory info into a
835 		 *  "fake" BootConfig structure.
836 		 *
837 		 *  An added twist is that we initialize the BootConfig
838 		 *  structure with our "available" physical memory regions
839 		 *  rather than the "total" physical memory regions.  Why?
840 		 *  Because:
841 		 *
842 		 *   (a) the VM code requires that the "free" pages it is
843 		 *       initialized with have consecutive indices.  This
844 		 *       allows it to use more efficient data structures
845 		 *       (presumably).
846 		 *   (b) the current pmap routines which report the initial
847 		 *       set of free page indices (pmap_next_page) and
848 		 *       which map addresses to indices (pmap_page_index)
849 		 *       assume that the free pages are consecutive across
850 		 *       memory region boundaries.
851 		 *
852 		 *  This means that memory which is "stolen" at startup time
853 		 *  (say, for page descriptors) MUST come from either the
854 		 *  bottom of the first region or the top of the last.
855 		 *
856 		 *  This requirement doesn't mesh well with OFW (or at least
857 		 *  our use of it).  We can get around it for the time being
858 		 *  by pretending that our "available" region array describes
859 		 *  all of our physical memory.  This may cause some important
860 		 *  information to be excluded from a dump file, but so far
861 		 *  I haven't come across any other negative effects.
862 		 *
863 		 *  In the long-run we should fix the index
864 		 *  generation/translation code in the pmap module.
865 		 */
866 
867 		if (DRAM_BLOCKS < (availcnt + 1))
868 			panic("more ofw memory regions than bootconfig blocks");
869 
870 		for (i = 0, mp = OFphysavail; i < nOFphysavail; i++, mp++) {
871 			bootconfig.dram[i].address = mp->start;
872 			bootconfig.dram[i].pages = btoc(mp->size);
873 		}
874 		bootconfig.dramblocks = availcnt;
875 	}
876 
877 	/* Load memory into UVM. */
878 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
879 
880 	/* XXX Please kill this code dead. */
881 	for (i = 0; i < bootconfig.dramblocks; i++) {
882 		paddr_t start = (paddr_t)bootconfig.dram[i].address;
883 		paddr_t end = start + (bootconfig.dram[i].pages * PAGE_SIZE);
884 #if NISADMA > 0
885 		paddr_t istart, isize;
886 #endif
887 
888 		if (start < physical_freestart)
889 			start = physical_freestart;
890 		if (end > physical_freeend)
891 			end = physical_freeend;
892 
893 #if 0
894 		printf("%d: %lx -> %lx\n", loop, start, end - 1);
895 #endif
896 
897 #if NISADMA > 0
898 		if (arm32_dma_range_intersect(shark_isa_dma_ranges,
899 					      shark_isa_dma_nranges,
900 					      start, end - start,
901 					      &istart, &isize)) {
902 			/*
903 			 * Place the pages that intersect with the
904 			 * ISA DMA range onto the ISA DMA free list.
905 			 */
906 #if 0
907 			printf("    ISADMA 0x%lx -> 0x%lx\n", istart,
908 			    istart + isize - 1);
909 #endif
910 			uvm_page_physload(atop(istart),
911 			    atop(istart + isize), atop(istart),
912 			    atop(istart + isize), VM_FREELIST_ISADMA);
913 
914 			/*
915 			 * Load the pieces that come before the
916 			 * intersection onto the default free list.
917 			 */
918 			if (start < istart) {
919 #if 0
920 				printf("    BEFORE 0x%lx -> 0x%lx\n",
921 				    start, istart - 1);
922 #endif
923 				uvm_page_physload(atop(start),
924 				    atop(istart), atop(start),
925 				    atop(istart), VM_FREELIST_DEFAULT);
926 			}
927 
928 			/*
929 			 * Load the pieces that come after the
930 			 * intersection onto the default free list.
931 			 */
932 			if ((istart + isize) < end) {
933 #if 0
934 				printf("     AFTER 0x%lx -> 0x%lx\n",
935 				    (istart + isize), end - 1);
936 #endif
937 				uvm_page_physload(atop(istart + isize),
938 				    atop(end), atop(istart + isize),
939 				    atop(end), VM_FREELIST_DEFAULT);
940 			}
941 		} else {
942 			uvm_page_physload(atop(start), atop(end),
943 			    atop(start), atop(end), VM_FREELIST_DEFAULT);
944 		}
945 #else /* NISADMA > 0 */
946 		uvm_page_physload(atop(start), atop(end),
947 		    atop(start), atop(end), VM_FREELIST_DEFAULT);
948 #endif /* NISADMA > 0 */
949 	}
950 
951 	/* Initialize pmap module. */
952 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
953 }
954 
955 
956 /*
957  ************************************************************
958 
959   Routines private to this module
960 
961  ************************************************************
962  */
963 
964 /* N.B.  Not supposed to call printf in callback-handler!  Could deadlock! */
965 static void
966 ofw_callbackhandler(void *v)
967 {
968 	struct ofw_cbargs *args = v;
969 	char *name = args->name;
970 	int nargs = args->nargs;
971 	int nreturns = args->nreturns;
972 	int *args_n_results = args->args_n_results;
973 
974 	ofw_callbacks++;
975 
976 #if defined(OFWGENCFG)
977 	/* Check this first, so that we don't waste IRQ time parsing. */
978 	if (strcmp(name, "tick") == 0) {
979 		vaddr_t frame;
980 
981 		/* Check format. */
982 		if (nargs != 1 || nreturns < 1) {
983 			args_n_results[nargs] = -1;
984 			args->nreturns = 1;
985 			return;
986 		}
987 		args_n_results[nargs] =	0;	/* properly formatted request */
988 
989 		/*
990 		 *  Note that we are running in the IRQ frame, with interrupts
991 		 *  disabled.
992 		 *
993 		 *  We need to do two things here:
994 		 *    - copy a few words out of the input frame into a global
995 		 *      area, for later use by our real tick-handling code
996 		 *    - patch a few words in the frame so that when OFW returns
997 		 *      from the interrupt it will resume with our handler
998 		 *      rather than the code that was actually interrupted.
999 		 *      Our handler will resume when it finishes with the code
1000 		 *      that was actually interrupted.
1001 		 *
1002 		 *  It's simplest to do this in assembler, since it requires
1003 		 *  switching frames and grovelling about with registers.
1004 		 */
1005 		frame = (vaddr_t)args_n_results[0];
1006 		if (ofw_handleticks)
1007 			dotickgrovelling(frame);
1008 		args_n_results[nargs + 1] = frame;
1009 		args->nreturns = 1;
1010 	} else
1011 #endif
1012 
1013 	if (strcmp(name, "map") == 0) {
1014 		vaddr_t va;
1015 		paddr_t pa;
1016 		vsize_t size;
1017 		int mode;
1018 		int ap_bits;
1019 		int dom_bits;
1020 		int cb_bits;
1021 
1022 		/* Check format. */
1023 		if (nargs != 4 || nreturns < 2) {
1024 			args_n_results[nargs] = -1;
1025 			args->nreturns = 1;
1026 			return;
1027 		}
1028 		args_n_results[nargs] =	0;	/* properly formatted request */
1029 
1030 		pa = (paddr_t)args_n_results[0];
1031 		va = (vaddr_t)args_n_results[1];
1032 		size = (vsize_t)args_n_results[2];
1033 		mode = args_n_results[3];
1034 		ap_bits =  (mode & 0x00000C00);
1035 		dom_bits = (mode & 0x000001E0);
1036 		cb_bits =  (mode & 0x000000C0);
1037 
1038 		/* Sanity checks. */
1039 		if ((va & PGOFSET) != 0 || va < OFW_VIRT_BASE ||
1040 		    (va + size) > (OFW_VIRT_BASE + OFW_VIRT_SIZE) ||
1041 		    (pa & PGOFSET) != 0 || (size & PGOFSET) != 0 ||
1042 		    size == 0 || (dom_bits >> 5) != 0) {
1043 			args_n_results[nargs + 1] = -1;
1044 			args->nreturns = 1;
1045 			return;
1046 		}
1047 
1048 		/* Write-back anything stuck in the cache. */
1049 		cpu_idcache_wbinv_all();
1050 
1051 		/* Install new mappings. */
1052 		{
1053 			pt_entry_t *pte = vtopte(va);
1054 			int npages = size >> PGSHIFT;
1055 
1056 			ap_bits >>= 10;
1057 			for (; npages > 0; pte++, pa += PAGE_SIZE, npages--)
1058 				*pte = (pa | L2_AP(ap_bits) | L2_TYPE_S |
1059 				    cb_bits);
1060 			PTE_SYNC_RANGE(vtopte(va), size >> PGSHIFT);
1061 		}
1062 
1063 		/* Clean out tlb. */
1064 		tlb_flush();
1065 
1066 		args_n_results[nargs + 1] = 0;
1067 		args->nreturns = 2;
1068 	} else if (strcmp(name, "unmap") == 0) {
1069 		vaddr_t va;
1070 		vsize_t size;
1071 
1072 		/* Check format. */
1073 		if (nargs != 2 || nreturns < 1) {
1074 			args_n_results[nargs] = -1;
1075 			args->nreturns = 1;
1076 			return;
1077 		}
1078 		args_n_results[nargs] =	0;	/* properly formatted request */
1079 
1080 		va = (vaddr_t)args_n_results[0];
1081 		size = (vsize_t)args_n_results[1];
1082 
1083 		/* Sanity checks. */
1084 		if ((va & PGOFSET) != 0 || va < OFW_VIRT_BASE ||
1085 		    (va + size) > (OFW_VIRT_BASE + OFW_VIRT_SIZE) ||
1086 		    (size & PGOFSET) != 0 || size == 0) {
1087 			args_n_results[nargs + 1] = -1;
1088 			args->nreturns = 1;
1089 			return;
1090 		}
1091 
1092 		/* Write-back anything stuck in the cache. */
1093 		cpu_idcache_wbinv_all();
1094 
1095 		/* Zero the mappings. */
1096 		{
1097 			pt_entry_t *pte = vtopte(va);
1098 			int npages = size >> PGSHIFT;
1099 
1100 			for (; npages > 0; pte++, npages--)
1101 				*pte = 0;
1102 			PTE_SYNC_RANGE(vtopte(va), size >> PGSHIFT);
1103 		}
1104 
1105 		/* Clean out tlb. */
1106 		tlb_flush();
1107 
1108 		args->nreturns = 1;
1109 	} else if (strcmp(name, "translate") == 0) {
1110 		vaddr_t va;
1111 		paddr_t pa;
1112 		int mode;
1113 		pt_entry_t pte;
1114 
1115 		/* Check format. */
1116 		if (nargs != 1 || nreturns < 4) {
1117 			args_n_results[nargs] = -1;
1118 			args->nreturns = 1;
1119 			return;
1120 		}
1121 		args_n_results[nargs] =	0;	/* properly formatted request */
1122 
1123 		va = (vaddr_t)args_n_results[0];
1124 
1125 		/* Sanity checks.
1126 		 * For now, I am only willing to translate va's in the
1127 		 * "ofw range." Eventually, I may be more generous. -JJK
1128 		 */
1129 		if ((va & PGOFSET) != 0 ||  va < OFW_VIRT_BASE ||
1130 		    va >= (OFW_VIRT_BASE + OFW_VIRT_SIZE)) {
1131 			args_n_results[nargs + 1] = -1;
1132 			args->nreturns = 1;
1133 			return;
1134 		}
1135 
1136 		/* Lookup mapping. */
1137 		pte = *vtopte(va);
1138 		if (pte == 0) {
1139 			/* No mapping. */
1140 			args_n_results[nargs + 1] = -1;
1141 			args->nreturns = 2;
1142 		} else {
1143 			/* Existing mapping. */
1144 			pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
1145 			mode = (pte & 0x0C00) | (0 << 5) | (pte & 0x000C);	/* AP | DOM | CB */
1146 
1147 			args_n_results[nargs + 1] = 0;
1148 			args_n_results[nargs + 2] = pa;
1149 			args_n_results[nargs + 3] =	mode;
1150 			args->nreturns = 4;
1151 		}
1152 	} else if (strcmp(name, "claim-phys") == 0) {
1153 		struct pglist alloclist;
1154 		paddr_t low, high, align;
1155 		psize_t size;
1156 
1157 		/*
1158 		 * XXX
1159 		 * XXX THIS IS A GROSS HACK AND NEEDS TO BE REWRITTEN. -- cgd
1160 		 * XXX
1161 		 */
1162 
1163 		/* Check format. */
1164 		if (nargs != 4 || nreturns < 3) {
1165 			args_n_results[nargs] = -1;
1166 			args->nreturns = 1;
1167 			return;
1168 		}
1169 		args_n_results[nargs] =	0;	/* properly formatted request */
1170 
1171 		low = args_n_results[0];
1172 		size = args_n_results[2];
1173 		align = args_n_results[3];
1174 		high = args_n_results[1] + size;
1175 
1176 #if 0
1177 		printf("claim-phys: low = 0x%x, size = 0x%x, align = 0x%x, high = 0x%x\n",
1178 		    low, size, align, high);
1179 		align = size;
1180 		printf("forcing align to be 0x%x\n", align);
1181 #endif
1182 
1183 		args_n_results[nargs + 1] =
1184 		uvm_pglistalloc(size, low, high, align, 0, &alloclist, 1, 0);
1185 #if 0
1186 		printf(" -> 0x%lx", args_n_results[nargs + 1]);
1187 #endif
1188 		if (args_n_results[nargs + 1] != 0) {
1189 #if 0
1190 			printf("(failed)\n");
1191 #endif
1192 			args_n_results[nargs + 1] = -1;
1193 			args->nreturns = 2;
1194 			return;
1195 		}
1196 		args_n_results[nargs + 2] = VM_PAGE_TO_PHYS(alloclist.tqh_first);
1197 #if 0
1198 		printf("(succeeded: pa = 0x%lx)\n", args_n_results[nargs + 2]);
1199 #endif
1200 		args->nreturns = 3;
1201 
1202 	} else if (strcmp(name, "release-phys") == 0) {
1203 		printf("unimplemented ofw callback - %s\n", name);
1204 		args_n_results[nargs] = -1;
1205 		args->nreturns = 1;
1206 	} else if (strcmp(name, "claim-virt") == 0) {
1207 		vaddr_t va;
1208 		vsize_t size;
1209 		vaddr_t align;
1210 
1211 		/* XXX - notyet */
1212 /*		printf("unimplemented ofw callback - %s\n", name);*/
1213 		args_n_results[nargs] = -1;
1214 		args->nreturns = 1;
1215 		return;
1216 
1217 		/* Check format. */
1218 		if (nargs != 2 || nreturns < 3) {
1219 		    args_n_results[nargs] = -1;
1220 		    args->nreturns = 1;
1221 		    return;
1222 		}
1223 		args_n_results[nargs] =	0;	/* properly formatted request */
1224 
1225 		/* Allocate size bytes with specified alignment. */
1226 		size = (vsize_t)args_n_results[0];
1227 		align = (vaddr_t)args_n_results[1];
1228 		if (align % PAGE_SIZE != 0) {
1229 			args_n_results[nargs + 1] = -1;
1230 			args->nreturns = 2;
1231 			return;
1232 		}
1233 
1234 		if (va == 0) {
1235 			/* Couldn't allocate. */
1236 			args_n_results[nargs + 1] = -1;
1237 			args->nreturns = 2;
1238 		} else {
1239 			/* Successful allocation. */
1240 			args_n_results[nargs + 1] = 0;
1241 			args_n_results[nargs + 2] = va;
1242 			args->nreturns = 3;
1243 		}
1244 	} else if (strcmp(name, "release-virt") == 0) {
1245 		vaddr_t va;
1246 		vsize_t size;
1247 
1248 		/* XXX - notyet */
1249 		printf("unimplemented ofw callback - %s\n", name);
1250 		args_n_results[nargs] = -1;
1251 		args->nreturns = 1;
1252 		return;
1253 
1254 		/* Check format. */
1255 		if (nargs != 2 || nreturns < 1) {
1256 			args_n_results[nargs] = -1;
1257 			args->nreturns = 1;
1258 			return;
1259 		}
1260 		args_n_results[nargs] =	0;	/* properly formatted request */
1261 
1262 		/* Release bytes. */
1263 		va = (vaddr_t)args_n_results[0];
1264 		size = (vsize_t)args_n_results[1];
1265 
1266 		args->nreturns = 1;
1267 	} else {
1268 		args_n_results[nargs] = -1;
1269 		args->nreturns = 1;
1270 	}
1271 }
1272 
1273 static void
1274 ofw_construct_proc0_addrspace(void)
1275 {
1276 	int i, oft;
1277 	static pv_addr_t proc0_pt_sys;
1278 	static pv_addr_t proc0_pt_kernel[KERNEL_IMG_PTS];
1279 	static pv_addr_t proc0_pt_vmdata[KERNEL_VMDATA_PTS];
1280 	static pv_addr_t proc0_pt_ofw[KERNEL_OFW_PTS];
1281 	static pv_addr_t proc0_pt_io[KERNEL_IO_PTS];
1282 	static pv_addr_t msgbuf;
1283 	vaddr_t L1pagetable;
1284 	struct mem_translation *tp;
1285 
1286 	/* Set-up the system page. */
1287 	KASSERT(vector_page == 0);	/* XXX for now */
1288 	systempage.pv_va = ofw_claimvirt(vector_page, PAGE_SIZE, 0);
1289 	if (systempage.pv_va == -1) {
1290 		/* Something was already mapped to vector_page's VA. */
1291 		systempage.pv_va = vector_page;
1292 		systempage.pv_pa = ofw_gettranslation(vector_page);
1293 		if (systempage.pv_pa == -1)
1294 			panic("bogus result from gettranslation(vector_page)");
1295 	} else {
1296 		/* We were just allocated the page-length range at VA 0. */
1297 		if (systempage.pv_va != vector_page)
1298 			panic("bogus result from claimvirt(vector_page, PAGE_SIZE, 0)");
1299 
1300 		/* Now allocate a physical page, and establish the mapping. */
1301 		systempage.pv_pa = ofw_claimphys(0, PAGE_SIZE, PAGE_SIZE);
1302 		if (systempage.pv_pa == -1)
1303 			panic("bogus result from claimphys(0, PAGE_SIZE, PAGE_SIZE)");
1304 		ofw_settranslation(systempage.pv_va, systempage.pv_pa,
1305 		    PAGE_SIZE, -1);	/* XXX - mode? -JJK */
1306 
1307 		/* Zero the memory. */
1308 		memset((char *)systempage.pv_va, 0, PAGE_SIZE);
1309 	}
1310 
1311 	/* Allocate/initialize space for the proc0, NetBSD-managed */
1312 	/* page tables that we will be switching to soon. */
1313 	ofw_claimpages(&virt_freeptr, &kernel_l1pt, L1_TABLE_SIZE);
1314 	ofw_claimpages(&virt_freeptr, &proc0_pt_sys, L2_TABLE_SIZE);
1315 	for (i = 0; i < KERNEL_IMG_PTS; i++)
1316 		ofw_claimpages(&virt_freeptr, &proc0_pt_kernel[i], L2_TABLE_SIZE);
1317 	for (i = 0; i < KERNEL_VMDATA_PTS; i++)
1318 		ofw_claimpages(&virt_freeptr, &proc0_pt_vmdata[i], L2_TABLE_SIZE);
1319 	for (i = 0; i < KERNEL_OFW_PTS; i++)
1320 		ofw_claimpages(&virt_freeptr, &proc0_pt_ofw[i], L2_TABLE_SIZE);
1321 	for (i = 0; i < KERNEL_IO_PTS; i++)
1322 		ofw_claimpages(&virt_freeptr, &proc0_pt_io[i], L2_TABLE_SIZE);
1323 
1324 	/* Allocate/initialize space for stacks. */
1325 #ifndef	OFWGENCFG
1326 	ofw_claimpages(&virt_freeptr, &irqstack, PAGE_SIZE);
1327 #endif
1328 	ofw_claimpages(&virt_freeptr, &undstack, PAGE_SIZE);
1329 	ofw_claimpages(&virt_freeptr, &abtstack, PAGE_SIZE);
1330 	ofw_claimpages(&virt_freeptr, &kernelstack, UPAGES * PAGE_SIZE);
1331 
1332 	/* Allocate/initialize space for msgbuf area. */
1333 	ofw_claimpages(&virt_freeptr, &msgbuf, MSGBUFSIZE);
1334 	msgbufphys = msgbuf.pv_pa;
1335 
1336 	/* Construct the proc0 L1 pagetable. */
1337 	L1pagetable = kernel_l1pt.pv_va;
1338 
1339 	pmap_link_l2pt(L1pagetable, 0x0, &proc0_pt_sys);
1340 	for (i = 0; i < KERNEL_IMG_PTS; i++)
1341 		pmap_link_l2pt(L1pagetable, KERNEL_BASE + i * 0x00400000,
1342 		    &proc0_pt_kernel[i]);
1343 	for (i = 0; i < KERNEL_VMDATA_PTS; i++)
1344 		pmap_link_l2pt(L1pagetable, KERNEL_VM_BASE + i * 0x00400000,
1345 		    &proc0_pt_vmdata[i]);
1346 	for (i = 0; i < KERNEL_OFW_PTS; i++)
1347 		pmap_link_l2pt(L1pagetable, OFW_VIRT_BASE + i * 0x00400000,
1348 		    &proc0_pt_ofw[i]);
1349 	for (i = 0; i < KERNEL_IO_PTS; i++)
1350 		pmap_link_l2pt(L1pagetable, IO_VIRT_BASE + i * 0x00400000,
1351 		    &proc0_pt_io[i]);
1352 
1353 	/*
1354 	 * OK, we're done allocating.
1355 	 * Get a dump of OFW's translations, and make the appropriate
1356 	 * entries in the L2 pagetables that we just allocated.
1357 	 */
1358 
1359 	ofw_getvirttranslations();
1360 
1361 	for (oft = 0,  tp = OFtranslations; oft < nOFtranslations;
1362 	    oft++, tp++) {
1363 
1364 		vaddr_t va;
1365 		paddr_t pa;
1366 		int npages = tp->size / PAGE_SIZE;
1367 
1368 		/* Size must be an integral number of pages. */
1369 		if (npages == 0 || tp->size % PAGE_SIZE != 0)
1370 			panic("illegal ofw translation (size)");
1371 
1372 		/* Make an entry for each page in the appropriate table. */
1373 		for (va = tp->virt, pa = tp->phys; npages > 0;
1374 		    va += PAGE_SIZE, pa += PAGE_SIZE, npages--) {
1375 			/*
1376 			 * Map the top bits to the appropriate L2 pagetable.
1377 			 * The only allowable regions are page0, the
1378 			 * kernel-static area, and the ofw area.
1379 			 */
1380 			switch (va >> (L1_S_SHIFT + 2)) {
1381 			case 0:
1382 				/* page0 */
1383 				break;
1384 
1385 #if KERNEL_IMG_PTS != 2
1386 #error "Update ofw translation range list"
1387 #endif
1388 			case ( KERNEL_BASE                 >> (L1_S_SHIFT + 2)):
1389 			case ((KERNEL_BASE   + 0x00400000) >> (L1_S_SHIFT + 2)):
1390 				/* kernel static area */
1391 				break;
1392 
1393 			case ( OFW_VIRT_BASE               >> (L1_S_SHIFT + 2)):
1394 			case ((OFW_VIRT_BASE + 0x00400000) >> (L1_S_SHIFT + 2)):
1395 			case ((OFW_VIRT_BASE + 0x00800000) >> (L1_S_SHIFT + 2)):
1396 			case ((OFW_VIRT_BASE + 0x00C00000) >> (L1_S_SHIFT + 2)):
1397 				/* ofw area */
1398 				break;
1399 
1400 			case ( IO_VIRT_BASE               >> (L1_S_SHIFT + 2)):
1401 			case ((IO_VIRT_BASE + 0x00400000) >> (L1_S_SHIFT + 2)):
1402 			case ((IO_VIRT_BASE + 0x00800000) >> (L1_S_SHIFT + 2)):
1403 			case ((IO_VIRT_BASE + 0x00C00000) >> (L1_S_SHIFT + 2)):
1404 				/* io area */
1405 				break;
1406 
1407 			default:
1408 				/* illegal */
1409 				panic("illegal ofw translation (addr) %#lx",
1410 				    va);
1411 			}
1412 
1413 			/* Make the entry. */
1414 			pmap_map_entry(L1pagetable, va, pa,
1415 			    VM_PROT_READ|VM_PROT_WRITE,
1416 			    (tp->mode & 0xC) == 0xC ? PTE_CACHE
1417 						    : PTE_NOCACHE);
1418 		}
1419 	}
1420 
1421 	/*
1422 	 * We don't actually want some of the mappings that we just
1423 	 * set up to appear in proc0's address space.  In particular,
1424 	 * we don't want aliases to physical addresses that the kernel
1425 	 * has-mapped/will-map elsewhere.
1426 	 */
1427 	ofw_discardmappings(proc0_pt_kernel[KERNEL_IMG_PTS - 1].pv_va,
1428 	    msgbuf.pv_va, MSGBUFSIZE);
1429 
1430 	/* update the top of the kernel VM */
1431 	pmap_curmaxkvaddr =
1432 	    KERNEL_VM_BASE + (KERNEL_VMDATA_PTS * 0x00400000);
1433 
1434 	/*
1435          * gross hack for the sake of not thrashing the TLB and making
1436 	 * cache flush more efficient: blast l1 ptes for sections.
1437          */
1438 	for (oft = 0, tp = OFtranslations; oft < nOFtranslations; oft++, tp++) {
1439 		vaddr_t va = tp->virt;
1440 		paddr_t pa = tp->phys;
1441 
1442 		if (((va | pa) & L1_S_OFFSET) == 0) {
1443 			int nsections = tp->size / L1_S_SIZE;
1444 
1445 			while (nsections--) {
1446 				/* XXXJRT prot?? */
1447 				pmap_map_section(L1pagetable, va, pa,
1448 				    VM_PROT_READ|VM_PROT_WRITE,
1449 				    (tp->mode & 0xC) == 0xC ? PTE_CACHE
1450 							    : PTE_NOCACHE);
1451 				va += L1_S_SIZE;
1452 				pa += L1_S_SIZE;
1453 			}
1454 		}
1455 	}
1456 }
1457 
1458 
1459 static void
1460 ofw_getphysmeminfo(void)
1461 {
1462 	int phandle;
1463 	int mem_len;
1464 	int avail_len;
1465 	int i;
1466 
1467 	if ((phandle = OF_finddevice("/memory")) == -1 ||
1468 	    (mem_len = OF_getproplen(phandle, "reg")) <= 0 ||
1469 	    (OFphysmem = (struct mem_region *)ofw_malloc(mem_len)) == 0 ||
1470 	    OF_getprop(phandle, "reg", OFphysmem, mem_len) != mem_len ||
1471 	    (avail_len = OF_getproplen(phandle, "available")) <= 0 ||
1472  	    (OFphysavail = (struct mem_region *)ofw_malloc(avail_len)) == 0 ||
1473 	    OF_getprop(phandle, "available", OFphysavail, avail_len)
1474 	    != avail_len)
1475 		panic("can't get physmeminfo from OFW");
1476 
1477 	nOFphysmem = mem_len / sizeof(struct mem_region);
1478 	nOFphysavail = avail_len / sizeof(struct mem_region);
1479 
1480 	/*
1481 	 * Sort the blocks in each array into ascending address order.
1482 	 * Also, page-align all blocks.
1483 	 */
1484 	for (i = 0; i < 2; i++) {
1485 		struct mem_region *tmp = (i == 0) ? OFphysmem : OFphysavail;
1486 		struct mem_region *mp;
1487 		int cnt =  (i == 0) ? nOFphysmem : nOFphysavail;
1488 		int j;
1489 
1490 #ifdef	OLDPRINTFS
1491 		printf("ofw_getphysmeminfo:  %d blocks\n", cnt);
1492 #endif
1493 
1494 		/* XXX - Convert all the values to host order. -JJK */
1495 		for (j = 0, mp = tmp; j < cnt; j++, mp++) {
1496 			mp->start = of_decode_int((unsigned char *)&mp->start);
1497 			mp->size = of_decode_int((unsigned char *)&mp->size);
1498 		}
1499 
1500 		for (j = 0, mp = tmp; j < cnt; j++, mp++) {
1501 			u_int s, sz;
1502 			struct mem_region *mp1;
1503 
1504 			/* Page-align start of the block. */
1505 			s = mp->start % PAGE_SIZE;
1506 			if (s != 0) {
1507 				s = (PAGE_SIZE - s);
1508 
1509 				if (mp->size >= s) {
1510 					mp->start += s;
1511 					mp->size -= s;
1512 				}
1513 			}
1514 
1515 			/* Page-align the size. */
1516 			mp->size -= mp->size % PAGE_SIZE;
1517 
1518 			/* Handle empty block. */
1519 			if (mp->size == 0) {
1520 				memmove(mp, mp + 1, (cnt - (mp - tmp))
1521 				    * sizeof(struct mem_region));
1522 				cnt--;
1523 				mp--;
1524 				continue;
1525 			}
1526 
1527 			/* Bubble sort. */
1528 			s = mp->start;
1529 			sz = mp->size;
1530 			for (mp1 = tmp; mp1 < mp; mp1++)
1531 				if (s < mp1->start)
1532 					break;
1533 			if (mp1 < mp) {
1534 				memmove(mp1 + 1, mp1, (char *)mp - (char *)mp1);
1535 				mp1->start = s;
1536 				mp1->size = sz;
1537 			}
1538 		}
1539 
1540 #ifdef	OLDPRINTFS
1541 		for (mp = tmp; mp->size; mp++) {
1542 			printf("%x, %x\n", mp->start, mp->size);
1543 		}
1544 #endif
1545 	}
1546 }
1547 
1548 
1549 static void
1550 ofw_getvirttranslations(void)
1551 {
1552 	int mmu_phandle;
1553 	int mmu_ihandle;
1554 	int trans_len;
1555 	int over, len;
1556 	int i;
1557 	struct mem_translation *tp;
1558 
1559 	mmu_ihandle = ofw_mmu_ihandle();
1560 
1561 	/* overallocate to avoid increases during allocation */
1562 	over = 4 * sizeof(struct mem_translation);
1563 	if ((mmu_phandle = OF_instance_to_package(mmu_ihandle)) == -1 ||
1564 	    (len = OF_getproplen(mmu_phandle, "translations")) <= 0 ||
1565 	    (OFtranslations = ofw_malloc(len + over)) == 0 ||
1566 	    (trans_len = OF_getprop(mmu_phandle, "translations",
1567 	    OFtranslations, len + over)) > (len + over))
1568 		panic("can't get virttranslations from OFW");
1569 
1570 	/* XXX - Convert all the values to host order. -JJK */
1571 	nOFtranslations = trans_len / sizeof(struct mem_translation);
1572 #ifdef	OLDPRINTFS
1573 	printf("ofw_getvirtmeminfo:  %d blocks\n", nOFtranslations);
1574 #endif
1575 	for (i = 0, tp = OFtranslations; i < nOFtranslations; i++, tp++) {
1576 		tp->virt = of_decode_int((unsigned char *)&tp->virt);
1577 		tp->size = of_decode_int((unsigned char *)&tp->size);
1578 		tp->phys = of_decode_int((unsigned char *)&tp->phys);
1579 		tp->mode = of_decode_int((unsigned char *)&tp->mode);
1580 	}
1581 }
1582 
1583 /*
1584  * ofw_valloc: allocate blocks of VM for IO and other special purposes
1585  */
1586 typedef struct _vfree {
1587 	struct _vfree *pNext;
1588 	vaddr_t start;
1589 	vsize_t size;
1590 } VFREE, *PVFREE;
1591 
1592 static VFREE vfinitial = { NULL, IO_VIRT_BASE, IO_VIRT_SIZE };
1593 
1594 static PVFREE vflist = &vfinitial;
1595 
1596 static vaddr_t
1597 ofw_valloc(vsize_t size, vaddr_t align)
1598 {
1599 	PVFREE        *ppvf;
1600 	PVFREE        pNew;
1601 	vaddr_t       new;
1602 	vaddr_t       lead;
1603 
1604 	for (ppvf = &vflist; *ppvf; ppvf = &((*ppvf)->pNext)) {
1605 		if (align == 0) {
1606 			new = (*ppvf)->start;
1607 			lead = 0;
1608 		} else {
1609 			new  = ((*ppvf)->start + (align - 1)) & ~(align - 1);
1610 			lead = new - (*ppvf)->start;
1611 		}
1612 
1613 		if (((*ppvf)->size - lead) >= size) {
1614  			if (lead == 0) {
1615 				/* using whole block */
1616 				if (size == (*ppvf)->size) {
1617 					/* splice out of list */
1618 					(*ppvf) = (*ppvf)->pNext;
1619 				} else { /* tail of block is free */
1620 					(*ppvf)->start = new + size;
1621 					(*ppvf)->size -= size;
1622 				}
1623 			} else {
1624 				vsize_t tail = ((*ppvf)->start
1625 				    + (*ppvf)->size) - (new + size);
1626 				/* free space at beginning */
1627 				(*ppvf)->size = lead;
1628 
1629 				if (tail != 0) {
1630 					/* free space at tail */
1631 					pNew = ofw_malloc(sizeof(VFREE));
1632 					pNew->pNext  = (*ppvf)->pNext;
1633 					(*ppvf)->pNext = pNew;
1634 					pNew->start  = new + size;
1635 					pNew->size   = tail;
1636 				}
1637 			}
1638 			return new;
1639 		} /* END if */
1640 	} /* END for */
1641 
1642 	return -1;
1643 }
1644 
1645 vaddr_t
1646 ofw_map(paddr_t pa, vsize_t size, int cb_bits)
1647 {
1648 	vaddr_t va;
1649 
1650 	if ((va = ofw_valloc(size, size)) == -1)
1651 		panic("cannot alloc virtual memory for %#lx", pa);
1652 
1653 	ofw_claimvirt(va, size, 0); /* make sure OFW knows about the memory */
1654 
1655 	ofw_settranslation(va, pa, size, L2_AP(AP_KRW) | cb_bits);
1656 
1657 	return va;
1658 }
1659 
1660 static int
1661 ofw_mem_ihandle(void)
1662 {
1663 	static int mem_ihandle = 0;
1664 	int chosen;
1665 
1666 	if (mem_ihandle != 0)
1667 		return(mem_ihandle);
1668 
1669 	if ((chosen = OF_finddevice("/chosen")) == -1 ||
1670 	    OF_getprop(chosen, "memory", &mem_ihandle, sizeof(int)) < 0)
1671 		panic("ofw_mem_ihandle");
1672 
1673 	mem_ihandle = of_decode_int((unsigned char *)&mem_ihandle);
1674 
1675 	return(mem_ihandle);
1676 }
1677 
1678 
1679 static int
1680 ofw_mmu_ihandle(void)
1681 {
1682 	static int mmu_ihandle = 0;
1683 	int chosen;
1684 
1685 	if (mmu_ihandle != 0)
1686 		return(mmu_ihandle);
1687 
1688 	if ((chosen = OF_finddevice("/chosen")) == -1 ||
1689 	    OF_getprop(chosen, "mmu", &mmu_ihandle, sizeof(int)) < 0)
1690 		panic("ofw_mmu_ihandle");
1691 
1692 	mmu_ihandle = of_decode_int((unsigned char *)&mmu_ihandle);
1693 
1694 	return(mmu_ihandle);
1695 }
1696 
1697 
1698 /* Return -1 on failure. */
1699 static paddr_t
1700 ofw_claimphys(paddr_t pa, psize_t size, paddr_t align)
1701 {
1702 	int mem_ihandle = ofw_mem_ihandle();
1703 
1704 /*	printf("ofw_claimphys (%x, %x, %x) --> ", pa, size, align);*/
1705 	if (align == 0) {
1706 		/* Allocate at specified base; alignment is ignored. */
1707 		pa = OF_call_method_1("claim", mem_ihandle, 3, pa, size, align);
1708 	} else {
1709 		/* Allocate anywhere, with specified alignment. */
1710 		pa = OF_call_method_1("claim", mem_ihandle, 2, size, align);
1711 	}
1712 
1713 /*	printf("%x\n", pa);*/
1714 	return(pa);
1715 }
1716 
1717 
1718 #if 0
1719 /* Return -1 on failure. */
1720 static paddr_t
1721 ofw_releasephys(paddr_t pa, psize_t size)
1722 {
1723 	int mem_ihandle = ofw_mem_ihandle();
1724 
1725 /*	printf("ofw_releasephys (%x, %x)\n", pa, size);*/
1726 
1727 	return (OF_call_method_1("release", mem_ihandle, 2, pa, size));
1728 }
1729 #endif
1730 
1731 /* Return -1 on failure. */
1732 static vaddr_t
1733 ofw_claimvirt(vaddr_t va, vsize_t size, vaddr_t align)
1734 {
1735 	int mmu_ihandle = ofw_mmu_ihandle();
1736 
1737 	/*printf("ofw_claimvirt (%x, %x, %x) --> ", va, size, align);*/
1738 	if (align == 0) {
1739 		/* Allocate at specified base; alignment is ignored. */
1740 		va = OF_call_method_1("claim", mmu_ihandle, 3, va, size, align);
1741 	} else {
1742 		/* Allocate anywhere, with specified alignment. */
1743 		va = OF_call_method_1("claim", mmu_ihandle, 2, size, align);
1744 	}
1745 
1746 	/*printf("%x\n", va);*/
1747 	return(va);
1748 }
1749 
1750 /* Return -1 if no mapping. */
1751 paddr_t
1752 ofw_gettranslation(vaddr_t va)
1753 {
1754 	int mmu_ihandle = ofw_mmu_ihandle();
1755 	paddr_t pa;
1756 	int mode;
1757 	int exists;
1758 
1759 #ifdef OFW_DEBUG
1760 	printf("ofw_gettranslation (%x) --> ", (uint32_t)va);
1761 #endif
1762 	exists = 0;	    /* gets set to true if translation exists */
1763 	if (OF_call_method("translate", mmu_ihandle, 1, 3, va, &pa, &mode,
1764 	    &exists) != 0)
1765 		return(-1);
1766 
1767 #ifdef OFW_DEBUG
1768 	printf("%d %x\n", exists, (uint32_t)pa);
1769 #endif
1770 	return(exists ? pa : -1);
1771 }
1772 
1773 
1774 static void
1775 ofw_settranslation(vaddr_t va, paddr_t pa, vsize_t size, int mode)
1776 {
1777 	int mmu_ihandle = ofw_mmu_ihandle();
1778 
1779 #ifdef OFW_DEBUG
1780 	printf("ofw_settranslation (%x, %x, %x, %x) --> void", (uint32_t)va,
1781 	    (uint32_t)pa, (uint32_t)size, (uint32_t)mode);
1782 #endif
1783 	if (OF_call_method("map", mmu_ihandle, 4, 0, pa, va, size, mode) != 0)
1784 		panic("ofw_settranslation failed");
1785 }
1786 
1787 /*
1788  *  Allocation routine used before the kernel takes over memory.
1789  *  Use this for efficient storage for things that aren't rounded to
1790  *  page size.
1791  *
1792  *  The point here is not necessarily to be very efficient (even though
1793  *  that's sort of nice), but to do proper dynamic allocation to avoid
1794  *  size-limitation errors.
1795  *
1796  */
1797 
1798 typedef struct _leftover {
1799 	struct _leftover *pNext;
1800 	vsize_t size;
1801 } LEFTOVER, *PLEFTOVER;
1802 
1803 /* leftover bits of pages.  first word is pointer to next.
1804    second word is size of leftover */
1805 static PLEFTOVER leftovers = NULL;
1806 
1807 static void *
1808 ofw_malloc(vsize_t size)
1809 {
1810 	PLEFTOVER   *ppLeftover;
1811 	PLEFTOVER   pLeft;
1812 	pv_addr_t   new;
1813 	vsize_t   newSize, claim_size;
1814 
1815 	/* round and set minimum size */
1816 	size = max(sizeof(LEFTOVER),
1817 	    ((size + (sizeof(LEFTOVER) - 1)) & ~(sizeof(LEFTOVER) - 1)));
1818 
1819 	for (ppLeftover = &leftovers; *ppLeftover;
1820 	    ppLeftover = &((*ppLeftover)->pNext))
1821 		if ((*ppLeftover)->size >= size)
1822 			break;
1823 
1824 	if (*ppLeftover) { /* have a leftover of the right size */
1825 		/* remember the leftover */
1826 		new.pv_va = (vaddr_t)*ppLeftover;
1827 		if ((*ppLeftover)->size < (size + sizeof(LEFTOVER))) {
1828 			/* splice out of chain */
1829 			*ppLeftover = (*ppLeftover)->pNext;
1830 		} else {
1831 			/* remember the next pointer */
1832 			pLeft = (*ppLeftover)->pNext;
1833 			newSize = (*ppLeftover)->size - size; /* reduce size */
1834 			/* move pointer */
1835 			*ppLeftover = (PLEFTOVER)(((vaddr_t)*ppLeftover)
1836 			    + size);
1837 			(*ppLeftover)->pNext = pLeft;
1838 			(*ppLeftover)->size  = newSize;
1839 		}
1840 	} else {
1841 		claim_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
1842 		ofw_claimpages(&virt_freeptr, &new, claim_size);
1843 		if ((size + sizeof(LEFTOVER)) <= claim_size) {
1844 			pLeft = (PLEFTOVER)(new.pv_va + size);
1845 			pLeft->pNext = leftovers;
1846 			pLeft->size = claim_size - size;
1847 			leftovers = pLeft;
1848 		}
1849 	}
1850 
1851 	return (void *)(new.pv_va);
1852 }
1853 
1854 /*
1855  *  Here is a really, really sleazy free.  It's not used right now,
1856  *  because it's not worth the extra complexity for just a few bytes.
1857  *
1858  */
1859 #if 0
1860 static void
1861 ofw_free(vaddr_t addr, vsize_t size)
1862 {
1863 	PLEFTOVER pLeftover = (PLEFTOVER)addr;
1864 
1865 	/* splice right into list without checks or compaction */
1866 	pLeftover->pNext = leftovers;
1867 	pLeftover->size  = size;
1868 	leftovers        = pLeftover;
1869 }
1870 #endif
1871 
1872 /*
1873  *  Allocate and zero round(size)/PAGE_SIZE pages of memory.
1874  *  We guarantee that the allocated memory will be
1875  *  aligned to a boundary equal to the smallest power of
1876  *  2 greater than or equal to size.
1877  *  free_pp is an IN/OUT parameter which points to the
1878  *  last allocated virtual address in an allocate-downwards
1879  *  stack.  pv_p is an OUT parameter which contains the
1880  *  virtual and physical base addresses of the allocated
1881  *  memory.
1882  */
1883 static void
1884 ofw_claimpages(vaddr_t *free_pp, pv_addr_t *pv_p, vsize_t size)
1885 {
1886 	/* round-up to page boundary */
1887 	vsize_t alloc_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
1888 	vsize_t aligned_size;
1889 	vaddr_t va;
1890 	paddr_t pa;
1891 
1892 	if (alloc_size == 0)
1893 		panic("ofw_claimpages zero");
1894 
1895 	for (aligned_size = 1; aligned_size < alloc_size; aligned_size <<= 1)
1896 		;
1897 
1898 	/*  The only way to provide the alignment guarantees is to
1899 	 *  allocate the virtual and physical ranges separately,
1900 	 *  then do an explicit map call.
1901 	 */
1902 	va = (*free_pp & ~(aligned_size - 1)) - aligned_size;
1903 	if (ofw_claimvirt(va, alloc_size, 0) != va)
1904 		panic("ofw_claimpages va alloc");
1905 	pa = ofw_claimphys(0, alloc_size, aligned_size);
1906 	if (pa == -1)
1907 		panic("ofw_claimpages pa alloc");
1908 	/* XXX - what mode? -JJK */
1909 	ofw_settranslation(va, pa, alloc_size, -1);
1910 
1911 	/* The memory's mapped-in now, so we can zero it. */
1912 	memset((char *)va, 0, alloc_size);
1913 
1914 	/* Set OUT parameters. */
1915 	*free_pp = va;
1916 	pv_p->pv_va = va;
1917 	pv_p->pv_pa = pa;
1918 }
1919 
1920 
1921 static void
1922 ofw_discardmappings(vaddr_t L2pagetable, vaddr_t va, vsize_t size)
1923 {
1924 	/* round-up to page boundary */
1925 	vsize_t alloc_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
1926 	int npages = alloc_size / PAGE_SIZE;
1927 
1928 	if (npages == 0)
1929 		panic("ofw_discardmappings zero");
1930 
1931 	/* Discard each mapping. */
1932 	for (; npages > 0; va += PAGE_SIZE, npages--) {
1933 		/* Sanity. The current entry should be non-null. */
1934 		if (ReadWord(L2pagetable + ((va >> 10) & 0x00000FFC)) == 0)
1935 			panic("ofw_discardmappings zero entry");
1936 
1937 		/* Clear the entry. */
1938 		WriteWord(L2pagetable + ((va >> 10) & 0x00000FFC), 0);
1939 	}
1940 }
1941 
1942 
1943 static void
1944 ofw_initallocator(void)
1945 {
1946 
1947 }
1948 
1949 #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
1950 static void
1951 reset_screen(void)
1952 {
1953 
1954 	if ((console_ihandle == 0) || (console_ihandle == -1))
1955 		return;
1956 
1957 	OF_call_method("install", console_ihandle, 0, 0);
1958 }
1959 #endif /* (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0) */
1960