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>
53 #include <uvm/uvm_extern.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>
71 #include <nfs/bootdata.h>
75 #include "machine/pio.h"
76 #include "machine/isa_machdep.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
101 extern BootConfig bootconfig
; /* temporary, I hope */
104 /* NOTE: These variables will be removed, well some of them */
105 extern u_int current_mask
;
108 extern int ofw_handleticks
;
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)))
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
;
139 pv_addr_t kernelstack
;
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);
153 /**************************************************************/
157 * Declarations and definitions private to this module
166 struct mem_translation
{
176 paddr_t parent_phys_start
;
183 paddr_t parent_phys_start
;
187 struct vl_isa_range
{
190 paddr_t parent_phys_hi
;
191 paddr_t parent_phys_lo
;
204 int args_n_results
[12];
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
);
234 static paddr_t
ofw_releasephys(paddr_t
, psize_t
);
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:
280 openfirmware(void *args
)
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
);
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
);
324 ofw_boot(int howto
, char *bootstr
)
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
],
334 printf("ipl_audio=%08x ipl_clock=%08x ipl_none=%08x\n",
335 irqmasks
[IPL_AUDIO
], irqmasks
[IPL_CLOCK
], irqmasks
[IPL_NONE
]);
341 * If we are still cold then hit the air brakes
342 * and crash to earth fast
346 pmf_system_shutdown(boothowto
);
347 printf("Halted while still in the ICE age.\n");
348 printf("The operating system has halted.\n");
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
))
362 /* Say NO to interrupts */
365 /* Do a dump if requested. */
366 if ((howto
& (RB_DUMP
| RB_HALT
)) == RB_DUMP
)
369 /* Run any shutdown hooks */
372 pmf_system_shutdown(boothowto
);
374 /* Make sure IRQ's are disabled */
377 if (howto
& RB_HALT
) {
378 printf("The operating system has halted.\n");
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");
394 strcpy(str
, bootstr
);
395 ap1
= ap
= str
+ strlen(str
);
400 if (howto
& RB_SINGLE
)
407 #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
415 printf("Calling OF_exit...\n");
416 #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
426 extern char *ip2dotted(struct in_addr
);
429 * Get DHCP data from OFW
433 get_fw_dhcp_data(struct bootdata
*bdp
)
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) {
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.
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 */
520 ofw_getbootinfo(char **bp_pp
, char **ba_pp
)
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
);
541 OF_getprop(chosen
, "bootpath", bootpathv
, bp_len
);
546 OF_getprop(chosen
, "bootargs", bootargsv
, ba_len
);
553 printf("bootpath=<%s>, bootargs=<%s>\n", bootpathv
, bootargsv
);
558 ofw_getcleaninfo(void)
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
)) {
570 printf("no OFW d-cache-flush-address property\n");
575 if ((pclean
= ofw_gettranslation(
576 of_decode_int((unsigned char *)&vclean
))) == -1)
577 panic("OFW failed to translate cache flush address");
583 ofw_configisa(paddr_t
*pio
, paddr_t
*pmem
)
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
);
594 ofw_configisaonly(paddr_t
*pio
, paddr_t
*pmem
)
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
)))
608 panic("unexpected size of OFW /isa ranges property: %d", size
);
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 */
625 if ((*pio
== -1) || (*pmem
== -1))
626 panic("bad OFW /isa ranges property");
631 ofw_configvl(int vl
, paddr_t
*pio
, paddr_t
*pmem
)
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",
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
);
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 */
677 if ((*pio
== -1) || (*pmem
== -1))
678 panic("bad OFW /isa ranges property");
682 struct arm32_dma_range
*shark_isa_dma_ranges
;
683 int shark_isa_dma_nranges
;
687 ofw_configisadma(paddr_t
*pdma
)
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
);
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
;
711 for (rangeidx
= 0, dr
= OFdmaranges
; rangeidx
< nOFdmaranges
;
713 dr
->start
= of_decode_int((unsigned char *)&dr
->start
);
714 dr
->size
= of_decode_int((unsigned char *)&dr
->size
);
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
;
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
);
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.)
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. */
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
);
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
;
799 /* physmem, physical_start, physical_end */
801 for (totalcnt
= 0, mp
= OFphysmem
; totalcnt
< nOFphysmem
;
804 printf("physmem: %x, %x\n", mp
->start
, mp
->size
);
806 physmem
+= btoc(mp
->size
);
808 physical_start
= OFphysmem
[0].start
;
810 physical_end
= mp
->start
+ mp
->size
;
812 /* free_pages, physical_freestart, physical_freeend */
814 for (availcnt
= 0, mp
= OFphysavail
; availcnt
< nOFphysavail
;
817 printf("physavail: %x, %x\n", mp
->start
, mp
->size
);
819 free_pages
+= btoc(mp
->size
);
821 physical_freestart
= OFphysavail
[0].start
;
823 physical_freeend
= mp
->start
+ mp
->size
;
825 printf("pmap_bootstrap: physmem = %x, free_pages = %x\n",
826 physmem
, free_pages
);
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?
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
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
);
885 paddr_t istart
, isize
;
888 if (start
< physical_freestart
)
889 start
= physical_freestart
;
890 if (end
> physical_freeend
)
891 end
= physical_freeend
;
894 printf("%d: %lx -> %lx\n", loop
, start
, end
- 1);
898 if (arm32_dma_range_intersect(shark_isa_dma_ranges
,
899 shark_isa_dma_nranges
,
903 * Place the pages that intersect with the
904 * ISA DMA range onto the ISA DMA free list.
907 printf(" ISADMA 0x%lx -> 0x%lx\n", istart
,
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
) {
920 printf(" BEFORE 0x%lx -> 0x%lx\n",
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
) {
934 printf(" AFTER 0x%lx -> 0x%lx\n",
935 (istart
+ isize
), end
- 1);
937 uvm_page_physload(atop(istart
+ isize
),
938 atop(end
), atop(istart
+ isize
),
939 atop(end
), VM_FREELIST_DEFAULT
);
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! */
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
;
976 #if defined(OFWGENCFG)
977 /* Check this first, so that we don't waste IRQ time parsing. */
978 if (strcmp(name
, "tick") == 0) {
982 if (nargs
!= 1 || nreturns
< 1) {
983 args_n_results
[nargs
] = -1;
987 args_n_results
[nargs
] = 0; /* properly formatted request */
990 * Note that we are running in the IRQ frame, with interrupts
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
;
1013 if (strcmp(name
, "map") == 0) {
1023 if (nargs
!= 4 || nreturns
< 2) {
1024 args_n_results
[nargs
] = -1;
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;
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
;
1057 for (; npages
> 0; pte
++, pa
+= PAGE_SIZE
, npages
--)
1058 *pte
= (pa
| L2_AP(ap_bits
) | L2_TYPE_S
|
1060 PTE_SYNC_RANGE(vtopte(va
), size
>> PGSHIFT
);
1063 /* Clean out tlb. */
1066 args_n_results
[nargs
+ 1] = 0;
1068 } else if (strcmp(name
, "unmap") == 0) {
1073 if (nargs
!= 2 || nreturns
< 1) {
1074 args_n_results
[nargs
] = -1;
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;
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
--)
1102 PTE_SYNC_RANGE(vtopte(va
), size
>> PGSHIFT
);
1105 /* Clean out tlb. */
1109 } else if (strcmp(name
, "translate") == 0) {
1116 if (nargs
!= 1 || nreturns
< 4) {
1117 args_n_results
[nargs
] = -1;
1121 args_n_results
[nargs
] = 0; /* properly formatted request */
1123 va
= (vaddr_t
)args_n_results
[0];
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;
1136 /* Lookup mapping. */
1140 args_n_results
[nargs
+ 1] = -1;
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
;
1152 } else if (strcmp(name
, "claim-phys") == 0) {
1153 struct pglist alloclist
;
1154 paddr_t low
, high
, align
;
1159 * XXX THIS IS A GROSS HACK AND NEEDS TO BE REWRITTEN. -- cgd
1164 if (nargs
!= 4 || nreturns
< 3) {
1165 args_n_results
[nargs
] = -1;
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
;
1177 printf("claim-phys: low = 0x%x, size = 0x%x, align = 0x%x, high = 0x%x\n",
1178 low
, size
, align
, high
);
1180 printf("forcing align to be 0x%x\n", align
);
1183 args_n_results
[nargs
+ 1] =
1184 uvm_pglistalloc(size
, low
, high
, align
, 0, &alloclist
, 1, 0);
1186 printf(" -> 0x%lx", args_n_results
[nargs
+ 1]);
1188 if (args_n_results
[nargs
+ 1] != 0) {
1190 printf("(failed)\n");
1192 args_n_results
[nargs
+ 1] = -1;
1196 args_n_results
[nargs
+ 2] = VM_PAGE_TO_PHYS(alloclist
.tqh_first
);
1198 printf("(succeeded: pa = 0x%lx)\n", args_n_results
[nargs
+ 2]);
1202 } else if (strcmp(name
, "release-phys") == 0) {
1203 printf("unimplemented ofw callback - %s\n", name
);
1204 args_n_results
[nargs
] = -1;
1206 } else if (strcmp(name
, "claim-virt") == 0) {
1212 /* printf("unimplemented ofw callback - %s\n", name);*/
1213 args_n_results
[nargs
] = -1;
1218 if (nargs
!= 2 || nreturns
< 3) {
1219 args_n_results
[nargs
] = -1;
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;
1235 /* Couldn't allocate. */
1236 args_n_results
[nargs
+ 1] = -1;
1239 /* Successful allocation. */
1240 args_n_results
[nargs
+ 1] = 0;
1241 args_n_results
[nargs
+ 2] = va
;
1244 } else if (strcmp(name
, "release-virt") == 0) {
1249 printf("unimplemented ofw callback - %s\n", name
);
1250 args_n_results
[nargs
] = -1;
1255 if (nargs
!= 2 || nreturns
< 1) {
1256 args_n_results
[nargs
] = -1;
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];
1268 args_n_results
[nargs
] = -1;
1274 ofw_construct_proc0_addrspace(void)
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)");
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. */
1326 ofw_claimpages(&virt_freeptr
, &irqstack
, PAGE_SIZE
);
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,
1349 for (i
= 0; i
< KERNEL_IO_PTS
; i
++)
1350 pmap_link_l2pt(L1pagetable
, IO_VIRT_BASE
+ i
* 0x00400000,
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
;
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)) {
1385 #if KERNEL_IMG_PTS != 2
1386 #error "Update ofw translation range list"
1388 case ( KERNEL_BASE
>> (L1_S_SHIFT
+ 2)):
1389 case ((KERNEL_BASE
+ 0x00400000) >> (L1_S_SHIFT
+ 2)):
1390 /* kernel static area */
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)):
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)):
1409 panic("illegal ofw translation (addr) %#lx",
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
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 */
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
--) {
1447 pmap_map_section(L1pagetable
, va
, pa
,
1448 VM_PROT_READ
|VM_PROT_WRITE
,
1449 (tp
->mode
& 0xC) == 0xC ? PTE_CACHE
1460 ofw_getphysmeminfo(void)
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
)
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
;
1491 printf("ofw_getphysmeminfo: %d blocks\n", cnt
);
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
++) {
1502 struct mem_region
*mp1
;
1504 /* Page-align start of the block. */
1505 s
= mp
->start
% PAGE_SIZE
;
1507 s
= (PAGE_SIZE
- s
);
1509 if (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
));
1530 for (mp1
= tmp
; mp1
< mp
; mp1
++)
1534 memmove(mp1
+ 1, mp1
, (char *)mp
- (char *)mp1
);
1541 for (mp
= tmp
; mp
->size
; mp
++) {
1542 printf("%x, %x\n", mp
->start
, mp
->size
);
1550 ofw_getvirttranslations(void)
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
);
1573 printf("ofw_getvirtmeminfo: %d blocks\n", nOFtranslations
);
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
;
1592 static VFREE vfinitial
= { NULL
, IO_VIRT_BASE
, IO_VIRT_SIZE
};
1594 static PVFREE vflist
= &vfinitial
;
1597 ofw_valloc(vsize_t size
, vaddr_t align
)
1604 for (ppvf
= &vflist
; *ppvf
; ppvf
= &((*ppvf
)->pNext
)) {
1606 new = (*ppvf
)->start
;
1609 new = ((*ppvf
)->start
+ (align
- 1)) & ~(align
- 1);
1610 lead
= new - (*ppvf
)->start
;
1613 if (((*ppvf
)->size
- lead
) >= size
) {
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
;
1624 vsize_t tail
= ((*ppvf
)->start
1625 + (*ppvf
)->size
) - (new + size
);
1626 /* free space at beginning */
1627 (*ppvf
)->size
= lead
;
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
;
1646 ofw_map(paddr_t pa
, vsize_t size
, int cb_bits
)
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
);
1661 ofw_mem_ihandle(void)
1663 static int mem_ihandle
= 0;
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
);
1680 ofw_mmu_ihandle(void)
1682 static int mmu_ihandle
= 0;
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. */
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);*/
1706 /* Allocate at specified base; alignment is ignored. */
1707 pa
= OF_call_method_1("claim", mem_ihandle
, 3, pa
, size
, align
);
1709 /* Allocate anywhere, with specified alignment. */
1710 pa
= OF_call_method_1("claim", mem_ihandle
, 2, size
, align
);
1713 /* printf("%x\n", pa);*/
1719 /* Return -1 on failure. */
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
));
1731 /* Return -1 on failure. */
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);*/
1739 /* Allocate at specified base; alignment is ignored. */
1740 va
= OF_call_method_1("claim", mmu_ihandle
, 3, va
, size
, align
);
1742 /* Allocate anywhere, with specified alignment. */
1743 va
= OF_call_method_1("claim", mmu_ihandle
, 2, size
, align
);
1746 /*printf("%x\n", va);*/
1750 /* Return -1 if no mapping. */
1752 ofw_gettranslation(vaddr_t va
)
1754 int mmu_ihandle
= ofw_mmu_ihandle();
1760 printf("ofw_gettranslation (%x) --> ", (uint32_t)va
);
1762 exists
= 0; /* gets set to true if translation exists */
1763 if (OF_call_method("translate", mmu_ihandle
, 1, 3, va
, &pa
, &mode
,
1768 printf("%d %x\n", exists
, (uint32_t)pa
);
1770 return(exists
? pa
: -1);
1775 ofw_settranslation(vaddr_t va
, paddr_t pa
, vsize_t size
, int mode
)
1777 int mmu_ihandle
= ofw_mmu_ihandle();
1780 printf("ofw_settranslation (%x, %x, %x, %x) --> void", (uint32_t)va
,
1781 (uint32_t)pa
, (uint32_t)size
, (uint32_t)mode
);
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
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
;
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
;
1808 ofw_malloc(vsize_t size
)
1810 PLEFTOVER
*ppLeftover
;
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
)
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
;
1831 /* remember the next pointer */
1832 pLeft
= (*ppLeftover
)->pNext
;
1833 newSize
= (*ppLeftover
)->size
- size
; /* reduce size */
1835 *ppLeftover
= (PLEFTOVER
)(((vaddr_t
)*ppLeftover
)
1837 (*ppLeftover
)->pNext
= pLeft
;
1838 (*ppLeftover
)->size
= newSize
;
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
;
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.
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
;
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
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
;
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
);
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. */
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
;
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);
1944 ofw_initallocator(void)
1949 #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
1954 if ((console_ihandle
== 0) || (console_ihandle
== -1))
1957 OF_call_method("install", console_ihandle
, 0, 0);
1959 #endif /* (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0) */