xref: /netbsd-src/sys/arch/mvme68k/mvme68k/pmap_bootstrap.c (revision 6e0b406276428d3151f43049552111f39c064b40)
1 /*	$NetBSD: pmap_bootstrap.c,v 1.53 2016/12/23 08:09:54 maya 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.53 2016/12/23 08:09:54 maya 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;
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
pmap_bootstrap(paddr_t nextpa,paddr_t firstpa)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 	 * Initialize the mem_clusters[] array for the crash dump
104 	 * code.  While we're at it, compute the total amount of
105 	 * physical memory in the system.
106 	 */
107 	for (i = 0; i < VM_PHYSSEG_MAX; i++) {
108 		if (RELOC(phys_seg_list[i].ps_start, paddr_t) ==
109 		    RELOC(phys_seg_list[i].ps_end, paddr_t)) {
110 			/*
111 			 * No more memory.
112 			 */
113 			break;
114 		}
115 
116 		/*
117 		 * Make sure these are properly rounded.
118 		 */
119 		RELOC(phys_seg_list[i].ps_start, paddr_t) =
120 		    m68k_round_page(RELOC(phys_seg_list[i].ps_start,
121 					  paddr_t));
122 		RELOC(phys_seg_list[i].ps_end, paddr_t) =
123 		    m68k_trunc_page(RELOC(phys_seg_list[i].ps_end,
124 					  paddr_t));
125 
126 		size = RELOC(phys_seg_list[i].ps_end, paddr_t) -
127 		    RELOC(phys_seg_list[i].ps_start, paddr_t);
128 
129 		RELOC(mem_clusters[i].start, u_quad_t) =
130 		    RELOC(phys_seg_list[i].ps_start, paddr_t);
131 		RELOC(mem_clusters[i].size, u_quad_t) = size;
132 
133 		RELOC(physmem, int) += size >> PGSHIFT;
134 
135 		RELOC(mem_cluster_cnt, int) += 1;
136 	}
137 
138 	/*
139 	 * Calculate important physical addresses:
140 	 *
141 	 *	lwp0upa		lwp0 u-area		UPAGES pages
142 	 *
143 	 *	kstpa		kernel segment table	1 page (!040)
144 	 *						N pages (040)
145 	 *
146 	 *	kptmpa		kernel PT map		1 page
147 	 *
148 	 *	kptpa		statically allocated
149 	 *			kernel PT pages		Sysptsize+ pages
150 	 *
151 	 * [ Sysptsize is the number of pages of PT, and iiomappages is the
152 	 *   number of PTEs, hence we need to round the total to a page
153 	 *   boundary with IO maps at the end. ]
154 	 *
155 	 * The KVA corresponding to any of these PAs is:
156 	 *	(PA - firstpa + KERNBASE).
157 	 */
158 	iiomappages = m68k_btop(RELOC(intiotop_phys, u_int) -
159 	    RELOC(intiobase_phys, u_int));
160 
161 	lwp0upa = nextpa;
162 	nextpa += USPACE;
163 #if defined(M68040) || defined(M68060)
164 	if (RELOC(mmutype, int) == MMU_68040)
165 		kstsize = MAXKL2SIZE / (NPTEPG/SG4_LEV2SIZE);
166 	else
167 #endif
168 		kstsize = 1;
169 	kstpa = nextpa;
170 	nextpa += kstsize * PAGE_SIZE;
171 	kptmpa = nextpa;
172 	nextpa += PAGE_SIZE;
173 	kptpa = nextpa;
174 	nptpages = RELOC(Sysptsize, int) + howmany(RELOC(physmem, int), NPTEPG) +
175 	    (iiomappages + NPTEPG - 1) / NPTEPG;
176 	nextpa += nptpages * PAGE_SIZE;
177 
178 	/*
179 	 * Clear all PTEs to zero
180 	 */
181 	for (pte = (pt_entry_t *)kstpa; pte < (pt_entry_t *)nextpa; pte++)
182 		*pte = 0;
183 
184 	/*
185 	 * Initialize segment table and kernel page table map.
186 	 *
187 	 * On 68030s and earlier MMUs the two are identical except for
188 	 * the valid bits so both are initialized with essentially the
189 	 * same values.  On the 68040, which has a mandatory 3-level
190 	 * structure, the segment table holds the level 1 table and part
191 	 * (or all) of the level 2 table and hence is considerably
192 	 * different.  Here the first level consists of 128 descriptors
193 	 * (512 bytes) each mapping 32mb of address space.  Each of these
194 	 * points to blocks of 128 second level descriptors (512 bytes)
195 	 * each mapping 256kb.  Note that there may be additional "segment
196 	 * table" pages depending on how large MAXKL2SIZE is.
197 	 *
198 	 * Portions of the last segment of KVA space (0xFFC00000 -
199 	 * 0xFFFFFFFF) are mapped for the kernel page tables.
200 	 *
201 	 * XXX cramming two levels of mapping into the single "segment"
202 	 * table on the 68040 is intended as a temporary hack to get things
203 	 * working.  The 224mb of address space that this allows will most
204 	 * likely be insufficient in the future (at least for the kernel).
205 	 */
206 #if defined(M68040) || defined(M68060)
207 	if (RELOC(mmutype, int) == MMU_68040) {
208 		int nl1desc, nl2desc;
209 
210 		/*
211 		 * First invalidate the entire "segment table" pages
212 		 * (levels 1 and 2 have the same "invalid" value).
213 		 */
214 		ste = (st_entry_t *)kstpa;
215 		este = &ste[kstsize * NPTEPG];
216 		while (ste < este)
217 			*ste++ = SG_NV;
218 		/*
219 		 * Initialize level 2 descriptors (which immediately
220 		 * follow the level 1 table).  We need:
221 		 *	NPTEPG / SG4_LEV3SIZE
222 		 * level 2 descriptors to map each of the nptpages
223 		 * pages of PTEs.  Note that we set the "used" bit
224 		 * now to save the HW the expense of doing it.
225 		 */
226 		nl2desc = nptpages * (NPTEPG / SG4_LEV3SIZE);
227 		ste = (st_entry_t *)kstpa;
228 		ste = &ste[SG4_LEV1SIZE];
229 		este = &ste[nl2desc];
230 		protoste = kptpa | SG_U | SG_RW | SG_V;
231 		while (ste < este) {
232 			*ste++ = protoste;
233 			protoste += (SG4_LEV3SIZE * sizeof(st_entry_t));
234 		}
235 		/*
236 		 * Initialize level 1 descriptors.  We need:
237 		 *	howmany(nl2desc, SG4_LEV2SIZE)
238 		 * level 1 descriptors to map the `nl2desc' level 2's.
239 		 */
240 		nl1desc = howmany(nl2desc, SG4_LEV2SIZE);
241 		ste = (st_entry_t *)kstpa;
242 		este = &ste[nl1desc];
243 		protoste = (paddr_t)&ste[SG4_LEV1SIZE] | SG_U | SG_RW | SG_V;
244 		while (ste < este) {
245 			*ste++ = protoste;
246 			protoste += (SG4_LEV2SIZE * sizeof(st_entry_t));
247 		}
248 		/*
249 		 * Initialize the final level 1 descriptor to map the next
250 		 * block of level 2 descriptors for Sysptmap.
251 		 */
252 		ste = (st_entry_t *)kstpa;
253 		ste = &ste[SG4_LEV1SIZE - 1];
254 		*ste = protoste;
255 		/*
256 		 * Now initialize the final portion of that block of
257 		 * descriptors to map Sysmap.
258 		 */
259 		i = SG4_LEV1SIZE + (nl1desc * SG4_LEV2SIZE);
260 		ste = (st_entry_t *)kstpa;
261 		ste = &ste[i + SG4_LEV2SIZE - (NPTEPG / SG4_LEV3SIZE)];
262 		este = &ste[NPTEPG / SG4_LEV3SIZE];
263 		protoste = kptmpa | SG_U | SG_RW | SG_V;
264 		while (ste < este) {
265 			*ste++ = protoste;
266 			protoste += (SG4_LEV3SIZE * sizeof(st_entry_t));
267 		}
268 		/*
269 		 * Calculate the free level 2 descriptor mask
270 		 * noting that we have used:
271 		 *	0:		level 1 table
272 		 *	1 to nl1desc:	map page tables
273 		 *	nl1desc + 1:	maps kptmpa and last-page page table
274 		 */
275 		/* mark an entry for level 1 table */
276 		stfree = ~l2tobm(0);
277 		/* mark entries for map page tables */
278 		for (i = 1; i <= nl1desc; i++)
279 			stfree &= ~l2tobm(i);
280 		/* mark an entry for kptmpa and lkptpa */
281 		stfree &= ~l2tobm(i);
282 		/* mark entries not available */
283 		for (i = MAXKL2SIZE; i < sizeof(stfree) * NBBY; i++)
284 			stfree &= ~l2tobm(i);
285 
286 		/*
287 		 * Initialize Sysptmap
288 		 */
289 		pte = (pt_entry_t *)kptmpa;
290 		epte = &pte[nptpages];
291 		protopte = kptpa | PG_RW | PG_CI | PG_U | PG_V;
292 		while (pte < epte) {
293 			*pte++ = protopte;
294 			protopte += PAGE_SIZE;
295 		}
296 		/*
297 		 * Invalidate all remaining entries.
298 		 */
299 		epte = (pt_entry_t *)kptmpa;
300 		epte = &epte[TIB_SIZE];
301 		while (pte < epte) {
302 			*pte++ = PG_NV;
303 		}
304 		/*
305 		 * Initialize the last one to point to Sysptmap.
306 		 */
307 		pte = (pt_entry_t *)kptmpa;
308 		pte = &pte[SYSMAP_VA >> SEGSHIFT];
309 		*pte = kptmpa | PG_RW | PG_CI | PG_V;
310 	} else
311 #endif /* M68040 || M68060 */
312 	{
313 		/*
314 		 * Map the page table pages in both the HW segment table
315 		 * and the software Sysptmap.
316 		 */
317 		ste = (st_entry_t *)kstpa;
318 		pte = (pt_entry_t *)kptmpa;
319 		epte = &pte[nptpages];
320 		protoste = kptpa | SG_RW | SG_V;
321 		protopte = kptpa | PG_RW | PG_CI | PG_V;
322 		while (pte < epte) {
323 			*ste++ = protoste;
324 			*pte++ = protopte;
325 			protoste += PAGE_SIZE;
326 			protopte += PAGE_SIZE;
327 		}
328 		/*
329 		 * Invalidate all remaining entries in both.
330 		 */
331 		este = (st_entry_t *)kstpa;
332 		este = &este[TIA_SIZE];
333 		while (ste < este)
334 			*ste++ = SG_NV;
335 		epte = (pt_entry_t *)kptmpa;
336 		epte = &epte[TIB_SIZE];
337 		while (pte < epte)
338 			*pte++ = PG_NV;
339 		/*
340 		 * Initialize the last one to point to Sysptmap.
341 		 */
342 		ste = (st_entry_t *)kstpa;
343 		ste = &ste[SYSMAP_VA >> SEGSHIFT];
344 		pte = (pt_entry_t *)kptmpa;
345 		pte = &pte[SYSMAP_VA >> SEGSHIFT];
346 		*ste = kptmpa | SG_RW | SG_V;
347 		*pte = kptmpa | PG_RW | PG_CI | PG_V;
348 	}
349 
350 	/*
351 	 * Initialize kernel page table.
352 	 * Start by invalidating the `nptpages' that we have allocated.
353 	 */
354 	pte = (pt_entry_t *)kptpa;
355 	epte = &pte[nptpages * NPTEPG];
356 	while (pte < epte)
357 		*pte++ = PG_NV;
358 	/*
359 	 * Validate PTEs for kernel text (RO).
360 	 */
361 	pte = (pt_entry_t *)kptpa;
362 	pte = &pte[m68k_btop(KERNBASE)];
363 	epte = &pte[m68k_btop(m68k_trunc_page(&etext))];
364 	protopte = firstpa | PG_RO | PG_U | PG_V;
365 	while (pte < epte) {
366 		*pte++ = protopte;
367 		protopte += PAGE_SIZE;
368 	}
369 	/*
370 	 * Validate PTEs for kernel data/bss, dynamic data allocated
371 	 * by us so far (kstpa - firstpa bytes), and pages for lwp0
372 	 * u-area and page table allocated below (RW).
373 	 */
374 	epte = (pt_entry_t *)kptpa;
375 	epte = &epte[m68k_btop(kstpa - firstpa)];
376 	protopte = (protopte & ~PG_PROT) | PG_RW;
377 	/*
378 	 * Enable copy-back caching of data pages
379 	 */
380 	if (RELOC(mmutype, int) == MMU_68040)
381 		protopte |= PG_CCB;
382 	while (pte < epte) {
383 		*pte++ = protopte;
384 		protopte += PAGE_SIZE;
385 	}
386 	/*
387 	 * Map the kernel segment table cache invalidated for 68040/68060.
388 	 * (for the 68040 not strictly necessary, but recommended by Motorola;
389 	 *  for the 68060 mandatory)
390 	 */
391 	epte = (pt_entry_t *)kptpa;
392 	epte = &epte[m68k_btop(nextpa - firstpa)];
393 	protopte = (protopte & ~PG_PROT) | PG_RW;
394 	if (RELOC(mmutype, int) == MMU_68040) {
395 		protopte &= ~PG_CMASK;
396 		protopte |= PG_CI;
397 	}
398 	while (pte < epte) {
399 		*pte++ = protopte;
400 		protopte += PAGE_SIZE;
401 	}
402 
403 	/*
404 	 * Finally, validate the internal IO space PTEs (RW+CI).
405 	 */
406 
407 #define	PTE2VA(pte)	m68k_ptob(pte - ((pt_entry_t *)kptpa))
408 
409 	protopte = RELOC(intiobase_phys, u_int) | PG_RW | PG_CI | PG_U | PG_V;
410 	epte = &pte[iiomappages];
411 	RELOC(intiobase, uint8_t *) = (uint8_t *)PTE2VA(pte);
412 	RELOC(intiolimit, uint8_t *) = (uint8_t *)PTE2VA(epte);
413 	while (pte < epte) {
414 		*pte++ = protopte;
415 		protopte += PAGE_SIZE;
416 	}
417 	RELOC(virtual_avail, vaddr_t) = PTE2VA(pte);
418 
419 	/*
420 	 * Calculate important exported kernel addresses and related values.
421 	 */
422 	/*
423 	 * Sysseg: base of kernel segment table
424 	 */
425 	RELOC(Sysseg, st_entry_t *) = (st_entry_t *)(kstpa - firstpa);
426 	RELOC(Sysseg_pa, paddr_t) = kstpa;
427 #if defined(M68040) || defined(M68060)
428 	if (RELOC(mmutype, int) == MMU_68040)
429 		RELOC(protostfree, u_int) = stfree;
430 #endif
431 	/*
432 	 * Sysptmap: base of kernel page table map
433 	 */
434 	RELOC(Sysptmap, pt_entry_t *) = (pt_entry_t *)(kptmpa - firstpa);
435 	/*
436 	 * Sysmap: kernel page table (as mapped through Sysptmap)
437 	 * Allocated at the end of KVA space.
438 	 */
439 	RELOC(Sysmap, pt_entry_t *) = (pt_entry_t *)SYSMAP_VA;
440 
441 	/*
442 	 * Remember the u-area address so it can be loaded in the lwp0
443 	 * via uvm_lwp_setuarea() later in pmap_bootstrap_finalize().
444 	 */
445 	RELOC(lwp0uarea, vaddr_t) = lwp0upa - firstpa;
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