pmap.c revision 1.60.2.6 1 /* $NetBSD: pmap.c,v 1.60.2.6 2010/10/30 08:41:10 uebayasi Exp $ */
2
3 /*
4 * Copyright 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
40 * Copyright (C) 1995, 1996 TooLs GmbH.
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by TooLs GmbH.
54 * 4. The name of TooLs GmbH may not be used to endorse or promote products
55 * derived from this software without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
61 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
62 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
63 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
64 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
65 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
66 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.60.2.6 2010/10/30 08:41:10 uebayasi Exp $");
71
72 #include "opt_xip.h"
73
74 #include <sys/param.h>
75 #include <sys/malloc.h>
76 #include <sys/proc.h>
77 #include <sys/queue.h>
78 #include <sys/systm.h>
79 #include <sys/pool.h>
80 #include <sys/device.h>
81
82 #include <uvm/uvm.h>
83
84 #include <machine/cpu.h>
85 #include <machine/pcb.h>
86 #include <machine/powerpc.h>
87
88 #include <powerpc/spr.h>
89 #include <powerpc/ibm4xx/spr.h>
90 #include <machine/tlb.h>
91
92 /*
93 * kernmap is an array of PTEs large enough to map in
94 * 4GB. At 16KB/page it is 256K entries or 2MB.
95 */
96 #define KERNMAP_SIZE ((0xffffffffU/PAGE_SIZE)+1)
97 void *kernmap;
98
99 #define MINCTX 2
100 #define NUMCTX 256
101
102 volatile struct pmap *ctxbusy[NUMCTX];
103
104 #define TLBF_USED 0x1
105 #define TLBF_REF 0x2
106 #define TLBF_LOCKED 0x4
107 #define TLB_LOCKED(i) (tlb_info[(i)].ti_flags & TLBF_LOCKED)
108
109 typedef struct tlb_info_s {
110 char ti_flags;
111 char ti_ctx; /* TLB_PID assiciated with the entry */
112 u_int ti_va;
113 } tlb_info_t;
114
115 volatile tlb_info_t tlb_info[NTLB];
116 /* We'll use a modified FIFO replacement policy cause it's cheap */
117 volatile int tlbnext;
118
119 static int tlb_nreserved = 0;
120 static int pmap_bootstrap_done = 0;
121
122 /* Event counters */
123 struct evcnt tlbmiss_ev = EVCNT_INITIALIZER(EVCNT_TYPE_TRAP,
124 NULL, "cpu", "tlbmiss");
125 struct evcnt tlbhit_ev = EVCNT_INITIALIZER(EVCNT_TYPE_TRAP,
126 NULL, "cpu", "tlbhit");
127 struct evcnt tlbflush_ev = EVCNT_INITIALIZER(EVCNT_TYPE_TRAP,
128 NULL, "cpu", "tlbflush");
129 struct evcnt tlbenter_ev = EVCNT_INITIALIZER(EVCNT_TYPE_TRAP,
130 NULL, "cpu", "tlbenter");
131
132 struct pmap kernel_pmap_;
133 struct pmap *const kernel_pmap_ptr = &kernel_pmap_;
134
135 static int npgs;
136 static u_int nextavail;
137 #ifndef MSGBUFADDR
138 extern paddr_t msgbuf_paddr;
139 #endif
140
141 static struct mem_region *mem, *avail;
142
143 /*
144 * This is a cache of referenced/modified bits.
145 * Bits herein are shifted by ATTRSHFT.
146 */
147 static char *pmap_attrib;
148
149 #define PV_WIRED 0x1
150 #define PV_WIRE(pv) ((pv)->pv_va |= PV_WIRED)
151 #define PV_UNWIRE(pv) ((pv)->pv_va &= ~PV_WIRED)
152 #define PV_ISWIRED(pv) ((pv)->pv_va & PV_WIRED)
153 #define PV_CMPVA(va,pv) (!(((pv)->pv_va ^ (va)) & (~PV_WIRED)))
154
155 struct pv_entry {
156 struct pv_entry *pv_next; /* Linked list of mappings */
157 vaddr_t pv_va; /* virtual address of mapping */
158 struct pmap *pv_pm;
159 };
160
161 /* Each index corresponds to TLB_SIZE_* value. */
162 static size_t tlbsize[] = {
163 1024, /* TLB_SIZE_1K */
164 4096, /* TLB_SIZE_4K */
165 16384, /* TLB_SIZE_16K */
166 65536, /* TLB_SIZE_64K */
167 262144, /* TLB_SIZE_256K */
168 1048576, /* TLB_SIZE_1M */
169 4194304, /* TLB_SIZE_4M */
170 16777216, /* TLB_SIZE_16M */
171 };
172
173 struct pv_entry *pv_table;
174 static struct pool pv_pool;
175
176 static int pmap_initialized;
177
178 static int ctx_flush(int);
179
180 inline struct pv_entry *pa_to_pv(paddr_t);
181 static inline char *pa_to_attr(paddr_t);
182
183 static inline volatile u_int *pte_find(struct pmap *, vaddr_t);
184 static inline int pte_enter(struct pmap *, vaddr_t, u_int);
185
186 static inline int pmap_enter_pv(struct pmap *, vaddr_t, paddr_t, int);
187 static void pmap_remove_pv(struct pmap *, vaddr_t, paddr_t);
188
189 static int ppc4xx_tlb_size_mask(size_t, int *, int *);
190
191
192 inline struct pv_entry *
193 pa_to_pv(paddr_t pa)
194 {
195 int bank, pg;
196
197 #ifdef XIP
198 bank = vm_physseg_find_device(atop(pa), &pg);
199 if (bank != -1)
200 return &VM_PHYSDEV_PTR(bank)->pmseg.pvent[pg];
201 #endif
202 bank = vm_physseg_find(atop(pa), &pg);
203 if (bank != -1)
204 return &VM_PHYSMEM_PTR(bank)->pmseg.pvent[pg];
205 return NULL;
206 }
207
208 static inline char *
209 pa_to_attr(paddr_t pa)
210 {
211 int bank, pg;
212
213 #ifdef XIP
214 bank = vm_physseg_find_device(atop(pa), &pg);
215 if (bank != -1)
216 return &VM_PHYSDEV_PTR(bank)->pmseg.attrs[pg];
217 #endif
218 bank = vm_physseg_find(atop(pa), &pg);
219 if (bank != -1)
220 return &VM_PHYSMEM_PTR(bank)->pmseg.attrs[pg];
221 return NULL;
222 }
223
224 /*
225 * Insert PTE into page table.
226 */
227 int
228 pte_enter(struct pmap *pm, vaddr_t va, u_int pte)
229 {
230 int seg = STIDX(va);
231 int ptn = PTIDX(va);
232 u_int oldpte;
233
234 if (!pm->pm_ptbl[seg]) {
235 /* Don't allocate a page to clear a non-existent mapping. */
236 if (!pte)
237 return (0);
238 /* Allocate a page XXXX this will sleep! */
239 pm->pm_ptbl[seg] =
240 (uint *)uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
241 UVM_KMF_WIRED | UVM_KMF_ZERO);
242 }
243 oldpte = pm->pm_ptbl[seg][ptn];
244 pm->pm_ptbl[seg][ptn] = pte;
245
246 /* Flush entry. */
247 ppc4xx_tlb_flush(va, pm->pm_ctx);
248 if (oldpte != pte) {
249 if (pte == 0)
250 pm->pm_stats.resident_count--;
251 else
252 pm->pm_stats.resident_count++;
253 }
254 return (1);
255 }
256
257 /*
258 * Get a pointer to a PTE in a page table.
259 */
260 volatile u_int *
261 pte_find(struct pmap *pm, vaddr_t va)
262 {
263 int seg = STIDX(va);
264 int ptn = PTIDX(va);
265
266 if (pm->pm_ptbl[seg])
267 return (&pm->pm_ptbl[seg][ptn]);
268
269 return (NULL);
270 }
271
272 /*
273 * This is called during initppc, before the system is really initialized.
274 */
275 void
276 pmap_bootstrap(u_int kernelstart, u_int kernelend)
277 {
278 struct mem_region *mp, *mp1;
279 int cnt, i;
280 u_int s, e, sz;
281
282 tlbnext = tlb_nreserved;
283
284 /*
285 * Allocate the kernel page table at the end of
286 * kernel space so it's in the locked TTE.
287 */
288 kernmap = (void *)kernelend;
289
290 /*
291 * Initialize kernel page table.
292 */
293 for (i = 0; i < STSZ; i++) {
294 pmap_kernel()->pm_ptbl[i] = 0;
295 }
296 ctxbusy[0] = ctxbusy[1] = pmap_kernel();
297
298 /*
299 * Announce page-size to the VM-system
300 */
301 uvmexp.pagesize = NBPG;
302 uvm_setpagesize();
303
304 /*
305 * Get memory.
306 */
307 mem_regions(&mem, &avail);
308 for (mp = mem; mp->size; mp++) {
309 physmem += btoc(mp->size);
310 printf("+%lx,",mp->size);
311 }
312 printf("\n");
313 ppc4xx_tlb_init();
314 /*
315 * Count the number of available entries.
316 */
317 for (cnt = 0, mp = avail; mp->size; mp++)
318 cnt++;
319
320 /*
321 * Page align all regions.
322 * Non-page aligned memory isn't very interesting to us.
323 * Also, sort the entries for ascending addresses.
324 */
325 kernelstart &= ~PGOFSET;
326 kernelend = (kernelend + PGOFSET) & ~PGOFSET;
327 for (mp = avail; mp->size; mp++) {
328 s = mp->start;
329 e = mp->start + mp->size;
330 printf("%08x-%08x -> ",s,e);
331 /*
332 * Check whether this region holds all of the kernel.
333 */
334 if (s < kernelstart && e > kernelend) {
335 avail[cnt].start = kernelend;
336 avail[cnt++].size = e - kernelend;
337 e = kernelstart;
338 }
339 /*
340 * Look whether this regions starts within the kernel.
341 */
342 if (s >= kernelstart && s < kernelend) {
343 if (e <= kernelend)
344 goto empty;
345 s = kernelend;
346 }
347 /*
348 * Now look whether this region ends within the kernel.
349 */
350 if (e > kernelstart && e <= kernelend) {
351 if (s >= kernelstart)
352 goto empty;
353 e = kernelstart;
354 }
355 /*
356 * Now page align the start and size of the region.
357 */
358 s = round_page(s);
359 e = trunc_page(e);
360 if (e < s)
361 e = s;
362 sz = e - s;
363 printf("%08x-%08x = %x\n",s,e,sz);
364 /*
365 * Check whether some memory is left here.
366 */
367 if (sz == 0) {
368 empty:
369 memmove(mp, mp + 1,
370 (cnt - (mp - avail)) * sizeof *mp);
371 cnt--;
372 mp--;
373 continue;
374 }
375 /*
376 * Do an insertion sort.
377 */
378 npgs += btoc(sz);
379 for (mp1 = avail; mp1 < mp; mp1++)
380 if (s < mp1->start)
381 break;
382 if (mp1 < mp) {
383 memmove(mp1 + 1, mp1, (char *)mp - (char *)mp1);
384 mp1->start = s;
385 mp1->size = sz;
386 } else {
387 mp->start = s;
388 mp->size = sz;
389 }
390 }
391
392 /*
393 * We cannot do pmap_steal_memory here,
394 * since we don't run with translation enabled yet.
395 */
396 #ifndef MSGBUFADDR
397 /*
398 * allow for msgbuf
399 */
400 sz = round_page(MSGBUFSIZE);
401 mp = NULL;
402 for (mp1 = avail; mp1->size; mp1++)
403 if (mp1->size >= sz)
404 mp = mp1;
405 if (mp == NULL)
406 panic("not enough memory?");
407
408 npgs -= btoc(sz);
409 msgbuf_paddr = mp->start + mp->size - sz;
410 mp->size -= sz;
411 if (mp->size <= 0)
412 memmove(mp, mp + 1, (cnt - (mp - avail)) * sizeof *mp);
413 #endif
414
415 for (mp = avail; mp->size; mp++)
416 uvm_page_physload(atop(mp->start), atop(mp->start + mp->size),
417 atop(mp->start), atop(mp->start + mp->size),
418 VM_FREELIST_DEFAULT);
419
420 /*
421 * Initialize kernel pmap and hardware.
422 */
423 /* Setup TLB pid allocator so it knows we alreadu using PID 1 */
424 pmap_kernel()->pm_ctx = KERNEL_PID;
425 nextavail = avail->start;
426
427 evcnt_attach_static(&tlbmiss_ev);
428 evcnt_attach_static(&tlbhit_ev);
429 evcnt_attach_static(&tlbflush_ev);
430 evcnt_attach_static(&tlbenter_ev);
431
432 pmap_bootstrap_done = 1;
433 }
434
435 /*
436 * Restrict given range to physical memory
437 *
438 * (Used by /dev/mem)
439 */
440 void
441 pmap_real_memory(paddr_t *start, psize_t *size)
442 {
443 struct mem_region *mp;
444
445 for (mp = mem; mp->size; mp++) {
446 if (*start + *size > mp->start &&
447 *start < mp->start + mp->size) {
448 if (*start < mp->start) {
449 *size -= mp->start - *start;
450 *start = mp->start;
451 }
452 if (*start + *size > mp->start + mp->size)
453 *size = mp->start + mp->size - *start;
454 return;
455 }
456 }
457 *size = 0;
458 }
459
460 /*
461 * Initialize anything else for pmap handling.
462 * Called during vm_init().
463 */
464 void
465 pmap_init(void)
466 {
467 struct pv_entry *pv;
468 vsize_t sz;
469 vaddr_t addr;
470 int i, s;
471 int bank;
472 char *attr;
473
474 sz = (vsize_t)((sizeof(struct pv_entry) + 1) * npgs);
475 sz = round_page(sz);
476 addr = uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_WIRED | UVM_KMF_ZERO);
477 s = splvm();
478 pv = pv_table = (struct pv_entry *)addr;
479 for (i = npgs; --i >= 0;)
480 pv++->pv_pm = NULL;
481 pmap_attrib = (char *)pv;
482 memset(pv, 0, npgs);
483
484 pv = pv_table;
485 attr = pmap_attrib;
486 for (bank = 0; bank < vm_nphysseg; bank++) {
487 sz = VM_PHYSMEM_PTR(bank)->end - VM_PHYSMEM_PTR(bank)->start;
488 VM_PHYSMEM_PTR(bank)->pmseg.pvent = pv;
489 VM_PHYSMEM_PTR(bank)->pmseg.attrs = attr;
490 pv += sz;
491 attr += sz;
492 }
493
494 pmap_initialized = 1;
495 splx(s);
496
497 /* Setup a pool for additional pvlist structures */
498 pool_init(&pv_pool, sizeof(struct pv_entry), 0, 0, 0, "pv_entry", NULL,
499 IPL_VM);
500 }
501
502 void
503 pmap_physseg_init(struct vm_physseg *seg)
504 {
505 size_t npages;
506 vsize_t sz;
507 struct pv_entry *pv;
508 char *attr;
509
510 npages = seg->end - seg->start + 1;
511 sz = (vsize_t)((sizeof(struct pv_entry) + 1) * npages);
512 sz = round_page(sz);
513 pv = (void *)uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_WIRED | UVM_KMF_ZERO);
514 attr = (void *)(pv + npages);
515
516 seg->pmseg.pvent = pv;
517 seg->pmseg.attrs = attr;
518 }
519
520 void
521 pmap_physseg_fini(struct vm_physseg *seg)
522 {
523 size_t npages;
524 vsize_t sz;
525
526 npages = seg->end - seg->start + 1;
527 sz = (vsize_t)((sizeof(struct pv_entry) + 1) * npages);
528 sz = round_page(sz);
529 uvm_km_free(kernel_map, (vaddr_t)seg->pmseg.pvent, sz, UVM_KMF_WIRED);
530 }
531
532 /*
533 * How much virtual space is available to the kernel?
534 */
535 void
536 pmap_virtual_space(vaddr_t *start, vaddr_t *end)
537 {
538
539 #if 0
540 /*
541 * Reserve one segment for kernel virtual memory
542 */
543 *start = (vaddr_t)(KERNEL_SR << ADDR_SR_SHFT);
544 *end = *start + SEGMENT_LENGTH;
545 #else
546 *start = (vaddr_t) VM_MIN_KERNEL_ADDRESS;
547 *end = (vaddr_t) VM_MAX_KERNEL_ADDRESS;
548 #endif
549 }
550
551 #ifdef PMAP_GROWKERNEL
552 /*
553 * Preallocate kernel page tables to a specified VA.
554 * This simply loops through the first TTE for each
555 * page table from the beginning of the kernel pmap,
556 * reads the entry, and if the result is
557 * zero (either invalid entry or no page table) it stores
558 * a zero there, populating page tables in the process.
559 * This is not the most efficient technique but i don't
560 * expect it to be called that often.
561 */
562 extern struct vm_page *vm_page_alloc1(void);
563 extern void vm_page_free1(struct vm_page *);
564
565 vaddr_t kbreak = VM_MIN_KERNEL_ADDRESS;
566
567 vaddr_t
568 pmap_growkernel(vaddr_t maxkvaddr)
569 {
570 int s;
571 int seg;
572 paddr_t pg;
573 struct pmap *pm = pmap_kernel();
574
575 s = splvm();
576
577 /* Align with the start of a page table */
578 for (kbreak &= ~(PTMAP-1); kbreak < maxkvaddr;
579 kbreak += PTMAP) {
580 seg = STIDX(kbreak);
581
582 if (pte_find(pm, kbreak))
583 continue;
584
585 if (uvm.page_init_done) {
586 pg = (paddr_t)VM_PAGE_TO_PHYS(vm_page_alloc1());
587 } else {
588 if (!uvm_page_physget(&pg))
589 panic("pmap_growkernel: no memory");
590 }
591 if (!pg)
592 panic("pmap_growkernel: no pages");
593 pmap_zero_page((paddr_t)pg);
594
595 /* XXX This is based on all phymem being addressable */
596 pm->pm_ptbl[seg] = (u_int *)pg;
597 }
598 splx(s);
599 return (kbreak);
600 }
601
602 /*
603 * vm_page_alloc1:
604 *
605 * Allocate and return a memory cell with no associated object.
606 */
607 struct vm_page *
608 vm_page_alloc1(void)
609 {
610 struct vm_page *pg;
611
612 pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
613 if (pg) {
614 pg->wire_count = 1; /* no mappings yet */
615 pg->flags &= ~PG_BUSY; /* never busy */
616 }
617 return pg;
618 }
619
620 /*
621 * vm_page_free1:
622 *
623 * Returns the given page to the free list,
624 * disassociating it with any VM object.
625 *
626 * Object and page must be locked prior to entry.
627 */
628 void
629 vm_page_free1(struct vm_page *pg)
630 {
631 #ifdef DIAGNOSTIC
632 if (pg->flags != (PG_CLEAN|PG_FAKE)) {
633 printf("Freeing invalid page %p\n", pg);
634 printf("pa = %llx\n", (unsigned long long)VM_PAGE_TO_PHYS(pg));
635 #ifdef DDB
636 Debugger();
637 #endif
638 return;
639 }
640 #endif
641 pg->flags |= PG_BUSY;
642 pg->wire_count = 0;
643 uvm_pagefree(pg);
644 }
645 #endif
646
647 /*
648 * Create and return a physical map.
649 */
650 struct pmap *
651 pmap_create(void)
652 {
653 struct pmap *pm;
654
655 pm = malloc(sizeof *pm, M_VMPMAP, M_WAITOK);
656 memset(pm, 0, sizeof *pm);
657 pm->pm_refs = 1;
658 return pm;
659 }
660
661 /*
662 * Add a reference to the given pmap.
663 */
664 void
665 pmap_reference(struct pmap *pm)
666 {
667
668 pm->pm_refs++;
669 }
670
671 /*
672 * Retire the given pmap from service.
673 * Should only be called if the map contains no valid mappings.
674 */
675 void
676 pmap_destroy(struct pmap *pm)
677 {
678 int i;
679
680 if (--pm->pm_refs > 0) {
681 return;
682 }
683 KASSERT(pm->pm_stats.resident_count == 0);
684 KASSERT(pm->pm_stats.wired_count == 0);
685 for (i = 0; i < STSZ; i++)
686 if (pm->pm_ptbl[i]) {
687 uvm_km_free(kernel_map, (vaddr_t)pm->pm_ptbl[i],
688 PAGE_SIZE, UVM_KMF_WIRED);
689 pm->pm_ptbl[i] = NULL;
690 }
691 if (pm->pm_ctx)
692 ctx_free(pm);
693 free(pm, M_VMPMAP);
694 }
695
696 /*
697 * Copy the range specified by src_addr/len
698 * from the source map to the range dst_addr/len
699 * in the destination map.
700 *
701 * This routine is only advisory and need not do anything.
702 */
703 void
704 pmap_copy(struct pmap *dst_pmap, struct pmap *src_pmap, vaddr_t dst_addr,
705 vsize_t len, vaddr_t src_addr)
706 {
707 }
708
709 /*
710 * Require that all active physical maps contain no
711 * incorrect entries NOW.
712 */
713 void
714 pmap_update(struct pmap *pmap)
715 {
716 }
717
718 /*
719 * Fill the given physical page with zeroes.
720 */
721 void
722 pmap_zero_page(paddr_t pa)
723 {
724
725 #ifdef PPC_4XX_NOCACHE
726 memset((void *)pa, 0, PAGE_SIZE);
727 #else
728 int i;
729
730 for (i = PAGE_SIZE/CACHELINESIZE; i > 0; i--) {
731 __asm volatile ("dcbz 0,%0" :: "r"(pa));
732 pa += CACHELINESIZE;
733 }
734 #endif
735 }
736
737 /*
738 * Copy the given physical source page to its destination.
739 */
740 void
741 pmap_copy_page(paddr_t src, paddr_t dst)
742 {
743
744 memcpy((void *)dst, (void *)src, PAGE_SIZE);
745 dcache_flush_page(dst);
746 }
747
748 /*
749 * This returns != 0 on success.
750 */
751 static inline int
752 pmap_enter_pv(struct pmap *pm, vaddr_t va, paddr_t pa, int flags)
753 {
754 struct pv_entry *pv, *npv = NULL;
755 int s;
756
757 if (!pmap_initialized)
758 return 0;
759
760 s = splvm();
761 pv = pa_to_pv(pa);
762 if (!pv->pv_pm) {
763 /*
764 * No entries yet, use header as the first entry.
765 */
766 pv->pv_va = va;
767 pv->pv_pm = pm;
768 pv->pv_next = NULL;
769 } else {
770 /*
771 * There is at least one other VA mapping this page.
772 * Place this entry after the header.
773 */
774 npv = pool_get(&pv_pool, PR_NOWAIT);
775 if (npv == NULL) {
776 if ((flags & PMAP_CANFAIL) == 0)
777 panic("pmap_enter_pv: failed");
778 splx(s);
779 return 0;
780 }
781 npv->pv_va = va;
782 npv->pv_pm = pm;
783 npv->pv_next = pv->pv_next;
784 pv->pv_next = npv;
785 pv = npv;
786 }
787 if (flags & PMAP_WIRED) {
788 PV_WIRE(pv);
789 pm->pm_stats.wired_count++;
790 }
791 splx(s);
792 return (1);
793 }
794
795 static void
796 pmap_remove_pv(struct pmap *pm, vaddr_t va, paddr_t pa)
797 {
798 struct pv_entry *pv, *npv;
799
800 /*
801 * Remove from the PV table.
802 */
803 pv = pa_to_pv(pa);
804 if (!pv)
805 return;
806
807 /*
808 * If it is the first entry on the list, it is actually
809 * in the header and we must copy the following entry up
810 * to the header. Otherwise we must search the list for
811 * the entry. In either case we free the now unused entry.
812 */
813 if (pm == pv->pv_pm && PV_CMPVA(va, pv)) {
814 if (PV_ISWIRED(pv)) {
815 pm->pm_stats.wired_count--;
816 }
817 if ((npv = pv->pv_next)) {
818 *pv = *npv;
819 pool_put(&pv_pool, npv);
820 } else
821 pv->pv_pm = NULL;
822 } else {
823 for (; (npv = pv->pv_next) != NULL; pv = npv)
824 if (pm == npv->pv_pm && PV_CMPVA(va, npv))
825 break;
826 if (npv) {
827 pv->pv_next = npv->pv_next;
828 if (PV_ISWIRED(npv)) {
829 pm->pm_stats.wired_count--;
830 }
831 pool_put(&pv_pool, npv);
832 }
833 }
834 }
835
836 /*
837 * Insert physical page at pa into the given pmap at virtual address va.
838 */
839 int
840 pmap_enter(struct pmap *pm, vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
841 {
842 int s;
843 u_int tte;
844 bool managed;
845
846 /*
847 * Have to remove any existing mapping first.
848 */
849 pmap_remove(pm, va, va + PAGE_SIZE);
850
851 if (flags & PMAP_WIRED)
852 flags |= prot;
853
854 managed = ((flags & PMAP_UNMANAGED) == 0) && uvm_pageismanaged(pa);
855
856 /*
857 * Generate TTE.
858 */
859 tte = TTE_PA(pa);
860 /* XXXX -- need to support multiple page sizes. */
861 tte |= TTE_SZ_16K;
862 #ifdef DIAGNOSTIC
863 if ((flags & (PME_NOCACHE | PME_WRITETHROUG)) ==
864 (PME_NOCACHE | PME_WRITETHROUG))
865 panic("pmap_enter: uncached & writethrough");
866 #endif
867 if (flags & PME_NOCACHE)
868 /* Must be I/O mapping */
869 tte |= TTE_I | TTE_G;
870 #ifdef PPC_4XX_NOCACHE
871 tte |= TTE_I;
872 #else
873 else if (flags & PME_WRITETHROUG)
874 /* Uncached and writethrough are not compatible */
875 tte |= TTE_W;
876 #endif
877 if (pm == pmap_kernel())
878 tte |= TTE_ZONE(ZONE_PRIV);
879 else
880 tte |= TTE_ZONE(ZONE_USER);
881
882 if (flags & VM_PROT_WRITE)
883 tte |= TTE_WR;
884
885 if (flags & VM_PROT_EXECUTE)
886 tte |= TTE_EX;
887
888 /*
889 * Now record mapping for later back-translation.
890 */
891 if (pmap_initialized && managed) {
892 char *attr;
893
894 if (!pmap_enter_pv(pm, va, pa, flags)) {
895 /* Could not enter pv on a managed page */
896 return 1;
897 }
898
899 /* Now set attributes. */
900 attr = pa_to_attr(pa);
901 #ifdef DIAGNOSTIC
902 if (!attr)
903 panic("managed but no attr");
904 #endif
905 if (flags & VM_PROT_ALL)
906 *attr |= PMAP_ATTR_REF;
907 if (flags & VM_PROT_WRITE)
908 *attr |= PMAP_ATTR_CHG;
909 }
910
911 s = splvm();
912
913 /* Insert page into page table. */
914 pte_enter(pm, va, tte);
915
916 /* If this is a real fault, enter it in the tlb */
917 if (tte && ((flags & PMAP_WIRED) == 0)) {
918 ppc4xx_tlb_enter(pm->pm_ctx, va, tte);
919 }
920 splx(s);
921
922 /* Flush the real memory from the instruction cache. */
923 if ((prot & VM_PROT_EXECUTE) && (tte & TTE_I) == 0)
924 __syncicache((void *)pa, PAGE_SIZE);
925
926 return 0;
927 }
928
929 void
930 pmap_unwire(struct pmap *pm, vaddr_t va)
931 {
932 struct pv_entry *pv;
933 paddr_t pa;
934 int s;
935
936 if (!pmap_extract(pm, va, &pa)) {
937 return;
938 }
939
940 pv = pa_to_pv(pa);
941 if (!pv)
942 return;
943
944 s = splvm();
945 while (pv != NULL) {
946 if (pm == pv->pv_pm && PV_CMPVA(va, pv)) {
947 if (PV_ISWIRED(pv)) {
948 PV_UNWIRE(pv);
949 pm->pm_stats.wired_count--;
950 }
951 break;
952 }
953 pv = pv->pv_next;
954 }
955 splx(s);
956 }
957
958 void
959 pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot, u_int flags)
960 {
961 int s;
962 u_int tte;
963 struct pmap *pm = pmap_kernel();
964
965 /*
966 * Have to remove any existing mapping first.
967 */
968
969 /*
970 * Generate TTE.
971 *
972 * XXXX
973 *
974 * Since the kernel does not handle execution privileges properly,
975 * we will handle read and execute permissions together.
976 */
977 tte = 0;
978 if (prot & VM_PROT_ALL) {
979
980 tte = TTE_PA(pa) | TTE_EX | TTE_ZONE(ZONE_PRIV);
981 /* XXXX -- need to support multiple page sizes. */
982 tte |= TTE_SZ_16K;
983 #ifdef DIAGNOSTIC
984 if ((prot & (PME_NOCACHE | PME_WRITETHROUG)) ==
985 (PME_NOCACHE | PME_WRITETHROUG))
986 panic("pmap_kenter_pa: uncached & writethrough");
987 #endif
988 if (prot & PME_NOCACHE)
989 /* Must be I/O mapping */
990 tte |= TTE_I | TTE_G;
991 #ifdef PPC_4XX_NOCACHE
992 tte |= TTE_I;
993 #else
994 else if (prot & PME_WRITETHROUG)
995 /* Uncached and writethrough are not compatible */
996 tte |= TTE_W;
997 #endif
998 if (prot & VM_PROT_WRITE)
999 tte |= TTE_WR;
1000 }
1001
1002 s = splvm();
1003
1004 /* Insert page into page table. */
1005 pte_enter(pm, va, tte);
1006 splx(s);
1007 }
1008
1009 void
1010 pmap_kremove(vaddr_t va, vsize_t len)
1011 {
1012
1013 while (len > 0) {
1014 pte_enter(pmap_kernel(), va, 0);
1015 va += PAGE_SIZE;
1016 len -= PAGE_SIZE;
1017 }
1018 }
1019
1020 /*
1021 * Remove the given range of mapping entries.
1022 */
1023 void
1024 pmap_remove(struct pmap *pm, vaddr_t va, vaddr_t endva)
1025 {
1026 int s;
1027 paddr_t pa;
1028 volatile u_int *ptp;
1029
1030 s = splvm();
1031 while (va < endva) {
1032
1033 if ((ptp = pte_find(pm, va)) && (pa = *ptp)) {
1034 pa = TTE_PA(pa);
1035 pmap_remove_pv(pm, va, pa);
1036 *ptp = 0;
1037 ppc4xx_tlb_flush(va, pm->pm_ctx);
1038 pm->pm_stats.resident_count--;
1039 }
1040 va += PAGE_SIZE;
1041 }
1042
1043 splx(s);
1044 }
1045
1046 /*
1047 * Convert the given kernel virtual address to the page frame
1048 * number (mmap cookie).
1049 */
1050 paddr_t
1051 pmap_mmap(vaddr_t addr, off_t off)
1052 {
1053
1054 return trunc_page((paddr_t)addr + off);
1055 }
1056
1057 /*
1058 * Get the physical page address for the given pmap/virtual address.
1059 */
1060 bool
1061 pmap_extract(struct pmap *pm, vaddr_t va, paddr_t *pap)
1062 {
1063 int seg = STIDX(va);
1064 int ptn = PTIDX(va);
1065 u_int pa = 0;
1066 int s;
1067
1068 s = splvm();
1069 if (pm->pm_ptbl[seg] && (pa = pm->pm_ptbl[seg][ptn])) {
1070 *pap = TTE_PA(pa) | (va & PGOFSET);
1071 }
1072 splx(s);
1073 return (pa != 0);
1074 }
1075
1076 /*
1077 * Lower the protection on the specified range of this pmap.
1078 *
1079 * There are only two cases: either the protection is going to 0,
1080 * or it is going to read-only.
1081 */
1082 void
1083 pmap_protect(struct pmap *pm, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
1084 {
1085 volatile u_int *ptp;
1086 int s, bic;
1087
1088 if ((prot & VM_PROT_READ) == 0) {
1089 pmap_remove(pm, sva, eva);
1090 return;
1091 }
1092 bic = 0;
1093 if ((prot & VM_PROT_WRITE) == 0) {
1094 bic |= TTE_WR;
1095 }
1096 if ((prot & VM_PROT_EXECUTE) == 0) {
1097 bic |= TTE_EX;
1098 }
1099 if (bic == 0) {
1100 return;
1101 }
1102 s = splvm();
1103 while (sva < eva) {
1104 if ((ptp = pte_find(pm, sva)) != NULL) {
1105 *ptp &= ~bic;
1106 ppc4xx_tlb_flush(sva, pm->pm_ctx);
1107 }
1108 sva += PAGE_SIZE;
1109 }
1110 splx(s);
1111 }
1112
1113 bool
1114 pmap_check_attr(struct vm_page *pg, u_int mask, int clear)
1115 {
1116 paddr_t pa;
1117 char *attr;
1118 int s, rv;
1119
1120 /*
1121 * First modify bits in cache.
1122 */
1123 pa = VM_PAGE_TO_PHYS(pg);
1124 attr = pa_to_attr(pa);
1125 if (attr == NULL)
1126 return false;
1127
1128 s = splvm();
1129 rv = ((*attr & mask) != 0);
1130 if (clear) {
1131 *attr &= ~mask;
1132 pmap_page_protect(pg, mask == PMAP_ATTR_CHG ? VM_PROT_READ : 0);
1133 }
1134 splx(s);
1135 return rv;
1136 }
1137
1138
1139 /*
1140 * Lower the protection on the specified physical page.
1141 *
1142 * There are only two cases: either the protection is going to 0,
1143 * or it is going to read-only.
1144 */
1145 void
1146 pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
1147 {
1148 paddr_t pa = VM_PAGE_TO_PHYS(pg);
1149 vaddr_t va;
1150 struct pv_entry *pvh, *pv, *npv;
1151 struct pmap *pm;
1152
1153 pvh = pa_to_pv(pa);
1154 if (pvh == NULL)
1155 return;
1156
1157 /* Handle extra pvs which may be deleted in the operation */
1158 for (pv = pvh->pv_next; pv; pv = npv) {
1159 npv = pv->pv_next;
1160
1161 pm = pv->pv_pm;
1162 va = pv->pv_va;
1163 pmap_protect(pm, va, va + PAGE_SIZE, prot);
1164 }
1165 /* Now check the head pv */
1166 if (pvh->pv_pm) {
1167 pv = pvh;
1168 pm = pv->pv_pm;
1169 va = pv->pv_va;
1170 pmap_protect(pm, va, va + PAGE_SIZE, prot);
1171 }
1172 }
1173
1174 /*
1175 * Activate the address space for the specified process. If the process
1176 * is the current process, load the new MMU context.
1177 */
1178 void
1179 pmap_activate(struct lwp *l)
1180 {
1181 #if 0
1182 struct pcb *pcb = &l->l_proc->p_addr->u_pcb;
1183 pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap;
1184
1185 /*
1186 * XXX Normally performed in cpu_lwp_fork().
1187 */
1188 printf("pmap_activate(%p), pmap=%p\n",l,pmap);
1189 pcb->pcb_pm = pmap;
1190 #endif
1191 }
1192
1193 /*
1194 * Deactivate the specified process's address space.
1195 */
1196 void
1197 pmap_deactivate(struct lwp *l)
1198 {
1199 }
1200
1201 /*
1202 * Synchronize caches corresponding to [addr, addr+len) in p.
1203 */
1204 void
1205 pmap_procwr(struct proc *p, vaddr_t va, size_t len)
1206 {
1207 struct pmap *pm = p->p_vmspace->vm_map.pmap;
1208 int msr, ctx, opid, step;
1209
1210 step = CACHELINESIZE;
1211
1212 /*
1213 * Need to turn off IMMU and switch to user context.
1214 * (icbi uses DMMU).
1215 */
1216 if (!(ctx = pm->pm_ctx)) {
1217 /* No context -- assign it one */
1218 ctx_alloc(pm);
1219 ctx = pm->pm_ctx;
1220 }
1221 __asm volatile("mfmsr %0;"
1222 "li %1, %7;"
1223 "andc %1,%0,%1;"
1224 "mtmsr %1;"
1225 "sync;isync;"
1226 "mfpid %1;"
1227 "mtpid %2;"
1228 "sync; isync;"
1229 "1:"
1230 "dcbf 0,%3;"
1231 "icbi 0,%3;"
1232 "add %3,%3,%5;"
1233 "addc. %4,%4,%6;"
1234 "bge 1b;"
1235 "mtpid %1;"
1236 "mtmsr %0;"
1237 "sync; isync"
1238 : "=&r" (msr), "=&r" (opid)
1239 : "r" (ctx), "r" (va), "r" (len), "r" (step), "r" (-step),
1240 "K" (PSL_IR | PSL_DR));
1241 }
1242
1243
1244 /* This has to be done in real mode !!! */
1245 void
1246 ppc4xx_tlb_flush(vaddr_t va, int pid)
1247 {
1248 u_long i, found;
1249 u_long msr;
1250
1251 /* If there's no context then it can't be mapped. */
1252 if (!pid)
1253 return;
1254
1255 __asm( "mfpid %1;" /* Save PID */
1256 "mfmsr %2;" /* Save MSR */
1257 "li %0,0;" /* Now clear MSR */
1258 "mtmsr %0;"
1259 "mtpid %4;" /* Set PID */
1260 "sync;"
1261 "tlbsx. %0,0,%3;" /* Search TLB */
1262 "sync;"
1263 "mtpid %1;" /* Restore PID */
1264 "mtmsr %2;" /* Restore MSR */
1265 "sync;isync;"
1266 "li %1,1;"
1267 "beq 1f;"
1268 "li %1,0;"
1269 "1:"
1270 : "=&r" (i), "=&r" (found), "=&r" (msr)
1271 : "r" (va), "r" (pid));
1272 if (found && !TLB_LOCKED(i)) {
1273
1274 /* Now flush translation */
1275 __asm volatile(
1276 "tlbwe %0,%1,0;"
1277 "sync;isync;"
1278 : : "r" (0), "r" (i));
1279
1280 tlb_info[i].ti_ctx = 0;
1281 tlb_info[i].ti_flags = 0;
1282 tlbnext = i;
1283 /* Successful flushes */
1284 tlbflush_ev.ev_count++;
1285 }
1286 }
1287
1288 void
1289 ppc4xx_tlb_flush_all(void)
1290 {
1291 u_long i;
1292
1293 for (i = 0; i < NTLB; i++)
1294 if (!TLB_LOCKED(i)) {
1295 __asm volatile(
1296 "tlbwe %0,%1,0;"
1297 "sync;isync;"
1298 : : "r" (0), "r" (i));
1299 tlb_info[i].ti_ctx = 0;
1300 tlb_info[i].ti_flags = 0;
1301 }
1302
1303 __asm volatile("sync;isync");
1304 }
1305
1306 /* Find a TLB entry to evict. */
1307 static int
1308 ppc4xx_tlb_find_victim(void)
1309 {
1310 int flags;
1311
1312 for (;;) {
1313 if (++tlbnext >= NTLB)
1314 tlbnext = tlb_nreserved;
1315 flags = tlb_info[tlbnext].ti_flags;
1316 if (!(flags & TLBF_USED) ||
1317 (flags & (TLBF_LOCKED | TLBF_REF)) == 0) {
1318 u_long va, stack = (u_long)&va;
1319
1320 if (!((tlb_info[tlbnext].ti_va ^ stack) & (~PGOFSET)) &&
1321 (tlb_info[tlbnext].ti_ctx == KERNEL_PID) &&
1322 (flags & TLBF_USED)) {
1323 /* Kernel stack page */
1324 flags |= TLBF_USED;
1325 tlb_info[tlbnext].ti_flags = flags;
1326 } else {
1327 /* Found it! */
1328 return (tlbnext);
1329 }
1330 } else {
1331 tlb_info[tlbnext].ti_flags = (flags & ~TLBF_REF);
1332 }
1333 }
1334 }
1335
1336 void
1337 ppc4xx_tlb_enter(int ctx, vaddr_t va, u_int pte)
1338 {
1339 u_long th, tl, idx;
1340 tlbpid_t pid;
1341 u_short msr;
1342 paddr_t pa;
1343 int s, sz;
1344
1345 tlbenter_ev.ev_count++;
1346
1347 sz = (pte & TTE_SZ_MASK) >> TTE_SZ_SHIFT;
1348 pa = (pte & TTE_RPN_MASK(sz));
1349 th = (va & TLB_EPN_MASK) | (sz << TLB_SIZE_SHFT) | TLB_VALID;
1350 tl = (pte & ~TLB_RPN_MASK) | pa;
1351 tl |= ppc4xx_tlbflags(va, pa);
1352
1353 s = splhigh();
1354 idx = ppc4xx_tlb_find_victim();
1355
1356 #ifdef DIAGNOSTIC
1357 if ((idx < tlb_nreserved) || (idx >= NTLB)) {
1358 panic("ppc4xx_tlb_enter: replacing entry %ld", idx);
1359 }
1360 #endif
1361
1362 tlb_info[idx].ti_va = (va & TLB_EPN_MASK);
1363 tlb_info[idx].ti_ctx = ctx;
1364 tlb_info[idx].ti_flags = TLBF_USED | TLBF_REF;
1365
1366 __asm volatile(
1367 "mfmsr %0;" /* Save MSR */
1368 "li %1,0;"
1369 "tlbwe %1,%3,0;" /* Invalidate old entry. */
1370 "mtmsr %1;" /* Clear MSR */
1371 "mfpid %1;" /* Save old PID */
1372 "mtpid %2;" /* Load translation ctx */
1373 "sync; isync;"
1374 #ifdef DEBUG
1375 "andi. %3,%3,63;"
1376 "tweqi %3,0;" /* XXXXX DEBUG trap on index 0 */
1377 #endif
1378 "tlbwe %4,%3,1; tlbwe %5,%3,0;" /* Set TLB */
1379 "sync; isync;"
1380 "mtpid %1; mtmsr %0;" /* Restore PID and MSR */
1381 "sync; isync;"
1382 : "=&r" (msr), "=&r" (pid)
1383 : "r" (ctx), "r" (idx), "r" (tl), "r" (th));
1384 splx(s);
1385 }
1386
1387 void
1388 ppc4xx_tlb_init(void)
1389 {
1390 int i;
1391
1392 /* Mark reserved TLB entries */
1393 for (i = 0; i < tlb_nreserved; i++) {
1394 tlb_info[i].ti_flags = TLBF_LOCKED | TLBF_USED;
1395 tlb_info[i].ti_ctx = KERNEL_PID;
1396 }
1397
1398 /* Setup security zones */
1399 /* Z0 - accessible by kernel only if TLB entry permissions allow
1400 * Z1,Z2 - access is controlled by TLB entry permissions
1401 * Z3 - full access regardless of TLB entry permissions
1402 */
1403
1404 __asm volatile(
1405 "mtspr %0,%1;"
1406 "sync;"
1407 :: "K"(SPR_ZPR), "r" (0x1b000000));
1408 }
1409
1410 /*
1411 * ppc4xx_tlb_size_mask:
1412 *
1413 * Roundup size to supported page size, return TLBHI mask and real size.
1414 */
1415 static int
1416 ppc4xx_tlb_size_mask(size_t size, int *mask, int *rsiz)
1417 {
1418 int i;
1419
1420 for (i = 0; i < __arraycount(tlbsize); i++)
1421 if (size <= tlbsize[i]) {
1422 *mask = (i << TLB_SIZE_SHFT);
1423 *rsiz = tlbsize[i];
1424 return (0);
1425 }
1426 return (EINVAL);
1427 }
1428
1429 /*
1430 * ppc4xx_tlb_mapiodev:
1431 *
1432 * Lookup virtual address of mapping previously entered via
1433 * ppc4xx_tlb_reserve. Search TLB directly so that we don't
1434 * need to waste extra storage for reserved mappings. Note
1435 * that reading TLBHI also sets PID, but all reserved mappings
1436 * use KERNEL_PID, so the side effect is nil.
1437 */
1438 void *
1439 ppc4xx_tlb_mapiodev(paddr_t base, psize_t len)
1440 {
1441 paddr_t pa;
1442 vaddr_t va;
1443 u_int lo, hi, sz;
1444 int i;
1445
1446 /* tlb_nreserved is only allowed to grow, so this is safe. */
1447 for (i = 0; i < tlb_nreserved; i++) {
1448 __asm volatile (
1449 " tlbre %0,%2,1 \n" /* TLBLO */
1450 " tlbre %1,%2,0 \n" /* TLBHI */
1451 : "=&r" (lo), "=&r" (hi)
1452 : "r" (i));
1453
1454 KASSERT(hi & TLB_VALID);
1455 KASSERT(mfspr(SPR_PID) == KERNEL_PID);
1456
1457 pa = (lo & TLB_RPN_MASK);
1458 if (base < pa)
1459 continue;
1460
1461 sz = tlbsize[(hi & TLB_SIZE_MASK) >> TLB_SIZE_SHFT];
1462 if ((base + len) > (pa + sz))
1463 continue;
1464
1465 va = (hi & TLB_EPN_MASK) + (base & (sz - 1)); /* sz = 2^n */
1466 return (void *)(va);
1467 }
1468
1469 return (NULL);
1470 }
1471
1472 /*
1473 * ppc4xx_tlb_reserve:
1474 *
1475 * Map physical range to kernel virtual chunk via reserved TLB entry.
1476 */
1477 void
1478 ppc4xx_tlb_reserve(paddr_t pa, vaddr_t va, size_t size, int flags)
1479 {
1480 u_int lo, hi;
1481 int szmask, rsize;
1482
1483 /* Called before pmap_bootstrap(), va outside kernel space. */
1484 KASSERT(va < VM_MIN_KERNEL_ADDRESS || va >= VM_MAX_KERNEL_ADDRESS);
1485 KASSERT(! pmap_bootstrap_done);
1486 KASSERT(tlb_nreserved < NTLB);
1487
1488 /* Resolve size. */
1489 if (ppc4xx_tlb_size_mask(size, &szmask, &rsize) != 0)
1490 panic("ppc4xx_tlb_reserve: entry %d, %zuB too large",
1491 size, tlb_nreserved);
1492
1493 /* Real size will be power of two >= 1024, so this is OK. */
1494 pa &= ~(rsize - 1); /* RPN */
1495 va &= ~(rsize - 1); /* EPN */
1496
1497 lo = pa | TLB_WR | flags;
1498 hi = va | TLB_VALID | szmask;
1499
1500 #ifdef PPC_4XX_NOCACHE
1501 lo |= TLB_I;
1502 #endif
1503
1504 __asm volatile(
1505 " tlbwe %1,%0,1 \n" /* write TLBLO */
1506 " tlbwe %2,%0,0 \n" /* write TLBHI */
1507 " sync \n"
1508 " isync \n"
1509 : : "r" (tlb_nreserved), "r" (lo), "r" (hi));
1510
1511 tlb_nreserved++;
1512 }
1513
1514 /*
1515 * We should pass the ctx in from trap code.
1516 */
1517 int
1518 pmap_tlbmiss(vaddr_t va, int ctx)
1519 {
1520 volatile u_int *pte;
1521 u_long tte;
1522
1523 tlbmiss_ev.ev_count++;
1524
1525 /*
1526 * We will reserve 0 upto VM_MIN_KERNEL_ADDRESS for va == pa mappings.
1527 * Physical RAM is expected to live in this range, care must be taken
1528 * to not clobber 0 upto ${physmem} with device mappings in machdep
1529 * code.
1530 */
1531 if (ctx != KERNEL_PID ||
1532 (va >= VM_MIN_KERNEL_ADDRESS && va < VM_MAX_KERNEL_ADDRESS)) {
1533 pte = pte_find((struct pmap *)__UNVOLATILE(ctxbusy[ctx]), va);
1534 if (pte == NULL) {
1535 /* Map unmanaged addresses directly for kernel access */
1536 return 1;
1537 }
1538 tte = *pte;
1539 if (tte == 0) {
1540 return 1;
1541 }
1542 } else {
1543 /* Create a 16MB writable mapping. */
1544 #ifdef PPC_4XX_NOCACHE
1545 tte = TTE_PA(va) | TTE_ZONE(ZONE_PRIV) | TTE_SZ_16M | TTE_I |TTE_WR;
1546 #else
1547 tte = TTE_PA(va) | TTE_ZONE(ZONE_PRIV) | TTE_SZ_16M | TTE_WR;
1548 #endif
1549 }
1550 tlbhit_ev.ev_count++;
1551 ppc4xx_tlb_enter(ctx, va, tte);
1552
1553 return 0;
1554 }
1555
1556 /*
1557 * Flush all the entries matching a context from the TLB.
1558 */
1559 static int
1560 ctx_flush(int cnum)
1561 {
1562 int i;
1563
1564 /* We gotta steal this context */
1565 for (i = tlb_nreserved; i < NTLB; i++) {
1566 if (tlb_info[i].ti_ctx == cnum) {
1567 /* Can't steal ctx if it has a locked entry. */
1568 if (TLB_LOCKED(i)) {
1569 #ifdef DIAGNOSTIC
1570 printf("ctx_flush: can't invalidate "
1571 "locked mapping %d "
1572 "for context %d\n", i, cnum);
1573 #ifdef DDB
1574 Debugger();
1575 #endif
1576 #endif
1577 return (1);
1578 }
1579 #ifdef DIAGNOSTIC
1580 if (i < tlb_nreserved)
1581 panic("TLB entry %d not locked", i);
1582 #endif
1583 /* Invalidate particular TLB entry regardless of locked status */
1584 __asm volatile("tlbwe %0,%1,0" : :"r"(0),"r"(i));
1585 tlb_info[i].ti_flags = 0;
1586 }
1587 }
1588 return (0);
1589 }
1590
1591 /*
1592 * Allocate a context. If necessary, steal one from someone else.
1593 *
1594 * The new context is flushed from the TLB before returning.
1595 */
1596 int
1597 ctx_alloc(struct pmap *pm)
1598 {
1599 int s, cnum;
1600 static int next = MINCTX;
1601
1602 if (pm == pmap_kernel()) {
1603 #ifdef DIAGNOSTIC
1604 printf("ctx_alloc: kernel pmap!\n");
1605 #endif
1606 return (0);
1607 }
1608 s = splvm();
1609
1610 /* Find a likely context. */
1611 cnum = next;
1612 do {
1613 if ((++cnum) > NUMCTX)
1614 cnum = MINCTX;
1615 } while (ctxbusy[cnum] != NULL && cnum != next);
1616
1617 /* Now clean it out */
1618 oops:
1619 if (cnum < MINCTX)
1620 cnum = MINCTX; /* Never steal ctx 0 or 1 */
1621 if (ctx_flush(cnum)) {
1622 /* oops -- something's wired. */
1623 if ((++cnum) > NUMCTX)
1624 cnum = MINCTX;
1625 goto oops;
1626 }
1627
1628 if (ctxbusy[cnum]) {
1629 #ifdef DEBUG
1630 /* We should identify this pmap and clear it */
1631 printf("Warning: stealing context %d\n", cnum);
1632 #endif
1633 ctxbusy[cnum]->pm_ctx = 0;
1634 }
1635 ctxbusy[cnum] = pm;
1636 next = cnum;
1637 splx(s);
1638 pm->pm_ctx = cnum;
1639
1640 return cnum;
1641 }
1642
1643 /*
1644 * Give away a context.
1645 */
1646 void
1647 ctx_free(struct pmap *pm)
1648 {
1649 int oldctx;
1650
1651 oldctx = pm->pm_ctx;
1652
1653 if (oldctx == 0)
1654 panic("ctx_free: freeing kernel context");
1655 #ifdef DIAGNOSTIC
1656 if (ctxbusy[oldctx] == 0)
1657 printf("ctx_free: freeing free context %d\n", oldctx);
1658 if (ctxbusy[oldctx] != pm) {
1659 printf("ctx_free: freeing someone esle's context\n "
1660 "ctxbusy[%d] = %p, pm->pm_ctx = %p\n",
1661 oldctx, (void *)(u_long)ctxbusy[oldctx], pm);
1662 #ifdef DDB
1663 Debugger();
1664 #endif
1665 }
1666 #endif
1667 /* We should verify it has not been stolen and reallocated... */
1668 ctxbusy[oldctx] = NULL;
1669 ctx_flush(oldctx);
1670 }
1671
1672
1673 #ifdef DEBUG
1674 /*
1675 * Test ref/modify handling.
1676 */
1677 void pmap_testout(void);
1678 void
1679 pmap_testout(void)
1680 {
1681 vaddr_t va;
1682 volatile int *loc;
1683 int val = 0;
1684 paddr_t pa;
1685 struct vm_page *pg;
1686 int ref, mod;
1687
1688 /* Allocate a page */
1689 va = (vaddr_t)uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
1690 UVM_KMF_WIRED | UVM_KMF_ZERO);
1691 loc = (int*)va;
1692
1693 pmap_extract(pmap_kernel(), va, &pa);
1694 pg = PHYS_TO_VM_PAGE(pa);
1695 pmap_unwire(pmap_kernel(), va);
1696
1697 pmap_kremove(va, PAGE_SIZE);
1698 pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
1699 pmap_update(pmap_kernel());
1700
1701 /* Now clear reference and modify */
1702 ref = pmap_clear_reference(pg);
1703 mod = pmap_clear_modify(pg);
1704 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1705 (void *)(u_long)va, (long)pa,
1706 ref, mod);
1707
1708 /* Check it's properly cleared */
1709 ref = pmap_is_referenced(pg);
1710 mod = pmap_is_modified(pg);
1711 printf("Checking cleared page: ref %d, mod %d\n",
1712 ref, mod);
1713
1714 /* Reference page */
1715 val = *loc;
1716
1717 ref = pmap_is_referenced(pg);
1718 mod = pmap_is_modified(pg);
1719 printf("Referenced page: ref %d, mod %d val %x\n",
1720 ref, mod, val);
1721
1722 /* Now clear reference and modify */
1723 ref = pmap_clear_reference(pg);
1724 mod = pmap_clear_modify(pg);
1725 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1726 (void *)(u_long)va, (long)pa,
1727 ref, mod);
1728
1729 /* Modify page */
1730 *loc = 1;
1731
1732 ref = pmap_is_referenced(pg);
1733 mod = pmap_is_modified(pg);
1734 printf("Modified page: ref %d, mod %d\n",
1735 ref, mod);
1736
1737 /* Now clear reference and modify */
1738 ref = pmap_clear_reference(pg);
1739 mod = pmap_clear_modify(pg);
1740 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1741 (void *)(u_long)va, (long)pa,
1742 ref, mod);
1743
1744 /* Check it's properly cleared */
1745 ref = pmap_is_referenced(pg);
1746 mod = pmap_is_modified(pg);
1747 printf("Checking cleared page: ref %d, mod %d\n",
1748 ref, mod);
1749
1750 /* Modify page */
1751 *loc = 1;
1752
1753 ref = pmap_is_referenced(pg);
1754 mod = pmap_is_modified(pg);
1755 printf("Modified page: ref %d, mod %d\n",
1756 ref, mod);
1757
1758 /* Check pmap_protect() */
1759 pmap_protect(pmap_kernel(), va, va+1, VM_PROT_READ);
1760 pmap_update(pmap_kernel());
1761 ref = pmap_is_referenced(pg);
1762 mod = pmap_is_modified(pg);
1763 printf("pmap_protect(VM_PROT_READ): ref %d, mod %d\n",
1764 ref, mod);
1765
1766 /* Now clear reference and modify */
1767 ref = pmap_clear_reference(pg);
1768 mod = pmap_clear_modify(pg);
1769 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1770 (void *)(u_long)va, (long)pa,
1771 ref, mod);
1772
1773 /* Reference page */
1774 val = *loc;
1775
1776 ref = pmap_is_referenced(pg);
1777 mod = pmap_is_modified(pg);
1778 printf("Referenced page: ref %d, mod %d val %x\n",
1779 ref, mod, val);
1780
1781 /* Now clear reference and modify */
1782 ref = pmap_clear_reference(pg);
1783 mod = pmap_clear_modify(pg);
1784 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1785 (void *)(u_long)va, (long)pa,
1786 ref, mod);
1787
1788 /* Modify page */
1789 #if 0
1790 pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
1791 pmap_update(pmap_kernel());
1792 #endif
1793 *loc = 1;
1794
1795 ref = pmap_is_referenced(pg);
1796 mod = pmap_is_modified(pg);
1797 printf("Modified page: ref %d, mod %d\n",
1798 ref, mod);
1799
1800 /* Check pmap_protect() */
1801 pmap_protect(pmap_kernel(), va, va+1, VM_PROT_NONE);
1802 pmap_update(pmap_kernel());
1803 ref = pmap_is_referenced(pg);
1804 mod = pmap_is_modified(pg);
1805 printf("pmap_protect(): ref %d, mod %d\n",
1806 ref, mod);
1807
1808 /* Now clear reference and modify */
1809 ref = pmap_clear_reference(pg);
1810 mod = pmap_clear_modify(pg);
1811 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1812 (void *)(u_long)va, (long)pa,
1813 ref, mod);
1814
1815 /* Reference page */
1816 val = *loc;
1817
1818 ref = pmap_is_referenced(pg);
1819 mod = pmap_is_modified(pg);
1820 printf("Referenced page: ref %d, mod %d val %x\n",
1821 ref, mod, val);
1822
1823 /* Now clear reference and modify */
1824 ref = pmap_clear_reference(pg);
1825 mod = pmap_clear_modify(pg);
1826 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1827 (void *)(u_long)va, (long)pa,
1828 ref, mod);
1829
1830 /* Modify page */
1831 #if 0
1832 pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
1833 pmap_update(pmap_kernel());
1834 #endif
1835 *loc = 1;
1836
1837 ref = pmap_is_referenced(pg);
1838 mod = pmap_is_modified(pg);
1839 printf("Modified page: ref %d, mod %d\n",
1840 ref, mod);
1841
1842 /* Check pmap_pag_protect() */
1843 pmap_page_protect(pg, VM_PROT_READ);
1844 ref = pmap_is_referenced(pg);
1845 mod = pmap_is_modified(pg);
1846 printf("pmap_page_protect(VM_PROT_READ): ref %d, mod %d\n",
1847 ref, mod);
1848
1849 /* Now clear reference and modify */
1850 ref = pmap_clear_reference(pg);
1851 mod = pmap_clear_modify(pg);
1852 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1853 (void *)(u_long)va, (long)pa,
1854 ref, mod);
1855
1856 /* Reference page */
1857 val = *loc;
1858
1859 ref = pmap_is_referenced(pg);
1860 mod = pmap_is_modified(pg);
1861 printf("Referenced page: ref %d, mod %d val %x\n",
1862 ref, mod, val);
1863
1864 /* Now clear reference and modify */
1865 ref = pmap_clear_reference(pg);
1866 mod = pmap_clear_modify(pg);
1867 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1868 (void *)(u_long)va, (long)pa,
1869 ref, mod);
1870
1871 /* Modify page */
1872 #if 0
1873 pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
1874 pmap_update(pmap_kernel());
1875 #endif
1876 *loc = 1;
1877
1878 ref = pmap_is_referenced(pg);
1879 mod = pmap_is_modified(pg);
1880 printf("Modified page: ref %d, mod %d\n",
1881 ref, mod);
1882
1883 /* Check pmap_pag_protect() */
1884 pmap_page_protect(pg, VM_PROT_NONE);
1885 ref = pmap_is_referenced(pg);
1886 mod = pmap_is_modified(pg);
1887 printf("pmap_page_protect(): ref %d, mod %d\n",
1888 ref, mod);
1889
1890 /* Now clear reference and modify */
1891 ref = pmap_clear_reference(pg);
1892 mod = pmap_clear_modify(pg);
1893 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1894 (void *)(u_long)va, (long)pa,
1895 ref, mod);
1896
1897
1898 /* Reference page */
1899 val = *loc;
1900
1901 ref = pmap_is_referenced(pg);
1902 mod = pmap_is_modified(pg);
1903 printf("Referenced page: ref %d, mod %d val %x\n",
1904 ref, mod, val);
1905
1906 /* Now clear reference and modify */
1907 ref = pmap_clear_reference(pg);
1908 mod = pmap_clear_modify(pg);
1909 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1910 (void *)(u_long)va, (long)pa,
1911 ref, mod);
1912
1913 /* Modify page */
1914 #if 0
1915 pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
1916 pmap_update(pmap_kernel());
1917 #endif
1918 *loc = 1;
1919
1920 ref = pmap_is_referenced(pg);
1921 mod = pmap_is_modified(pg);
1922 printf("Modified page: ref %d, mod %d\n",
1923 ref, mod);
1924
1925 /* Unmap page */
1926 pmap_remove(pmap_kernel(), va, va+1);
1927 pmap_update(pmap_kernel());
1928 ref = pmap_is_referenced(pg);
1929 mod = pmap_is_modified(pg);
1930 printf("Unmapped page: ref %d, mod %d\n", ref, mod);
1931
1932 /* Now clear reference and modify */
1933 ref = pmap_clear_reference(pg);
1934 mod = pmap_clear_modify(pg);
1935 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1936 (void *)(u_long)va, (long)pa, ref, mod);
1937
1938 /* Check it's properly cleared */
1939 ref = pmap_is_referenced(pg);
1940 mod = pmap_is_modified(pg);
1941 printf("Checking cleared page: ref %d, mod %d\n",
1942 ref, mod);
1943
1944 pmap_remove(pmap_kernel(), va, va + PAGE_SIZE);
1945 pmap_kenter_pa(va, pa, VM_PROT_ALL, 0);
1946 uvm_km_free(kernel_map, (vaddr_t)va, PAGE_SIZE, UVM_KMF_WIRED);
1947 }
1948 #endif
1949