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