Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / shark / ofw / ofw.c
blob4c365f32ad471d4297a7edb94007bf4a61efa684
1 /* $NetBSD$ */
3 /*
4 * Copyright 1997
5 * Digital Equipment Corporation. All rights reserved.
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.
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.
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.
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.
37 * Routines for interfacing between NetBSD and OFW.
39 * Parts of this could be moved to an MI file in time. -JJK
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD$");
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>
53 #include <uvm/uvm_extern.h>
55 #include <dev/cons.h>
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>
65 #include <dev/ofw/openfirm.h>
66 #include <machine/ofw.h>
68 #include <netinet/in.h>
70 #if BOOT_FW_DHCP
71 #include <nfs/bootdata.h>
72 #endif
74 #ifdef SHARK
75 #include "machine/pio.h"
76 #include "machine/isa_machdep.h"
77 #endif
79 #include "isadma.h"
80 #include "igsfb_ofbus.h"
81 #include "vga_ofbus.h"
83 #define IO_VIRT_BASE (OFW_VIRT_BASE + OFW_VIRT_SIZE)
84 #define IO_VIRT_SIZE 0x01000000
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
91 #define KERNEL_VM_BASE (KERNEL_BASE + 0x01000000)
93 * The range 0xf1000000 - 0xf6ffffff is available for kernel VM space
94 * OFW sits at 0xf7000000
96 #define KERNEL_VM_SIZE 0x06000000
99 * Imported variables
101 extern BootConfig bootconfig; /* temporary, I hope */
103 #ifdef DIAGNOSTIC
104 /* NOTE: These variables will be removed, well some of them */
105 extern u_int current_mask;
106 #endif
108 extern int ofw_handleticks;
112 * Imported routines
114 extern void dump_spl_masks(void);
115 extern void dumpsys(void);
116 extern void dotickgrovelling(vaddr_t);
118 #define WriteWord(a, b) \
119 *((volatile unsigned int *)(a)) = (b)
121 #define ReadWord(a) \
122 (*((volatile unsigned int *)(a)))
126 * Exported variables
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;
141 paddr_t msgbufphys;
143 /* for storage allocation, used to be local to ofw_construct_proc0_addrspace */
144 static vaddr_t virt_freeptr;
146 int ofw_callbacks = 0; /* debugging counter */
148 #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
149 int console_ihandle = 0;
150 static void reset_screen(void);
151 #endif
153 /**************************************************************/
157 * Declarations and definitions private to this module
161 struct mem_region {
162 paddr_t start;
163 psize_t size;
166 struct mem_translation {
167 vaddr_t virt;
168 vsize_t size;
169 paddr_t phys;
170 unsigned int mode;
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;
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;
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;
195 struct dma_range {
196 paddr_t start;
197 psize_t size;
200 struct ofw_cbargs {
201 char *name;
202 int nargs;
203 int nreturns;
204 int args_n_results[12];
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;
218 /* The OFW client services handle. */
219 /* Initialized by ofw_init(). */
220 static ofw_handle_t ofw_client_services_handle;
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);
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.
253 /**************************************************************/
258 * Support routines for xxx_machdep.c
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.
264 * The exported routines are:
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
280 openfirmware(void *args)
282 int ofw_result;
283 u_int saved_irq_state;
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);
290 return(ofw_result);
294 void
295 ofw_init(ofw_handle_t ofw_handle)
297 ofw_client_services_handle = ofw_handle;
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).
319 virt_freeptr = KERNEL_BASE + (0x00400000 * KERNEL_IMG_PTS);
323 void
324 ofw_boot(int howto, char *bootstr)
327 #ifdef DIAGNOSTIC
328 printf("boot: howto=%08x curlwp=%p\n", howto, curlwp);
329 printf("current_mask=%08x\n", current_mask);
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]);
337 dump_spl_masks();
338 #endif
341 * If we are still cold then hit the air brakes
342 * and crash to earth fast
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*/
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.
359 if (!(howto & RB_NOSYNC))
360 bootsync();
362 /* Say NO to interrupts */
363 splhigh();
365 /* Do a dump if requested. */
366 if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
367 dumpsys();
369 /* Run any shutdown hooks */
370 doshutdownhooks();
372 pmf_system_shutdown(boothowto);
374 /* Make sure IRQ's are disabled */
375 IRQdisable;
377 if (howto & RB_HALT) {
378 printf("The operating system has halted.\n");
379 goto ofw_exit;
382 /* Tell the user we are booting */
383 printf("rebooting...\n");
385 /* Jump into the OFW boot routine. */
387 static char str[256];
388 char *ap = str, *ap1 = ap;
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++ = ' ';
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*/
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*/
424 #if BOOT_FW_DHCP
426 extern char *ip2dotted(struct in_addr);
429 * Get DHCP data from OFW
432 void
433 get_fw_dhcp_data(struct bootdata *bdp)
435 int chosen;
436 int dhcplen;
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;
447 * OFW saved a DHCP (or BOOTP) packet for us.
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");
455 * Collect the interesting data from DHCP into
456 * the bootdata structure.
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;
471 * If the DHCP packet did not contain all the necessary data,
472 * look in NVRAM for the missing parts.
475 int options;
476 int proplen;
477 #define BOOTJUNKV_SIZE 256
478 char bootjunkv[BOOTJUNKV_SIZE]; /* minimize stack usage */
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;
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;
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';
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);
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);
517 #endif /* BOOT_FW_DHCP */
519 void
520 ofw_getbootinfo(char **bp_pp, char **ba_pp)
522 int chosen;
523 int bp_len;
524 int ba_len;
525 char *bootpathv;
526 char *bootargsv;
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");
537 bootpathv = (char *)ofw_malloc(bp_len);
538 bootargsv = (char *)ofw_malloc(ba_len);
540 if (bp_len)
541 OF_getprop(chosen, "bootpath", bootpathv, bp_len);
542 else
543 bootpathv[0] = '\0';
545 if (ba_len)
546 OF_getprop(chosen, "bootargs", bootargsv, ba_len);
547 else
548 bootargsv[0] = '\0';
550 *bp_pp = bootpathv;
551 *ba_pp = bootargsv;
552 #ifdef DIAGNOSTIC
553 printf("bootpath=<%s>, bootargs=<%s>\n", bootpathv, bootargsv);
554 #endif
557 paddr_t
558 ofw_getcleaninfo(void)
560 int cpu;
561 vaddr_t vclean;
562 paddr_t pclean;
564 if ((cpu = OF_finddevice("/cpu")) == -1)
565 panic("no /cpu from OFW");
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;
575 if ((pclean = ofw_gettranslation(
576 of_decode_int((unsigned char *)&vclean))) == -1)
577 panic("OFW failed to translate cache flush address");
579 return pclean;
582 void
583 ofw_configisa(paddr_t *pio, paddr_t *pmem)
585 int vl;
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);
593 static void
594 ofw_configisaonly(paddr_t *pio, paddr_t *pmem)
596 int isa;
597 int rangeidx;
598 int size;
599 paddr_t hi, start;
600 struct isa_range ranges[2];
602 if ((isa = OF_finddevice("/isa")) == -1)
603 panic("OFW has no /isa device node");
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);
610 *pio = *pmem = -1;
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);
618 if (hi & 1) { /* then I/O space */
619 *pio = start;
620 } else {
621 *pmem = start;
623 } /* END for */
625 if ((*pio == -1) || (*pmem == -1))
626 panic("bad OFW /isa ranges property");
630 static void
631 ofw_configvl(int vl, paddr_t *pio, paddr_t *pmem)
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];
640 if ((isa = OF_finddevice("/vlbus/isa")) == -1)
641 panic("OFW has no /vlbus/isa device node");
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);
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);
654 *pio = -1;
655 *pmem = -1;
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);
668 if (hi & 1) { /* then I/O space */
669 *pio = start;
670 } else {
671 *pmem = start;
673 } /* END if */
674 } /* END for */
675 } /* END for */
677 if ((*pio == -1) || (*pmem == -1))
678 panic("bad OFW /isa ranges property");
681 #if NISADMA > 0
682 struct arm32_dma_range *shark_isa_dma_ranges;
683 int shark_isa_dma_nranges;
684 #endif
686 void
687 ofw_configisadma(paddr_t *pdma)
689 int root;
690 int rangeidx;
691 int size;
692 struct dma_range *dr;
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");
700 nOFdmaranges = size / sizeof(struct dma_range);
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
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
722 #ifdef DEBUG
723 printf("DMA ranges size = %d\n", size);
725 for (rangeidx = 0; rangeidx < nOFdmaranges; ++rangeidx) {
726 printf("%08lx %08lx\n",
727 (u_long)OFdmaranges[rangeidx].start,
728 (u_long)OFdmaranges[rangeidx].size);
730 #endif
734 * Memory configuration:
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.
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.
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.)
755 void
756 ofw_configmem(void)
758 int i;
760 /* Set-up proc0 address space. */
761 ofw_construct_proc0_addrspace();
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.
769 ofw_getphysmeminfo();
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.
776 /* First initialize our callback memory allocator. */
777 ofw_initallocator();
779 OF_set_callback(ofw_callbackhandler);
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));
788 * Moved from cpu_startup() as data_abort_handler() references
789 * this during uvm init
791 uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
793 /* Set-up the various globals which describe physical memory for pmap. */
795 struct mem_region *mp;
796 int totalcnt;
797 int availcnt;
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);
808 physical_start = OFphysmem[0].start;
809 mp--;
810 physical_end = mp->start + mp->size;
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);
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
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.
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:
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.
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.
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.
863 * In the long-run we should fix the index
864 * generation/translation code in the pmap module.
867 if (DRAM_BLOCKS < (availcnt + 1))
868 panic("more ofw memory regions than bootconfig blocks");
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);
874 bootconfig.dramblocks = availcnt;
877 /* Load memory into UVM. */
878 uvm_setpagesize(); /* initialize PAGE_SIZE-dependent variables */
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
888 if (start < physical_freestart)
889 start = physical_freestart;
890 if (end > physical_freeend)
891 end = physical_freeend;
893 #if 0
894 printf("%d: %lx -> %lx\n", loop, start, end - 1);
895 #endif
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)) {
903 * Place the pages that intersect with the
904 * ISA DMA range onto the ISA DMA free list.
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);
915 * Load the pieces that come before the
916 * intersection onto the default free list.
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);
929 * Load the pieces that come after the
930 * intersection onto the default free list.
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);
941 } else {
942 uvm_page_physload(atop(start), atop(end),
943 atop(start), atop(end), VM_FREELIST_DEFAULT);
945 #else /* NISADMA > 0 */
946 uvm_page_physload(atop(start), atop(end),
947 atop(start), atop(end), VM_FREELIST_DEFAULT);
948 #endif /* NISADMA > 0 */
951 /* Initialize pmap module. */
952 pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
957 ************************************************************
959 Routines private to this module
961 ************************************************************
964 /* N.B. Not supposed to call printf in callback-handler! Could deadlock! */
965 static void
966 ofw_callbackhandler(void *v)
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;
974 ofw_callbacks++;
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;
981 /* Check format. */
982 if (nargs != 1 || nreturns < 1) {
983 args_n_results[nargs] = -1;
984 args->nreturns = 1;
985 return;
987 args_n_results[nargs] = 0; /* properly formatted request */
990 * Note that we are running in the IRQ frame, with interrupts
991 * disabled.
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.
1002 * It's simplest to do this in assembler, since it requires
1003 * switching frames and grovelling about with registers.
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
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;
1022 /* Check format. */
1023 if (nargs != 4 || nreturns < 2) {
1024 args_n_results[nargs] = -1;
1025 args->nreturns = 1;
1026 return;
1028 args_n_results[nargs] = 0; /* properly formatted request */
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);
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;
1048 /* Write-back anything stuck in the cache. */
1049 cpu_idcache_wbinv_all();
1051 /* Install new mappings. */
1053 pt_entry_t *pte = vtopte(va);
1054 int npages = size >> PGSHIFT;
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);
1063 /* Clean out tlb. */
1064 tlb_flush();
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;
1072 /* Check format. */
1073 if (nargs != 2 || nreturns < 1) {
1074 args_n_results[nargs] = -1;
1075 args->nreturns = 1;
1076 return;
1078 args_n_results[nargs] = 0; /* properly formatted request */
1080 va = (vaddr_t)args_n_results[0];
1081 size = (vsize_t)args_n_results[1];
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;
1092 /* Write-back anything stuck in the cache. */
1093 cpu_idcache_wbinv_all();
1095 /* Zero the mappings. */
1097 pt_entry_t *pte = vtopte(va);
1098 int npages = size >> PGSHIFT;
1100 for (; npages > 0; pte++, npages--)
1101 *pte = 0;
1102 PTE_SYNC_RANGE(vtopte(va), size >> PGSHIFT);
1105 /* Clean out tlb. */
1106 tlb_flush();
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;
1115 /* Check format. */
1116 if (nargs != 1 || nreturns < 4) {
1117 args_n_results[nargs] = -1;
1118 args->nreturns = 1;
1119 return;
1121 args_n_results[nargs] = 0; /* properly formatted request */
1123 va = (vaddr_t)args_n_results[0];
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
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;
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 */
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;
1152 } else if (strcmp(name, "claim-phys") == 0) {
1153 struct pglist alloclist;
1154 paddr_t low, high, align;
1155 psize_t size;
1158 * XXX
1159 * XXX THIS IS A GROSS HACK AND NEEDS TO BE REWRITTEN. -- cgd
1160 * XXX
1163 /* Check format. */
1164 if (nargs != 4 || nreturns < 3) {
1165 args_n_results[nargs] = -1;
1166 args->nreturns = 1;
1167 return;
1169 args_n_results[nargs] = 0; /* properly formatted request */
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;
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
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;
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;
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;
1211 /* XXX - notyet */
1212 /* printf("unimplemented ofw callback - %s\n", name);*/
1213 args_n_results[nargs] = -1;
1214 args->nreturns = 1;
1215 return;
1217 /* Check format. */
1218 if (nargs != 2 || nreturns < 3) {
1219 args_n_results[nargs] = -1;
1220 args->nreturns = 1;
1221 return;
1223 args_n_results[nargs] = 0; /* properly formatted request */
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;
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;
1244 } else if (strcmp(name, "release-virt") == 0) {
1245 vaddr_t va;
1246 vsize_t size;
1248 /* XXX - notyet */
1249 printf("unimplemented ofw callback - %s\n", name);
1250 args_n_results[nargs] = -1;
1251 args->nreturns = 1;
1252 return;
1254 /* Check format. */
1255 if (nargs != 2 || nreturns < 1) {
1256 args_n_results[nargs] = -1;
1257 args->nreturns = 1;
1258 return;
1260 args_n_results[nargs] = 0; /* properly formatted request */
1262 /* Release bytes. */
1263 va = (vaddr_t)args_n_results[0];
1264 size = (vsize_t)args_n_results[1];
1266 args->nreturns = 1;
1267 } else {
1268 args_n_results[nargs] = -1;
1269 args->nreturns = 1;
1273 static void
1274 ofw_construct_proc0_addrspace(void)
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;
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)");
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 */
1307 /* Zero the memory. */
1308 memset((char *)systempage.pv_va, 0, PAGE_SIZE);
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);
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);
1332 /* Allocate/initialize space for msgbuf area. */
1333 ofw_claimpages(&virt_freeptr, &msgbuf, MSGBUFSIZE);
1334 msgbufphys = msgbuf.pv_pa;
1336 /* Construct the proc0 L1 pagetable. */
1337 L1pagetable = kernel_l1pt.pv_va;
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]);
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.
1359 ofw_getvirttranslations();
1361 for (oft = 0, tp = OFtranslations; oft < nOFtranslations;
1362 oft++, tp++) {
1364 vaddr_t va;
1365 paddr_t pa;
1366 int npages = tp->size / PAGE_SIZE;
1368 /* Size must be an integral number of pages. */
1369 if (npages == 0 || tp->size % PAGE_SIZE != 0)
1370 panic("illegal ofw translation (size)");
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--) {
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.
1380 switch (va >> (L1_S_SHIFT + 2)) {
1381 case 0:
1382 /* page0 */
1383 break;
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;
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;
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;
1407 default:
1408 /* illegal */
1409 panic("illegal ofw translation (addr) %#lx",
1410 va);
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);
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.
1427 ofw_discardmappings(proc0_pt_kernel[KERNEL_IMG_PTS - 1].pv_va,
1428 msgbuf.pv_va, MSGBUFSIZE);
1430 /* update the top of the kernel VM */
1431 pmap_curmaxkvaddr =
1432 KERNEL_VM_BASE + (KERNEL_VMDATA_PTS * 0x00400000);
1435 * gross hack for the sake of not thrashing the TLB and making
1436 * cache flush more efficient: blast l1 ptes for sections.
1438 for (oft = 0, tp = OFtranslations; oft < nOFtranslations; oft++, tp++) {
1439 vaddr_t va = tp->virt;
1440 paddr_t pa = tp->phys;
1442 if (((va | pa) & L1_S_OFFSET) == 0) {
1443 int nsections = tp->size / L1_S_SIZE;
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;
1459 static void
1460 ofw_getphysmeminfo(void)
1462 int phandle;
1463 int mem_len;
1464 int avail_len;
1465 int i;
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");
1477 nOFphysmem = mem_len / sizeof(struct mem_region);
1478 nOFphysavail = avail_len / sizeof(struct mem_region);
1481 * Sort the blocks in each array into ascending address order.
1482 * Also, page-align all blocks.
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;
1490 #ifdef OLDPRINTFS
1491 printf("ofw_getphysmeminfo: %d blocks\n", cnt);
1492 #endif
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);
1500 for (j = 0, mp = tmp; j < cnt; j++, mp++) {
1501 u_int s, sz;
1502 struct mem_region *mp1;
1504 /* Page-align start of the block. */
1505 s = mp->start % PAGE_SIZE;
1506 if (s != 0) {
1507 s = (PAGE_SIZE - s);
1509 if (mp->size >= s) {
1510 mp->start += s;
1511 mp->size -= s;
1515 /* Page-align the size. */
1516 mp->size -= mp->size % PAGE_SIZE;
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;
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;
1540 #ifdef OLDPRINTFS
1541 for (mp = tmp; mp->size; mp++) {
1542 printf("%x, %x\n", mp->start, mp->size);
1544 #endif
1549 static void
1550 ofw_getvirttranslations(void)
1552 int mmu_phandle;
1553 int mmu_ihandle;
1554 int trans_len;
1555 int over, len;
1556 int i;
1557 struct mem_translation *tp;
1559 mmu_ihandle = ofw_mmu_ihandle();
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");
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);
1584 * ofw_valloc: allocate blocks of VM for IO and other special purposes
1586 typedef struct _vfree {
1587 struct _vfree *pNext;
1588 vaddr_t start;
1589 vsize_t size;
1590 } VFREE, *PVFREE;
1592 static VFREE vfinitial = { NULL, IO_VIRT_BASE, IO_VIRT_SIZE };
1594 static PVFREE vflist = &vfinitial;
1596 static vaddr_t
1597 ofw_valloc(vsize_t size, vaddr_t align)
1599 PVFREE *ppvf;
1600 PVFREE pNew;
1601 vaddr_t new;
1602 vaddr_t lead;
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;
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;
1623 } else {
1624 vsize_t tail = ((*ppvf)->start
1625 + (*ppvf)->size) - (new + size);
1626 /* free space at beginning */
1627 (*ppvf)->size = lead;
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;
1638 return new;
1639 } /* END if */
1640 } /* END for */
1642 return -1;
1645 vaddr_t
1646 ofw_map(paddr_t pa, vsize_t size, int cb_bits)
1648 vaddr_t va;
1650 if ((va = ofw_valloc(size, size)) == -1)
1651 panic("cannot alloc virtual memory for %#lx", pa);
1653 ofw_claimvirt(va, size, 0); /* make sure OFW knows about the memory */
1655 ofw_settranslation(va, pa, size, L2_AP(AP_KRW) | cb_bits);
1657 return va;
1660 static int
1661 ofw_mem_ihandle(void)
1663 static int mem_ihandle = 0;
1664 int chosen;
1666 if (mem_ihandle != 0)
1667 return(mem_ihandle);
1669 if ((chosen = OF_finddevice("/chosen")) == -1 ||
1670 OF_getprop(chosen, "memory", &mem_ihandle, sizeof(int)) < 0)
1671 panic("ofw_mem_ihandle");
1673 mem_ihandle = of_decode_int((unsigned char *)&mem_ihandle);
1675 return(mem_ihandle);
1679 static int
1680 ofw_mmu_ihandle(void)
1682 static int mmu_ihandle = 0;
1683 int chosen;
1685 if (mmu_ihandle != 0)
1686 return(mmu_ihandle);
1688 if ((chosen = OF_finddevice("/chosen")) == -1 ||
1689 OF_getprop(chosen, "mmu", &mmu_ihandle, sizeof(int)) < 0)
1690 panic("ofw_mmu_ihandle");
1692 mmu_ihandle = of_decode_int((unsigned char *)&mmu_ihandle);
1694 return(mmu_ihandle);
1698 /* Return -1 on failure. */
1699 static paddr_t
1700 ofw_claimphys(paddr_t pa, psize_t size, paddr_t align)
1702 int mem_ihandle = ofw_mem_ihandle();
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);
1713 /* printf("%x\n", pa);*/
1714 return(pa);
1718 #if 0
1719 /* Return -1 on failure. */
1720 static paddr_t
1721 ofw_releasephys(paddr_t pa, psize_t size)
1723 int mem_ihandle = ofw_mem_ihandle();
1725 /* printf("ofw_releasephys (%x, %x)\n", pa, size);*/
1727 return (OF_call_method_1("release", mem_ihandle, 2, pa, size));
1729 #endif
1731 /* Return -1 on failure. */
1732 static vaddr_t
1733 ofw_claimvirt(vaddr_t va, vsize_t size, vaddr_t align)
1735 int mmu_ihandle = ofw_mmu_ihandle();
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);
1746 /*printf("%x\n", va);*/
1747 return(va);
1750 /* Return -1 if no mapping. */
1751 paddr_t
1752 ofw_gettranslation(vaddr_t va)
1754 int mmu_ihandle = ofw_mmu_ihandle();
1755 paddr_t pa;
1756 int mode;
1757 int exists;
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);
1767 #ifdef OFW_DEBUG
1768 printf("%d %x\n", exists, (uint32_t)pa);
1769 #endif
1770 return(exists ? pa : -1);
1774 static void
1775 ofw_settranslation(vaddr_t va, paddr_t pa, vsize_t size, int mode)
1777 int mmu_ihandle = ofw_mmu_ihandle();
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");
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.
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.
1798 typedef struct _leftover {
1799 struct _leftover *pNext;
1800 vsize_t size;
1801 } LEFTOVER, *PLEFTOVER;
1803 /* leftover bits of pages. first word is pointer to next.
1804 second word is size of leftover */
1805 static PLEFTOVER leftovers = NULL;
1807 static void *
1808 ofw_malloc(vsize_t size)
1810 PLEFTOVER *ppLeftover;
1811 PLEFTOVER pLeft;
1812 pv_addr_t new;
1813 vsize_t newSize, claim_size;
1815 /* round and set minimum size */
1816 size = max(sizeof(LEFTOVER),
1817 ((size + (sizeof(LEFTOVER) - 1)) & ~(sizeof(LEFTOVER) - 1)));
1819 for (ppLeftover = &leftovers; *ppLeftover;
1820 ppLeftover = &((*ppLeftover)->pNext))
1821 if ((*ppLeftover)->size >= size)
1822 break;
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;
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;
1851 return (void *)(new.pv_va);
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.
1859 #if 0
1860 static void
1861 ofw_free(vaddr_t addr, vsize_t size)
1863 PLEFTOVER pLeftover = (PLEFTOVER)addr;
1865 /* splice right into list without checks or compaction */
1866 pLeftover->pNext = leftovers;
1867 pLeftover->size = size;
1868 leftovers = pLeftover;
1870 #endif
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.
1883 static void
1884 ofw_claimpages(vaddr_t *free_pp, pv_addr_t *pv_p, vsize_t size)
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;
1892 if (alloc_size == 0)
1893 panic("ofw_claimpages zero");
1895 for (aligned_size = 1; aligned_size < alloc_size; aligned_size <<= 1)
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.
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);
1911 /* The memory's mapped-in now, so we can zero it. */
1912 memset((char *)va, 0, alloc_size);
1914 /* Set OUT parameters. */
1915 *free_pp = va;
1916 pv_p->pv_va = va;
1917 pv_p->pv_pa = pa;
1921 static void
1922 ofw_discardmappings(vaddr_t L2pagetable, vaddr_t va, vsize_t size)
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;
1928 if (npages == 0)
1929 panic("ofw_discardmappings zero");
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");
1937 /* Clear the entry. */
1938 WriteWord(L2pagetable + ((va >> 10) & 0x00000FFC), 0);
1943 static void
1944 ofw_initallocator(void)
1949 #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
1950 static void
1951 reset_screen(void)
1954 if ((console_ihandle == 0) || (console_ihandle == -1))
1955 return;
1957 OF_call_method("install", console_ihandle, 0, 0);
1959 #endif /* (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0) */