Home | History | Annotate | Line # | Download | only in atari
      1 /*	$NetBSD: pmap_bootstrap.c,v 1.15 2026/11/15 15:16:23 thorpej Exp $	*/
      2 /*-
      3  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Jason R. Thorpe.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * Copyright (c) 1991 Regents of the University of California.
     33  * All rights reserved.
     34  *
     35  * This code is derived from software contributed to Berkeley by
     36  * the Systems Programming Group of the University of Utah Computer
     37  * Science Department.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. Neither the name of the University nor the names of its contributors
     48  *    may be used to endorse or promote products derived from this software
     49  *    without specific prior written permission.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61  * SUCH DAMAGE.
     62  *
     63  *	@(#)pmap.c	7.5 (Berkeley) 5/10/91
     64  */
     65 
     66 /*
     67  * Copyright (c) 1995 Leo Weppelman
     68  * Copyright (c) 1994 Michael L. Hitch
     69  * Copyright (c) 1993 Markus Wild
     70  * All rights reserved.
     71  *
     72  * Redistribution and use in source and binary forms, with or without
     73  * modification, are permitted provided that the following conditions
     74  * are met:
     75  * 1. Redistributions of source code must retain the above copyright
     76  *    notice, this list of conditions and the following disclaimer.
     77  * 2. Redistributions in binary form must reproduce the above copyright
     78  *    notice, this list of conditions and the following disclaimer in the
     79  *    documentation and/or other materials provided with the distribution.
     80  * 3. All advertising materials mentioning features or use of this software
     81  *    must display the following acknowledgement:
     82  *      This product includes software developed by Markus Wild.
     83  * 4. The name of the author may not be used to endorse or promote products
     84  *    derived from this software without specific prior written permission
     85  *
     86  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     87  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     88  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     89  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     90  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     91  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     92  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     93  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     94  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     95  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     96  */
     97 
     98 #include <sys/cdefs.h>
     99 
    100 #include "opt_m68k_arch.h"
    101 #include "opt_mbtype.h"
    102 
    103 #include <sys/param.h>
    104 #include <sys/systm.h>
    105 #include <sys/proc.h>
    106 
    107 #include <uvm/uvm.h>
    108 
    109 #include <machine/pte.h>
    110 #include <machine/cpu.h>
    111 #include <machine/vmparam.h>
    112 
    113 #include <m68k/cacheops.h>
    114 #include <machine/iomap.h>
    115 #include <sys/bus.h>
    116 
    117 static vaddr_t bootstrap_vstart;
    118 
    119 /*
    120  * Keep these declarations here during preparation for NEW_PMAP.
    121  * They may become unnecessary after the transition, so defer moving
    122  * them to a header.
    123  */
    124 extern psize_t st_pool_size;
    125 extern paddr_t st_pool_phys;
    126 extern vaddr_t st_pool_virt;
    127 extern vaddr_t page_zero;
    128 
    129 #define	RELOC_PA(base, pa)	((base) + (pa))	/* used to set up PTE etc. */
    130 int kernel_copyback = 1;
    131 
    132 static void mmu030_setup(paddr_t, u_int, paddr_t, psize_t, paddr_t, paddr_t);
    133 static void map_io_areas(paddr_t, psize_t, u_int);
    134 #if defined(M68040) || defined(M68060)
    135 static void mmu040_setup(paddr_t, u_int, paddr_t, psize_t, paddr_t, paddr_t);
    136 #endif
    137 
    138 extern paddr_t	avail_start;
    139 extern paddr_t	avail_end;
    140 
    141 extern paddr_t	msgbufpa;
    142 
    143 /*
    144  * All those kernel PT submaps that BSD is so fond of
    145  */
    146 void		*CADDR1, *CADDR2;
    147 
    148 paddr_t __attribute__((no_instrument_function))
    149 pmap_bootstrap1(paddr_t nextpa, paddr_t reloff)
    150 {
    151 	extern void etext(void);
    152 	paddr_t pstart, ptpa, Sysptmap_pa;
    153 	vaddr_t vstart;		/* Next available virtual address */
    154 	vaddr_t kva;
    155 	vsize_t avail;
    156 	psize_t ptsize;
    157 	u_int ptextra, kstsize;
    158 	pt_entry_t *pg, *epg, pg_proto;
    159 
    160 	/*
    161 	 * We run the kernel from ST memory at the moment.
    162 	 * The kernel segment table is put just behind the loaded image.
    163 	 * pstart: start of usable ST memory
    164 	 * avail : size of ST memory available.
    165 	 */
    166 	vstart = (vaddr_t)nextpa;
    167 	vstart = m68k_round_page(vstart);
    168 	pstart = (paddr_t)vstart;	/* pre-reloc PA == kernel VA here */
    169 	avail  = st_pool_phys - pstart;
    170 
    171 	/*
    172 	 * Save KVA of lwp0 uarea and allocate it.
    173 	 */
    174 	lwp0uarea  = vstart;
    175 	pstart    += USPACE;
    176 	vstart    += USPACE;
    177 	avail     -= USPACE;
    178 
    179 	/*
    180 	 * Calculate the number of pages needed for Sysseg.
    181 	 * For the 68030, we need 256 descriptors (segment-table-entries).
    182 	 * This easily fits into one page.
    183 	 * For the 68040, both the level-1 and level-2 descriptors are
    184 	 * stored into Sysseg. We currently handle a maximum sum of MAXKL2SIZE
    185 	 * level-1 & level-2 tables.
    186 	 */
    187 #if defined(M68040) || defined(M68060)
    188 	if (mmutype == MMU_68040)
    189 		kstsize = MAXKL2SIZE / (NPTEPG/SG4_LEV2SIZE);
    190 	else
    191 #endif
    192 		kstsize = 1;
    193 	/*
    194 	 * allocate the kernel segment table
    195 	 */
    196 	Sysseg_pa  = pstart;			/* pre-reloc PA to init STEs */
    197 	Sysseg     = (st_entry_t *)vstart;
    198 	pstart    += kstsize * PAGE_SIZE;
    199 	vstart    += kstsize * PAGE_SIZE;
    200 	avail     -= kstsize * PAGE_SIZE;
    201 
    202 	/*
    203 	 * allocate kernel page table map
    204 	 */
    205 	Sysptmap_pa = pstart;			/* pre-reloc PA to init PTEs */
    206 	Sysptmap = (pt_entry_t *)vstart;
    207 	pstart  += PAGE_SIZE;
    208 	vstart  += PAGE_SIZE;
    209 	avail   -= PAGE_SIZE;
    210 
    211 	/*
    212 	 * Determine the number of pte's we need for extra's like
    213 	 * ST I/O map's.
    214 	 */
    215 	ptextra = btoc(STIO_SIZE);
    216 
    217 	/*
    218 	 * If present, add pci areas
    219 	 */
    220 	if (machineid & ATARI_HADES)
    221 		ptextra += btoc(PCI_CONFIG_SIZE + PCI_IO_SIZE + PCI_MEM_SIZE);
    222 	if (machineid & ATARI_MILAN)
    223 		ptextra += btoc(PCI_IO_SIZE + PCI_MEM_SIZE);
    224 	ptextra += btoc(BOOTM_VA_POOL);
    225 	/*
    226 	 * now need to account for the kmem area, which is allocated
    227 	 * before pmap_init() is called.  It is roughly the size of physical
    228 	 * memory.
    229 	 */
    230 	ptextra += physmem;
    231 
    232 	/*
    233 	 * The 'pt' (the initial kernel pagetable) has to map the kernel and
    234 	 * the I/O areas. The various I/O areas are mapped (virtually) at
    235 	 * the top of the address space mapped by 'pt' (ie. just below Sysmap).
    236 	 */
    237 	ptpa	= pstart;			/* pre-reloc PA to init PTEs */
    238 	ptsize  = (Sysptsize + howmany(ptextra, NPTEPG)) << PGSHIFT;
    239 	pstart += ptsize;
    240 	vstart += ptsize;
    241 	avail  -= ptsize;
    242 
    243 	/*
    244 	 * Sysmap is now placed at the end of Supervisor virtual address space.
    245 	 */
    246 	Sysmap = (pt_entry_t *)SYSMAP_VA;
    247 
    248 	/*
    249 	 * Initialize segment tables
    250 	 */
    251 #if defined(M68040) || defined(M68060)
    252 	if (mmutype == MMU_68040)
    253 		mmu040_setup(Sysseg_pa, kstsize, ptpa, ptsize, Sysptmap_pa,
    254 		    reloff);
    255 	else
    256 #endif /* defined(M68040) || defined(M68060) */
    257 		mmu030_setup(Sysseg_pa, kstsize, ptpa, ptsize, Sysptmap_pa,
    258 		    reloff);
    259 
    260 	/*
    261 	 * initialize kernel page table page(s).
    262 	 * Assume load at VA 0.
    263 	 * - Text pages are RO
    264 	 * - Page zero is invalid
    265 	 */
    266 	pg_proto = RELOC_PA(reloff, 0) | PG_RO | PG_V;
    267 	pg       = (pt_entry_t *)ptpa;
    268 	*pg++    = PG_NV;
    269 
    270 	pg_proto += PAGE_SIZE;
    271 	for (kva = PAGE_SIZE; kva < (vaddr_t)etext; kva += PAGE_SIZE) {
    272 		*pg++ = pg_proto;
    273 		pg_proto += PAGE_SIZE;
    274 	}
    275 
    276 	/*
    277 	 * data, bss and dynamic tables are read/write
    278 	 */
    279 	pg_proto = (pg_proto & PG_FRAME) | PG_RW | PG_V;
    280 
    281 #if defined(M68040) || defined(M68060)
    282 	/*
    283 	 * Map the kernel segment table cache invalidated for 68040/68060.
    284 	 * (for the 68040 not strictly necessary, but recommended by Motorola;
    285 	 *  for the 68060 mandatory)
    286 	 */
    287 	if (mmutype == MMU_68040) {
    288 
    289 		if (kernel_copyback)
    290 			pg_proto |= PG_CCB;
    291 
    292 		for (; kva < (vaddr_t)Sysseg; kva += PAGE_SIZE) {
    293 			*pg++ = pg_proto;
    294 			pg_proto += PAGE_SIZE;
    295 		}
    296 
    297 		pg_proto = (pg_proto & ~PG_CCB) | PG_CI;
    298 		for (; kva < (vaddr_t)Sysptmap; kva += PAGE_SIZE) {
    299 			*pg++ = pg_proto;
    300 			pg_proto += PAGE_SIZE;
    301 		}
    302 
    303 		pg_proto = (pg_proto & ~PG_CI);
    304 		if (kernel_copyback)
    305 			pg_proto |= PG_CCB;
    306 	}
    307 #endif /* defined(M68040) || defined(M68060) */
    308 
    309 	/*
    310 	 * go till end of data allocated so far
    311 	 * plus lwp0 u-area (to be allocated)
    312 	 */
    313 	for (; kva < vstart; kva += PAGE_SIZE) {
    314 		*pg++ = pg_proto;
    315 		pg_proto += PAGE_SIZE;
    316 	}
    317 
    318 	/*
    319 	 * invalidate remainder of kernel PT
    320 	 */
    321 	epg = (pt_entry_t *)ptpa;
    322 	epg = &epg[ptsize / sizeof(pt_entry_t)];
    323 	while (pg < epg)
    324 		*pg++ = PG_NV;
    325 
    326 	/*
    327 	 * Map various I/O areas
    328 	 */
    329 	map_io_areas(ptpa, ptsize, ptextra);
    330 
    331 	/*
    332 	 * Map the allocated space in ST-ram now. In the contig-case, there
    333 	 * is no need to make a distinction between virtual and physical
    334 	 * addresses. But I make it anyway to be prepared.
    335 	 * Physical space is already reserved!
    336 	 */
    337 	st_pool_virt = vstart;
    338 	pg           = (pt_entry_t *)ptpa;
    339 	pg           = &pg[vstart / PAGE_SIZE];
    340 	pg_proto     = st_pool_phys | PG_RW | PG_CI | PG_V;
    341 	vstart      += st_pool_size;
    342 	while (pg_proto < (st_pool_phys + st_pool_size)) {
    343 		*pg++     = pg_proto;
    344 		pg_proto += PAGE_SIZE;
    345 	}
    346 
    347 	/*
    348 	 * Map physical page_zero and page-zero+1 (First ST-ram page). We need
    349 	 * to reference it in the reboot code. Two pages are mapped, because
    350 	 * we must make sure 'doboot()' is contained in it (see the tricky
    351 	 * copying there....).
    352 	 */
    353 	page_zero  = vstart;
    354 	pg         = (pt_entry_t *)ptpa;
    355 	pg         = &pg[vstart / PAGE_SIZE];
    356 	*pg++      = PG_RW | PG_CI | PG_V;
    357 	vstart    += PAGE_SIZE;
    358 	*pg        = PG_RW | PG_CI | PG_V | PAGE_SIZE;
    359 	vstart    += PAGE_SIZE;
    360 
    361 	/*
    362 	 * All necessary STEs and PTEs have been initialized.
    363 	 * Update Sysseg_pa and Sysptmap_pa to point relocated PA.
    364 	 */
    365 	if (reloff) {
    366 		Sysseg_pa   += reloff;
    367 		Sysptmap_pa += reloff;
    368 	}
    369 
    370 	bootstrap_vstart = vstart;
    371 	return pstart;
    372 }
    373 
    374 /*
    375  * Do the dull work of mapping the various I/O areas. They MUST be Cache
    376  * inhibited!
    377  * All I/O areas are virtually mapped at the end of the pt-table.
    378  */
    379 static void __attribute__((no_instrument_function))
    380 map_io_areas(paddr_t ptpa, psize_t ptsize, u_int ptextra)
    381 	/* ptsize:	 Size of 'pt' in bytes		*/
    382 	/* ptextra:	 #of additional I/O pte's	*/
    383 {
    384 	vaddr_t		ioaddr;
    385 	pt_entry_t	*pt, *pg, *epg;
    386 	pt_entry_t	pg_proto;
    387 	u_long		mask;
    388 
    389 	pt = (pt_entry_t *)ptpa;
    390 	ioaddr = ((ptsize / sizeof(pt_entry_t)) - ptextra) * PAGE_SIZE;
    391 
    392 	/*
    393 	 * Map ST-IO area
    394 	 */
    395 	stio_addr = ioaddr;
    396 	ioaddr   += STIO_SIZE;
    397 	pg        = &pt[stio_addr / PAGE_SIZE];
    398 	epg       = &pg[btoc(STIO_SIZE)];
    399 #ifdef _MILANHW_
    400 	/*
    401 	 * Turn on byte swaps in the ST I/O area. On the Milan, the
    402 	 * U0 signal of the MMU controls the BigEndian signal
    403 	 * of the PLX9080. We use this setting so we can read/write the
    404 	 * PLX registers (and PCI-config space) in big-endian mode.
    405 	 */
    406 	pg_proto  = STIO_PHYS | PG_RW | PG_CI | PG_V | PTE40_U0;
    407 #else
    408 	pg_proto  = STIO_PHYS | PG_RW | PG_CI | PG_V;
    409 #endif
    410 	while (pg < epg) {
    411 		*pg++     = pg_proto;
    412 		pg_proto += PAGE_SIZE;
    413 	}
    414 
    415 	/*
    416 	 * Map PCI areas
    417 	 */
    418 	if (machineid & ATARI_HADES) {
    419 		/*
    420 		 * Only Hades maps the PCI-config space!
    421 		 */
    422 		pci_conf_addr = ioaddr;
    423 		ioaddr       += PCI_CONFIG_SIZE;
    424 		pg            = &pt[pci_conf_addr / PAGE_SIZE];
    425 		epg           = &pg[btoc(PCI_CONFIG_SIZE)];
    426 		mask          = PCI_CONFM_PHYS;
    427 		pg_proto      = PCI_CONFB_PHYS | PG_RW | PG_CI | PG_V;
    428 		for (; pg < epg; mask <<= 1)
    429 			*pg++ = pg_proto | mask;
    430 	} else
    431 		pci_conf_addr = 0; /* XXX: should crash */
    432 
    433 	if (machineid & (ATARI_HADES|ATARI_MILAN)) {
    434 		pci_io_addr   = ioaddr;
    435 		ioaddr       += PCI_IO_SIZE;
    436 		pg	      = &pt[pci_io_addr / PAGE_SIZE];
    437 		epg           = &pg[btoc(PCI_IO_SIZE)];
    438 		pg_proto      = PCI_IO_PHYS | PG_RW | PG_CI | PG_V;
    439 		while (pg < epg) {
    440 			*pg++     = pg_proto;
    441 			pg_proto += PAGE_SIZE;
    442 		}
    443 
    444 		pci_mem_addr  = ioaddr;
    445 		/* Provide an uncached PCI address for the MILAN */
    446 		pci_mem_uncached = ioaddr;
    447 		ioaddr       += PCI_MEM_SIZE;
    448 		epg           = &pg[btoc(PCI_MEM_SIZE)];
    449 		pg_proto      = PCI_VGA_PHYS | PG_RW | PG_CI | PG_V;
    450 		while (pg < epg) {
    451 			*pg++     = pg_proto;
    452 			pg_proto += PAGE_SIZE;
    453 		}
    454 	}
    455 
    456 	bootm_init(ioaddr, pg, BOOTM_VA_POOL);
    457 	/*
    458 	 * ioaddr += BOOTM_VA_POOL;
    459 	 * pg = &pg[btoc(BOOTM_VA_POOL)];
    460 	 */
    461 }
    462 
    463 void __attribute__((no_instrument_function))
    464 mmu030_setup(paddr_t sysseg_pa, u_int kstsize, paddr_t ptpa, psize_t ptsize,
    465     paddr_t sysptmap_pa, paddr_t kbase)
    466 	/* sysseg_pa:	 System segment table		*/
    467 	/* kstsize:	 size of 'sysseg' in pages	*/
    468 	/* ptpa:	 Kernel page table		*/
    469 	/* ptsize:	 size	of 'pt' in bytes	*/
    470 	/* sysptmap_pa:	 System page table		*/
    471 {
    472 	st_entry_t	sg_proto, *sg, *esg;
    473 	pt_entry_t	pg_proto, *pg, *epg;
    474 
    475 	/*
    476 	 * Map the page table pages in both the HW segment table
    477 	 * and the software Sysptmap.
    478 	 */
    479 	sg  = (st_entry_t *)sysseg_pa;
    480 	pg  = (pt_entry_t *)sysptmap_pa;
    481 	epg = &pg[ptsize >> PGSHIFT];
    482 	sg_proto = RELOC_PA(kbase, ptpa) | SG_RW | SG_V;
    483 	pg_proto = RELOC_PA(kbase, ptpa) | PG_RW | PG_CI | PG_V;
    484 	while (pg < epg) {
    485 		*sg++ = sg_proto;
    486 		*pg++ = pg_proto;
    487 		sg_proto += PAGE_SIZE;
    488 		pg_proto += PAGE_SIZE;
    489 	}
    490 
    491 	/*
    492 	 * Invalidate the remainder of the tables.
    493 	 */
    494 	esg = (st_entry_t *)sysseg_pa;
    495 	esg = &esg[TIA_SIZE];
    496 	while (sg < esg)
    497 		*sg++ = SG_NV;
    498 	epg = (pt_entry_t *)sysptmap_pa;
    499 	epg = &epg[TIB_SIZE];
    500 	while (pg < epg)
    501 		*pg++ = PG_NV;
    502 
    503 	/*
    504 	 * Initialize the PTE for the last one to point Sysptmap.
    505 	 */
    506 	sg = (st_entry_t *)sysseg_pa;
    507 	sg = &sg[SYSMAP_VA >> SEGSHIFT];
    508 	pg = (pt_entry_t *)sysptmap_pa;
    509 	pg = &pg[SYSMAP_VA >> SEGSHIFT];
    510 	*sg = RELOC_PA(kbase, sysptmap_pa) | SG_RW | SG_V;
    511 	*pg = RELOC_PA(kbase, sysptmap_pa) | PG_RW | PG_CI | PG_V;
    512 }
    513 
    514 #if defined(M68040) || defined(M68060)
    515 void __attribute__((no_instrument_function))
    516 mmu040_setup(paddr_t sysseg_pa, u_int kstsize, paddr_t ptpa, psize_t ptsize,
    517     paddr_t sysptmap_pa, paddr_t kbase)
    518 	/* sysseg_pa:	 System segment table		*/
    519 	/* kstsize:	 size of 'sysseg' in pages	*/
    520 	/* ptpa:	 Kernel page table		*/
    521 	/* ptsize:	 size	of 'pt' in bytes	*/
    522 	/* sysptmap_pa:	 System page table		*/
    523 {
    524 	int		nl1desc, nl2desc, i;
    525 	st_entry_t	sg_proto, *sg, *esg;
    526 	pt_entry_t	pg_proto, *pg, *epg;
    527 
    528 	/*
    529 	 * First invalidate the entire "segment table" pages
    530 	 * (levels 1 and 2 have the same "invalid" values).
    531 	 */
    532 	sg  = (st_entry_t *)sysseg_pa;
    533 	esg = &sg[kstsize * NPTEPG];
    534 	while (sg < esg)
    535 		*sg++ = SG_NV;
    536 
    537 	/*
    538 	 * Initialize level 2 descriptors (which immediately
    539 	 * follow the level 1 table).
    540 	 * We need:
    541 	 *	NPTEPG / SG4_LEV3SIZE
    542 	 * level 2 descriptors to map each of the nptpages
    543 	 * pages of PTEs.  Note that we set the "used" bit
    544 	 * now to save the HW the expense of doing it.
    545 	 */
    546 	nl2desc = (ptsize >> PGSHIFT) * (NPTEPG / SG4_LEV3SIZE);
    547 	sg  = (st_entry_t *)sysseg_pa;
    548 	sg  = &sg[SG4_LEV1SIZE];
    549 	esg = &sg[nl2desc];
    550 	sg_proto = RELOC_PA(kbase, ptpa) | SG_U | SG_RW | SG_V;
    551 	while (sg < esg) {
    552 		*sg++     = sg_proto;
    553 		sg_proto += (SG4_LEV3SIZE * sizeof(st_entry_t));
    554 	}
    555 
    556 	/*
    557 	 * Initialize level 1 descriptors.  We need:
    558 	 *	howmany(nl2desc, SG4_LEV2SIZE)
    559 	 * level 1 descriptors to map the 'nl2desc' level 2's.
    560 	 */
    561 	nl1desc = howmany(nl2desc, SG4_LEV2SIZE);
    562 	sg  = (st_entry_t *)sysseg_pa;
    563 	esg = &sg[nl1desc];
    564 	sg_proto = RELOC_PA(kbase, (paddr_t)&sg[SG4_LEV1SIZE])
    565 	    | SG_U | SG_RW | SG_V;
    566 	while (sg < esg) {
    567 		*sg++     = sg_proto;
    568 		sg_proto += (SG4_LEV2SIZE * sizeof(st_entry_t));
    569 	}
    570 
    571 	/* Sysmap is last entry in level 1 */
    572 	sg  = (st_entry_t *)sysseg_pa;
    573 	sg  = &sg[SG4_LEV1SIZE - 1];
    574 	*sg = sg_proto;
    575 
    576 	/*
    577 	 * Kernel segment table at end of next level 2 table
    578 	 */
    579 	i = SG4_LEV1SIZE + (nl1desc * SG4_LEV2SIZE);
    580 	sg  = (st_entry_t *)sysseg_pa;
    581 	sg  = &sg[i + SG4_LEV2SIZE - (NPTEPG / SG4_LEV3SIZE)];
    582 	esg = &sg[NPTEPG / SG4_LEV3SIZE];
    583 	sg_proto = RELOC_PA(kbase, sysptmap_pa) | SG_U | SG_RW | SG_V;
    584 	while (sg < esg) {
    585 		*sg++ = sg_proto;
    586 		sg_proto += (SG4_LEV3SIZE * sizeof(st_entry_t));
    587 	}
    588 
    589 	/* Include additional level 2 table for Sysmap in protostfree */
    590 	protostfree = (~0U << (1 + nl1desc + 1)) /* & ~(~0U << MAXKL2SIZE) */;
    591 
    592 	/*
    593 	 * Initialize Sysptmap
    594 	 */
    595 	pg  = (pt_entry_t *)sysptmap_pa;
    596 	epg = &pg[ptsize >> PGSHIFT];
    597 	pg_proto = RELOC_PA(kbase, ptpa) | PG_RW | PG_CI | PG_V;
    598 	while (pg < epg) {
    599 		*pg++ = pg_proto;
    600 		pg_proto += PAGE_SIZE;
    601 	}
    602 
    603 	/*
    604 	 * Invalidate rest of Sysptmap page.
    605 	 */
    606 	epg = (pt_entry_t *)sysptmap_pa;
    607 	epg = &epg[TIB_SIZE];
    608 	while (pg < epg)
    609 		*pg++ = PG_NV;
    610 
    611 	/*
    612 	 * Initialize the PTE for the last one to point Sysptmap.
    613 	 */
    614 	pg = (pt_entry_t *)sysptmap_pa;
    615 	pg = &pg[SYSMAP_VA >> SEGSHIFT];
    616 	*pg = RELOC_PA(kbase, sysptmap_pa) | PG_RW | PG_CI | PG_V;
    617 }
    618 #endif /* M68040 */
    619 
    620 #ifdef DEBUG
    621 void dump_segtable(u_int *);
    622 void dump_pagetable(u_int *, u_int, u_int);
    623 
    624 void
    625 dump_segtable(u_int *stp)
    626 {
    627 	u_int *s, *es;
    628 	int shift, i;
    629 
    630 	s = stp;
    631 	{
    632 		es = s + (M68K_STSIZE >> 2);
    633 		shift = SG_ISHIFT;
    634 	}
    635 
    636 	/*
    637 	 * XXX need changes for 68040
    638 	 */
    639 	for (i = 0; s < es; s++, i++)
    640 		if (*s & SG_V)
    641 			printf("$%08x: $%08x\t", i << shift, *s & SG_FRAME);
    642 	printf("\n");
    643 }
    644 
    645 void
    646 dump_pagetable(u_int *ptp, u_int i, u_int n)
    647 {
    648 	u_int *p, *ep;
    649 
    650 	p = ptp + i;
    651 	ep = p + n;
    652 	for (; p < ep; p++, i++)
    653 		if (*p & PG_V)
    654 			printf("$%08x -> $%08x\t", i, *p & PG_FRAME);
    655 	printf("\n");
    656 }
    657 
    658 #endif
    659 
    660 /*
    661  *	Bootstrap the system enough to run with virtual memory.
    662  *
    663  *	This is called after mapping has already been enabled
    664  *	and just syncs the pmap module with what has already been done.
    665  */
    666 void __attribute__((no_instrument_function))
    667 pmap_bootstrap(void)
    668 {
    669 	vaddr_t	va;
    670 	vaddr_t vstart = bootstrap_vstart;
    671 	int	i;
    672 
    673 	/*
    674 	 * Announce page-size to the VM-system
    675 	 */
    676 	uvmexp.pagesize = NBPG;
    677 	uvm_md_init();
    678 
    679 	/*
    680 	 * Setup physical address ranges
    681 	 */
    682 	for (i = 0; i < NMEM_SEGS && usable_segs[i].start; i++)
    683 		continue;
    684 	/* XXX: allow for msgbuf */
    685 	usable_segs[i - 1].end -= m68k_round_page(MSGBUFSIZE);
    686 	msgbufpa = usable_segs[i - 1].end;
    687 
    688 	/*
    689 	 * Count physical memory
    690 	 */
    691 	extern vsize_t mem_size;
    692 	mem_size = 0;
    693 	for (i = 0; i < NMEM_SEGS; i++) {
    694 		if (boot_segs[i].start == boot_segs[i].end)
    695 			break;
    696 		mem_size += boot_segs[i].end - boot_segs[i].start;
    697 	}
    698 
    699 	/*
    700 	 * Announce available memory to the VM-system
    701 	 */
    702 	for (i = 0; i < NMEM_SEGS && usable_segs[i].start; i++)
    703 		uvm_page_physload(atop(usable_segs[i].start),
    704 				 atop(usable_segs[i].end),
    705 				 atop(usable_segs[i].start),
    706 				 atop(usable_segs[i].end),
    707 				 usable_segs[i].free_list);
    708 
    709 	avail_start = usable_segs[0].start;
    710 	avail_end   = usable_segs[i - 1].end;
    711 
    712 	virtual_avail = vstart;
    713 	virtual_end   = VM_MAX_KERNEL_ADDRESS;
    714 
    715 	/*
    716 	 * Allocate all the submaps we need
    717 	 */
    718 #define	SYSMAP(c, v, n)	\
    719 	v = (c)va; va += ((n)*PAGE_SIZE);
    720 
    721 	va = virtual_avail;
    722 
    723 	SYSMAP(void *   ,CADDR1	,1				)
    724 	SYSMAP(void *   ,CADDR2	,1				)
    725 	SYSMAP(vaddr_t  ,cpu_info_primary.ci_tmpva_vmmap,1	)
    726 	SYSMAP(void *	,msgbufaddr ,btoc(MSGBUFSIZE)		)
    727 
    728 	DCIS();
    729 
    730 	virtual_avail = reserve_dumppages(va);
    731 }
    732