Home | History | Annotate | Line # | Download | only in mvme68k
pmap_bootstrap.c revision 1.51
      1 /*	$NetBSD: pmap_bootstrap.c,v 1.50 2012/02/10 06:21:15 mhitch Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * the Systems Programming Group of the University of Utah Computer
      9  * Science Department.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)pmap_bootstrap.c	8.1 (Berkeley) 6/10/93
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: pmap_bootstrap.c,v 1.50 2012/02/10 06:21:15 mhitch Exp $");
     40 
     41 #include "opt_m68k_arch.h"
     42 
     43 #include <sys/param.h>
     44 #include <sys/kcore.h>
     45 #include <uvm/uvm_extern.h>
     46 
     47 #include <machine/cpu.h>
     48 #include <machine/pte.h>
     49 #include <machine/vmparam.h>
     50 
     51 #include <mvme68k/mvme68k/seglist.h>
     52 
     53 #define RELOC(v, t)	*((t*)((uintptr_t)&(v) + firstpa))
     54 
     55 extern char *etext;
     56 
     57 extern int maxmem, physmem;
     58 extern paddr_t avail_start, avail_end;
     59 extern phys_ram_seg_t mem_clusters[];
     60 extern int mem_cluster_cnt;
     61 extern paddr_t msgbufpa;
     62 
     63 /*
     64  * Special purpose kernel virtual addresses, used for mapping
     65  * physical pages for a variety of temporary or permanent purposes:
     66  *
     67  *	CADDR1, CADDR2:	pmap zero/copy operations
     68  *	vmmap:		/dev/mem, crash dumps, parity error checking
     69  *	msgbufaddr:	kernel message buffer
     70  */
     71 void *CADDR1, *CADDR2;
     72 char *vmmap;
     73 void *msgbufaddr;
     74 
     75 void pmap_bootstrap(paddr_t, paddr_t);
     76 
     77 /*
     78  * Bootstrap the VM system.
     79  *
     80  * Called with MMU off so we must relocate all global references by `firstpa'
     81  * (don't call any functions here!)  `nextpa' is the first available physical
     82  * memory address.  Returns an updated first PA reflecting the memory we
     83  * have allocated.  MMU is still off when we return.
     84  *
     85  * XXX assumes sizeof(u_int) == sizeof(pt_entry_t)
     86  * XXX a PIC compiler would make this much easier.
     87  */
     88 void
     89 pmap_bootstrap(paddr_t nextpa, paddr_t firstpa)
     90 {
     91 	paddr_t lwp0upa, kstpa, kptmpa, kptpa;
     92 	u_int nptpages, kstsize;
     93 	st_entry_t protoste, *ste, *este;
     94 	pt_entry_t protopte, *pte, *epte;
     95 	psize_t size;
     96 	u_int iiomappages;
     97 	int i;
     98 #if defined(M68040) || defined(M68060)
     99 	u_int stfree = 0;	/* XXX: gcc -Wuninitialized */
    100 #endif
    101 
    102 	/*
    103 	 * Calculate important physical addresses:
    104 	 *
    105 	 *	lwp0upa		lwp0 u-area		UPAGES pages
    106 	 *
    107 	 *	kstpa		kernel segment table	1 page (!040)
    108 	 *						N pages (040)
    109 	 *
    110 	 *	kptmpa		kernel PT map		1 page
    111 	 *
    112 	 *	kptpa		statically allocated
    113 	 *			kernel PT pages		Sysptsize+ pages
    114 	 *
    115 	 * [ Sysptsize is the number of pages of PT, and iiomappages is the
    116 	 *   number of PTEs, hence we need to round the total to a page
    117 	 *   boundary with IO maps at the end. ]
    118 	 *
    119 	 * The KVA corresponding to any of these PAs is:
    120 	 *	(PA - firstpa + KERNBASE).
    121 	 */
    122 	iiomappages = m68k_btop(RELOC(intiotop_phys, u_int) -
    123 	    RELOC(intiobase_phys, u_int));
    124 
    125 	lwp0upa = nextpa;
    126 	nextpa += USPACE;
    127 #if defined(M68040) || defined(M68060)
    128 	if (RELOC(mmutype, int) == MMU_68040)
    129 		kstsize = MAXKL2SIZE / (NPTEPG/SG4_LEV2SIZE);
    130 	else
    131 #endif
    132 		kstsize = 1;
    133 	kstpa = nextpa;
    134 	nextpa += kstsize * PAGE_SIZE;
    135 	kptmpa = nextpa;
    136 	nextpa += PAGE_SIZE;
    137 	kptpa = nextpa;
    138 	nptpages = RELOC(Sysptsize, int) + howmany(RELOC(physmem, int), NPTEPG) +
    139 	    (iiomappages + NPTEPG - 1) / NPTEPG;
    140 	nextpa += nptpages * PAGE_SIZE;
    141 
    142 	/*
    143 	 * Clear all PTEs to zero
    144 	 */
    145 	for (pte = (pt_entry_t *)kstpa; pte < (pt_entry_t *)nextpa; pte++)
    146 		*pte = 0;
    147 
    148 	/*
    149 	 * Initialize segment table and kernel page table map.
    150 	 *
    151 	 * On 68030s and earlier MMUs the two are identical except for
    152 	 * the valid bits so both are initialized with essentially the
    153 	 * same values.  On the 68040, which has a mandatory 3-level
    154 	 * structure, the segment table holds the level 1 table and part
    155 	 * (or all) of the level 2 table and hence is considerably
    156 	 * different.  Here the first level consists of 128 descriptors
    157 	 * (512 bytes) each mapping 32mb of address space.  Each of these
    158 	 * points to blocks of 128 second level descriptors (512 bytes)
    159 	 * each mapping 256kb.  Note that there may be additional "segment
    160 	 * table" pages depending on how large MAXKL2SIZE is.
    161 	 *
    162 	 * Portions of the last segment of KVA space (0xFFC00000 -
    163 	 * 0xFFFFFFFF) are mapped for the kernel page tables.
    164 	 *
    165 	 * XXX cramming two levels of mapping into the single "segment"
    166 	 * table on the 68040 is intended as a temporary hack to get things
    167 	 * working.  The 224mb of address space that this allows will most
    168 	 * likely be insufficient in the future (at least for the kernel).
    169 	 */
    170 #if defined(M68040) || defined(M68060)
    171 	if (RELOC(mmutype, int) == MMU_68040) {
    172 		int nl1desc, nl2desc;
    173 
    174 		/*
    175 		 * First invalidate the entire "segment table" pages
    176 		 * (levels 1 and 2 have the same "invalid" value).
    177 		 */
    178 		ste = (st_entry_t *)kstpa;
    179 		este = &ste[kstsize * NPTEPG];
    180 		while (ste < este)
    181 			*ste++ = SG_NV;
    182 		/*
    183 		 * Initialize level 2 descriptors (which immediately
    184 		 * follow the level 1 table).  We need:
    185 		 *	NPTEPG / SG4_LEV3SIZE
    186 		 * level 2 descriptors to map each of the nptpages
    187 		 * pages of PTEs.  Note that we set the "used" bit
    188 		 * now to save the HW the expense of doing it.
    189 		 */
    190 		nl2desc = nptpages * (NPTEPG / SG4_LEV3SIZE);
    191 		ste = (st_entry_t *)kstpa;
    192 		ste = &ste[SG4_LEV1SIZE];
    193 		este = &ste[nl2desc];
    194 		protoste = kptpa | SG_U | SG_RW | SG_V;
    195 		while (ste < este) {
    196 			*ste++ = protoste;
    197 			protoste += (SG4_LEV3SIZE * sizeof(st_entry_t));
    198 		}
    199 		/*
    200 		 * Initialize level 1 descriptors.  We need:
    201 		 *	howmany(nl2desc, SG4_LEV2SIZE)
    202 		 * level 1 descriptors to map the `nl2desc' level 2's.
    203 		 */
    204 		nl1desc = howmany(nl2desc, SG4_LEV2SIZE);
    205 		ste = (st_entry_t *)kstpa;
    206 		este = &ste[nl1desc];
    207 		protoste = (paddr_t)&ste[SG4_LEV1SIZE] | SG_U | SG_RW | SG_V;
    208 		while (ste < este) {
    209 			*ste++ = protoste;
    210 			protoste += (SG4_LEV2SIZE * sizeof(st_entry_t));
    211 		}
    212 		/*
    213 		 * Initialize the final level 1 descriptor to map the next
    214 		 * block of level 2 descriptors for Sysptmap.
    215 		 */
    216 		ste = (st_entry_t *)kstpa;
    217 		ste = &ste[SG4_LEV1SIZE - 1];
    218 		*ste = protoste;
    219 		/*
    220 		 * Now initialize the final portion of that block of
    221 		 * descriptors to map Sysmap.
    222 		 */
    223 		i = SG4_LEV1SIZE + (nl1desc * SG4_LEV2SIZE);
    224 		ste = (st_entry_t *)kstpa;
    225 		ste = &ste[i + SG4_LEV2SIZE - (NPTEPG / SG4_LEV3SIZE)];
    226 		este = &ste[NPTEPG / SG4_LEV3SIZE];
    227 		protoste = kptmpa | SG_U | SG_RW | SG_V;
    228 		while (ste < este) {
    229 			*ste++ = protoste;
    230 			protoste += (SG4_LEV3SIZE * sizeof(st_entry_t));
    231 		}
    232 		/*
    233 		 * Calculate the free level 2 descriptor mask
    234 		 * noting that we have used:
    235 		 *	0:		level 1 table
    236 		 *	1 to nl1desc:	map page tables
    237 		 *	nl1desc + 1:	maps kptmpa and last-page page table
    238 		 */
    239 		/* mark an entry for level 1 table */
    240 		stfree = ~l2tobm(0);
    241 		/* mark entries for map page tables */
    242 		for (i = 1; i <= nl1desc; i++)
    243 			stfree &= ~l2tobm(i);
    244 		/* mark an entry for kptmpa and lkptpa */
    245 		stfree &= ~l2tobm(i);
    246 		/* mark entries not available */
    247 		for (i = MAXKL2SIZE; i < sizeof(stfree) * NBBY; i++)
    248 			stfree &= ~l2tobm(i);
    249 
    250 		/*
    251 		 * Initialize Sysptmap
    252 		 */
    253 		pte = (pt_entry_t *)kptmpa;
    254 		epte = &pte[nptpages];
    255 		protopte = kptpa | PG_RW | PG_CI | PG_U | PG_V;
    256 		while (pte < epte) {
    257 			*pte++ = protopte;
    258 			protopte += PAGE_SIZE;
    259 		}
    260 		/*
    261 		 * Invalidate all remaining entries.
    262 		 */
    263 		epte = (pt_entry_t *)kptmpa;
    264 		epte = &epte[TIB_SIZE];
    265 		while (pte < epte) {
    266 			*pte++ = PG_NV;
    267 		}
    268 		/*
    269 		 * Initialize the last one to point to Sysptmap.
    270 		 */
    271 		pte = (pt_entry_t *)kptmpa;
    272 		pte = &pte[SYSMAP_VA >> SEGSHIFT];
    273 		*pte = kptmpa | PG_RW | PG_CI | PG_V;
    274 	} else
    275 #endif /* M68040 || M68060 */
    276 	{
    277 		/*
    278 		 * Map the page table pages in both the HW segment table
    279 		 * and the software Sysptmap.
    280 		 */
    281 		ste = (st_entry_t *)kstpa;
    282 		pte = (pt_entry_t *)kptmpa;
    283 		epte = &pte[nptpages];
    284 		protoste = kptpa | SG_RW | SG_V;
    285 		protopte = kptpa | PG_RW | PG_CI | PG_V;
    286 		while (pte < epte) {
    287 			*ste++ = protoste;
    288 			*pte++ = protopte;
    289 			protoste += PAGE_SIZE;
    290 			protopte += PAGE_SIZE;
    291 		}
    292 		/*
    293 		 * Invalidate all remaining entries in both.
    294 		 */
    295 		este = (st_entry_t *)kstpa;
    296 		este = &este[TIA_SIZE];
    297 		while (ste < este)
    298 			*ste++ = SG_NV;
    299 		epte = (pt_entry_t *)kptmpa;
    300 		epte = &epte[TIB_SIZE];
    301 		while (pte < epte)
    302 			*pte++ = PG_NV;
    303 		/*
    304 		 * Initialize the last one to point to Sysptmap.
    305 		 */
    306 		ste = (st_entry_t *)kstpa;
    307 		ste = &ste[SYSMAP_VA >> SEGSHIFT];
    308 		pte = (pt_entry_t *)kptmpa;
    309 		pte = &pte[SYSMAP_VA >> SEGSHIFT];
    310 		*ste = kptmpa | SG_RW | SG_V;
    311 		*pte = kptmpa | PG_RW | PG_CI | PG_V;
    312 	}
    313 
    314 	/*
    315 	 * Initialize kernel page table.
    316 	 * Start by invalidating the `nptpages' that we have allocated.
    317 	 */
    318 	pte = (pt_entry_t *)kptpa;
    319 	epte = &pte[nptpages * NPTEPG];
    320 	while (pte < epte)
    321 		*pte++ = PG_NV;
    322 	/*
    323 	 * Validate PTEs for kernel text (RO).
    324 	 */
    325 	pte = (pt_entry_t *)kptpa;
    326 	pte = &pte[m68k_btop(KERNBASE)];
    327 	epte = &pte[m68k_btop(m68k_trunc_page(&etext))];
    328 	protopte = firstpa | PG_RO | PG_U | PG_V;
    329 	while (pte < epte) {
    330 		*pte++ = protopte;
    331 		protopte += PAGE_SIZE;
    332 	}
    333 	/*
    334 	 * Validate PTEs for kernel data/bss, dynamic data allocated
    335 	 * by us so far (kstpa - firstpa bytes), and pages for lwp0
    336 	 * u-area and page table allocated below (RW).
    337 	 */
    338 	epte = (pt_entry_t *)kptpa;
    339 	epte = &epte[m68k_btop(kstpa - firstpa)];
    340 	protopte = (protopte & ~PG_PROT) | PG_RW;
    341 	/*
    342 	 * Enable copy-back caching of data pages
    343 	 */
    344 	if (RELOC(mmutype, int) == MMU_68040)
    345 		protopte |= PG_CCB;
    346 	while (pte < epte) {
    347 		*pte++ = protopte;
    348 		protopte += PAGE_SIZE;
    349 	}
    350 	/*
    351 	 * Map the kernel segment table cache invalidated for 68040/68060.
    352 	 * (for the 68040 not strictly necessary, but recommended by Motorola;
    353 	 *  for the 68060 mandatory)
    354 	 */
    355 	epte = (pt_entry_t *)kptpa;
    356 	epte = &epte[m68k_btop(nextpa - firstpa)];
    357 	protopte = (protopte & ~PG_PROT) | PG_RW;
    358 	if (RELOC(mmutype, int) == MMU_68040) {
    359 		protopte &= ~PG_CMASK;
    360 		protopte |= PG_CI;
    361 	}
    362 	while (pte < epte) {
    363 		*pte++ = protopte;
    364 		protopte += PAGE_SIZE;
    365 	}
    366 
    367 	/*
    368 	 * Finally, validate the internal IO space PTEs (RW+CI).
    369 	 */
    370 
    371 #define	PTE2VA(pte)	m68k_ptob(pte - ((pt_entry_t *)kptpa))
    372 
    373 	protopte = RELOC(intiobase_phys, u_int) | PG_RW | PG_CI | PG_U | PG_V;
    374 	epte = &pte[iiomappages];
    375 	RELOC(intiobase, uint8_t *) = (uint8_t *)PTE2VA(pte);
    376 	RELOC(intiolimit, uint8_t *) = (uint8_t *)PTE2VA(epte);
    377 	while (pte < epte) {
    378 		*pte++ = protopte;
    379 		protopte += PAGE_SIZE;
    380 	}
    381 	RELOC(virtual_avail, vaddr_t) = PTE2VA(pte);
    382 
    383 	/*
    384 	 * Calculate important exported kernel addresses and related values.
    385 	 */
    386 	/*
    387 	 * Sysseg: base of kernel segment table
    388 	 */
    389 	RELOC(Sysseg, st_entry_t *) = (st_entry_t *)(kstpa - firstpa);
    390 	RELOC(Sysseg_pa, paddr_t) = kstpa;
    391 #if defined(M68040) || defined(M68060)
    392 	if (RELOC(mmutype, int) == MMU_68040)
    393 		RELOC(protostfree, u_int) = stfree;
    394 #endif
    395 	/*
    396 	 * Sysptmap: base of kernel page table map
    397 	 */
    398 	RELOC(Sysptmap, pt_entry_t *) = (pt_entry_t *)(kptmpa - firstpa);
    399 	/*
    400 	 * Sysmap: kernel page table (as mapped through Sysptmap)
    401 	 * Allocated at the end of KVA space.
    402 	 */
    403 	RELOC(Sysmap, pt_entry_t *) = (pt_entry_t *)SYSMAP_VA;
    404 
    405 	/*
    406 	 * Remember the u-area address so it can be loaded in the lwp0
    407 	 * via uvm_lwp_setuarea() later in pmap_bootstrap_finalize().
    408 	 */
    409 	RELOC(lwp0uarea, vaddr_t) = lwp0upa - firstpa;
    410 
    411 	/*
    412 	 * Initialize the mem_clusters[] array for the crash dump
    413 	 * code.  While we're at it, compute the total amount of
    414 	 * physical memory in the system.
    415 	 */
    416 	for (i = 0; i < VM_PHYSSEG_MAX; i++) {
    417 		if (RELOC(phys_seg_list[i].ps_start, paddr_t) ==
    418 		    RELOC(phys_seg_list[i].ps_end, paddr_t)) {
    419 			/*
    420 			 * No more memory.
    421 			 */
    422 			break;
    423 		}
    424 
    425 		/*
    426 		 * Make sure these are properly rounded.
    427 		 */
    428 		RELOC(phys_seg_list[i].ps_start, paddr_t) =
    429 		    m68k_round_page(RELOC(phys_seg_list[i].ps_start,
    430 					  paddr_t));
    431 		RELOC(phys_seg_list[i].ps_end, paddr_t) =
    432 		    m68k_trunc_page(RELOC(phys_seg_list[i].ps_end,
    433 					  paddr_t));
    434 
    435 		size = RELOC(phys_seg_list[i].ps_end, paddr_t) -
    436 		    RELOC(phys_seg_list[i].ps_start, paddr_t);
    437 
    438 		RELOC(mem_clusters[i].start, u_quad_t) =
    439 		    RELOC(phys_seg_list[i].ps_start, paddr_t);
    440 		RELOC(mem_clusters[i].size, u_quad_t) = size;
    441 
    442 		RELOC(physmem, int) += size >> PGSHIFT;
    443 
    444 		RELOC(mem_cluster_cnt, int) += 1;
    445 	}
    446 
    447 	/*
    448 	 * Scoot the start of available on-board RAM forward to
    449 	 * account for:
    450 	 *
    451 	 *	(1) The bootstrap programs in low memory (so
    452 	 *	    that we can jump back to them without
    453 	 *	    reloading).
    454 	 *
    455 	 *	(2) The kernel text, data, and bss.
    456 	 *
    457 	 *	(3) The pages we stole above for pmap data
    458 	 *	    structures.
    459 	 */
    460 	RELOC(phys_seg_list[0].ps_start, paddr_t) = nextpa;
    461 
    462 	/*
    463 	 * Reserve space at the end of on-board RAM for the message
    464 	 * buffer.  We force it into on-board RAM because VME RAM
    465 	 * gets cleared very early on in locore.s (to initialise
    466 	 * parity on boards that need it). This would clobber the
    467 	 * messages from a previous running NetBSD system.
    468 	 */
    469 	RELOC(phys_seg_list[0].ps_end, paddr_t) -=
    470 	    m68k_round_page(MSGBUFSIZE);
    471 	RELOC(msgbufpa, paddr_t) =
    472 	    RELOC(phys_seg_list[0].ps_end, paddr_t);
    473 
    474 	/*
    475 	 * Initialize avail_start and avail_end.
    476 	 */
    477 	i = RELOC(mem_cluster_cnt, int) - 1;
    478 	RELOC(avail_start, paddr_t) =
    479 	    RELOC(phys_seg_list[0].ps_start, paddr_t);
    480 	RELOC(avail_end, paddr_t) =
    481 	    RELOC(phys_seg_list[i].ps_end, paddr_t);
    482 
    483 	RELOC(mem_size, vsize_t) = m68k_ptob(RELOC(physmem, int));
    484 
    485 	RELOC(virtual_end, vaddr_t) = VM_MAX_KERNEL_ADDRESS;
    486 
    487 	/*
    488 	 * Allocate some fixed, special purpose kernel virtual addresses
    489 	 */
    490 	{
    491 		vaddr_t va = RELOC(virtual_avail, vaddr_t);
    492 
    493 		RELOC(CADDR1, void *) = (void *)va;
    494 		va += PAGE_SIZE;
    495 		RELOC(CADDR2, void *) = (void *)va;
    496 		va += PAGE_SIZE;
    497 		RELOC(vmmap, void *) = (void *)va;
    498 		va += PAGE_SIZE;
    499 		RELOC(msgbufaddr, void *) = (void *)va;
    500 		va += m68k_round_page(MSGBUFSIZE);
    501 		RELOC(virtual_avail, vaddr_t) = va;
    502 	}
    503 }
    504