1 /*	$NetBSD: pmap_bootstrap.c,v 1.48 2011/01/02 18:48:06 tsutsui 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.48 2011/01/02 18:48:06 tsutsui 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) + (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 	RELOC(intiolimit, uint8_t *) = (uint8_t *)PTE2VA(epte);
376 	while (pte < epte) {
377 		*pte++ = protopte;
378 		protopte += PAGE_SIZE;
379 	}
380 	RELOC(virtual_avail, vaddr_t) = PTE2VA(pte);
381 
382 	/*
383 	 * Calculate important exported kernel addresses and related values.
384 	 */
385 	/*
386 	 * Sysseg: base of kernel segment table
387 	 */
388 	RELOC(Sysseg, st_entry_t *) = (st_entry_t *)(kstpa - firstpa);
389 	RELOC(Sysseg_pa, paddr_t) = kstpa;
390 #if defined(M68040) || defined(M68060)
391 	if (RELOC(mmutype, int) == MMU_68040)
392 		RELOC(protostfree, u_int) = stfree;
393 #endif
394 	/*
395 	 * Sysptmap: base of kernel page table map
396 	 */
397 	RELOC(Sysptmap, pt_entry_t *) = (pt_entry_t *)(kptmpa - firstpa);
398 	/*
399 	 * Sysmap: kernel page table (as mapped through Sysptmap)
400 	 * Allocated at the end of KVA space.
401 	 */
402 	RELOC(Sysmap, pt_entry_t *) = (pt_entry_t *)SYSMAP_VA;
403 
404 	/*
405 	 * Remember the u-area address so it can be loaded in the lwp0
406 	 * via uvm_lwp_setuarea() later in pmap_bootstrap_finalize().
407 	 */
408 	RELOC(lwp0uarea, vaddr_t) = lwp0upa - firstpa;
409 
410 	/*
411 	 * Initialize the mem_clusters[] array for the crash dump
412 	 * code.  While we're at it, compute the total amount of
413 	 * physical memory in the system.
414 	 */
415 	for (i = 0; i < VM_PHYSSEG_MAX; i++) {
416 		if (RELOC(phys_seg_list[i].ps_start, paddr_t) ==
417 		    RELOC(phys_seg_list[i].ps_end, paddr_t)) {
418 			/*
419 			 * No more memory.
420 			 */
421 			break;
422 		}
423 
424 		/*
425 		 * Make sure these are properly rounded.
426 		 */
427 		RELOC(phys_seg_list[i].ps_start, paddr_t) =
428 		    m68k_round_page(RELOC(phys_seg_list[i].ps_start,
429 					  paddr_t));
430 		RELOC(phys_seg_list[i].ps_end, paddr_t) =
431 		    m68k_trunc_page(RELOC(phys_seg_list[i].ps_end,
432 					  paddr_t));
433 
434 		size = RELOC(phys_seg_list[i].ps_end, paddr_t) -
435 		    RELOC(phys_seg_list[i].ps_start, paddr_t);
436 
437 		RELOC(mem_clusters[i].start, u_quad_t) =
438 		    RELOC(phys_seg_list[i].ps_start, paddr_t);
439 		RELOC(mem_clusters[i].size, u_quad_t) = size;
440 
441 		RELOC(physmem, int) += size >> PGSHIFT;
442 
443 		RELOC(mem_cluster_cnt, int) += 1;
444 	}
445 
446 	/*
447 	 * Scoot the start of available on-board RAM forward to
448 	 * account for:
449 	 *
450 	 *	(1) The bootstrap programs in low memory (so
451 	 *	    that we can jump back to them without
452 	 *	    reloading).
453 	 *
454 	 *	(2) The kernel text, data, and bss.
455 	 *
456 	 *	(3) The pages we stole above for pmap data
457 	 *	    structures.
458 	 */
459 	RELOC(phys_seg_list[0].ps_start, paddr_t) = nextpa;
460 
461 	/*
462 	 * Reserve space at the end of on-board RAM for the message
463 	 * buffer.  We force it into on-board RAM because VME RAM
464 	 * gets cleared very early on in locore.s (to initialise
465 	 * parity on boards that need it). This would clobber the
466 	 * messages from a previous running NetBSD system.
467 	 */
468 	RELOC(phys_seg_list[0].ps_end, paddr_t) -=
469 	    m68k_round_page(MSGBUFSIZE);
470 	RELOC(msgbufpa, paddr_t) =
471 	    RELOC(phys_seg_list[0].ps_end, paddr_t);
472 
473 	/*
474 	 * Initialize avail_start and avail_end.
475 	 */
476 	i = RELOC(mem_cluster_cnt, int) - 1;
477 	RELOC(avail_start, paddr_t) =
478 	    RELOC(phys_seg_list[0].ps_start, paddr_t);
479 	RELOC(avail_end, paddr_t) =
480 	    RELOC(phys_seg_list[i].ps_end, paddr_t);
481 
482 	RELOC(mem_size, vsize_t) = m68k_ptob(RELOC(physmem, int));
483 
484 	RELOC(virtual_end, vaddr_t) = VM_MAX_KERNEL_ADDRESS;
485 
486 	/*
487 	 * Allocate some fixed, special purpose kernel virtual addresses
488 	 */
489 	{
490 		vaddr_t va = RELOC(virtual_avail, vaddr_t);
491 
492 		RELOC(CADDR1, void *) = (void *)va;
493 		va += PAGE_SIZE;
494 		RELOC(CADDR2, void *) = (void *)va;
495 		va += PAGE_SIZE;
496 		RELOC(vmmap, void *) = (void *)va;
497 		va += PAGE_SIZE;
498 		RELOC(msgbufaddr, void *) = (void *)va;
499 		va += m68k_round_page(MSGBUFSIZE);
500 		RELOC(virtual_avail, vaddr_t) = va;
501 	}
502 }
503