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