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