xref: /netbsd-src/sys/arch/mac68k/mac68k/pmap_bootstrap.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: pmap_bootstrap.c,v 1.89 2010/06/06 04:50:07 mrg 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.89 2010/06/06 04:50:07 mrg Exp $");
40 
41 #include "opt_ddb.h"
42 #include "opt_kgdb.h"
43 #include "opt_m68k_arch.h"
44 
45 #include "zsc.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/reboot.h>
50 
51 #include <uvm/uvm_extern.h>
52 
53 #include <machine/pte.h>
54 #include <machine/vmparam.h>
55 #include <machine/cpu.h>
56 #include <machine/pmap.h>
57 #include <machine/autoconf.h>
58 #include <machine/video.h>
59 
60 #include <mac68k/mac68k/macrom.h>
61 
62 #define PA2VA(v, t)	(t)((u_int)(v) - firstpa)
63 
64 extern char *etext;
65 extern char *extiobase;
66 
67 extern paddr_t avail_start;
68 extern paddr_t avail_end;
69 
70 #if NZSC > 0
71 extern	int	zsinited;
72 #endif
73 
74 /*
75  * These are used to map the RAM:
76  */
77 int	numranges;	/* = 0 == don't use the ranges */
78 u_long	low[8];
79 u_long	high[8];
80 u_long	maxaddr;	/* PA of the last physical page */
81 int	vidlen;
82 #define VIDMAPSIZE	btoc(vidlen)
83 static vaddr_t	newvideoaddr;
84 
85 extern void *	ROMBase;
86 
87 /*
88  * Special purpose kernel virtual addresses, used for mapping
89  * physical pages for a variety of temporary or permanent purposes:
90  *
91  *	CADDR1, CADDR2:	pmap zero/copy operations
92  *	vmmap:		/dev/mem, crash dumps, parity error checking
93  *	msgbufaddr:	kernel message buffer
94  */
95 void *CADDR1, *CADDR2;
96 char *vmmap;
97 void *msgbufaddr;
98 
99 void	pmap_bootstrap(paddr_t, paddr_t);
100 void	bootstrap_mac68k(int);
101 
102 /*
103  * Bootstrap the VM system.
104  *
105  * This is called with the MMU either on or off.  If it's on, we assume
106  * that it's mapped with the same PA <=> LA mapping that we eventually
107  * want.  The page sizes and the protections will be wrong, anyway.
108  *
109  * nextpa is the first address following the loaded kernel.  On a IIsi
110  * on 12 May 1996, that was 0xf9000 beyond firstpa.
111  */
112 void
113 pmap_bootstrap(paddr_t nextpa, paddr_t firstpa)
114 {
115 	paddr_t kstpa, kptpa, kptmpa, lwp0upa;
116 	u_int nptpages, kstsize;
117 	paddr_t avail_next;
118 	int avail_remaining;
119 	int avail_range;
120 	int i;
121 	st_entry_t protoste, *ste, *este;
122 	pt_entry_t protopte, *pte, *epte;
123 	u_int stfree = 0;	/* XXX: gcc -Wuninitialized */
124 	extern char start[];
125 
126 	vidlen = m68k_round_page(mac68k_video.mv_height *
127 	    mac68k_video.mv_stride + m68k_page_offset(mac68k_video.mv_phys));
128 
129 	/*
130 	 * Calculate important physical addresses:
131 	 *
132 	 *	lwp0upa		lwp 0 u-area		UPAGES pages
133 	 *
134 	 *	kstpa		kernel segment table	1 page (!040)
135 	 *						N pages (040)
136 	 *
137 	 *	kptmpa		kernel PT map		1 page
138 	 *
139 	 *	kptpa		statically allocated
140 	 *			kernel PT pages		Sysptsize+ pages
141 	 *
142 	 * [ Sysptsize is the number of pages of PT, IIOMAPSIZE and
143 	 *   NBMAPSIZE are the number of PTEs, hence we need to round
144 	 *   the total to a page boundary with IO maps at the end. ]
145 	 *
146 	 */
147 	lwp0upa = nextpa;
148 	nextpa += USPACE;
149 	if (mmutype == MMU_68040)
150 		kstsize = MAXKL2SIZE / (NPTEPG/SG4_LEV2SIZE);
151 	else
152 		kstsize = 1;
153 	kstpa = nextpa;
154 	nextpa += kstsize * PAGE_SIZE;
155 	kptmpa = nextpa;
156 	nextpa += PAGE_SIZE;
157 	kptpa = nextpa;
158 	nptpages = Sysptsize +
159 		(IIOMAPSIZE + ROMMAPSIZE + VIDMAPSIZE + NPTEPG - 1) / NPTEPG;
160 	nextpa += nptpages * PAGE_SIZE;
161 
162 	for (i = 0; i < numranges; i++)
163 		if (low[i] <= firstpa && firstpa < high[i])
164 			break;
165 	if (i >= numranges || nextpa > high[i]) {
166 		if (mac68k_machine.do_graybars) {
167 			printf("Failure in NetBSD boot; ");
168 			if (i < numranges)
169 				printf("nextpa=0x%lx, high[%d]=0x%lx.\n",
170 				    nextpa, i, high[i]);
171 			else
172 				printf("can't find kernel RAM segment.\n");
173 			printf("You're hosed!  Try booting with 32-bit ");
174 			printf("addressing enabled in the memory control ");
175 			printf("panel.\n");
176 			printf("Older machines may need Mode32 to get that ");
177 			printf("option.\n");
178 		}
179 		panic("Cannot work with the current memory mappings.");
180 	}
181 
182 	/*
183 	 * Initialize segment table and kernel page table map.
184 	 *
185 	 * On 68030s and earlier MMUs the two are identical except for
186 	 * the valid bits so both are initialized with essentially the
187 	 * same values.  On the 68040, which has a mandatory 3-level
188 	 * structure, the segment table holds the level 1 table and part
189 	 * (or all) of the level 2 table and hence is considerably
190 	 * different.  Here the first level consists of 128 descriptors
191 	 * (512 bytes) each mapping 32mb of address space.  Each of these
192 	 * points to blocks of 128 second level descriptors (512 bytes)
193 	 * each mapping 256kb.  Note that there may be additional "segment
194 	 * table" pages depending on how large MAXKL2SIZE is.
195 	 *
196 	 * XXX cramming two levels of mapping into the single "segment"
197 	 * table on the 68040 is intended as a temporary hack to get things
198 	 * working.  The 224mb of address space that this allows will most
199 	 * likely be insufficient in the future (at least for the kernel).
200 	 */
201 	if (mmutype == MMU_68040) {
202 		int nl1desc, nl2desc;
203 
204 		/*
205 		 * First invalidate the entire "segment table" pages
206 		 * (levels 1 and 2 have the same "invalid" value).
207 		 */
208 		ste = PA2VA(kstpa, st_entry_t *);
209 		este = &ste[kstsize * NPTEPG];
210 		while (ste < este)
211 			*ste++ = SG_NV;
212 		/*
213 		 * Initialize level 2 descriptors (which immediately
214 		 * follow the level 1 table).  We need:
215 		 *	NPTEPG / SG4_LEV3SIZE
216 		 * level 2 descriptors to map each of the nptpages
217 		 * pages of PTEs.  Note that we set the "used" bit
218 		 * now to save the HW the expense of doing it.
219 		 */
220 		nl2desc = nptpages * (NPTEPG / SG4_LEV3SIZE);
221 		ste = PA2VA(kstpa, st_entry_t *);
222 		ste = &ste[SG4_LEV1SIZE];
223 		este = &ste[nl2desc];
224 		protoste = kptpa | SG_U | SG_RW | SG_V;
225 		while (ste < este) {
226 			*ste++ = protoste;
227 			protoste += (SG4_LEV3SIZE * sizeof(st_entry_t));
228 		}
229 		/*
230 		 * Initialize level 1 descriptors.  We need:
231 		 *	howmany(nl2desc, SG4_LEV2SIZE)
232 		 * level 1 descriptors to map the `nl2desc' level 2's.
233 		 */
234 		nl1desc = howmany(nl2desc, SG4_LEV2SIZE);
235 		ste = PA2VA(kstpa, u_int *);
236 		este = &ste[nl1desc];
237 		protoste = (paddr_t)&ste[SG4_LEV1SIZE] | SG_U | SG_RW | SG_V;
238 		while (ste < este) {
239 			*ste++ = protoste;
240 			protoste += (SG4_LEV2SIZE * sizeof(st_entry_t));
241 		}
242 		/*
243 		 * Initialize the final level 1 descriptor to map the next
244 		 * block of level 2 descriptors for Sysptmap.
245 		 */
246 		ste = PA2VA(kstpa, st_entry_t *);
247 		ste = &ste[SG4_LEV1SIZE - 1];
248 		*ste = protoste;
249 		/*
250 		 * Now initialize the final portion of that block of
251 		 * descriptors to map Sysmap.
252 		 */
253 		i = SG4_LEV1SIZE + (nl1desc * SG4_LEV2SIZE);
254 		ste = PA2VA(kstpa, st_entry_t *);
255 		ste = &ste[i + SG4_LEV2SIZE - (NPTEPG / SG4_LEV3SIZE)];
256 		este = &ste[NPTEPG / SG4_LEV3SIZE];
257 		protoste = kptmpa | SG_U | SG_RW | SG_V;
258 		while (ste < este) {
259 			*ste++ = protoste;
260 			protoste += (SG4_LEV3SIZE * sizeof(st_entry_t));
261 		}
262 		/*
263 		 * Calculate the free level 2 descriptor mask
264 		 * noting that we have used:
265 		 *	0:		level 1 table
266 		 *	1 to nl1desc:	map page tables
267 		 *	nl1desc + 1:	maps kptmpa and last-page page table
268 		 */
269 		/* mark an entry for level 1 table */
270 		stfree = ~l2tobm(0);
271 		/* mark entries for map page tables */
272 		for (i = 1; i <= nl1desc; i++)
273 			stfree &= ~l2tobm(i);
274 		/* mark an entry for kptmpa and lkptpa */
275 		stfree &= ~l2tobm(i);
276 		/* mark entries not available */
277 		for (i = MAXKL2SIZE; i < sizeof(stfree) * NBBY; i++)
278 			stfree &= ~l2tobm(i);
279 
280 		/*
281 		 * Initialize Sysptmap
282 		 */
283 		pte = PA2VA(kptmpa, pt_entry_t *);
284 		epte = &pte[nptpages];
285 		protopte = kptpa | PG_RW | PG_CI | PG_V;
286 		while (pte < epte) {
287 			*pte++ = protopte;
288 			protopte += PAGE_SIZE;
289 		}
290 		/*
291 		 * Invalidate all remaining entries.
292 		 */
293 		epte = PA2VA(kptmpa, pt_entry_t *);
294 		epte = &epte[TIB_SIZE];
295 		while (pte < epte) {
296 			*pte++ = PG_NV;
297 		}
298 		/*
299 		 * Initialize the last one to point to Sysptmap.
300 		 */
301 		pte = PA2VA(kptmpa, pt_entry_t *);
302 		pte = &pte[SYSMAP_VA >> SEGSHIFT];
303 		*pte = kptmpa | PG_RW | PG_CI | PG_V;
304 	} else {
305 		/*
306 		 * Map the page table pages in both the HW segment table
307 		 * and the software Sysptmap.
308 		 */
309 		ste = PA2VA(kstpa, st_entry_t *);
310 		pte = PA2VA(kptmpa, pt_entry_t *);
311 		epte = &pte[nptpages];
312 		protoste = kptpa | SG_RW | SG_V;
313 		protopte = kptpa | PG_RW | PG_CI | PG_V;
314 		while (pte < epte) {
315 			*ste++ = protoste;
316 			*pte++ = protopte;
317 			protoste += PAGE_SIZE;
318 			protopte += PAGE_SIZE;
319 		}
320 		/*
321 		 * Invalidate all remaining entries in both.
322 		 */
323 		este = PA2VA(kstpa, st_entry_t *);
324 		este = &este[TIA_SIZE];
325 		while (ste < este)
326 			*ste++ = SG_NV;
327 		epte = PA2VA(kptmpa, pt_entry_t *);
328 		epte = &epte[TIB_SIZE];
329 		while (pte < epte)
330 			*pte++ = PG_NV;
331 		/*
332 		 * Initialize the last one to point to Sysptmap.
333 		 */
334 		ste = PA2VA(kstpa, st_entry_t *);
335 		ste = &ste[SYSMAP_VA >> SEGSHIFT];
336 		*ste = kptmpa | SG_RW | SG_V;
337 		pte = PA2VA(kptmpa, pt_entry_t *);
338 		pte = &pte[SYSMAP_VA >> SEGSHIFT];
339 		*pte = kptmpa | PG_RW | PG_CI | PG_V;
340 	}
341 
342 	/*
343 	 * Initialize kernel page table.
344 	 * Start by invalidating the `nptpages' that we have allocated.
345 	 */
346 	pte = PA2VA(kptpa, pt_entry_t *);
347 	epte = &pte[nptpages * NPTEPG];
348 	while (pte < epte)
349 		*pte++ = PG_NV;
350 
351 	/*
352 	 * Validate PTEs for kernel text (RO).
353 	 * Pages up to "start" must be writable for the ROM.
354 	 */
355 	pte = PA2VA(kptpa, pt_entry_t *);
356 	pte = &pte[m68k_btop(KERNBASE)];
357 	/* XXX why KERNBASE relative? */
358 	epte = &pte[m68k_btop(m68k_round_page(start))];
359 	protopte = firstpa | PG_RW | PG_V;
360 	while (pte < epte) {
361 		*pte++ = protopte;
362 		protopte += PAGE_SIZE;
363 	}
364 	/* XXX why KERNBASE relative? */
365 	epte = &pte[m68k_btop(m68k_trunc_page(&etext))];
366 	protopte = (protopte & ~PG_PROT) | PG_RO;
367 	while (pte < epte) {
368 		*pte++ = protopte;
369 		protopte += PAGE_SIZE;
370 	}
371 	/*
372 	 * Validate PTEs for kernel data/bss, dynamic data allocated
373 	 * by us so far (nextpa - firstpa bytes), and pages for lwp0
374 	 * u-area and page table allocated below (RW).
375 	 */
376 	epte = PA2VA(kptpa, pt_entry_t *);
377 	epte = &epte[m68k_btop(nextpa - firstpa)];
378 	protopte = (protopte & ~PG_PROT) | PG_RW;
379 	/*
380 	 * Enable copy-back caching of data pages
381 	 */
382 	if (mmutype == MMU_68040)
383 		protopte |= PG_CCB;
384 	while (pte < epte) {
385 		*pte++ = protopte;
386 		protopte += PAGE_SIZE;
387 	}
388 
389 #define	PTE2VA(pte)	m68k_ptob(pte - PA2VA(kptpa, pt_entry_t *))
390 
391 	protopte = IOBase | PG_RW | PG_CI | PG_V;
392 	IOBase = PTE2VA(pte);
393 	epte = &pte[IIOMAPSIZE];
394 	while (pte < epte) {
395 		*pte++ = protopte;
396 		protopte += PAGE_SIZE;
397 	}
398 
399 	protopte = (pt_entry_t)ROMBase | PG_RO | PG_V;
400 	ROMBase = (void *)PTE2VA(pte);
401 	epte = &pte[ROMMAPSIZE];
402 	while (pte < epte) {
403 		*pte++ = protopte;
404 		protopte += PAGE_SIZE;
405 	}
406 
407 	if (vidlen) {
408 		protopte = m68k_trunc_page(mac68k_video.mv_phys) |
409 		    PG_RW | PG_V | PG_CI;
410 		newvideoaddr = PTE2VA(pte)
411 		    + m68k_page_offset(mac68k_video.mv_phys);
412 		epte = &pte[VIDMAPSIZE];
413 		while (pte < epte) {
414 			*pte++ = protopte;
415 			protopte += PAGE_SIZE;
416 		}
417 	}
418 	virtual_avail = PTE2VA(pte);
419 
420 	/*
421 	 * Calculate important exported kernel addresses and related values.
422 	 */
423 	/*
424 	 * Sysseg: base of kernel segment table
425 	 */
426 	Sysseg = PA2VA(kstpa, st_entry_t *);
427 	Sysseg_pa = PA2VA(kstpa, paddr_t);
428 #if defined(M68040)
429 	if (mmutype == MMU_68040)
430 		protostfree = stfree;
431 #endif
432 	/*
433 	 * Sysptmap: base of kernel page table map
434 	 */
435 	Sysptmap = PA2VA(kptmpa, pt_entry_t *);
436 	/*
437 	 * Sysmap: kernel page table (as mapped through Sysptmap)
438 	 * Allocated at the end of KVA space.
439 	 */
440 	Sysmap = (pt_entry_t *)SYSMAP_VA;
441 
442 	/*
443 	 * Remember the u-area address so it can be loaded in the lwp0
444 	 * via uvm_lwp_setuarea() later in pmap_bootstrap_finalize().
445 	 */
446 	lwp0uarea = PA2VA(lwp0upa, vaddr_t);
447 
448 	/*
449 	 * VM data structures are now initialized, set up data for
450 	 * the pmap module.
451 	 *
452 	 * Note about avail_end: msgbuf is initialized just after
453 	 * avail_end in machdep.c.  Since the last page is used
454 	 * for rebooting the system (code is copied there and
455 	 * excution continues from copied code before the MMU
456 	 * is disabled), the msgbuf will get trounced between
457 	 * reboots if it's placed in the last physical page.
458 	 * To work around this, we move avail_end back one more
459 	 * page so the msgbuf can be preserved.
460 	 */
461 	avail_next = avail_start = m68k_round_page(nextpa);
462 	avail_remaining = 0;
463 	avail_range = -1;
464 	for (i = 0; i < numranges; i++) {
465 		if (low[i] <= avail_next && avail_next < high[i]) {
466 			avail_range = i;
467 			avail_remaining = high[i] - avail_next;
468 		} else if (avail_range != -1) {
469 			avail_remaining += (high[i] - low[i]);
470 		}
471 	}
472 	physmem = m68k_btop(avail_remaining + nextpa - firstpa);
473 
474 	maxaddr = high[numranges - 1] - m68k_ptob(1);
475 	high[numranges - 1] -= (m68k_round_page(MSGBUFSIZE) + m68k_ptob(1));
476 	avail_end = high[numranges - 1];
477 	mem_size = m68k_ptob(physmem);
478 	virtual_end = VM_MAX_KERNEL_ADDRESS;
479 
480 	/*
481 	 * Allocate some fixed, special purpose kernel virtual addresses
482 	 */
483 	{
484 		vaddr_t va = virtual_avail;
485 
486 		CADDR1 = (void *)va;
487 		va += PAGE_SIZE;
488 		CADDR2 = (void *)va;
489 		va += PAGE_SIZE;
490 		vmmap = (void *)va;
491 		va += PAGE_SIZE;
492 		msgbufaddr = (void *)va;
493 		va += m68k_round_page(MSGBUFSIZE);
494 		virtual_avail = va;
495 	}
496 }
497 
498 void
499 bootstrap_mac68k(int tc)
500 {
501 #if NZSC > 0
502 	extern void zs_init(void);
503 #endif
504 	extern int *esym;
505 	paddr_t nextpa;
506 	void *oldROMBase;
507 
508 	if (mac68k_machine.do_graybars)
509 		printf("Bootstrapping NetBSD/mac68k.\n");
510 
511 	oldROMBase = ROMBase;
512 	mac68k_video.mv_phys = mac68k_video.mv_kvaddr;
513 
514 	if (((tc & 0x80000000) && (mmutype == MMU_68030)) ||
515 	    ((tc & 0x8000) && (mmutype == MMU_68040))) {
516 		if (mac68k_machine.do_graybars)
517 			printf("Getting mapping from MMU.\n");
518 		(void) get_mapping();
519 		if (mac68k_machine.do_graybars)
520 			printf("Done.\n");
521 	} else {
522 		/* MMU not enabled.  Fake up ranges. */
523 		numranges = 1;
524 		low[0] = 0;
525 		high[0] = mac68k_machine.mach_memsize * (1024 * 1024);
526 		if (mac68k_machine.do_graybars)
527 			printf("Faked range to byte 0x%lx.\n", high[0]);
528 	}
529 	nextpa = load_addr + m68k_round_page(esym);
530 
531 	if (mac68k_machine.do_graybars)
532 		printf("Bootstrapping the pmap system.\n");
533 
534 	pmap_bootstrap(nextpa, load_addr);
535 
536 	if (mac68k_machine.do_graybars)
537 		printf("Pmap bootstrapped.\n");
538 
539 	if (!vidlen)
540 		panic("Don't know how to relocate video!");
541 
542 	if (mac68k_machine.do_graybars)
543 		printf("Moving ROMBase from %p to %p.\n", oldROMBase, ROMBase);
544 
545 	mrg_fixupROMBase(oldROMBase, ROMBase);
546 
547 	if (mac68k_machine.do_graybars)
548 		printf("Video address 0x%p -> 0x%p.\n",
549 		    (void *)mac68k_video.mv_kvaddr, (void *)newvideoaddr);
550 
551 	mac68k_set_io_offsets(IOBase);
552 
553 	/*
554 	 * If the serial ports are going (for console or 'echo'), then
555 	 * we need to make sure the IO change gets propagated properly.
556 	 * This resets the base addresses for the 8530 (serial) driver.
557 	 *
558 	 * WARNING!!! No printfs() (etc) BETWEEN zs_init() and the end
559 	 * of this function (where we start using the MMU, so the new
560 	 * address is correct.
561 	 */
562 #if NZSC > 0
563 	if (zsinited != 0)
564 		zs_init();
565 #endif
566 
567 	mac68k_video.mv_kvaddr = newvideoaddr;
568 }
569