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