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