xref: /netbsd/sys/arch/cesfic/cesfic/pmap_bootstrap.c (revision c4a72b64)
1 /*	$NetBSD: pmap_bootstrap.c,v 1.2 2002/11/05 07:41:17 chs 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. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	@(#)pmap_bootstrap.c	8.1 (Berkeley) 6/10/93
40  */
41 
42 #include <sys/param.h>
43 #include <sys/msgbuf.h>
44 #include <sys/proc.h>
45 
46 #include <machine/frame.h>
47 #include <machine/cpu.h>
48 #include <machine/vmparam.h>
49 #include <machine/pte.h>
50 
51 #include <uvm/uvm_extern.h>
52 
53 #define RELOC(v, t)	*((t*)((u_int)&(v) + firstpa - KERNBASE))
54 
55 extern char *etext;
56 extern int Sysptsize;
57 extern char *proc0paddr;
58 extern st_entry_t *Sysseg;
59 extern pt_entry_t *Sysptmap, *Sysmap;
60 
61 extern int physmem;
62 extern vm_offset_t avail_start, avail_end, virtual_avail, virtual_end;
63 extern int protection_codes[];
64 
65 void	pmap_bootstrap __P((vm_offset_t, vm_offset_t));
66 
67 /*
68  * Special purpose kernel virtual addresses, used for mapping
69  * physical pages for a variety of temporary or permanent purposes:
70  *
71  *	CADDR1, CADDR2:	pmap zero/copy operations
72  *	vmmap:		/dev/mem, crash dumps, parity error checking
73  *	msgbufp:	kernel message buffer
74  */
75 caddr_t		CADDR1, CADDR2, vmmap;
76 extern caddr_t	msgbufaddr;
77 
78 /*
79  * Bootstrap the VM system.
80  *
81  * Called with MMU off so we must relocate all global references by `firstpa'
82  * (don't call any functions here!)  `nextpa' is the first available physical
83  * memory address.  Returns an updated first PA reflecting the memory we
84  * have allocated.  MMU is still off when we return.
85  *
86  * XXX assumes sizeof(u_int) == sizeof(pt_entry_t)
87  * XXX a PIC compiler would make this much easier.
88  */
89 void
90 pmap_bootstrap(nextpa, firstpa)
91 	vm_offset_t nextpa;
92 	vm_offset_t firstpa;
93 {
94 	vm_offset_t kstpa, kptpa, kptmpa, lkptpa, p0upa;
95 	u_int nptpages, kstsize;
96 	st_entry_t protoste, *ste;
97 	pt_entry_t protopte, *pte, *epte;
98 
99 	/*
100 	 * Calculate important physical addresses:
101 	 *
102 	 *	kstpa		kernel segment table	1 page (!040)
103 	 *						N pages (040)
104 	 *
105 	 *	kptpa		statically allocated
106 	 *			kernel PT pages		Sysptsize+ pages
107 	 *
108 	 *	iiopa		internal IO space
109 	 *			PT pages		IIOMAPSIZE pages
110 	 *
111 	 *	eiopa		external IO space
112 	 *			PT pages		EIOMAPSIZE pages
113 	 *
114 	 * [ Sysptsize is the number of pages of PT, IIOMAPSIZE and
115 	 *   EIOMAPSIZE are the number of PTEs, hence we need to round
116 	 *   the total to a page boundary with IO maps at the end. ]
117 	 *
118 	 *	kptmpa		kernel PT map		1 page
119 	 *
120 	 *	lkptpa		last kernel PT page	1 page
121 	 *
122 	 *	p0upa		proc 0 u-area		UPAGES pages
123 	 *
124 	 * The KVA corresponding to any of these PAs is:
125 	 *	(PA - firstpa + KERNBASE).
126 	 */
127 	if (RELOC(mmutype, int) == MMU_68040)
128 		kstsize = MAXKL2SIZE / (NPTEPG/SG4_LEV2SIZE);
129 	else
130 		kstsize = 1;
131 	kstpa = nextpa;
132 	nextpa += kstsize * NBPG;
133 	kptpa = nextpa;
134 	nptpages = RELOC(Sysptsize, int);
135 	nextpa += nptpages * NBPG;
136 	kptmpa = nextpa;
137 	nextpa += NBPG;
138 	lkptpa = nextpa;
139 	nextpa += NBPG;
140 	p0upa = nextpa;
141 	nextpa += USPACE;
142 
143 	/*
144 	 * Initialize segment table and kernel page table map.
145 	 *
146 	 * On 68030s and earlier MMUs the two are identical except for
147 	 * the valid bits so both are initialized with essentially the
148 	 * same values.  On the 68040, which has a mandatory 3-level
149 	 * structure, the segment table holds the level 1 table and part
150 	 * (or all) of the level 2 table and hence is considerably
151 	 * different.  Here the first level consists of 128 descriptors
152 	 * (512 bytes) each mapping 32mb of address space.  Each of these
153 	 * points to blocks of 128 second level descriptors (512 bytes)
154 	 * each mapping 256kb.  Note that there may be additional "segment
155 	 * table" pages depending on how large MAXKL2SIZE is.
156 	 *
157 	 * Portions of the last segment of KVA space (0xFFF00000 -
158 	 * 0xFFFFFFFF) are mapped for a couple of purposes.  0xFFF00000
159 	 * for UPAGES is used for mapping the current process u-area
160 	 * (u + kernel stack).  The very last page (0xFFFFF000) is mapped
161 	 * to the last physical page of RAM to give us a region in which
162 	 * PA == VA.  We use the first part of this page for enabling
163 	 * and disabling mapping.  The last part of this page also contains
164 	 * info left by the boot ROM.
165 	 *
166 	 * XXX cramming two levels of mapping into the single "segment"
167 	 * table on the 68040 is intended as a temporary hack to get things
168 	 * working.  The 224mb of address space that this allows will most
169 	 * likely be insufficient in the future (at least for the kernel).
170 	 */
171 	if (RELOC(mmutype, int) == MMU_68040) {
172 		int num;
173 
174 		/*
175 		 * First invalidate the entire "segment table" pages
176 		 * (levels 1 and 2 have the same "invalid" value).
177 		 */
178 		pte = (u_int *)kstpa;
179 		epte = &pte[kstsize * NPTEPG];
180 		while (pte < epte)
181 			*pte++ = SG_NV;
182 
183 		/*
184 		 * Initialize level 2 descriptors (which immediately
185 		 * follow the level 1 table).  We need:
186 		 *	NPTEPG / SG4_LEV3SIZE
187 		 * level 2 descriptors to map each of the nptpages+1
188 		 * pages of PTEs.  Note that we set the "used" bit
189 		 * now to save the HW the expense of doing it.
190 		 */
191 		num = (nptpages + 1) * (NPTEPG / SG4_LEV3SIZE);
192 		pte = &((u_int *)kstpa)[SG4_LEV1SIZE];
193 		epte = &pte[num];
194 		protoste = kptpa | SG_U | SG_RW | SG_V;
195 		while (pte < epte) {
196 			*pte++ = protoste;
197 			protoste += (SG4_LEV3SIZE * sizeof(st_entry_t));
198 		}
199 
200 		/*
201 		 * Initialize level 1 descriptors.  We need:
202 		 *	roundup(num, SG4_LEV2SIZE) / SG4_LEV2SIZE
203 		 * level 1 descriptors to map the `num' level 2's.
204 		 */
205 		pte = (u_int *)kstpa;
206 		epte = &pte[roundup(num, SG4_LEV2SIZE) / SG4_LEV2SIZE];
207 		protoste = (u_int)&pte[SG4_LEV1SIZE] | SG_U | SG_RW | SG_V;
208 		while (pte < epte) {
209 			*pte++ = protoste;
210 			protoste += (SG4_LEV2SIZE * sizeof(st_entry_t));
211 		}
212 
213 		/*
214 		 * Initialize the final level 1 descriptor to map the last
215 		 * block of level 2 descriptors.
216 		 */
217 		ste = &((u_int *)kstpa)[SG4_LEV1SIZE-1];
218 		pte = &((u_int *)kstpa)[kstsize*NPTEPG - SG4_LEV2SIZE];
219 		*ste = (u_int)pte | SG_U | SG_RW | SG_V;
220 		/*
221 		 * Now initialize the final portion of that block of
222 		 * descriptors to map the "last PT page".
223 		 */
224 		pte = &((u_int *)kstpa)[kstsize*NPTEPG - NPTEPG/SG4_LEV3SIZE];
225 		epte = &pte[NPTEPG/SG4_LEV3SIZE];
226 		protoste = lkptpa | SG_U | SG_RW | SG_V;
227 		while (pte < epte) {
228 			*pte++ = protoste;
229 			protoste += (SG4_LEV3SIZE * sizeof(st_entry_t));
230 		}
231 
232 		/*
233 		 * Initialize Sysptmap
234 		 */
235 		pte = (u_int *)kptmpa;
236 		epte = &pte[nptpages+1];
237 		protopte = kptpa | PG_RW | PG_CI | PG_V;
238 		while (pte < epte) {
239 			*pte++ = protopte;
240 			protopte += NBPG;
241 		}
242 
243 		/*
244 		 * Invalidate all but the last remaining entry.
245 		 */
246 		epte = &((u_int *)kptmpa)[NPTEPG-1];
247 		while (pte < epte) {
248 			*pte++ = PG_NV;
249 		}
250 		/*
251 		 * Initialize the last to point to the page
252 		 * table page allocated earlier.
253 		 */
254 		*pte = lkptpa | PG_RW | PG_CI | PG_V;
255 	} else {
256 		/*
257 		 * Map the page table pages in both the HW segment table
258 		 * and the software Sysptmap.  Note that Sysptmap is also
259 		 * considered a PT page hence the +1.
260 		 */
261 		ste = (u_int *)kstpa;
262 		pte = (u_int *)kptmpa;
263 		epte = &pte[nptpages+1];
264 		protoste = kptpa | SG_RW | SG_V;
265 		protopte = kptpa | PG_RW | PG_CI | PG_V;
266 		while (pte < epte) {
267 			*ste++ = protoste;
268 			*pte++ = protopte;
269 			protoste += NBPG;
270 			protopte += NBPG;
271 		}
272 		/*
273 		 * Invalidate all but the last remaining entries in both.
274 		 */
275 		epte = &((u_int *)kptmpa)[NPTEPG-1];
276 		while (pte < epte) {
277 			*ste++ = SG_NV;
278 			*pte++ = PG_NV;
279 		}
280 		/*
281 		 * Initialize the last to point to point to the page
282 		 * table page allocated earlier.
283 		 */
284 		*ste = lkptpa | SG_RW | SG_V;
285 		*pte = lkptpa | PG_RW | PG_CI | PG_V;
286 	}
287 	/*
288 	 * Invalidate all but the final entry in the last kernel PT page
289 	 * (u-area PTEs will be validated later).  The final entry maps
290 	 * the last page of physical memory.
291 	 */
292 	pte = (u_int *)lkptpa;
293 	epte = &pte[NPTEPG];
294 	while (pte < epte)
295 		*pte++ = PG_NV;
296 
297 	/*
298 	 * Initialize kernel page table.
299 	 * Start by invalidating the `nptpages' that we have allocated.
300 	 */
301 	pte = (u_int *)kptpa;
302 	epte = &pte[nptpages * NPTEPG];
303 	while (pte < epte)
304 		*pte++ = PG_NV;
305 
306 	/*
307 	 * Validate PTEs for kernel text (RO).
308 	 */
309 	pte = &((u_int *)kptpa)[m68k_btop(KERNBASE)];
310 	epte = &((u_int *)kptpa)[m68k_btop(m68k_trunc_page(&etext))];
311 	protopte = firstpa | PG_RO | PG_V;
312 	while (pte < epte) {
313 		*pte++ = protopte;
314 		protopte += NBPG;
315 	}
316 	/*
317 	 * Validate PTEs for kernel data/bss, dynamic data allocated
318 	 * by us so far (nextpa - firstpa bytes), and pages for proc0
319 	 * u-area and page table allocated below (RW).
320 	 */
321 	epte = &((u_int *)kptpa)[m68k_btop(KERNBASE + nextpa - firstpa)];
322 	protopte = (protopte & ~PG_PROT) | PG_RW;
323 	/*
324 	 * Enable copy-back caching of data pages
325 	 */
326 	if (RELOC(mmutype, int) == MMU_68040)
327 		protopte |= PG_CCB;
328 
329 	while (pte < epte) {
330 		*pte++ = protopte;
331 		protopte += NBPG;
332 	}
333 
334 	/*
335 	 * Calculate important exported kernel virtual addresses
336 	 */
337 	/*
338 	 * Sysseg: base of kernel segment table
339 	 */
340 	RELOC(Sysseg, st_entry_t *) =
341 		(st_entry_t *)(kstpa - firstpa + KERNBASE);
342 	/*
343 	 * Sysptmap: base of kernel page table map
344 	 */
345 	RELOC(Sysptmap, pt_entry_t *) =
346 		(pt_entry_t *)(kptmpa - firstpa + KERNBASE);
347 	/*
348 	 * Sysmap: kernel page table (as mapped through Sysptmap)
349 	 * Immediately follows `nptpages' of static kernel page table.
350 	 */
351 	RELOC(Sysmap, pt_entry_t *) =
352 		(pt_entry_t *)m68k_ptob(nptpages * NPTEPG);
353 
354 	/*
355 	 * Setup u-area for process 0.
356 	 */
357 	/*
358 	 * Zero the u-area.
359 	 * NOTE: `pte' and `epte' aren't PTEs here.
360 	 */
361 	pte = (u_int *)p0upa;
362 	epte = (u_int *)(p0upa + USPACE);
363 	while (pte < epte)
364 		*pte++ = 0;
365 	/*
366 	 * Remember the u-area address so it can be loaded in the
367 	 * proc struct p_addr field later.
368 	 */
369 	RELOC(proc0paddr, char *) = (char *)(p0upa - firstpa + KERNBASE);
370 
371 	/*
372 	 * VM data structures are now initialized, set up data for
373 	 * the pmap module.
374 	 *
375 	 * Note about avail_end: msgbuf is initialized just after
376 	 * avail_end in machdep.c.  Since the last page is used
377 	 * for rebooting the system (code is copied there and
378 	 * excution continues from copied code before the MMU
379 	 * is disabled), the msgbuf will get trounced between
380 	 * reboots if it's placed in the last physical page.
381 	 * To work around this, we move avail_end back one more
382 	 * page so the msgbuf can be preserved.
383 	 */
384 	RELOC(avail_start, vm_offset_t) = nextpa;
385 	RELOC(avail_end, vm_offset_t) = firstpa
386 	  + m68k_ptob(RELOC(physmem, int))
387 	  - m68k_round_page(MSGBUFSIZE)
388 	  - NBPG; /* if that start of last page??? */
389 	RELOC(virtual_avail, vm_offset_t) =
390 		KERNBASE + (nextpa - firstpa);
391 	RELOC(virtual_end, vm_offset_t) = VM_MAX_KERNEL_ADDRESS;
392 
393 	/*
394 	 * Initialize protection array.
395 	 * XXX don't use a switch statement, it might produce an
396 	 * absolute "jmp" table.
397 	 */
398 	{
399 		int *kp;
400 
401 		kp = &RELOC(protection_codes, int);
402 		kp[VM_PROT_NONE|VM_PROT_NONE|VM_PROT_NONE] = 0;
403 		kp[VM_PROT_READ|VM_PROT_NONE|VM_PROT_NONE] = PG_RO;
404 		kp[VM_PROT_READ|VM_PROT_NONE|VM_PROT_EXECUTE] = PG_RO;
405 		kp[VM_PROT_NONE|VM_PROT_NONE|VM_PROT_EXECUTE] = PG_RO;
406 		kp[VM_PROT_NONE|VM_PROT_WRITE|VM_PROT_NONE] = PG_RW;
407 		kp[VM_PROT_NONE|VM_PROT_WRITE|VM_PROT_EXECUTE] = PG_RW;
408 		kp[VM_PROT_READ|VM_PROT_WRITE|VM_PROT_NONE] = PG_RW;
409 		kp[VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE] = PG_RW;
410 	}
411 
412 	/*
413 	 * Kernel page/segment table allocated in locore,
414 	 * just initialize pointers.
415 	 */
416 	{
417 		struct pmap *kpm = &RELOC(kernel_pmap_store, struct pmap);
418 
419 		kpm->pm_stab = RELOC(Sysseg, st_entry_t *);
420 		kpm->pm_ptab = RELOC(Sysmap, pt_entry_t *);
421 		simple_lock_init(&kpm->pm_lock);
422 		kpm->pm_count = 1;
423 		kpm->pm_stpa = (st_entry_t *)kstpa;
424 		/*
425 		 * For the 040 we also initialize the free level 2
426 		 * descriptor mask noting that we have used:
427 		 *	0:		level 1 table
428 		 *	1 to `num':	map page tables
429 		 *	MAXKL2SIZE-1:	maps last-page page table
430 		 */
431 		if (RELOC(mmutype, int) == MMU_68040) {
432 			int num;
433 
434 			kpm->pm_stfree = ~l2tobm(0);
435 			num = roundup((nptpages + 1) * (NPTEPG / SG4_LEV3SIZE),
436 				      SG4_LEV2SIZE) / SG4_LEV2SIZE;
437 			while (num)
438 				kpm->pm_stfree &= ~l2tobm(num--);
439 			kpm->pm_stfree &= ~l2tobm(MAXKL2SIZE-1);
440 			for (num = MAXKL2SIZE;
441 			     num < sizeof(kpm->pm_stfree)*NBBY;
442 			     num++)
443 				kpm->pm_stfree &= ~l2tobm(num);
444 		}
445 	}
446 
447 	/*
448 	 * Allocate some fixed, special purpose kernel virtual addresses
449 	 */
450 	{
451 		vm_offset_t va = RELOC(virtual_avail, vm_offset_t);
452 
453 		RELOC(CADDR1, caddr_t) = (caddr_t)va;
454 		va += NBPG;
455 		RELOC(CADDR2, caddr_t) = (caddr_t)va;
456 		va += NBPG;
457 		RELOC(vmmap, caddr_t) = (caddr_t)va;
458 		va += NBPG;
459 		RELOC(msgbufaddr, caddr_t) = (caddr_t)va;
460 		va += m68k_round_page(MSGBUFSIZE);
461 		RELOC(virtual_avail, vm_offset_t) = va;
462 	}
463 }
464 
465 void
466 pmap_init_md(void)
467 {
468 	/* Nothing here. */
469 }
470