pmap_bootstrap.c revision 1.28 1 /* $NetBSD: pmap_bootstrap.c,v 1.28 2007/03/04 05:59:49 christos 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.28 2007/03/04 05:59:49 christos Exp $");
40
41 #include <sys/param.h>
42 #include <sys/proc.h>
43
44 #include <machine/frame.h>
45 #include <machine/cpu.h>
46 #include <machine/hp300spu.h>
47 #include <machine/vmparam.h>
48 #include <machine/pte.h>
49
50 #include <hp300/hp300/clockreg.h>
51
52 #include <uvm/uvm_extern.h>
53
54 #define RELOC(v, t) *((t*)((u_int)&(v) + firstpa))
55
56 extern char *etext;
57 extern int Sysptsize;
58 extern char *extiobase, *proc0paddr;
59 extern st_entry_t *Sysseg;
60 extern pt_entry_t *Sysptmap, *Sysmap;
61 extern vaddr_t CLKbase, MMUbase;
62 extern paddr_t bootinfo_pa;
63 extern vaddr_t bootinfo_va;
64
65 extern int maxmem, physmem;
66 extern paddr_t avail_start, avail_end;
67 extern vaddr_t virtual_avail, virtual_end;
68 extern vsize_t mem_size;
69 extern int protection_codes[];
70 #ifdef M68K_MMU_HP
71 extern int pmap_aliasmask;
72 #endif
73
74 void pmap_bootstrap __P((paddr_t, paddr_t));
75
76 /*
77 * Special purpose kernel virtual addresses, used for mapping
78 * physical pages for a variety of temporary or permanent purposes:
79 *
80 * CADDR1, CADDR2: pmap zero/copy operations
81 * vmmap: /dev/mem, crash dumps, parity error checking
82 * ledbase: SPU LEDs
83 * msgbufaddr: kernel message buffer
84 */
85 void * CADDR1, CADDR2, vmmap, ledbase;
86 extern void * msgbufaddr;
87
88 /*
89 * Bootstrap the VM system.
90 *
91 * Called with MMU off so we must relocate all global references by `firstpa'
92 * (don't call any functions here!) `nextpa' is the first available physical
93 * memory address. Returns an updated first PA reflecting the memory we
94 * have allocated. MMU is still off when we return.
95 *
96 * XXX assumes sizeof(u_int) == sizeof(pt_entry_t)
97 * XXX a PIC compiler would make this much easier.
98 */
99 void
100 pmap_bootstrap(paddr_t nextpa, paddr_t firstpa)
101 {
102 paddr_t kstpa, kptpa, kptmpa, lkptpa, p0upa;
103 u_int nptpages, kstsize;
104 st_entry_t protoste, *ste;
105 pt_entry_t protopte, *pte, *epte;
106
107 /*
108 * Calculate important physical addresses:
109 *
110 * kstpa kernel segment table 1 page (!040)
111 * N pages (040)
112 *
113 * kptpa statically allocated
114 * kernel PT pages Sysptsize+ pages
115 *
116 * [ Sysptsize is the number of pages of PT, IIOMAPSIZE and
117 * EIOMAPSIZE are the number of PTEs, hence we need to round
118 * the total to a page boundary with IO maps at the end. ]
119 *
120 * kptmpa kernel PT map 1 page
121 *
122 * lkptpa last kernel PT page 1 page
123 *
124 * p0upa proc 0 u-area UPAGES pages
125 *
126 * The KVA corresponding to any of these PAs is:
127 * (PA - firstpa + KERNBASE).
128 */
129 if (RELOC(mmutype, int) == MMU_68040)
130 kstsize = MAXKL2SIZE / (NPTEPG/SG4_LEV2SIZE);
131 else
132 kstsize = 1;
133 kstpa = nextpa;
134 nextpa += kstsize * PAGE_SIZE;
135 kptmpa = nextpa;
136 nextpa += PAGE_SIZE;
137 lkptpa = nextpa;
138 nextpa += PAGE_SIZE;
139 p0upa = nextpa;
140 nextpa += USPACE;
141 kptpa = nextpa;
142 nptpages = RELOC(Sysptsize, int) +
143 (IIOMAPSIZE + EIOMAPSIZE + NPTEPG - 1) / NPTEPG;
144 nextpa += nptpages * PAGE_SIZE;
145
146 /*
147 * Initialize segment table and kernel page table map.
148 *
149 * On 68030s and earlier MMUs the two are identical except for
150 * the valid bits so both are initialized with essentially the
151 * same values. On the 68040, which has a mandatory 3-level
152 * structure, the segment table holds the level 1 table and part
153 * (or all) of the level 2 table and hence is considerably
154 * different. Here the first level consists of 128 descriptors
155 * (512 bytes) each mapping 32mb of address space. Each of these
156 * points to blocks of 128 second level descriptors (512 bytes)
157 * each mapping 256kb. Note that there may be additional "segment
158 * table" pages depending on how large MAXKL2SIZE is.
159 *
160 * Portions of the last segment of KVA space (0xFFF00000 -
161 * 0xFFFFFFFF) are mapped for a couple of purposes. 0xFFF00000
162 * for UPAGES is used for mapping the current process u-area
163 * (u + kernel stack). The very last page (0xFFFFF000) is mapped
164 * to the last physical page of RAM to give us a region in which
165 * PA == VA. We use the first part of this page for enabling
166 * and disabling mapping. The last part of this page also contains
167 * info left by the boot ROM.
168 *
169 * XXX cramming two levels of mapping into the single "segment"
170 * table on the 68040 is intended as a temporary hack to get things
171 * working. The 224mb of address space that this allows will most
172 * likely be insufficient in the future (at least for the kernel).
173 */
174 if (RELOC(mmutype, int) == MMU_68040) {
175 int num;
176
177 /*
178 * First invalidate the entire "segment table" pages
179 * (levels 1 and 2 have the same "invalid" value).
180 */
181 pte = (u_int *)kstpa;
182 epte = &pte[kstsize * NPTEPG];
183 while (pte < epte)
184 *pte++ = SG_NV;
185 /*
186 * Initialize level 2 descriptors (which immediately
187 * follow the level 1 table). We need:
188 * NPTEPG / SG4_LEV3SIZE
189 * level 2 descriptors to map each of the nptpages
190 * pages of PTEs. Note that we set the "used" bit
191 * now to save the HW the expense of doing it.
192 */
193 num = nptpages * (NPTEPG / SG4_LEV3SIZE);
194 pte = &((u_int *)kstpa)[SG4_LEV1SIZE];
195 epte = &pte[num];
196 protoste = kptpa | SG_U | SG_RW | SG_V;
197 while (pte < epte) {
198 *pte++ = protoste;
199 protoste += (SG4_LEV3SIZE * sizeof(st_entry_t));
200 }
201 /*
202 * Initialize level 1 descriptors. We need:
203 * roundup(num, SG4_LEV2SIZE) / SG4_LEV2SIZE
204 * level 1 descriptors to map the `num' level 2's.
205 */
206 pte = (u_int *)kstpa;
207 epte = &pte[roundup(num, SG4_LEV2SIZE) / SG4_LEV2SIZE];
208 protoste = (u_int)&pte[SG4_LEV1SIZE] | SG_U | SG_RW | SG_V;
209 while (pte < epte) {
210 *pte++ = protoste;
211 protoste += (SG4_LEV2SIZE * sizeof(st_entry_t));
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 kptmpa and the "last PT page".
223 */
224 pte = &((u_int *)kstpa)[kstsize*NPTEPG - NPTEPG/SG4_LEV3SIZE*2];
225 epte = &pte[NPTEPG/SG4_LEV3SIZE];
226 protoste = kptmpa | SG_U | SG_RW | SG_V;
227 while (pte < epte) {
228 *pte++ = protoste;
229 protoste += (SG4_LEV3SIZE * sizeof(st_entry_t));
230 }
231 epte = &pte[NPTEPG/SG4_LEV3SIZE];
232 protoste = lkptpa | SG_U | SG_RW | SG_V;
233 while (pte < epte) {
234 *pte++ = protoste;
235 protoste += (SG4_LEV3SIZE * sizeof(st_entry_t));
236 }
237 /*
238 * Initialize Sysptmap
239 */
240 pte = (u_int *)kptmpa;
241 epte = &pte[nptpages];
242 protopte = kptpa | PG_RW | PG_CI | PG_V;
243 while (pte < epte) {
244 *pte++ = protopte;
245 protopte += PAGE_SIZE;
246 }
247 /*
248 * Invalidate all but the last remaining entry.
249 */
250 epte = &((u_int *)kptmpa)[NPTEPG-2];
251 while (pte < epte) {
252 *pte++ = PG_NV;
253 }
254 /*
255 * Initialize the last ones to point to kptmpa and the page
256 * table page allocated earlier.
257 */
258 *pte = kptmpa | PG_RW | PG_CI | PG_V;
259 pte++;
260 *pte = lkptpa | PG_RW | PG_CI | PG_V;
261 } else {
262 /*
263 * Map the page table pages in both the HW segment table
264 * and the software Sysptmap.
265 */
266 ste = (u_int *)kstpa;
267 pte = (u_int *)kptmpa;
268 epte = &pte[nptpages];
269 protoste = kptpa | SG_RW | SG_V;
270 protopte = kptpa | PG_RW | PG_CI | PG_V;
271 while (pte < epte) {
272 *ste++ = protoste;
273 *pte++ = protopte;
274 protoste += PAGE_SIZE;
275 protopte += PAGE_SIZE;
276 }
277 /*
278 * Invalidate all but the last remaining entries in both.
279 */
280 epte = &((u_int *)kptmpa)[NPTEPG-2];
281 while (pte < epte) {
282 *ste++ = SG_NV;
283 *pte++ = PG_NV;
284 }
285 /*
286 * Initialize the last ones to point to kptmpa and the page
287 * table page allocated earlier.
288 */
289 *ste = kptmpa | SG_RW | SG_V;
290 *pte = kptmpa | PG_RW | PG_CI | PG_V;
291 ste++;
292 pte++;
293 *ste = lkptpa | SG_RW | SG_V;
294 *pte = lkptpa | PG_RW | PG_CI | PG_V;
295 }
296 /*
297 * Invalidate all but the final entry in the last kernel PT page
298 * (u-area PTEs will be validated later). The final entry maps
299 * the last page of physical memory.
300 */
301 pte = (u_int *)lkptpa;
302 epte = &pte[NPTEPG-1];
303 while (pte < epte)
304 *pte++ = PG_NV;
305 *pte = MAXADDR | PG_RW | PG_CI | PG_V;
306 /*
307 * Initialize kernel page table.
308 * Start by invalidating the `nptpages' that we have allocated.
309 */
310 pte = (u_int *)kptpa;
311 epte = &pte[nptpages * NPTEPG];
312 while (pte < epte)
313 *pte++ = PG_NV;
314
315 /*
316 * The page of kernel text is zero-filled in locore.s,
317 * and not mapped (at VA 0). The boot loader places the
318 * bootinfo here after the kernel is loaded. Remember
319 * the physical address; we'll map it to a virtual address
320 * later.
321 */
322 RELOC(bootinfo_pa, paddr_t) = firstpa;
323
324 /*
325 * Validate PTEs for kernel text (RO). The first page
326 * of kernel text remains invalid; see locore.s
327 */
328 pte = &((u_int *)kptpa)[m68k_btop(KERNBASE + PAGE_SIZE)];
329 epte = &pte[m68k_btop(m68k_trunc_page(&etext))];
330 protopte = (firstpa + PAGE_SIZE) | PG_RO | PG_V;
331 while (pte < epte) {
332 *pte++ = protopte;
333 protopte += PAGE_SIZE;
334 }
335 /*
336 * Validate PTEs for kernel data/bss, dynamic data allocated
337 * by us so far (nextpa - firstpa bytes), and pages for proc0
338 * u-area and page table allocated below (RW).
339 */
340 epte = &((u_int *)kptpa)[m68k_btop(nextpa - firstpa)];
341 protopte = (protopte & ~PG_PROT) | PG_RW;
342 /*
343 * Enable copy-back caching of data pages
344 */
345 if (RELOC(mmutype, int) == MMU_68040)
346 protopte |= PG_CCB;
347 while (pte < epte) {
348 *pte++ = protopte;
349 protopte += PAGE_SIZE;
350 }
351 /*
352 * Finally, validate the internal IO space PTEs (RW+CI).
353 * We do this here since the 320/350 MMU registers (also
354 * used, but to a lesser extent, on other models) are mapped
355 * in this range and it would be nice to be able to access
356 * them after the MMU is turned on.
357 */
358
359 #define PTE2VA(pte) m68k_ptob(pte - ((pt_entry_t *)kptpa))
360
361 protopte = INTIOBASE | PG_RW | PG_CI | PG_V;
362 epte = &pte[IIOMAPSIZE];
363 RELOC(intiobase, char *) = (char *)PTE2VA(pte);
364 RELOC(intiolimit, char *) = (char *)PTE2VA(epte);
365 while (pte < epte) {
366 *pte++ = protopte;
367 protopte += PAGE_SIZE;
368 }
369 RELOC(extiobase, char *) = (char *)PTE2VA(pte);
370 pte += EIOMAPSIZE;
371 RELOC(virtual_avail, vaddr_t) = PTE2VA(pte);
372
373 /*
374 * Calculate important exported kernel virtual addresses
375 */
376 /*
377 * Sysseg: base of kernel segment table
378 */
379 RELOC(Sysseg, st_entry_t *) =
380 (st_entry_t *)(kstpa - firstpa);
381 /*
382 * Sysptmap: base of kernel page table map
383 */
384 RELOC(Sysptmap, pt_entry_t *) =
385 (pt_entry_t *)(kptmpa - firstpa);
386 /*
387 * Sysmap: kernel page table (as mapped through Sysptmap)
388 * Immediately follows `nptpages' of static kernel page table.
389 */
390 RELOC(Sysmap, pt_entry_t *) =
391 (pt_entry_t *)m68k_ptob((NPTEPG - 2) * NPTEPG);
392 /*
393 * CLKbase, MMUbase: important registers in internal IO space
394 * accessed from assembly language.
395 */
396 RELOC(CLKbase, vaddr_t) =
397 (vaddr_t)RELOC(intiobase, char *) + CLKBASE;
398 RELOC(MMUbase, vaddr_t) =
399 (vaddr_t)RELOC(intiobase, char *) + MMUBASE;
400
401 /*
402 * Setup u-area for process 0.
403 */
404 /*
405 * Zero the u-area.
406 * NOTE: `pte' and `epte' aren't PTEs here.
407 */
408 pte = (u_int *)p0upa;
409 epte = (u_int *)(p0upa + USPACE);
410 while (pte < epte)
411 *pte++ = 0;
412 /*
413 * Remember the u-area address so it can be loaded in the
414 * proc struct p_addr field later.
415 */
416 RELOC(proc0paddr, char *) = (char *)(p0upa - firstpa);
417
418 /*
419 * VM data structures are now initialized, set up data for
420 * the pmap module.
421 *
422 * Note about avail_end: msgbuf is initialized just after
423 * avail_end in machdep.c. Since the last page is used
424 * for rebooting the system (code is copied there and
425 * excution continues from copied code before the MMU
426 * is disabled), the msgbuf will get trounced between
427 * reboots if it's placed in the last physical page.
428 * To work around this, we move avail_end back one more
429 * page so the msgbuf can be preserved.
430 */
431 RELOC(avail_start, paddr_t) = nextpa;
432 RELOC(avail_end, paddr_t) = m68k_ptob(RELOC(maxmem, int)) -
433 (m68k_round_page(MSGBUFSIZE) + m68k_ptob(1));
434 RELOC(mem_size, vsize_t) = m68k_ptob(RELOC(physmem, int));
435 RELOC(virtual_end, vaddr_t) = VM_MAX_KERNEL_ADDRESS;
436
437 #ifdef M68K_MMU_HP
438 /*
439 * Determine VA aliasing distance if any
440 */
441 if (RELOC(ectype, int) == EC_VIRT) {
442 if (RELOC(machineid, int) == HP_320)
443 RELOC(pmap_aliasmask, int) = 0x3fff; /* 16k */
444 else if (RELOC(machineid, int) == HP_350)
445 RELOC(pmap_aliasmask, int) = 0x7fff; /* 32k */
446 }
447 #endif
448
449 /*
450 * Initialize protection array.
451 * XXX don't use a switch statement, it might produce an
452 * absolute "jmp" table.
453 */
454 {
455 int *kp;
456
457 kp = &RELOC(protection_codes, int);
458 kp[VM_PROT_NONE|VM_PROT_NONE|VM_PROT_NONE] = 0;
459 kp[VM_PROT_READ|VM_PROT_NONE|VM_PROT_NONE] = PG_RO;
460 kp[VM_PROT_READ|VM_PROT_NONE|VM_PROT_EXECUTE] = PG_RO;
461 kp[VM_PROT_NONE|VM_PROT_NONE|VM_PROT_EXECUTE] = PG_RO;
462 kp[VM_PROT_NONE|VM_PROT_WRITE|VM_PROT_NONE] = PG_RW;
463 kp[VM_PROT_NONE|VM_PROT_WRITE|VM_PROT_EXECUTE] = PG_RW;
464 kp[VM_PROT_READ|VM_PROT_WRITE|VM_PROT_NONE] = PG_RW;
465 kp[VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE] = PG_RW;
466 }
467
468 /*
469 * Kernel page/segment table allocated above,
470 * just initialize pointers.
471 */
472 {
473 struct pmap *kpm = &RELOC(kernel_pmap_store, struct pmap);
474
475 kpm->pm_stab = RELOC(Sysseg, st_entry_t *);
476 kpm->pm_ptab = RELOC(Sysmap, pt_entry_t *);
477 simple_lock_init(&kpm->pm_lock);
478 kpm->pm_count = 1;
479 kpm->pm_stpa = (st_entry_t *)kstpa;
480 /*
481 * For the 040 we also initialize the free level 2
482 * descriptor mask noting that we have used:
483 * 0: level 1 table
484 * 1 to `num': map page tables
485 * MAXKL2SIZE-1: maps kptmpa and last-page page table
486 */
487 if (RELOC(mmutype, int) == MMU_68040) {
488 int num;
489
490 kpm->pm_stfree = ~l2tobm(0);
491 num = roundup(nptpages * (NPTEPG / SG4_LEV3SIZE),
492 SG4_LEV2SIZE) / SG4_LEV2SIZE;
493 while (num)
494 kpm->pm_stfree &= ~l2tobm(num--);
495 kpm->pm_stfree &= ~l2tobm(MAXKL2SIZE-1);
496 for (num = MAXKL2SIZE;
497 num < sizeof(kpm->pm_stfree)*NBBY;
498 num++)
499 kpm->pm_stfree &= ~l2tobm(num);
500 }
501 }
502
503 /*
504 * Allocate some fixed, special purpose kernel virtual addresses
505 */
506 {
507 vaddr_t va = RELOC(virtual_avail, vaddr_t);
508
509 RELOC(bootinfo_va, vaddr_t) = (vaddr_t)va;
510 va += PAGE_SIZE;
511 RELOC(CADDR1, void *) = (void *)va;
512 va += PAGE_SIZE;
513 RELOC(CADDR2, void *) = (void *)va;
514 va += PAGE_SIZE;
515 RELOC(vmmap, void *) = (void *)va;
516 va += PAGE_SIZE;
517 RELOC(ledbase, void *) = (void *)va;
518 va += PAGE_SIZE;
519 RELOC(msgbufaddr, void *) = (void *)va;
520 va += m68k_round_page(MSGBUFSIZE);
521 RELOC(virtual_avail, vaddr_t) = va;
522 }
523 }
524