Home | History | Annotate | Line # | Download | only in mvme68k
      1 /*	$NetBSD: pmap_bootstrap.c,v 1.67 2026/04/05 14:35:49 thorpej 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.67 2026/04/05 14:35:49 thorpej 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 #define RELOC(v, t)	*((t*)((uintptr_t)&(v) + firstpa))
     52 
     53 extern char *etext;
     54 
     55 extern vaddr_t kernel_reloc_offset;
     56 
     57 /*
     58  * Special purpose kernel virtual addresses, used for mapping
     59  * physical pages for a variety of temporary or permanent purposes:
     60  *
     61  *	CADDR1, CADDR2:	pmap zero/copy operations
     62  *	vmmap:		/dev/mem, crash dumps, parity error checking
     63  *	msgbufaddr:	kernel message buffer
     64  */
     65 void *CADDR1, *CADDR2;
     66 char *vmmap;
     67 extern void *msgbufaddr;
     68 
     69 paddr_t pmap_bootstrap1(paddr_t, paddr_t);
     70 
     71 /*
     72  * Bootstrap the VM system.
     73  *
     74  * Called with MMU off so we must relocate all global references by `firstpa'
     75  * (don't call any functions here!)  `nextpa' is the first available physical
     76  * memory address.  Returns an updated first PA reflecting the memory we
     77  * have allocated.  MMU is still off when we return.
     78  *
     79  * XXX assumes sizeof(u_int) == sizeof(pt_entry_t)
     80  * XXX a PIC compiler would make this much easier.
     81  */
     82 paddr_t __attribute__((no_instrument_function))
     83 pmap_bootstrap1(paddr_t nextpa, paddr_t firstpa)
     84 {
     85 	paddr_t lwp0upa, kstpa, kptmpa, kptpa;
     86 	u_int nptpages, kstsize;
     87 	st_entry_t protoste, *ste, *este;
     88 	pt_entry_t protopte, *pte, *epte;
     89 	u_int iiomappages;
     90 #if defined(M68040) || defined(M68060)
     91 	int i;
     92 	u_int stfree = 0;	/* XXX: gcc -Wuninitialized */
     93 #endif
     94 
     95 	nextpa = m68k_round_page(nextpa);
     96 
     97 	/*
     98 	 * Calculate important physical addresses:
     99 	 *
    100 	 *	lwp0upa		lwp0 u-area		UPAGES pages
    101 	 *
    102 	 *	kstpa		kernel segment table	1 page (!040)
    103 	 *						N pages (040)
    104 	 *
    105 	 *	kptmpa		kernel PT map		1 page
    106 	 *
    107 	 *	kptpa		statically allocated
    108 	 *			kernel PT pages		Sysptsize+ pages
    109 	 *
    110 	 * [ Sysptsize is the number of pages of PT, and iiomappages is the
    111 	 *   number of PTEs, hence we need to round the total to a page
    112 	 *   boundary with IO maps at the end. ]
    113 	 *
    114 	 * The KVA corresponding to any of these PAs is:
    115 	 *	(PA - firstpa + KERNBASE).
    116 	 */
    117 	RELOC(kernel_reloc_offset, vaddr_t) = firstpa;
    118 
    119 	u_int intio_endpa =
    120 	    RELOC(intiobase_phys, u_int) + RELOC(intiosize, u_int);
    121 	iiomappages = m68k_btop(intio_endpa - RELOC(intiobase_phys, u_int));
    122 
    123 	lwp0upa = nextpa;
    124 	nextpa += USPACE;
    125 #if defined(M68040) || defined(M68060)
    126 	if (RELOC(mmutype, int) == MMU_68040)
    127 		kstsize = MAXKL2SIZE / (NPTEPG/SG4_LEV2SIZE);
    128 	else
    129 #endif
    130 		kstsize = 1;
    131 	kstpa = nextpa;
    132 	nextpa += kstsize * PAGE_SIZE;
    133 	kptmpa = nextpa;
    134 	nextpa += PAGE_SIZE;
    135 	kptpa = nextpa;
    136 	nptpages = RELOC(Sysptsize, int) +
    137 	    howmany(RELOC(physmem, psize_t), NPTEPG) +
    138 	    (iiomappages + NPTEPG - 1) / NPTEPG;
    139 	nextpa += nptpages * PAGE_SIZE;
    140 
    141 	/*
    142 	 * Clear all PTEs to zero
    143 	 */
    144 	for (pte = (pt_entry_t *)kstpa; pte < (pt_entry_t *)nextpa; pte++)
    145 		*pte = 0;
    146 
    147 	/*
    148 	 * Initialize segment table and kernel page table map.
    149 	 *
    150 	 * On 68030s and earlier MMUs the two are identical except for
    151 	 * the valid bits so both are initialized with essentially the
    152 	 * same values.  On the 68040, which has a mandatory 3-level
    153 	 * structure, the segment table holds the level 1 table and part
    154 	 * (or all) of the level 2 table and hence is considerably
    155 	 * different.  Here the first level consists of 128 descriptors
    156 	 * (512 bytes) each mapping 32mb of address space.  Each of these
    157 	 * points to blocks of 128 second level descriptors (512 bytes)
    158 	 * each mapping 256kb.  Note that there may be additional "segment
    159 	 * table" pages depending on how large MAXKL2SIZE is.
    160 	 *
    161 	 * Portions of the last segment of KVA space (0xFFC00000 -
    162 	 * 0xFFFFFFFF) are mapped for the kernel page tables.
    163 	 *
    164 	 * XXX cramming two levels of mapping into the single "segment"
    165 	 * table on the 68040 is intended as a temporary hack to get things
    166 	 * working.  The 224mb of address space that this allows will most
    167 	 * likely be insufficient in the future (at least for the kernel).
    168 	 */
    169 #if defined(M68040) || defined(M68060)
    170 	if (RELOC(mmutype, int) == MMU_68040) {
    171 		int nl1desc, nl2desc;
    172 
    173 		/*
    174 		 * First invalidate the entire "segment table" pages
    175 		 * (levels 1 and 2 have the same "invalid" value).
    176 		 */
    177 		ste = (st_entry_t *)kstpa;
    178 		este = &ste[kstsize * NPTEPG];
    179 		while (ste < este)
    180 			*ste++ = SG_NV;
    181 		/*
    182 		 * Initialize level 2 descriptors (which immediately
    183 		 * follow the level 1 table).  We need:
    184 		 *	NPTEPG / SG4_LEV3SIZE
    185 		 * level 2 descriptors to map each of the nptpages
    186 		 * pages of PTEs.  Note that we set the "used" bit
    187 		 * now to save the HW the expense of doing it.
    188 		 */
    189 		nl2desc = nptpages * (NPTEPG / SG4_LEV3SIZE);
    190 		ste = (st_entry_t *)kstpa;
    191 		ste = &ste[SG4_LEV1SIZE];
    192 		este = &ste[nl2desc];
    193 		protoste = kptpa | SG_U | SG_RW | SG_V;
    194 		while (ste < este) {
    195 			*ste++ = protoste;
    196 			protoste += (SG4_LEV3SIZE * sizeof(st_entry_t));
    197 		}
    198 		/*
    199 		 * Initialize level 1 descriptors.  We need:
    200 		 *	howmany(nl2desc, SG4_LEV2SIZE)
    201 		 * level 1 descriptors to map the `nl2desc' level 2's.
    202 		 */
    203 		nl1desc = howmany(nl2desc, SG4_LEV2SIZE);
    204 		ste = (st_entry_t *)kstpa;
    205 		este = &ste[nl1desc];
    206 		protoste = (paddr_t)&ste[SG4_LEV1SIZE] | SG_U | SG_RW | SG_V;
    207 		while (ste < este) {
    208 			*ste++ = protoste;
    209 			protoste += (SG4_LEV2SIZE * sizeof(st_entry_t));
    210 		}
    211 		/*
    212 		 * Initialize the final level 1 descriptor to map the next
    213 		 * block of level 2 descriptors for Sysptmap.
    214 		 */
    215 		ste = (st_entry_t *)kstpa;
    216 		ste = &ste[SG4_LEV1SIZE - 1];
    217 		*ste = protoste;
    218 		/*
    219 		 * Now initialize the final portion of that block of
    220 		 * descriptors to map Sysmap.
    221 		 */
    222 		i = SG4_LEV1SIZE + (nl1desc * SG4_LEV2SIZE);
    223 		ste = (st_entry_t *)kstpa;
    224 		ste = &ste[i + SG4_LEV2SIZE - (NPTEPG / SG4_LEV3SIZE)];
    225 		este = &ste[NPTEPG / SG4_LEV3SIZE];
    226 		protoste = kptmpa | SG_U | SG_RW | SG_V;
    227 		while (ste < este) {
    228 			*ste++ = protoste;
    229 			protoste += (SG4_LEV3SIZE * sizeof(st_entry_t));
    230 		}
    231 		/*
    232 		 * Calculate the free level 2 descriptor mask
    233 		 * noting that we have used:
    234 		 *	0:		level 1 table
    235 		 *	1 to nl1desc:	map page tables
    236 		 *	nl1desc + 1:	maps kptmpa and last-page page table
    237 		 */
    238 		/* mark an entry for level 1 table */
    239 		stfree = ~l2tobm(0);
    240 		/* mark entries for map page tables */
    241 		for (i = 1; i <= nl1desc; i++)
    242 			stfree &= ~l2tobm(i);
    243 		/* mark an entry for kptmpa and lkptpa */
    244 		stfree &= ~l2tobm(i);
    245 		/* mark entries not available */
    246 		for (i = MAXKL2SIZE; i < sizeof(stfree) * NBBY; i++)
    247 			stfree &= ~l2tobm(i);
    248 
    249 		/*
    250 		 * Initialize Sysptmap
    251 		 */
    252 		pte = (pt_entry_t *)kptmpa;
    253 		epte = &pte[nptpages];
    254 		protopte = kptpa | PG_RW | PG_CI | PG_U | PG_V;
    255 		while (pte < epte) {
    256 			*pte++ = protopte;
    257 			protopte += PAGE_SIZE;
    258 		}
    259 		/*
    260 		 * Invalidate all remaining entries.
    261 		 */
    262 		epte = (pt_entry_t *)kptmpa;
    263 		epte = &epte[TIB_SIZE];
    264 		while (pte < epte) {
    265 			*pte++ = PG_NV;
    266 		}
    267 		/*
    268 		 * Initialize the last one to point to Sysptmap.
    269 		 */
    270 		pte = (pt_entry_t *)kptmpa;
    271 		pte = &pte[SYSMAP_VA >> SEGSHIFT];
    272 		*pte = kptmpa | PG_RW | PG_CI | PG_V;
    273 	} else
    274 #endif /* M68040 || M68060 */
    275 	{
    276 		/*
    277 		 * Map the page table pages in both the HW segment table
    278 		 * and the software Sysptmap.
    279 		 */
    280 		ste = (st_entry_t *)kstpa;
    281 		pte = (pt_entry_t *)kptmpa;
    282 		epte = &pte[nptpages];
    283 		protoste = kptpa | SG_RW | SG_V;
    284 		protopte = kptpa | PG_RW | PG_CI | PG_V;
    285 		while (pte < epte) {
    286 			*ste++ = protoste;
    287 			*pte++ = protopte;
    288 			protoste += PAGE_SIZE;
    289 			protopte += PAGE_SIZE;
    290 		}
    291 		/*
    292 		 * Invalidate all remaining entries in both.
    293 		 */
    294 		este = (st_entry_t *)kstpa;
    295 		este = &este[TIA_SIZE];
    296 		while (ste < este)
    297 			*ste++ = SG_NV;
    298 		epte = (pt_entry_t *)kptmpa;
    299 		epte = &epte[TIB_SIZE];
    300 		while (pte < epte)
    301 			*pte++ = PG_NV;
    302 		/*
    303 		 * Initialize the last one to point to Sysptmap.
    304 		 */
    305 		ste = (st_entry_t *)kstpa;
    306 		ste = &ste[SYSMAP_VA >> SEGSHIFT];
    307 		pte = (pt_entry_t *)kptmpa;
    308 		pte = &pte[SYSMAP_VA >> SEGSHIFT];
    309 		*ste = kptmpa | SG_RW | SG_V;
    310 		*pte = kptmpa | PG_RW | PG_CI | PG_V;
    311 	}
    312 
    313 	/*
    314 	 * Initialize kernel page table.
    315 	 * Start by invalidating the `nptpages' that we have allocated.
    316 	 */
    317 	pte = (pt_entry_t *)kptpa;
    318 	epte = &pte[nptpages * NPTEPG];
    319 	while (pte < epte)
    320 		*pte++ = PG_NV;
    321 	/*
    322 	 * Validate PTEs for kernel text (RO).
    323 	 */
    324 	pte = (pt_entry_t *)kptpa;
    325 	pte = &pte[m68k_btop(KERNBASE)];
    326 	epte = &pte[m68k_btop(m68k_trunc_page(&etext))];
    327 	protopte = firstpa | PG_RO | PG_U | PG_V;
    328 	while (pte < epte) {
    329 		*pte++ = protopte;
    330 		protopte += PAGE_SIZE;
    331 	}
    332 	/*
    333 	 * Validate PTEs for kernel data/bss, dynamic data allocated
    334 	 * by us so far (kstpa - firstpa bytes), and pages for lwp0
    335 	 * u-area and page table allocated below (RW).
    336 	 */
    337 	epte = (pt_entry_t *)kptpa;
    338 	epte = &epte[m68k_btop(kstpa - firstpa)];
    339 	protopte = (protopte & ~PG_PROT) | PG_RW;
    340 	/*
    341 	 * Enable copy-back caching of data pages
    342 	 */
    343 	if (RELOC(mmutype, int) == MMU_68040)
    344 		protopte |= PG_CCB;
    345 	while (pte < epte) {
    346 		*pte++ = protopte;
    347 		protopte += PAGE_SIZE;
    348 	}
    349 	/*
    350 	 * Map the kernel segment table cache invalidated for 68040/68060.
    351 	 * (for the 68040 not strictly necessary, but recommended by Motorola;
    352 	 *  for the 68060 mandatory)
    353 	 */
    354 	epte = (pt_entry_t *)kptpa;
    355 	epte = &epte[m68k_btop(nextpa - firstpa)];
    356 	protopte = (protopte & ~PG_PROT) | PG_RW;
    357 	if (RELOC(mmutype, int) == MMU_68040) {
    358 		protopte &= ~PG_CMASK;
    359 		protopte |= PG_CI;
    360 	}
    361 	while (pte < epte) {
    362 		*pte++ = protopte;
    363 		protopte += PAGE_SIZE;
    364 	}
    365 
    366 	/*
    367 	 * Finally, validate the internal IO space PTEs (RW+CI).
    368 	 */
    369 
    370 #define	PTE2VA(pte)	m68k_ptob(pte - ((pt_entry_t *)kptpa))
    371 
    372 	protopte = RELOC(intiobase_phys, u_int) | PG_RW | PG_CI | PG_U | PG_V;
    373 	epte = &pte[iiomappages];
    374 	RELOC(intiobase, uint8_t *) = (uint8_t *)PTE2VA(pte);
    375 	while (pte < epte) {
    376 		*pte++ = protopte;
    377 		protopte += PAGE_SIZE;
    378 	}
    379 	RELOC(virtual_avail, vaddr_t) = PTE2VA(pte);
    380 
    381 	/*
    382 	 * Calculate important exported kernel addresses and related values.
    383 	 */
    384 	/*
    385 	 * Sysseg: base of kernel segment table
    386 	 */
    387 	RELOC(Sysseg, st_entry_t *) = (st_entry_t *)(kstpa - firstpa);
    388 	RELOC(Sysseg_pa, paddr_t) = kstpa;
    389 #if defined(M68040) || defined(M68060)
    390 	if (RELOC(mmutype, int) == MMU_68040)
    391 		RELOC(protostfree, u_int) = stfree;
    392 #endif
    393 	/*
    394 	 * Sysptmap: base of kernel page table map
    395 	 */
    396 	RELOC(Sysptmap, pt_entry_t *) = (pt_entry_t *)(kptmpa - firstpa);
    397 	/*
    398 	 * Sysmap: kernel page table (as mapped through Sysptmap)
    399 	 * Allocated at the end of KVA space.
    400 	 */
    401 	RELOC(Sysmap, pt_entry_t *) = (pt_entry_t *)SYSMAP_VA;
    402 
    403 	/*
    404 	 * Remember the u-area address so it can be loaded in the lwp0
    405 	 * via uvm_lwp_setuarea() later in pmap_bootstrap2().
    406 	 */
    407 	RELOC(lwp0uarea, vaddr_t) = lwp0upa - firstpa;
    408 
    409 	RELOC(virtual_end, vaddr_t) = VM_MAX_KERNEL_ADDRESS;
    410 
    411 	/*
    412 	 * Allocate some fixed, special purpose kernel virtual addresses
    413 	 */
    414 	{
    415 		vaddr_t va = RELOC(virtual_avail, vaddr_t);
    416 
    417 		RELOC(CADDR1, void *) = (void *)va;
    418 		va += PAGE_SIZE;
    419 		RELOC(CADDR2, void *) = (void *)va;
    420 		va += PAGE_SIZE;
    421 		RELOC(vmmap, void *) = (void *)va;
    422 		va += PAGE_SIZE;
    423 		RELOC(msgbufaddr, void *) = (void *)va;
    424 		va += m68k_round_page(MSGBUFSIZE);
    425 		RELOC(virtual_avail, vaddr_t) = va;
    426 	}
    427 
    428 	return nextpa;
    429 }
    430