]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sparc64/sparc64/pmap.c
Merge OpenSSL 1.0.2m.
[FreeBSD/FreeBSD.git] / sys / sparc64 / sparc64 / pmap.c
1 /*-
2  * Copyright (c) 1991 Regents of the University of California.
3  * All rights reserved.
4  * Copyright (c) 1994 John S. Dyson
5  * All rights reserved.
6  * Copyright (c) 1994 David Greenman
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * the Systems Programming Group of the University of Utah Computer
11  * Science Department and William Jolitz of UUNET Technologies Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *      from:   @(#)pmap.c      7.7 (Berkeley)  5/12/91
38  */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 /*
44  * Manages physical address maps.
45  *
46  * Since the information managed by this module is also stored by the
47  * logical address mapping module, this module may throw away valid virtual
48  * to physical mappings at almost any time.  However, invalidations of
49  * mappings must be done as requested.
50  *
51  * In order to cope with hardware architectures which make virtual to
52  * physical map invalidates expensive, this module may delay invalidate
53  * reduced protection operations until such time as they are actually
54  * necessary.  This module is given full information as to which processors
55  * are currently using which maps, and to when physical maps must be made
56  * correct.
57  */
58
59 #include "opt_kstack_pages.h"
60 #include "opt_pmap.h"
61
62 #include <sys/param.h>
63 #include <sys/kernel.h>
64 #include <sys/ktr.h>
65 #include <sys/lock.h>
66 #include <sys/msgbuf.h>
67 #include <sys/mutex.h>
68 #include <sys/proc.h>
69 #include <sys/rwlock.h>
70 #include <sys/smp.h>
71 #include <sys/sysctl.h>
72 #include <sys/systm.h>
73 #include <sys/vmmeter.h>
74
75 #include <dev/ofw/openfirm.h>
76
77 #include <vm/vm.h>
78 #include <vm/vm_param.h>
79 #include <vm/vm_kern.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_map.h>
82 #include <vm/vm_object.h>
83 #include <vm/vm_extern.h>
84 #include <vm/vm_pageout.h>
85 #include <vm/vm_pager.h>
86 #include <vm/vm_phys.h>
87
88 #include <machine/cache.h>
89 #include <machine/frame.h>
90 #include <machine/instr.h>
91 #include <machine/md_var.h>
92 #include <machine/metadata.h>
93 #include <machine/ofw_mem.h>
94 #include <machine/smp.h>
95 #include <machine/tlb.h>
96 #include <machine/tte.h>
97 #include <machine/tsb.h>
98 #include <machine/ver.h>
99
100 /*
101  * Virtual address of message buffer
102  */
103 struct msgbuf *msgbufp;
104
105 /*
106  * Map of physical memory reagions
107  */
108 vm_paddr_t phys_avail[128];
109 static struct ofw_mem_region mra[128];
110 struct ofw_mem_region sparc64_memreg[128];
111 int sparc64_nmemreg;
112 static struct ofw_map translations[128];
113 static int translations_size;
114
115 static vm_offset_t pmap_idle_map;
116 static vm_offset_t pmap_temp_map_1;
117 static vm_offset_t pmap_temp_map_2;
118
119 /*
120  * First and last available kernel virtual addresses
121  */
122 vm_offset_t virtual_avail;
123 vm_offset_t virtual_end;
124 vm_offset_t kernel_vm_end;
125
126 vm_offset_t vm_max_kernel_address;
127
128 /*
129  * Kernel pmap
130  */
131 struct pmap kernel_pmap_store;
132
133 struct rwlock_padalign tte_list_global_lock;
134
135 /*
136  * Allocate physical memory for use in pmap_bootstrap.
137  */
138 static vm_paddr_t pmap_bootstrap_alloc(vm_size_t size, uint32_t colors);
139
140 static void pmap_bootstrap_set_tte(struct tte *tp, u_long vpn, u_long data);
141 static void pmap_cache_remove(vm_page_t m, vm_offset_t va);
142 static int pmap_protect_tte(struct pmap *pm1, struct pmap *pm2,
143     struct tte *tp, vm_offset_t va);
144 static int pmap_unwire_tte(pmap_t pm, pmap_t pm2, struct tte *tp,
145     vm_offset_t va);
146 static void pmap_init_qpages(void);
147
148 /*
149  * Map the given physical page at the specified virtual address in the
150  * target pmap with the protection requested.  If specified the page
151  * will be wired down.
152  *
153  * The page queues and pmap must be locked.
154  */
155 static int pmap_enter_locked(pmap_t pm, vm_offset_t va, vm_page_t m,
156     vm_prot_t prot, u_int flags, int8_t psind);
157
158 extern int tl1_dmmu_miss_direct_patch_tsb_phys_1[];
159 extern int tl1_dmmu_miss_direct_patch_tsb_phys_end_1[];
160 extern int tl1_dmmu_miss_patch_asi_1[];
161 extern int tl1_dmmu_miss_patch_quad_ldd_1[];
162 extern int tl1_dmmu_miss_patch_tsb_1[];
163 extern int tl1_dmmu_miss_patch_tsb_2[];
164 extern int tl1_dmmu_miss_patch_tsb_mask_1[];
165 extern int tl1_dmmu_miss_patch_tsb_mask_2[];
166 extern int tl1_dmmu_prot_patch_asi_1[];
167 extern int tl1_dmmu_prot_patch_quad_ldd_1[];
168 extern int tl1_dmmu_prot_patch_tsb_1[];
169 extern int tl1_dmmu_prot_patch_tsb_2[];
170 extern int tl1_dmmu_prot_patch_tsb_mask_1[];
171 extern int tl1_dmmu_prot_patch_tsb_mask_2[];
172 extern int tl1_immu_miss_patch_asi_1[];
173 extern int tl1_immu_miss_patch_quad_ldd_1[];
174 extern int tl1_immu_miss_patch_tsb_1[];
175 extern int tl1_immu_miss_patch_tsb_2[];
176 extern int tl1_immu_miss_patch_tsb_mask_1[];
177 extern int tl1_immu_miss_patch_tsb_mask_2[];
178
179 /*
180  * If user pmap is processed with pmap_remove and with pmap_remove and the
181  * resident count drops to 0, there are no more pages to remove, so we
182  * need not continue.
183  */
184 #define PMAP_REMOVE_DONE(pm) \
185         ((pm) != kernel_pmap && (pm)->pm_stats.resident_count == 0)
186
187 /*
188  * The threshold (in bytes) above which tsb_foreach() is used in pmap_remove()
189  * and pmap_protect() instead of trying each virtual address.
190  */
191 #define PMAP_TSB_THRESH ((TSB_SIZE / 2) * PAGE_SIZE)
192
193 SYSCTL_NODE(_debug, OID_AUTO, pmap_stats, CTLFLAG_RD, 0, "");
194
195 PMAP_STATS_VAR(pmap_nenter);
196 PMAP_STATS_VAR(pmap_nenter_update);
197 PMAP_STATS_VAR(pmap_nenter_replace);
198 PMAP_STATS_VAR(pmap_nenter_new);
199 PMAP_STATS_VAR(pmap_nkenter);
200 PMAP_STATS_VAR(pmap_nkenter_oc);
201 PMAP_STATS_VAR(pmap_nkenter_stupid);
202 PMAP_STATS_VAR(pmap_nkremove);
203 PMAP_STATS_VAR(pmap_nqenter);
204 PMAP_STATS_VAR(pmap_nqremove);
205 PMAP_STATS_VAR(pmap_ncache_enter);
206 PMAP_STATS_VAR(pmap_ncache_enter_c);
207 PMAP_STATS_VAR(pmap_ncache_enter_oc);
208 PMAP_STATS_VAR(pmap_ncache_enter_cc);
209 PMAP_STATS_VAR(pmap_ncache_enter_coc);
210 PMAP_STATS_VAR(pmap_ncache_enter_nc);
211 PMAP_STATS_VAR(pmap_ncache_enter_cnc);
212 PMAP_STATS_VAR(pmap_ncache_remove);
213 PMAP_STATS_VAR(pmap_ncache_remove_c);
214 PMAP_STATS_VAR(pmap_ncache_remove_oc);
215 PMAP_STATS_VAR(pmap_ncache_remove_cc);
216 PMAP_STATS_VAR(pmap_ncache_remove_coc);
217 PMAP_STATS_VAR(pmap_ncache_remove_nc);
218 PMAP_STATS_VAR(pmap_nzero_page);
219 PMAP_STATS_VAR(pmap_nzero_page_c);
220 PMAP_STATS_VAR(pmap_nzero_page_oc);
221 PMAP_STATS_VAR(pmap_nzero_page_nc);
222 PMAP_STATS_VAR(pmap_nzero_page_area);
223 PMAP_STATS_VAR(pmap_nzero_page_area_c);
224 PMAP_STATS_VAR(pmap_nzero_page_area_oc);
225 PMAP_STATS_VAR(pmap_nzero_page_area_nc);
226 PMAP_STATS_VAR(pmap_ncopy_page);
227 PMAP_STATS_VAR(pmap_ncopy_page_c);
228 PMAP_STATS_VAR(pmap_ncopy_page_oc);
229 PMAP_STATS_VAR(pmap_ncopy_page_nc);
230 PMAP_STATS_VAR(pmap_ncopy_page_dc);
231 PMAP_STATS_VAR(pmap_ncopy_page_doc);
232 PMAP_STATS_VAR(pmap_ncopy_page_sc);
233 PMAP_STATS_VAR(pmap_ncopy_page_soc);
234
235 PMAP_STATS_VAR(pmap_nnew_thread);
236 PMAP_STATS_VAR(pmap_nnew_thread_oc);
237
238 static inline u_long dtlb_get_data(u_int tlb, u_int slot);
239
240 /*
241  * Quick sort callout for comparing memory regions
242  */
243 static int mr_cmp(const void *a, const void *b);
244 static int om_cmp(const void *a, const void *b);
245
246 static int
247 mr_cmp(const void *a, const void *b)
248 {
249         const struct ofw_mem_region *mra;
250         const struct ofw_mem_region *mrb;
251
252         mra = a;
253         mrb = b;
254         if (mra->mr_start < mrb->mr_start)
255                 return (-1);
256         else if (mra->mr_start > mrb->mr_start)
257                 return (1);
258         else
259                 return (0);
260 }
261
262 static int
263 om_cmp(const void *a, const void *b)
264 {
265         const struct ofw_map *oma;
266         const struct ofw_map *omb;
267
268         oma = a;
269         omb = b;
270         if (oma->om_start < omb->om_start)
271                 return (-1);
272         else if (oma->om_start > omb->om_start)
273                 return (1);
274         else
275                 return (0);
276 }
277
278 static inline u_long
279 dtlb_get_data(u_int tlb, u_int slot)
280 {
281         u_long data;
282         register_t s;
283
284         slot = TLB_DAR_SLOT(tlb, slot);
285         /*
286          * We read ASI_DTLB_DATA_ACCESS_REG twice back-to-back in order to
287          * work around errata of USIII and beyond.
288          */
289         s = intr_disable();
290         (void)ldxa(slot, ASI_DTLB_DATA_ACCESS_REG);
291         data = ldxa(slot, ASI_DTLB_DATA_ACCESS_REG);
292         intr_restore(s);
293         return (data);
294 }
295
296 /*
297  * Bootstrap the system enough to run with virtual memory.
298  */
299 void
300 pmap_bootstrap(u_int cpu_impl)
301 {
302         struct pmap *pm;
303         struct tte *tp;
304         vm_offset_t off;
305         vm_offset_t va;
306         vm_paddr_t pa;
307         vm_size_t physsz;
308         vm_size_t virtsz;
309         u_long data;
310         u_long vpn;
311         phandle_t pmem;
312         phandle_t vmem;
313         u_int dtlb_slots_avail;
314         int i;
315         int j;
316         int sz;
317         uint32_t asi;
318         uint32_t colors;
319         uint32_t ldd;
320
321         /*
322          * Set the kernel context.
323          */
324         pmap_set_kctx();
325
326         colors = dcache_color_ignore != 0 ? 1 : DCACHE_COLORS;
327
328         /*
329          * Find out what physical memory is available from the PROM and
330          * initialize the phys_avail array.  This must be done before
331          * pmap_bootstrap_alloc is called.
332          */
333         if ((pmem = OF_finddevice("/memory")) == -1)
334                 OF_panic("%s: finddevice /memory", __func__);
335         if ((sz = OF_getproplen(pmem, "available")) == -1)
336                 OF_panic("%s: getproplen /memory/available", __func__);
337         if (sizeof(phys_avail) < sz)
338                 OF_panic("%s: phys_avail too small", __func__);
339         if (sizeof(mra) < sz)
340                 OF_panic("%s: mra too small", __func__);
341         bzero(mra, sz);
342         if (OF_getprop(pmem, "available", mra, sz) == -1)
343                 OF_panic("%s: getprop /memory/available", __func__);
344         sz /= sizeof(*mra);
345 #ifdef DIAGNOSTIC
346         OF_printf("pmap_bootstrap: physical memory\n");
347 #endif
348         qsort(mra, sz, sizeof (*mra), mr_cmp);
349         physsz = 0;
350         getenv_quad("hw.physmem", &physmem);
351         physmem = btoc(physmem);
352         for (i = 0, j = 0; i < sz; i++, j += 2) {
353 #ifdef DIAGNOSTIC
354                 OF_printf("start=%#lx size=%#lx\n", mra[i].mr_start,
355                     mra[i].mr_size);
356 #endif
357                 if (physmem != 0 && btoc(physsz + mra[i].mr_size) >= physmem) {
358                         if (btoc(physsz) < physmem) {
359                                 phys_avail[j] = mra[i].mr_start;
360                                 phys_avail[j + 1] = mra[i].mr_start +
361                                     (ctob(physmem) - physsz);
362                                 physsz = ctob(physmem);
363                         }
364                         break;
365                 }
366                 phys_avail[j] = mra[i].mr_start;
367                 phys_avail[j + 1] = mra[i].mr_start + mra[i].mr_size;
368                 physsz += mra[i].mr_size;
369         }
370         physmem = btoc(physsz);
371
372         /*
373          * Calculate the size of kernel virtual memory, and the size and mask
374          * for the kernel TSB based on the phsyical memory size but limited
375          * by the amount of dTLB slots available for locked entries if we have
376          * to lock the TSB in the TLB (given that for spitfire-class CPUs all
377          * of the dt64 slots can hold locked entries but there is no large
378          * dTLB for unlocked ones, we don't use more than half of it for the
379          * TSB).
380          * Note that for reasons unknown OpenSolaris doesn't take advantage of
381          * ASI_ATOMIC_QUAD_LDD_PHYS on UltraSPARC-III.  However, given that no
382          * public documentation is available for these, the latter just might
383          * not support it, yet.
384          */
385         if (cpu_impl == CPU_IMPL_SPARC64V ||
386             cpu_impl >= CPU_IMPL_ULTRASPARCIIIp) {
387                 tsb_kernel_ldd_phys = 1;
388                 virtsz = roundup(5 / 3 * physsz, PAGE_SIZE_4M <<
389                     (PAGE_SHIFT - TTE_SHIFT));
390         } else {
391                 dtlb_slots_avail = 0;
392                 for (i = 0; i < dtlb_slots; i++) {
393                         data = dtlb_get_data(cpu_impl ==
394                             CPU_IMPL_ULTRASPARCIII ? TLB_DAR_T16 :
395                             TLB_DAR_T32, i);
396                         if ((data & (TD_V | TD_L)) != (TD_V | TD_L))
397                                 dtlb_slots_avail++;
398                 }
399 #ifdef SMP
400                 dtlb_slots_avail -= PCPU_PAGES;
401 #endif
402                 if (cpu_impl >= CPU_IMPL_ULTRASPARCI &&
403                     cpu_impl < CPU_IMPL_ULTRASPARCIII)
404                         dtlb_slots_avail /= 2;
405                 virtsz = roundup(physsz, PAGE_SIZE_4M <<
406                     (PAGE_SHIFT - TTE_SHIFT));
407                 virtsz = MIN(virtsz, (dtlb_slots_avail * PAGE_SIZE_4M) <<
408                     (PAGE_SHIFT - TTE_SHIFT));
409         }
410         vm_max_kernel_address = VM_MIN_KERNEL_ADDRESS + virtsz;
411         tsb_kernel_size = virtsz >> (PAGE_SHIFT - TTE_SHIFT);
412         tsb_kernel_mask = (tsb_kernel_size >> TTE_SHIFT) - 1;
413
414         /*
415          * Allocate the kernel TSB and lock it in the TLB if necessary.
416          */
417         pa = pmap_bootstrap_alloc(tsb_kernel_size, colors);
418         if (pa & PAGE_MASK_4M)
419                 OF_panic("%s: TSB unaligned", __func__);
420         tsb_kernel_phys = pa;
421         if (tsb_kernel_ldd_phys == 0) {
422                 tsb_kernel =
423                     (struct tte *)(VM_MIN_KERNEL_ADDRESS - tsb_kernel_size);
424                 pmap_map_tsb();
425                 bzero(tsb_kernel, tsb_kernel_size);
426         } else {
427                 tsb_kernel =
428                     (struct tte *)TLB_PHYS_TO_DIRECT(tsb_kernel_phys);
429                 aszero(ASI_PHYS_USE_EC, tsb_kernel_phys, tsb_kernel_size);
430         }
431
432         /*
433          * Allocate and map the dynamic per-CPU area for the BSP.
434          */
435         pa = pmap_bootstrap_alloc(DPCPU_SIZE, colors);
436         dpcpu0 = (void *)TLB_PHYS_TO_DIRECT(pa);
437
438         /*
439          * Allocate and map the message buffer.
440          */
441         pa = pmap_bootstrap_alloc(msgbufsize, colors);
442         msgbufp = (struct msgbuf *)TLB_PHYS_TO_DIRECT(pa);
443
444         /*
445          * Patch the TSB addresses and mask as well as the ASIs used to load
446          * it into the trap table.
447          */
448
449 #define LDDA_R_I_R(rd, imm_asi, rs1, rs2)                               \
450         (EIF_OP(IOP_LDST) | EIF_F3_RD(rd) | EIF_F3_OP3(INS3_LDDA) |     \
451             EIF_F3_RS1(rs1) | EIF_F3_I(0) | EIF_F3_IMM_ASI(imm_asi) |   \
452             EIF_F3_RS2(rs2))
453 #define OR_R_I_R(rd, imm13, rs1)                                        \
454         (EIF_OP(IOP_MISC) | EIF_F3_RD(rd) | EIF_F3_OP3(INS2_OR) |       \
455             EIF_F3_RS1(rs1) | EIF_F3_I(1) | EIF_IMM(imm13, 13))
456 #define SETHI(rd, imm22)                                                \
457         (EIF_OP(IOP_FORM2) | EIF_F2_RD(rd) | EIF_F2_OP2(INS0_SETHI) |   \
458             EIF_IMM((imm22) >> 10, 22))
459 #define WR_R_I(rd, imm13, rs1)                                          \
460         (EIF_OP(IOP_MISC) | EIF_F3_RD(rd) | EIF_F3_OP3(INS2_WR) |       \
461             EIF_F3_RS1(rs1) | EIF_F3_I(1) | EIF_IMM(imm13, 13))
462
463 #define PATCH_ASI(addr, asi) do {                                       \
464         if (addr[0] != WR_R_I(IF_F3_RD(addr[0]), 0x0,                   \
465             IF_F3_RS1(addr[0])))                                        \
466                 OF_panic("%s: patched instructions have changed",       \
467                     __func__);                                          \
468         addr[0] |= EIF_IMM((asi), 13);                                  \
469         flush(addr);                                                    \
470 } while (0)
471
472 #define PATCH_LDD(addr, asi) do {                                       \
473         if (addr[0] != LDDA_R_I_R(IF_F3_RD(addr[0]), 0x0,               \
474             IF_F3_RS1(addr[0]), IF_F3_RS2(addr[0])))                    \
475                 OF_panic("%s: patched instructions have changed",       \
476                     __func__);                                          \
477         addr[0] |= EIF_F3_IMM_ASI(asi);                                 \
478         flush(addr);                                                    \
479 } while (0)
480
481 #define PATCH_TSB(addr, val) do {                                       \
482         if (addr[0] != SETHI(IF_F2_RD(addr[0]), 0x0) ||                 \
483             addr[1] != OR_R_I_R(IF_F3_RD(addr[1]), 0x0,                 \
484             IF_F3_RS1(addr[1])) ||                                      \
485             addr[3] != SETHI(IF_F2_RD(addr[3]), 0x0))                   \
486                 OF_panic("%s: patched instructions have changed",       \
487                     __func__);                                          \
488         addr[0] |= EIF_IMM((val) >> 42, 22);                            \
489         addr[1] |= EIF_IMM((val) >> 32, 10);                            \
490         addr[3] |= EIF_IMM((val) >> 10, 22);                            \
491         flush(addr);                                                    \
492         flush(addr + 1);                                                \
493         flush(addr + 3);                                                \
494 } while (0)
495
496 #define PATCH_TSB_MASK(addr, val) do {                                  \
497         if (addr[0] != SETHI(IF_F2_RD(addr[0]), 0x0) ||                 \
498             addr[1] != OR_R_I_R(IF_F3_RD(addr[1]), 0x0,                 \
499             IF_F3_RS1(addr[1])))                                        \
500                 OF_panic("%s: patched instructions have changed",       \
501                     __func__);                                          \
502         addr[0] |= EIF_IMM((val) >> 10, 22);                            \
503         addr[1] |= EIF_IMM((val), 10);                                  \
504         flush(addr);                                                    \
505         flush(addr + 1);                                                \
506 } while (0)
507
508         if (tsb_kernel_ldd_phys == 0) {
509                 asi = ASI_N;
510                 ldd = ASI_NUCLEUS_QUAD_LDD;
511                 off = (vm_offset_t)tsb_kernel;
512         } else {
513                 asi = ASI_PHYS_USE_EC;
514                 ldd = ASI_ATOMIC_QUAD_LDD_PHYS;
515                 off = (vm_offset_t)tsb_kernel_phys;
516         }
517         PATCH_TSB(tl1_dmmu_miss_direct_patch_tsb_phys_1, tsb_kernel_phys);
518         PATCH_TSB(tl1_dmmu_miss_direct_patch_tsb_phys_end_1,
519             tsb_kernel_phys + tsb_kernel_size - 1);
520         PATCH_ASI(tl1_dmmu_miss_patch_asi_1, asi);
521         PATCH_LDD(tl1_dmmu_miss_patch_quad_ldd_1, ldd);
522         PATCH_TSB(tl1_dmmu_miss_patch_tsb_1, off);
523         PATCH_TSB(tl1_dmmu_miss_patch_tsb_2, off);
524         PATCH_TSB_MASK(tl1_dmmu_miss_patch_tsb_mask_1, tsb_kernel_mask);
525         PATCH_TSB_MASK(tl1_dmmu_miss_patch_tsb_mask_2, tsb_kernel_mask);
526         PATCH_ASI(tl1_dmmu_prot_patch_asi_1, asi);
527         PATCH_LDD(tl1_dmmu_prot_patch_quad_ldd_1, ldd);
528         PATCH_TSB(tl1_dmmu_prot_patch_tsb_1, off);
529         PATCH_TSB(tl1_dmmu_prot_patch_tsb_2, off);
530         PATCH_TSB_MASK(tl1_dmmu_prot_patch_tsb_mask_1, tsb_kernel_mask);
531         PATCH_TSB_MASK(tl1_dmmu_prot_patch_tsb_mask_2, tsb_kernel_mask);
532         PATCH_ASI(tl1_immu_miss_patch_asi_1, asi);
533         PATCH_LDD(tl1_immu_miss_patch_quad_ldd_1, ldd);
534         PATCH_TSB(tl1_immu_miss_patch_tsb_1, off);
535         PATCH_TSB(tl1_immu_miss_patch_tsb_2, off);
536         PATCH_TSB_MASK(tl1_immu_miss_patch_tsb_mask_1, tsb_kernel_mask);
537         PATCH_TSB_MASK(tl1_immu_miss_patch_tsb_mask_2, tsb_kernel_mask);
538
539         /*
540          * Enter fake 8k pages for the 4MB kernel pages, so that
541          * pmap_kextract() will work for them.
542          */
543         for (i = 0; i < kernel_tlb_slots; i++) {
544                 pa = kernel_tlbs[i].te_pa;
545                 va = kernel_tlbs[i].te_va;
546                 for (off = 0; off < PAGE_SIZE_4M; off += PAGE_SIZE) {
547                         tp = tsb_kvtotte(va + off);
548                         vpn = TV_VPN(va + off, TS_8K);
549                         data = TD_V | TD_8K | TD_PA(pa + off) | TD_REF |
550                             TD_SW | TD_CP | TD_CV | TD_P | TD_W;
551                         pmap_bootstrap_set_tte(tp, vpn, data);
552                 }
553         }
554
555         /*
556          * Set the start and end of KVA.  The kernel is loaded starting
557          * at the first available 4MB super page, so we advance to the
558          * end of the last one used for it.
559          */
560         virtual_avail = KERNBASE + kernel_tlb_slots * PAGE_SIZE_4M;
561         virtual_end = vm_max_kernel_address;
562         kernel_vm_end = vm_max_kernel_address;
563
564         /*
565          * Allocate kva space for temporary mappings.
566          */
567         pmap_idle_map = virtual_avail;
568         virtual_avail += PAGE_SIZE * colors;
569         pmap_temp_map_1 = virtual_avail;
570         virtual_avail += PAGE_SIZE * colors;
571         pmap_temp_map_2 = virtual_avail;
572         virtual_avail += PAGE_SIZE * colors;
573
574         /*
575          * Allocate a kernel stack with guard page for thread0 and map it
576          * into the kernel TSB.  We must ensure that the virtual address is
577          * colored properly for corresponding CPUs, since we're allocating
578          * from phys_avail so the memory won't have an associated vm_page_t.
579          */
580         pa = pmap_bootstrap_alloc(KSTACK_PAGES * PAGE_SIZE, colors);
581         kstack0_phys = pa;
582         virtual_avail += roundup(KSTACK_GUARD_PAGES, colors) * PAGE_SIZE;
583         kstack0 = virtual_avail;
584         virtual_avail += roundup(KSTACK_PAGES, colors) * PAGE_SIZE;
585         if (dcache_color_ignore == 0)
586                 KASSERT(DCACHE_COLOR(kstack0) == DCACHE_COLOR(kstack0_phys),
587                     ("pmap_bootstrap: kstack0 miscolored"));
588         for (i = 0; i < KSTACK_PAGES; i++) {
589                 pa = kstack0_phys + i * PAGE_SIZE;
590                 va = kstack0 + i * PAGE_SIZE;
591                 tp = tsb_kvtotte(va);
592                 vpn = TV_VPN(va, TS_8K);
593                 data = TD_V | TD_8K | TD_PA(pa) | TD_REF | TD_SW | TD_CP |
594                     TD_CV | TD_P | TD_W;
595                 pmap_bootstrap_set_tte(tp, vpn, data);
596         }
597
598         /*
599          * Calculate the last available physical address.
600          */
601         for (i = 0; phys_avail[i + 2] != 0; i += 2)
602                 ;
603         Maxmem = sparc64_btop(phys_avail[i + 1]);
604
605         /*
606          * Add the PROM mappings to the kernel TSB.
607          */
608         if ((vmem = OF_finddevice("/virtual-memory")) == -1)
609                 OF_panic("%s: finddevice /virtual-memory", __func__);
610         if ((sz = OF_getproplen(vmem, "translations")) == -1)
611                 OF_panic("%s: getproplen translations", __func__);
612         if (sizeof(translations) < sz)
613                 OF_panic("%s: translations too small", __func__);
614         bzero(translations, sz);
615         if (OF_getprop(vmem, "translations", translations, sz) == -1)
616                 OF_panic("%s: getprop /virtual-memory/translations",
617                     __func__);
618         sz /= sizeof(*translations);
619         translations_size = sz;
620 #ifdef DIAGNOSTIC
621         OF_printf("pmap_bootstrap: translations\n");
622 #endif
623         qsort(translations, sz, sizeof (*translations), om_cmp);
624         for (i = 0; i < sz; i++) {
625 #ifdef DIAGNOSTIC
626                 OF_printf("translation: start=%#lx size=%#lx tte=%#lx\n",
627                     translations[i].om_start, translations[i].om_size,
628                     translations[i].om_tte);
629 #endif
630                 if ((translations[i].om_tte & TD_V) == 0)
631                         continue;
632                 if (translations[i].om_start < VM_MIN_PROM_ADDRESS ||
633                     translations[i].om_start > VM_MAX_PROM_ADDRESS)
634                         continue;
635                 for (off = 0; off < translations[i].om_size;
636                     off += PAGE_SIZE) {
637                         va = translations[i].om_start + off;
638                         tp = tsb_kvtotte(va);
639                         vpn = TV_VPN(va, TS_8K);
640                         data = ((translations[i].om_tte &
641                             ~((TD_SOFT2_MASK << TD_SOFT2_SHIFT) |
642                             (cpu_impl >= CPU_IMPL_ULTRASPARCI &&
643                             cpu_impl < CPU_IMPL_ULTRASPARCIII ?
644                             (TD_DIAG_SF_MASK << TD_DIAG_SF_SHIFT) :
645                             (TD_RSVD_CH_MASK << TD_RSVD_CH_SHIFT)) |
646                             (TD_SOFT_MASK << TD_SOFT_SHIFT))) | TD_EXEC) +
647                             off;
648                         pmap_bootstrap_set_tte(tp, vpn, data);
649                 }
650         }
651
652         /*
653          * Get the available physical memory ranges from /memory/reg.  These
654          * are only used for kernel dumps, but it may not be wise to do PROM
655          * calls in that situation.
656          */
657         if ((sz = OF_getproplen(pmem, "reg")) == -1)
658                 OF_panic("%s: getproplen /memory/reg", __func__);
659         if (sizeof(sparc64_memreg) < sz)
660                 OF_panic("%s: sparc64_memreg too small", __func__);
661         if (OF_getprop(pmem, "reg", sparc64_memreg, sz) == -1)
662                 OF_panic("%s: getprop /memory/reg", __func__);
663         sparc64_nmemreg = sz / sizeof(*sparc64_memreg);
664
665         /*
666          * Initialize the kernel pmap (which is statically allocated).
667          */
668         pm = kernel_pmap;
669         PMAP_LOCK_INIT(pm);
670         for (i = 0; i < MAXCPU; i++)
671                 pm->pm_context[i] = TLB_CTX_KERNEL;
672         CPU_FILL(&pm->pm_active);
673
674         /*
675          * Initialize the global tte list lock, which is more commonly
676          * known as the pmap pv global lock.
677          */
678         rw_init(&tte_list_global_lock, "pmap pv global");
679
680         /*
681          * Flush all non-locked TLB entries possibly left over by the
682          * firmware.
683          */
684         tlb_flush_nonlocked();
685 }
686
687 static void
688 pmap_init_qpages(void)
689 {
690         struct pcpu *pc;
691         int i;
692
693         if (dcache_color_ignore != 0)
694                 return;
695
696         CPU_FOREACH(i) {
697                 pc = pcpu_find(i);
698                 pc->pc_qmap_addr = kva_alloc(PAGE_SIZE * DCACHE_COLORS);
699                 if (pc->pc_qmap_addr == 0)
700                         panic("pmap_init_qpages: unable to allocate KVA");
701         }
702 }
703
704 SYSINIT(qpages_init, SI_SUB_CPU, SI_ORDER_ANY, pmap_init_qpages, NULL);
705
706 /*
707  * Map the 4MB kernel TSB pages.
708  */
709 void
710 pmap_map_tsb(void)
711 {
712         vm_offset_t va;
713         vm_paddr_t pa;
714         u_long data;
715         int i;
716
717         for (i = 0; i < tsb_kernel_size; i += PAGE_SIZE_4M) {
718                 va = (vm_offset_t)tsb_kernel + i;
719                 pa = tsb_kernel_phys + i;
720                 data = TD_V | TD_4M | TD_PA(pa) | TD_L | TD_CP | TD_CV |
721                     TD_P | TD_W;
722                 stxa(AA_DMMU_TAR, ASI_DMMU, TLB_TAR_VA(va) |
723                     TLB_TAR_CTX(TLB_CTX_KERNEL));
724                 stxa_sync(0, ASI_DTLB_DATA_IN_REG, data);
725         }
726 }
727
728 /*
729  * Set the secondary context to be the kernel context (needed for FP block
730  * operations in the kernel).
731  */
732 void
733 pmap_set_kctx(void)
734 {
735
736         stxa(AA_DMMU_SCXR, ASI_DMMU, (ldxa(AA_DMMU_SCXR, ASI_DMMU) &
737             TLB_CXR_PGSZ_MASK) | TLB_CTX_KERNEL);
738         flush(KERNBASE);
739 }
740
741 /*
742  * Allocate a physical page of memory directly from the phys_avail map.
743  * Can only be called from pmap_bootstrap before avail start and end are
744  * calculated.
745  */
746 static vm_paddr_t
747 pmap_bootstrap_alloc(vm_size_t size, uint32_t colors)
748 {
749         vm_paddr_t pa;
750         int i;
751
752         size = roundup(size, PAGE_SIZE * colors);
753         for (i = 0; phys_avail[i + 1] != 0; i += 2) {
754                 if (phys_avail[i + 1] - phys_avail[i] < size)
755                         continue;
756                 pa = phys_avail[i];
757                 phys_avail[i] += size;
758                 return (pa);
759         }
760         OF_panic("%s: no suitable region found", __func__);
761 }
762
763 /*
764  * Set a TTE.  This function is intended as a helper when tsb_kernel is
765  * direct-mapped but we haven't taken over the trap table, yet, as it's the
766  * case when we are taking advantage of ASI_ATOMIC_QUAD_LDD_PHYS to access
767  * the kernel TSB.
768  */
769 void
770 pmap_bootstrap_set_tte(struct tte *tp, u_long vpn, u_long data)
771 {
772
773         if (tsb_kernel_ldd_phys == 0) {
774                 tp->tte_vpn = vpn;
775                 tp->tte_data = data;
776         } else {
777                 stxa((vm_paddr_t)tp + offsetof(struct tte, tte_vpn),
778                     ASI_PHYS_USE_EC, vpn);
779                 stxa((vm_paddr_t)tp + offsetof(struct tte, tte_data),
780                     ASI_PHYS_USE_EC, data);
781         }
782 }
783
784 /*
785  * Initialize a vm_page's machine-dependent fields.
786  */
787 void
788 pmap_page_init(vm_page_t m)
789 {
790
791         TAILQ_INIT(&m->md.tte_list);
792         m->md.color = DCACHE_COLOR(VM_PAGE_TO_PHYS(m));
793         m->md.pmap = NULL;
794 }
795
796 /*
797  * Initialize the pmap module.
798  */
799 void
800 pmap_init(void)
801 {
802         vm_offset_t addr;
803         vm_size_t size;
804         int result;
805         int i;
806
807         for (i = 0; i < translations_size; i++) {
808                 addr = translations[i].om_start;
809                 size = translations[i].om_size;
810                 if ((translations[i].om_tte & TD_V) == 0)
811                         continue;
812                 if (addr < VM_MIN_PROM_ADDRESS || addr > VM_MAX_PROM_ADDRESS)
813                         continue;
814                 result = vm_map_find(kernel_map, NULL, 0, &addr, size, 0,
815                     VMFS_NO_SPACE, VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
816                 if (result != KERN_SUCCESS || addr != translations[i].om_start)
817                         panic("pmap_init: vm_map_find");
818         }
819 }
820
821 /*
822  * Extract the physical page address associated with the given
823  * map/virtual_address pair.
824  */
825 vm_paddr_t
826 pmap_extract(pmap_t pm, vm_offset_t va)
827 {
828         struct tte *tp;
829         vm_paddr_t pa;
830
831         if (pm == kernel_pmap)
832                 return (pmap_kextract(va));
833         PMAP_LOCK(pm);
834         tp = tsb_tte_lookup(pm, va);
835         if (tp == NULL)
836                 pa = 0;
837         else
838                 pa = TTE_GET_PA(tp) | (va & TTE_GET_PAGE_MASK(tp));
839         PMAP_UNLOCK(pm);
840         return (pa);
841 }
842
843 /*
844  * Atomically extract and hold the physical page with the given
845  * pmap and virtual address pair if that mapping permits the given
846  * protection.
847  */
848 vm_page_t
849 pmap_extract_and_hold(pmap_t pm, vm_offset_t va, vm_prot_t prot)
850 {
851         struct tte *tp;
852         vm_page_t m;
853         vm_paddr_t pa;
854
855         m = NULL;
856         pa = 0;
857         PMAP_LOCK(pm);
858 retry:
859         if (pm == kernel_pmap) {
860                 if (va >= VM_MIN_DIRECT_ADDRESS) {
861                         tp = NULL;
862                         m = PHYS_TO_VM_PAGE(TLB_DIRECT_TO_PHYS(va));
863                         (void)vm_page_pa_tryrelock(pm, TLB_DIRECT_TO_PHYS(va),
864                             &pa);
865                         vm_page_hold(m);
866                 } else {
867                         tp = tsb_kvtotte(va);
868                         if ((tp->tte_data & TD_V) == 0)
869                                 tp = NULL;
870                 }
871         } else
872                 tp = tsb_tte_lookup(pm, va);
873         if (tp != NULL && ((tp->tte_data & TD_SW) ||
874             (prot & VM_PROT_WRITE) == 0)) {
875                 if (vm_page_pa_tryrelock(pm, TTE_GET_PA(tp), &pa))
876                         goto retry;
877                 m = PHYS_TO_VM_PAGE(TTE_GET_PA(tp));
878                 vm_page_hold(m);
879         }
880         PA_UNLOCK_COND(pa);
881         PMAP_UNLOCK(pm);
882         return (m);
883 }
884
885 /*
886  * Extract the physical page address associated with the given kernel virtual
887  * address.
888  */
889 vm_paddr_t
890 pmap_kextract(vm_offset_t va)
891 {
892         struct tte *tp;
893
894         if (va >= VM_MIN_DIRECT_ADDRESS)
895                 return (TLB_DIRECT_TO_PHYS(va));
896         tp = tsb_kvtotte(va);
897         if ((tp->tte_data & TD_V) == 0)
898                 return (0);
899         return (TTE_GET_PA(tp) | (va & TTE_GET_PAGE_MASK(tp)));
900 }
901
902 int
903 pmap_cache_enter(vm_page_t m, vm_offset_t va)
904 {
905         struct tte *tp;
906         int color;
907
908         rw_assert(&tte_list_global_lock, RA_WLOCKED);
909         KASSERT((m->flags & PG_FICTITIOUS) == 0,
910             ("pmap_cache_enter: fake page"));
911         PMAP_STATS_INC(pmap_ncache_enter);
912
913         if (dcache_color_ignore != 0)
914                 return (1);
915
916         /*
917          * Find the color for this virtual address and note the added mapping.
918          */
919         color = DCACHE_COLOR(va);
920         m->md.colors[color]++;
921
922         /*
923          * If all existing mappings have the same color, the mapping is
924          * cacheable.
925          */
926         if (m->md.color == color) {
927                 KASSERT(m->md.colors[DCACHE_OTHER_COLOR(color)] == 0,
928                     ("pmap_cache_enter: cacheable, mappings of other color"));
929                 if (m->md.color == DCACHE_COLOR(VM_PAGE_TO_PHYS(m)))
930                         PMAP_STATS_INC(pmap_ncache_enter_c);
931                 else
932                         PMAP_STATS_INC(pmap_ncache_enter_oc);
933                 return (1);
934         }
935
936         /*
937          * If there are no mappings of the other color, and the page still has
938          * the wrong color, this must be a new mapping.  Change the color to
939          * match the new mapping, which is cacheable.  We must flush the page
940          * from the cache now.
941          */
942         if (m->md.colors[DCACHE_OTHER_COLOR(color)] == 0) {
943                 KASSERT(m->md.colors[color] == 1,
944                     ("pmap_cache_enter: changing color, not new mapping"));
945                 dcache_page_inval(VM_PAGE_TO_PHYS(m));
946                 m->md.color = color;
947                 if (m->md.color == DCACHE_COLOR(VM_PAGE_TO_PHYS(m)))
948                         PMAP_STATS_INC(pmap_ncache_enter_cc);
949                 else
950                         PMAP_STATS_INC(pmap_ncache_enter_coc);
951                 return (1);
952         }
953
954         /*
955          * If the mapping is already non-cacheable, just return.
956          */
957         if (m->md.color == -1) {
958                 PMAP_STATS_INC(pmap_ncache_enter_nc);
959                 return (0);
960         }
961
962         PMAP_STATS_INC(pmap_ncache_enter_cnc);
963
964         /*
965          * Mark all mappings as uncacheable, flush any lines with the other
966          * color out of the dcache, and set the color to none (-1).
967          */
968         TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
969                 atomic_clear_long(&tp->tte_data, TD_CV);
970                 tlb_page_demap(TTE_GET_PMAP(tp), TTE_GET_VA(tp));
971         }
972         dcache_page_inval(VM_PAGE_TO_PHYS(m));
973         m->md.color = -1;
974         return (0);
975 }
976
977 static void
978 pmap_cache_remove(vm_page_t m, vm_offset_t va)
979 {
980         struct tte *tp;
981         int color;
982
983         rw_assert(&tte_list_global_lock, RA_WLOCKED);
984         CTR3(KTR_PMAP, "pmap_cache_remove: m=%p va=%#lx c=%d", m, va,
985             m->md.colors[DCACHE_COLOR(va)]);
986         KASSERT((m->flags & PG_FICTITIOUS) == 0,
987             ("pmap_cache_remove: fake page"));
988         PMAP_STATS_INC(pmap_ncache_remove);
989
990         if (dcache_color_ignore != 0)
991                 return;
992
993         KASSERT(m->md.colors[DCACHE_COLOR(va)] > 0,
994             ("pmap_cache_remove: no mappings %d <= 0",
995             m->md.colors[DCACHE_COLOR(va)]));
996
997         /*
998          * Find the color for this virtual address and note the removal of
999          * the mapping.
1000          */
1001         color = DCACHE_COLOR(va);
1002         m->md.colors[color]--;
1003
1004         /*
1005          * If the page is cacheable, just return and keep the same color, even
1006          * if there are no longer any mappings.
1007          */
1008         if (m->md.color != -1) {
1009                 if (m->md.color == DCACHE_COLOR(VM_PAGE_TO_PHYS(m)))
1010                         PMAP_STATS_INC(pmap_ncache_remove_c);
1011                 else
1012                         PMAP_STATS_INC(pmap_ncache_remove_oc);
1013                 return;
1014         }
1015
1016         KASSERT(m->md.colors[DCACHE_OTHER_COLOR(color)] != 0,
1017             ("pmap_cache_remove: uncacheable, no mappings of other color"));
1018
1019         /*
1020          * If the page is not cacheable (color is -1), and the number of
1021          * mappings for this color is not zero, just return.  There are
1022          * mappings of the other color still, so remain non-cacheable.
1023          */
1024         if (m->md.colors[color] != 0) {
1025                 PMAP_STATS_INC(pmap_ncache_remove_nc);
1026                 return;
1027         }
1028
1029         /*
1030          * The number of mappings for this color is now zero.  Recache the
1031          * other colored mappings, and change the page color to the other
1032          * color.  There should be no lines in the data cache for this page,
1033          * so flushing should not be needed.
1034          */
1035         TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
1036                 atomic_set_long(&tp->tte_data, TD_CV);
1037                 tlb_page_demap(TTE_GET_PMAP(tp), TTE_GET_VA(tp));
1038         }
1039         m->md.color = DCACHE_OTHER_COLOR(color);
1040
1041         if (m->md.color == DCACHE_COLOR(VM_PAGE_TO_PHYS(m)))
1042                 PMAP_STATS_INC(pmap_ncache_remove_cc);
1043         else
1044                 PMAP_STATS_INC(pmap_ncache_remove_coc);
1045 }
1046
1047 /*
1048  * Map a wired page into kernel virtual address space.
1049  */
1050 void
1051 pmap_kenter(vm_offset_t va, vm_page_t m)
1052 {
1053         vm_offset_t ova;
1054         struct tte *tp;
1055         vm_page_t om;
1056         u_long data;
1057
1058         rw_assert(&tte_list_global_lock, RA_WLOCKED);
1059         PMAP_STATS_INC(pmap_nkenter);
1060         tp = tsb_kvtotte(va);
1061         CTR4(KTR_PMAP, "pmap_kenter: va=%#lx pa=%#lx tp=%p data=%#lx",
1062             va, VM_PAGE_TO_PHYS(m), tp, tp->tte_data);
1063         if (DCACHE_COLOR(VM_PAGE_TO_PHYS(m)) != DCACHE_COLOR(va)) {
1064                 CTR5(KTR_SPARE2,
1065         "pmap_kenter: off color va=%#lx pa=%#lx o=%p ot=%d pi=%#lx",
1066                     va, VM_PAGE_TO_PHYS(m), m->object,
1067                     m->object ? m->object->type : -1,
1068                     m->pindex);
1069                 PMAP_STATS_INC(pmap_nkenter_oc);
1070         }
1071         if ((tp->tte_data & TD_V) != 0) {
1072                 om = PHYS_TO_VM_PAGE(TTE_GET_PA(tp));
1073                 ova = TTE_GET_VA(tp);
1074                 if (m == om && va == ova) {
1075                         PMAP_STATS_INC(pmap_nkenter_stupid);
1076                         return;
1077                 }
1078                 TAILQ_REMOVE(&om->md.tte_list, tp, tte_link);
1079                 pmap_cache_remove(om, ova);
1080                 if (va != ova)
1081                         tlb_page_demap(kernel_pmap, ova);
1082         }
1083         data = TD_V | TD_8K | VM_PAGE_TO_PHYS(m) | TD_REF | TD_SW | TD_CP |
1084             TD_P | TD_W;
1085         if (pmap_cache_enter(m, va) != 0)
1086                 data |= TD_CV;
1087         tp->tte_vpn = TV_VPN(va, TS_8K);
1088         tp->tte_data = data;
1089         TAILQ_INSERT_TAIL(&m->md.tte_list, tp, tte_link);
1090 }
1091
1092 /*
1093  * Map a wired page into kernel virtual address space.  This additionally
1094  * takes a flag argument which is or'ed to the TTE data.  This is used by
1095  * sparc64_bus_mem_map().
1096  * NOTE: if the mapping is non-cacheable, it's the caller's responsibility
1097  * to flush entries that might still be in the cache, if applicable.
1098  */
1099 void
1100 pmap_kenter_flags(vm_offset_t va, vm_paddr_t pa, u_long flags)
1101 {
1102         struct tte *tp;
1103
1104         tp = tsb_kvtotte(va);
1105         CTR4(KTR_PMAP, "pmap_kenter_flags: va=%#lx pa=%#lx tp=%p data=%#lx",
1106             va, pa, tp, tp->tte_data);
1107         tp->tte_vpn = TV_VPN(va, TS_8K);
1108         tp->tte_data = TD_V | TD_8K | TD_PA(pa) | TD_REF | TD_P | flags;
1109 }
1110
1111 /*
1112  * Remove a wired page from kernel virtual address space.
1113  */
1114 void
1115 pmap_kremove(vm_offset_t va)
1116 {
1117         struct tte *tp;
1118         vm_page_t m;
1119
1120         rw_assert(&tte_list_global_lock, RA_WLOCKED);
1121         PMAP_STATS_INC(pmap_nkremove);
1122         tp = tsb_kvtotte(va);
1123         CTR3(KTR_PMAP, "pmap_kremove: va=%#lx tp=%p data=%#lx", va, tp,
1124             tp->tte_data);
1125         if ((tp->tte_data & TD_V) == 0)
1126                 return;
1127         m = PHYS_TO_VM_PAGE(TTE_GET_PA(tp));
1128         TAILQ_REMOVE(&m->md.tte_list, tp, tte_link);
1129         pmap_cache_remove(m, va);
1130         TTE_ZERO(tp);
1131 }
1132
1133 /*
1134  * Inverse of pmap_kenter_flags, used by bus_space_unmap().
1135  */
1136 void
1137 pmap_kremove_flags(vm_offset_t va)
1138 {
1139         struct tte *tp;
1140
1141         tp = tsb_kvtotte(va);
1142         CTR3(KTR_PMAP, "pmap_kremove_flags: va=%#lx tp=%p data=%#lx", va, tp,
1143             tp->tte_data);
1144         TTE_ZERO(tp);
1145 }
1146
1147 /*
1148  * Map a range of physical addresses into kernel virtual address space.
1149  *
1150  * The value passed in *virt is a suggested virtual address for the mapping.
1151  * Architectures which can support a direct-mapped physical to virtual region
1152  * can return the appropriate address within that region, leaving '*virt'
1153  * unchanged.
1154  */
1155 vm_offset_t
1156 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
1157 {
1158
1159         return (TLB_PHYS_TO_DIRECT(start));
1160 }
1161
1162 /*
1163  * Map a list of wired pages into kernel virtual address space.  This is
1164  * intended for temporary mappings which do not need page modification or
1165  * references recorded.  Existing mappings in the region are overwritten.
1166  */
1167 void
1168 pmap_qenter(vm_offset_t sva, vm_page_t *m, int count)
1169 {
1170         vm_offset_t va;
1171
1172         PMAP_STATS_INC(pmap_nqenter);
1173         va = sva;
1174         rw_wlock(&tte_list_global_lock);
1175         while (count-- > 0) {
1176                 pmap_kenter(va, *m);
1177                 va += PAGE_SIZE;
1178                 m++;
1179         }
1180         rw_wunlock(&tte_list_global_lock);
1181         tlb_range_demap(kernel_pmap, sva, va);
1182 }
1183
1184 /*
1185  * Remove page mappings from kernel virtual address space.  Intended for
1186  * temporary mappings entered by pmap_qenter.
1187  */
1188 void
1189 pmap_qremove(vm_offset_t sva, int count)
1190 {
1191         vm_offset_t va;
1192
1193         PMAP_STATS_INC(pmap_nqremove);
1194         va = sva;
1195         rw_wlock(&tte_list_global_lock);
1196         while (count-- > 0) {
1197                 pmap_kremove(va);
1198                 va += PAGE_SIZE;
1199         }
1200         rw_wunlock(&tte_list_global_lock);
1201         tlb_range_demap(kernel_pmap, sva, va);
1202 }
1203
1204 /*
1205  * Initialize the pmap associated with process 0.
1206  */
1207 void
1208 pmap_pinit0(pmap_t pm)
1209 {
1210         int i;
1211
1212         PMAP_LOCK_INIT(pm);
1213         for (i = 0; i < MAXCPU; i++)
1214                 pm->pm_context[i] = TLB_CTX_KERNEL;
1215         CPU_ZERO(&pm->pm_active);
1216         pm->pm_tsb = NULL;
1217         pm->pm_tsb_obj = NULL;
1218         bzero(&pm->pm_stats, sizeof(pm->pm_stats));
1219 }
1220
1221 /*
1222  * Initialize a preallocated and zeroed pmap structure, such as one in a
1223  * vmspace structure.
1224  */
1225 int
1226 pmap_pinit(pmap_t pm)
1227 {
1228         vm_page_t ma[TSB_PAGES];
1229         int i;
1230
1231         /*
1232          * Allocate KVA space for the TSB.
1233          */
1234         if (pm->pm_tsb == NULL) {
1235                 pm->pm_tsb = (struct tte *)kva_alloc(TSB_BSIZE);
1236                 if (pm->pm_tsb == NULL)
1237                         return (0);
1238                 }
1239
1240         /*
1241          * Allocate an object for it.
1242          */
1243         if (pm->pm_tsb_obj == NULL)
1244                 pm->pm_tsb_obj = vm_object_allocate(OBJT_PHYS, TSB_PAGES);
1245
1246         for (i = 0; i < MAXCPU; i++)
1247                 pm->pm_context[i] = -1;
1248         CPU_ZERO(&pm->pm_active);
1249
1250         VM_OBJECT_WLOCK(pm->pm_tsb_obj);
1251         (void)vm_page_grab_pages(pm->pm_tsb_obj, 0, VM_ALLOC_NORMAL |
1252             VM_ALLOC_NOBUSY | VM_ALLOC_WIRED | VM_ALLOC_ZERO, ma, TSB_PAGES);
1253         VM_OBJECT_WUNLOCK(pm->pm_tsb_obj);
1254         for (i = 0; i < TSB_PAGES; i++)
1255                 ma[i]->md.pmap = pm;
1256         pmap_qenter((vm_offset_t)pm->pm_tsb, ma, TSB_PAGES);
1257
1258         bzero(&pm->pm_stats, sizeof(pm->pm_stats));
1259         return (1);
1260 }
1261
1262 /*
1263  * Release any resources held by the given physical map.
1264  * Called when a pmap initialized by pmap_pinit is being released.
1265  * Should only be called if the map contains no valid mappings.
1266  */
1267 void
1268 pmap_release(pmap_t pm)
1269 {
1270         vm_object_t obj;
1271         vm_page_t m;
1272 #ifdef SMP
1273         struct pcpu *pc;
1274 #endif
1275
1276         CTR2(KTR_PMAP, "pmap_release: ctx=%#x tsb=%p",
1277             pm->pm_context[curcpu], pm->pm_tsb);
1278         KASSERT(pmap_resident_count(pm) == 0,
1279             ("pmap_release: resident pages %ld != 0",
1280             pmap_resident_count(pm)));
1281
1282         /*
1283          * After the pmap was freed, it might be reallocated to a new process.
1284          * When switching, this might lead us to wrongly assume that we need
1285          * not switch contexts because old and new pmap pointer are equal.
1286          * Therefore, make sure that this pmap is not referenced by any PCPU
1287          * pointer any more.  This could happen in two cases:
1288          * - A process that referenced the pmap is currently exiting on a CPU.
1289          *   However, it is guaranteed to not switch in any more after setting
1290          *   its state to PRS_ZOMBIE.
1291          * - A process that referenced this pmap ran on a CPU, but we switched
1292          *   to a kernel thread, leaving the pmap pointer unchanged.
1293          */
1294 #ifdef SMP
1295         sched_pin();
1296         STAILQ_FOREACH(pc, &cpuhead, pc_allcpu)
1297                 atomic_cmpset_rel_ptr((uintptr_t *)&pc->pc_pmap,
1298                     (uintptr_t)pm, (uintptr_t)NULL);
1299         sched_unpin();
1300 #else
1301         critical_enter();
1302         if (PCPU_GET(pmap) == pm)
1303                 PCPU_SET(pmap, NULL);
1304         critical_exit();
1305 #endif
1306
1307         pmap_qremove((vm_offset_t)pm->pm_tsb, TSB_PAGES);
1308         obj = pm->pm_tsb_obj;
1309         VM_OBJECT_WLOCK(obj);
1310         KASSERT(obj->ref_count == 1, ("pmap_release: tsbobj ref count != 1"));
1311         while (!TAILQ_EMPTY(&obj->memq)) {
1312                 m = TAILQ_FIRST(&obj->memq);
1313                 m->md.pmap = NULL;
1314                 m->wire_count--;
1315                 atomic_subtract_int(&vm_cnt.v_wire_count, 1);
1316                 vm_page_free_zero(m);
1317         }
1318         VM_OBJECT_WUNLOCK(obj);
1319 }
1320
1321 /*
1322  * Grow the number of kernel page table entries.  Unneeded.
1323  */
1324 void
1325 pmap_growkernel(vm_offset_t addr)
1326 {
1327
1328         panic("pmap_growkernel: can't grow kernel");
1329 }
1330
1331 int
1332 pmap_remove_tte(struct pmap *pm, struct pmap *pm2, struct tte *tp,
1333     vm_offset_t va)
1334 {
1335         vm_page_t m;
1336         u_long data;
1337
1338         rw_assert(&tte_list_global_lock, RA_WLOCKED);
1339         data = atomic_readandclear_long(&tp->tte_data);
1340         if ((data & TD_FAKE) == 0) {
1341                 m = PHYS_TO_VM_PAGE(TD_PA(data));
1342                 TAILQ_REMOVE(&m->md.tte_list, tp, tte_link);
1343                 if ((data & TD_WIRED) != 0)
1344                         pm->pm_stats.wired_count--;
1345                 if ((data & TD_PV) != 0) {
1346                         if ((data & TD_W) != 0)
1347                                 vm_page_dirty(m);
1348                         if ((data & TD_REF) != 0)
1349                                 vm_page_aflag_set(m, PGA_REFERENCED);
1350                         if (TAILQ_EMPTY(&m->md.tte_list))
1351                                 vm_page_aflag_clear(m, PGA_WRITEABLE);
1352                         pm->pm_stats.resident_count--;
1353                 }
1354                 pmap_cache_remove(m, va);
1355         }
1356         TTE_ZERO(tp);
1357         if (PMAP_REMOVE_DONE(pm))
1358                 return (0);
1359         return (1);
1360 }
1361
1362 /*
1363  * Remove the given range of addresses from the specified map.
1364  */
1365 void
1366 pmap_remove(pmap_t pm, vm_offset_t start, vm_offset_t end)
1367 {
1368         struct tte *tp;
1369         vm_offset_t va;
1370
1371         CTR3(KTR_PMAP, "pmap_remove: ctx=%#lx start=%#lx end=%#lx",
1372             pm->pm_context[curcpu], start, end);
1373         if (PMAP_REMOVE_DONE(pm))
1374                 return;
1375         rw_wlock(&tte_list_global_lock);
1376         PMAP_LOCK(pm);
1377         if (end - start > PMAP_TSB_THRESH) {
1378                 tsb_foreach(pm, NULL, start, end, pmap_remove_tte);
1379                 tlb_context_demap(pm);
1380         } else {
1381                 for (va = start; va < end; va += PAGE_SIZE)
1382                         if ((tp = tsb_tte_lookup(pm, va)) != NULL &&
1383                             !pmap_remove_tte(pm, NULL, tp, va))
1384                                 break;
1385                 tlb_range_demap(pm, start, end - 1);
1386         }
1387         PMAP_UNLOCK(pm);
1388         rw_wunlock(&tte_list_global_lock);
1389 }
1390
1391 void
1392 pmap_remove_all(vm_page_t m)
1393 {
1394         struct pmap *pm;
1395         struct tte *tpn;
1396         struct tte *tp;
1397         vm_offset_t va;
1398
1399         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1400             ("pmap_remove_all: page %p is not managed", m));
1401         rw_wlock(&tte_list_global_lock);
1402         for (tp = TAILQ_FIRST(&m->md.tte_list); tp != NULL; tp = tpn) {
1403                 tpn = TAILQ_NEXT(tp, tte_link);
1404                 if ((tp->tte_data & TD_PV) == 0)
1405                         continue;
1406                 pm = TTE_GET_PMAP(tp);
1407                 va = TTE_GET_VA(tp);
1408                 PMAP_LOCK(pm);
1409                 if ((tp->tte_data & TD_WIRED) != 0)
1410                         pm->pm_stats.wired_count--;
1411                 if ((tp->tte_data & TD_REF) != 0)
1412                         vm_page_aflag_set(m, PGA_REFERENCED);
1413                 if ((tp->tte_data & TD_W) != 0)
1414                         vm_page_dirty(m);
1415                 tp->tte_data &= ~TD_V;
1416                 tlb_page_demap(pm, va);
1417                 TAILQ_REMOVE(&m->md.tte_list, tp, tte_link);
1418                 pm->pm_stats.resident_count--;
1419                 pmap_cache_remove(m, va);
1420                 TTE_ZERO(tp);
1421                 PMAP_UNLOCK(pm);
1422         }
1423         vm_page_aflag_clear(m, PGA_WRITEABLE);
1424         rw_wunlock(&tte_list_global_lock);
1425 }
1426
1427 static int
1428 pmap_protect_tte(struct pmap *pm, struct pmap *pm2, struct tte *tp,
1429     vm_offset_t va)
1430 {
1431         u_long data;
1432         vm_page_t m;
1433
1434         PMAP_LOCK_ASSERT(pm, MA_OWNED);
1435         data = atomic_clear_long(&tp->tte_data, TD_SW | TD_W);
1436         if ((data & (TD_PV | TD_W)) == (TD_PV | TD_W)) {
1437                 m = PHYS_TO_VM_PAGE(TD_PA(data));
1438                 vm_page_dirty(m);
1439         }
1440         return (1);
1441 }
1442
1443 /*
1444  * Set the physical protection on the specified range of this map as requested.
1445  */
1446 void
1447 pmap_protect(pmap_t pm, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1448 {
1449         vm_offset_t va;
1450         struct tte *tp;
1451
1452         CTR4(KTR_PMAP, "pmap_protect: ctx=%#lx sva=%#lx eva=%#lx prot=%#lx",
1453             pm->pm_context[curcpu], sva, eva, prot);
1454
1455         if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1456                 pmap_remove(pm, sva, eva);
1457                 return;
1458         }
1459
1460         if (prot & VM_PROT_WRITE)
1461                 return;
1462
1463         PMAP_LOCK(pm);
1464         if (eva - sva > PMAP_TSB_THRESH) {
1465                 tsb_foreach(pm, NULL, sva, eva, pmap_protect_tte);
1466                 tlb_context_demap(pm);
1467         } else {
1468                 for (va = sva; va < eva; va += PAGE_SIZE)
1469                         if ((tp = tsb_tte_lookup(pm, va)) != NULL)
1470                                 pmap_protect_tte(pm, NULL, tp, va);
1471                 tlb_range_demap(pm, sva, eva - 1);
1472         }
1473         PMAP_UNLOCK(pm);
1474 }
1475
1476 /*
1477  * Map the given physical page at the specified virtual address in the
1478  * target pmap with the protection requested.  If specified the page
1479  * will be wired down.
1480  */
1481 int
1482 pmap_enter(pmap_t pm, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1483     u_int flags, int8_t psind)
1484 {
1485         int rv;
1486
1487         rw_wlock(&tte_list_global_lock);
1488         PMAP_LOCK(pm);
1489         rv = pmap_enter_locked(pm, va, m, prot, flags, psind);
1490         rw_wunlock(&tte_list_global_lock);
1491         PMAP_UNLOCK(pm);
1492         return (rv);
1493 }
1494
1495 /*
1496  * Map the given physical page at the specified virtual address in the
1497  * target pmap with the protection requested.  If specified the page
1498  * will be wired down.
1499  *
1500  * The page queues and pmap must be locked.
1501  */
1502 static int
1503 pmap_enter_locked(pmap_t pm, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1504     u_int flags, int8_t psind __unused)
1505 {
1506         struct tte *tp;
1507         vm_paddr_t pa;
1508         vm_page_t real;
1509         u_long data;
1510         boolean_t wired;
1511
1512         rw_assert(&tte_list_global_lock, RA_WLOCKED);
1513         PMAP_LOCK_ASSERT(pm, MA_OWNED);
1514         if ((m->oflags & VPO_UNMANAGED) == 0 && !vm_page_xbusied(m))
1515                 VM_OBJECT_ASSERT_LOCKED(m->object);
1516         PMAP_STATS_INC(pmap_nenter);
1517         pa = VM_PAGE_TO_PHYS(m);
1518         wired = (flags & PMAP_ENTER_WIRED) != 0;
1519
1520         /*
1521          * If this is a fake page from the device_pager, but it covers actual
1522          * physical memory, convert to the real backing page.
1523          */
1524         if ((m->flags & PG_FICTITIOUS) != 0) {
1525                 real = vm_phys_paddr_to_vm_page(pa);
1526                 if (real != NULL)
1527                         m = real;
1528         }
1529
1530         CTR6(KTR_PMAP,
1531             "pmap_enter_locked: ctx=%p m=%p va=%#lx pa=%#lx prot=%#x wired=%d",
1532             pm->pm_context[curcpu], m, va, pa, prot, wired);
1533
1534         /*
1535          * If there is an existing mapping, and the physical address has not
1536          * changed, must be protection or wiring change.
1537          */
1538         if ((tp = tsb_tte_lookup(pm, va)) != NULL && TTE_GET_PA(tp) == pa) {
1539                 CTR0(KTR_PMAP, "pmap_enter_locked: update");
1540                 PMAP_STATS_INC(pmap_nenter_update);
1541
1542                 /*
1543                  * Wiring change, just update stats.
1544                  */
1545                 if (wired) {
1546                         if ((tp->tte_data & TD_WIRED) == 0) {
1547                                 tp->tte_data |= TD_WIRED;
1548                                 pm->pm_stats.wired_count++;
1549                         }
1550                 } else {
1551                         if ((tp->tte_data & TD_WIRED) != 0) {
1552                                 tp->tte_data &= ~TD_WIRED;
1553                                 pm->pm_stats.wired_count--;
1554                         }
1555                 }
1556
1557                 /*
1558                  * Save the old bits and clear the ones we're interested in.
1559                  */
1560                 data = tp->tte_data;
1561                 tp->tte_data &= ~(TD_EXEC | TD_SW | TD_W);
1562
1563                 /*
1564                  * If we're turning off write permissions, sense modify status.
1565                  */
1566                 if ((prot & VM_PROT_WRITE) != 0) {
1567                         tp->tte_data |= TD_SW;
1568                         if (wired)
1569                                 tp->tte_data |= TD_W;
1570                         if ((m->oflags & VPO_UNMANAGED) == 0)
1571                                 vm_page_aflag_set(m, PGA_WRITEABLE);
1572                 } else if ((data & TD_W) != 0)
1573                         vm_page_dirty(m);
1574
1575                 /*
1576                  * If we're turning on execute permissions, flush the icache.
1577                  */
1578                 if ((prot & VM_PROT_EXECUTE) != 0) {
1579                         if ((data & TD_EXEC) == 0)
1580                                 icache_page_inval(pa);
1581                         tp->tte_data |= TD_EXEC;
1582                 }
1583
1584                 /*
1585                  * Delete the old mapping.
1586                  */
1587                 tlb_page_demap(pm, TTE_GET_VA(tp));
1588         } else {
1589                 /*
1590                  * If there is an existing mapping, but its for a different
1591                  * physical address, delete the old mapping.
1592                  */
1593                 if (tp != NULL) {
1594                         CTR0(KTR_PMAP, "pmap_enter_locked: replace");
1595                         PMAP_STATS_INC(pmap_nenter_replace);
1596                         pmap_remove_tte(pm, NULL, tp, va);
1597                         tlb_page_demap(pm, va);
1598                 } else {
1599                         CTR0(KTR_PMAP, "pmap_enter_locked: new");
1600                         PMAP_STATS_INC(pmap_nenter_new);
1601                 }
1602
1603                 /*
1604                  * Now set up the data and install the new mapping.
1605                  */
1606                 data = TD_V | TD_8K | TD_PA(pa);
1607                 if (pm == kernel_pmap)
1608                         data |= TD_P;
1609                 if ((prot & VM_PROT_WRITE) != 0) {
1610                         data |= TD_SW;
1611                         if ((m->oflags & VPO_UNMANAGED) == 0)
1612                                 vm_page_aflag_set(m, PGA_WRITEABLE);
1613                 }
1614                 if (prot & VM_PROT_EXECUTE) {
1615                         data |= TD_EXEC;
1616                         icache_page_inval(pa);
1617                 }
1618
1619                 /*
1620                  * If its wired update stats.  We also don't need reference or
1621                  * modify tracking for wired mappings, so set the bits now.
1622                  */
1623                 if (wired) {
1624                         pm->pm_stats.wired_count++;
1625                         data |= TD_REF | TD_WIRED;
1626                         if ((prot & VM_PROT_WRITE) != 0)
1627                                 data |= TD_W;
1628                 }
1629
1630                 tsb_tte_enter(pm, m, va, TS_8K, data);
1631         }
1632
1633         return (KERN_SUCCESS);
1634 }
1635
1636 /*
1637  * Maps a sequence of resident pages belonging to the same object.
1638  * The sequence begins with the given page m_start.  This page is
1639  * mapped at the given virtual address start.  Each subsequent page is
1640  * mapped at a virtual address that is offset from start by the same
1641  * amount as the page is offset from m_start within the object.  The
1642  * last page in the sequence is the page with the largest offset from
1643  * m_start that can be mapped at a virtual address less than the given
1644  * virtual address end.  Not every virtual page between start and end
1645  * is mapped; only those for which a resident page exists with the
1646  * corresponding offset from m_start are mapped.
1647  */
1648 void
1649 pmap_enter_object(pmap_t pm, vm_offset_t start, vm_offset_t end,
1650     vm_page_t m_start, vm_prot_t prot)
1651 {
1652         vm_page_t m;
1653         vm_pindex_t diff, psize;
1654
1655         VM_OBJECT_ASSERT_LOCKED(m_start->object);
1656
1657         psize = atop(end - start);
1658         m = m_start;
1659         rw_wlock(&tte_list_global_lock);
1660         PMAP_LOCK(pm);
1661         while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1662                 pmap_enter_locked(pm, start + ptoa(diff), m, prot &
1663                     (VM_PROT_READ | VM_PROT_EXECUTE), 0, 0);
1664                 m = TAILQ_NEXT(m, listq);
1665         }
1666         rw_wunlock(&tte_list_global_lock);
1667         PMAP_UNLOCK(pm);
1668 }
1669
1670 void
1671 pmap_enter_quick(pmap_t pm, vm_offset_t va, vm_page_t m, vm_prot_t prot)
1672 {
1673
1674         rw_wlock(&tte_list_global_lock);
1675         PMAP_LOCK(pm);
1676         pmap_enter_locked(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
1677             0, 0);
1678         rw_wunlock(&tte_list_global_lock);
1679         PMAP_UNLOCK(pm);
1680 }
1681
1682 void
1683 pmap_object_init_pt(pmap_t pm, vm_offset_t addr, vm_object_t object,
1684     vm_pindex_t pindex, vm_size_t size)
1685 {
1686
1687         VM_OBJECT_ASSERT_WLOCKED(object);
1688         KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
1689             ("pmap_object_init_pt: non-device object"));
1690 }
1691
1692 static int
1693 pmap_unwire_tte(pmap_t pm, pmap_t pm2, struct tte *tp, vm_offset_t va)
1694 {
1695
1696         PMAP_LOCK_ASSERT(pm, MA_OWNED);
1697         if ((tp->tte_data & TD_WIRED) == 0)
1698                 panic("pmap_unwire_tte: tp %p is missing TD_WIRED", tp);
1699         atomic_clear_long(&tp->tte_data, TD_WIRED);
1700         pm->pm_stats.wired_count--;
1701         return (1);
1702 }
1703
1704 /*
1705  * Clear the wired attribute from the mappings for the specified range of
1706  * addresses in the given pmap.  Every valid mapping within that range must
1707  * have the wired attribute set.  In contrast, invalid mappings cannot have
1708  * the wired attribute set, so they are ignored.
1709  *
1710  * The wired attribute of the translation table entry is not a hardware
1711  * feature, so there is no need to invalidate any TLB entries.
1712  */
1713 void
1714 pmap_unwire(pmap_t pm, vm_offset_t sva, vm_offset_t eva)
1715 {
1716         vm_offset_t va;
1717         struct tte *tp;
1718
1719         PMAP_LOCK(pm);
1720         if (eva - sva > PMAP_TSB_THRESH)
1721                 tsb_foreach(pm, NULL, sva, eva, pmap_unwire_tte);
1722         else {
1723                 for (va = sva; va < eva; va += PAGE_SIZE)
1724                         if ((tp = tsb_tte_lookup(pm, va)) != NULL)
1725                                 pmap_unwire_tte(pm, NULL, tp, va);
1726         }
1727         PMAP_UNLOCK(pm);
1728 }
1729
1730 static int
1731 pmap_copy_tte(pmap_t src_pmap, pmap_t dst_pmap, struct tte *tp,
1732     vm_offset_t va)
1733 {
1734         vm_page_t m;
1735         u_long data;
1736
1737         if ((tp->tte_data & TD_FAKE) != 0)
1738                 return (1);
1739         if (tsb_tte_lookup(dst_pmap, va) == NULL) {
1740                 data = tp->tte_data &
1741                     ~(TD_PV | TD_REF | TD_SW | TD_CV | TD_W);
1742                 m = PHYS_TO_VM_PAGE(TTE_GET_PA(tp));
1743                 tsb_tte_enter(dst_pmap, m, va, TS_8K, data);
1744         }
1745         return (1);
1746 }
1747
1748 void
1749 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
1750     vm_size_t len, vm_offset_t src_addr)
1751 {
1752         struct tte *tp;
1753         vm_offset_t va;
1754
1755         if (dst_addr != src_addr)
1756                 return;
1757         rw_wlock(&tte_list_global_lock);
1758         if (dst_pmap < src_pmap) {
1759                 PMAP_LOCK(dst_pmap);
1760                 PMAP_LOCK(src_pmap);
1761         } else {
1762                 PMAP_LOCK(src_pmap);
1763                 PMAP_LOCK(dst_pmap);
1764         }
1765         if (len > PMAP_TSB_THRESH) {
1766                 tsb_foreach(src_pmap, dst_pmap, src_addr, src_addr + len,
1767                     pmap_copy_tte);
1768                 tlb_context_demap(dst_pmap);
1769         } else {
1770                 for (va = src_addr; va < src_addr + len; va += PAGE_SIZE)
1771                         if ((tp = tsb_tte_lookup(src_pmap, va)) != NULL)
1772                                 pmap_copy_tte(src_pmap, dst_pmap, tp, va);
1773                 tlb_range_demap(dst_pmap, src_addr, src_addr + len - 1);
1774         }
1775         rw_wunlock(&tte_list_global_lock);
1776         PMAP_UNLOCK(src_pmap);
1777         PMAP_UNLOCK(dst_pmap);
1778 }
1779
1780 void
1781 pmap_zero_page(vm_page_t m)
1782 {
1783         struct tte *tp;
1784         vm_offset_t va;
1785         vm_paddr_t pa;
1786
1787         KASSERT((m->flags & PG_FICTITIOUS) == 0,
1788             ("pmap_zero_page: fake page"));
1789         PMAP_STATS_INC(pmap_nzero_page);
1790         pa = VM_PAGE_TO_PHYS(m);
1791         if (dcache_color_ignore != 0 || m->md.color == DCACHE_COLOR(pa)) {
1792                 PMAP_STATS_INC(pmap_nzero_page_c);
1793                 va = TLB_PHYS_TO_DIRECT(pa);
1794                 cpu_block_zero((void *)va, PAGE_SIZE);
1795         } else if (m->md.color == -1) {
1796                 PMAP_STATS_INC(pmap_nzero_page_nc);
1797                 aszero(ASI_PHYS_USE_EC, pa, PAGE_SIZE);
1798         } else {
1799                 PMAP_STATS_INC(pmap_nzero_page_oc);
1800                 PMAP_LOCK(kernel_pmap);
1801                 va = pmap_temp_map_1 + (m->md.color * PAGE_SIZE);
1802                 tp = tsb_kvtotte(va);
1803                 tp->tte_data = TD_V | TD_8K | TD_PA(pa) | TD_CP | TD_CV | TD_W;
1804                 tp->tte_vpn = TV_VPN(va, TS_8K);
1805                 cpu_block_zero((void *)va, PAGE_SIZE);
1806                 tlb_page_demap(kernel_pmap, va);
1807                 PMAP_UNLOCK(kernel_pmap);
1808         }
1809 }
1810
1811 void
1812 pmap_zero_page_area(vm_page_t m, int off, int size)
1813 {
1814         struct tte *tp;
1815         vm_offset_t va;
1816         vm_paddr_t pa;
1817
1818         KASSERT((m->flags & PG_FICTITIOUS) == 0,
1819             ("pmap_zero_page_area: fake page"));
1820         KASSERT(off + size <= PAGE_SIZE, ("pmap_zero_page_area: bad off/size"));
1821         PMAP_STATS_INC(pmap_nzero_page_area);
1822         pa = VM_PAGE_TO_PHYS(m);
1823         if (dcache_color_ignore != 0 || m->md.color == DCACHE_COLOR(pa)) {
1824                 PMAP_STATS_INC(pmap_nzero_page_area_c);
1825                 va = TLB_PHYS_TO_DIRECT(pa);
1826                 bzero((void *)(va + off), size);
1827         } else if (m->md.color == -1) {
1828                 PMAP_STATS_INC(pmap_nzero_page_area_nc);
1829                 aszero(ASI_PHYS_USE_EC, pa + off, size);
1830         } else {
1831                 PMAP_STATS_INC(pmap_nzero_page_area_oc);
1832                 PMAP_LOCK(kernel_pmap);
1833                 va = pmap_temp_map_1 + (m->md.color * PAGE_SIZE);
1834                 tp = tsb_kvtotte(va);
1835                 tp->tte_data = TD_V | TD_8K | TD_PA(pa) | TD_CP | TD_CV | TD_W;
1836                 tp->tte_vpn = TV_VPN(va, TS_8K);
1837                 bzero((void *)(va + off), size);
1838                 tlb_page_demap(kernel_pmap, va);
1839                 PMAP_UNLOCK(kernel_pmap);
1840         }
1841 }
1842
1843 void
1844 pmap_copy_page(vm_page_t msrc, vm_page_t mdst)
1845 {
1846         vm_offset_t vdst;
1847         vm_offset_t vsrc;
1848         vm_paddr_t pdst;
1849         vm_paddr_t psrc;
1850         struct tte *tp;
1851
1852         KASSERT((mdst->flags & PG_FICTITIOUS) == 0,
1853             ("pmap_copy_page: fake dst page"));
1854         KASSERT((msrc->flags & PG_FICTITIOUS) == 0,
1855             ("pmap_copy_page: fake src page"));
1856         PMAP_STATS_INC(pmap_ncopy_page);
1857         pdst = VM_PAGE_TO_PHYS(mdst);
1858         psrc = VM_PAGE_TO_PHYS(msrc);
1859         if (dcache_color_ignore != 0 ||
1860             (msrc->md.color == DCACHE_COLOR(psrc) &&
1861             mdst->md.color == DCACHE_COLOR(pdst))) {
1862                 PMAP_STATS_INC(pmap_ncopy_page_c);
1863                 vdst = TLB_PHYS_TO_DIRECT(pdst);
1864                 vsrc = TLB_PHYS_TO_DIRECT(psrc);
1865                 cpu_block_copy((void *)vsrc, (void *)vdst, PAGE_SIZE);
1866         } else if (msrc->md.color == -1 && mdst->md.color == -1) {
1867                 PMAP_STATS_INC(pmap_ncopy_page_nc);
1868                 ascopy(ASI_PHYS_USE_EC, psrc, pdst, PAGE_SIZE);
1869         } else if (msrc->md.color == -1) {
1870                 if (mdst->md.color == DCACHE_COLOR(pdst)) {
1871                         PMAP_STATS_INC(pmap_ncopy_page_dc);
1872                         vdst = TLB_PHYS_TO_DIRECT(pdst);
1873                         ascopyfrom(ASI_PHYS_USE_EC, psrc, (void *)vdst,
1874                             PAGE_SIZE);
1875                 } else {
1876                         PMAP_STATS_INC(pmap_ncopy_page_doc);
1877                         PMAP_LOCK(kernel_pmap);
1878                         vdst = pmap_temp_map_1 + (mdst->md.color * PAGE_SIZE);
1879                         tp = tsb_kvtotte(vdst);
1880                         tp->tte_data =
1881                             TD_V | TD_8K | TD_PA(pdst) | TD_CP | TD_CV | TD_W;
1882                         tp->tte_vpn = TV_VPN(vdst, TS_8K);
1883                         ascopyfrom(ASI_PHYS_USE_EC, psrc, (void *)vdst,
1884                             PAGE_SIZE);
1885                         tlb_page_demap(kernel_pmap, vdst);
1886                         PMAP_UNLOCK(kernel_pmap);
1887                 }
1888         } else if (mdst->md.color == -1) {
1889                 if (msrc->md.color == DCACHE_COLOR(psrc)) {
1890                         PMAP_STATS_INC(pmap_ncopy_page_sc);
1891                         vsrc = TLB_PHYS_TO_DIRECT(psrc);
1892                         ascopyto((void *)vsrc, ASI_PHYS_USE_EC, pdst,
1893                             PAGE_SIZE);
1894                 } else {
1895                         PMAP_STATS_INC(pmap_ncopy_page_soc);
1896                         PMAP_LOCK(kernel_pmap);
1897                         vsrc = pmap_temp_map_1 + (msrc->md.color * PAGE_SIZE);
1898                         tp = tsb_kvtotte(vsrc);
1899                         tp->tte_data =
1900                             TD_V | TD_8K | TD_PA(psrc) | TD_CP | TD_CV | TD_W;
1901                         tp->tte_vpn = TV_VPN(vsrc, TS_8K);
1902                         ascopyto((void *)vsrc, ASI_PHYS_USE_EC, pdst,
1903                             PAGE_SIZE);
1904                         tlb_page_demap(kernel_pmap, vsrc);
1905                         PMAP_UNLOCK(kernel_pmap);
1906                 }
1907         } else {
1908                 PMAP_STATS_INC(pmap_ncopy_page_oc);
1909                 PMAP_LOCK(kernel_pmap);
1910                 vdst = pmap_temp_map_1 + (mdst->md.color * PAGE_SIZE);
1911                 tp = tsb_kvtotte(vdst);
1912                 tp->tte_data =
1913                     TD_V | TD_8K | TD_PA(pdst) | TD_CP | TD_CV | TD_W;
1914                 tp->tte_vpn = TV_VPN(vdst, TS_8K);
1915                 vsrc = pmap_temp_map_2 + (msrc->md.color * PAGE_SIZE);
1916                 tp = tsb_kvtotte(vsrc);
1917                 tp->tte_data =
1918                     TD_V | TD_8K | TD_PA(psrc) | TD_CP | TD_CV | TD_W;
1919                 tp->tte_vpn = TV_VPN(vsrc, TS_8K);
1920                 cpu_block_copy((void *)vsrc, (void *)vdst, PAGE_SIZE);
1921                 tlb_page_demap(kernel_pmap, vdst);
1922                 tlb_page_demap(kernel_pmap, vsrc);
1923                 PMAP_UNLOCK(kernel_pmap);
1924         }
1925 }
1926
1927 vm_offset_t
1928 pmap_quick_enter_page(vm_page_t m)
1929 {
1930         vm_paddr_t pa;
1931         vm_offset_t qaddr;
1932         struct tte *tp;
1933
1934         pa = VM_PAGE_TO_PHYS(m);
1935         if (dcache_color_ignore != 0 || m->md.color == DCACHE_COLOR(pa))
1936                 return (TLB_PHYS_TO_DIRECT(pa));
1937
1938         critical_enter();
1939         qaddr = PCPU_GET(qmap_addr);
1940         qaddr += (PAGE_SIZE * ((DCACHE_COLORS + DCACHE_COLOR(pa) -
1941             DCACHE_COLOR(qaddr)) % DCACHE_COLORS));
1942         tp = tsb_kvtotte(qaddr);
1943
1944         KASSERT(tp->tte_data == 0, ("pmap_quick_enter_page: PTE busy"));
1945         
1946         tp->tte_data = TD_V | TD_8K | TD_PA(pa) | TD_CP | TD_CV | TD_W;
1947         tp->tte_vpn = TV_VPN(qaddr, TS_8K);
1948
1949         return (qaddr);
1950 }
1951
1952 void
1953 pmap_quick_remove_page(vm_offset_t addr)
1954 {
1955         vm_offset_t qaddr;
1956         struct tte *tp;
1957
1958         if (addr >= VM_MIN_DIRECT_ADDRESS)
1959                 return;
1960
1961         tp = tsb_kvtotte(addr);
1962         qaddr = PCPU_GET(qmap_addr);
1963         
1964         KASSERT((addr >= qaddr) && (addr < (qaddr + (PAGE_SIZE * DCACHE_COLORS))),
1965             ("pmap_quick_remove_page: invalid address"));
1966         KASSERT(tp->tte_data != 0, ("pmap_quick_remove_page: PTE not in use"));
1967         
1968         stxa(TLB_DEMAP_VA(addr) | TLB_DEMAP_NUCLEUS | TLB_DEMAP_PAGE, ASI_DMMU_DEMAP, 0);
1969         stxa(TLB_DEMAP_VA(addr) | TLB_DEMAP_NUCLEUS | TLB_DEMAP_PAGE, ASI_IMMU_DEMAP, 0);
1970         flush(KERNBASE);
1971         TTE_ZERO(tp);
1972         critical_exit();
1973 }
1974
1975 int unmapped_buf_allowed;
1976
1977 void
1978 pmap_copy_pages(vm_page_t ma[], vm_offset_t a_offset, vm_page_t mb[],
1979     vm_offset_t b_offset, int xfersize)
1980 {
1981
1982         panic("pmap_copy_pages: not implemented");
1983 }
1984
1985 /*
1986  * Returns true if the pmap's pv is one of the first
1987  * 16 pvs linked to from this page.  This count may
1988  * be changed upwards or downwards in the future; it
1989  * is only necessary that true be returned for a small
1990  * subset of pmaps for proper page aging.
1991  */
1992 boolean_t
1993 pmap_page_exists_quick(pmap_t pm, vm_page_t m)
1994 {
1995         struct tte *tp;
1996         int loops;
1997         boolean_t rv;
1998
1999         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2000             ("pmap_page_exists_quick: page %p is not managed", m));
2001         loops = 0;
2002         rv = FALSE;
2003         rw_wlock(&tte_list_global_lock);
2004         TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
2005                 if ((tp->tte_data & TD_PV) == 0)
2006                         continue;
2007                 if (TTE_GET_PMAP(tp) == pm) {
2008                         rv = TRUE;
2009                         break;
2010                 }
2011                 if (++loops >= 16)
2012                         break;
2013         }
2014         rw_wunlock(&tte_list_global_lock);
2015         return (rv);
2016 }
2017
2018 /*
2019  * Return the number of managed mappings to the given physical page
2020  * that are wired.
2021  */
2022 int
2023 pmap_page_wired_mappings(vm_page_t m)
2024 {
2025         struct tte *tp;
2026         int count;
2027
2028         count = 0;
2029         if ((m->oflags & VPO_UNMANAGED) != 0)
2030                 return (count);
2031         rw_wlock(&tte_list_global_lock);
2032         TAILQ_FOREACH(tp, &m->md.tte_list, tte_link)
2033                 if ((tp->tte_data & (TD_PV | TD_WIRED)) == (TD_PV | TD_WIRED))
2034                         count++;
2035         rw_wunlock(&tte_list_global_lock);
2036         return (count);
2037 }
2038
2039 /*
2040  * Remove all pages from specified address space, this aids process exit
2041  * speeds.  This is much faster than pmap_remove in the case of running down
2042  * an entire address space.  Only works for the current pmap.
2043  */
2044 void
2045 pmap_remove_pages(pmap_t pm)
2046 {
2047
2048 }
2049
2050 /*
2051  * Returns TRUE if the given page has a managed mapping.
2052  */
2053 boolean_t
2054 pmap_page_is_mapped(vm_page_t m)
2055 {
2056         struct tte *tp;
2057         boolean_t rv;
2058
2059         rv = FALSE;
2060         if ((m->oflags & VPO_UNMANAGED) != 0)
2061                 return (rv);
2062         rw_wlock(&tte_list_global_lock);
2063         TAILQ_FOREACH(tp, &m->md.tte_list, tte_link)
2064                 if ((tp->tte_data & TD_PV) != 0) {
2065                         rv = TRUE;
2066                         break;
2067                 }
2068         rw_wunlock(&tte_list_global_lock);
2069         return (rv);
2070 }
2071
2072 /*
2073  * Return a count of reference bits for a page, clearing those bits.
2074  * It is not necessary for every reference bit to be cleared, but it
2075  * is necessary that 0 only be returned when there are truly no
2076  * reference bits set.
2077  *
2078  * As an optimization, update the page's dirty field if a modified bit is
2079  * found while counting reference bits.  This opportunistic update can be
2080  * performed at low cost and can eliminate the need for some future calls
2081  * to pmap_is_modified().  However, since this function stops after
2082  * finding PMAP_TS_REFERENCED_MAX reference bits, it may not detect some
2083  * dirty pages.  Those dirty pages will only be detected by a future call
2084  * to pmap_is_modified().
2085  */
2086 int
2087 pmap_ts_referenced(vm_page_t m)
2088 {
2089         struct tte *tpf;
2090         struct tte *tpn;
2091         struct tte *tp;
2092         u_long data;
2093         int count;
2094
2095         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2096             ("pmap_ts_referenced: page %p is not managed", m));
2097         count = 0;
2098         rw_wlock(&tte_list_global_lock);
2099         if ((tp = TAILQ_FIRST(&m->md.tte_list)) != NULL) {
2100                 tpf = tp;
2101                 do {
2102                         tpn = TAILQ_NEXT(tp, tte_link);
2103                         TAILQ_REMOVE(&m->md.tte_list, tp, tte_link);
2104                         TAILQ_INSERT_TAIL(&m->md.tte_list, tp, tte_link);
2105                         if ((tp->tte_data & TD_PV) == 0)
2106                                 continue;
2107                         data = atomic_clear_long(&tp->tte_data, TD_REF);
2108                         if ((data & TD_W) != 0)
2109                                 vm_page_dirty(m);
2110                         if ((data & TD_REF) != 0 && ++count >=
2111                             PMAP_TS_REFERENCED_MAX)
2112                                 break;
2113                 } while ((tp = tpn) != NULL && tp != tpf);
2114         }
2115         rw_wunlock(&tte_list_global_lock);
2116         return (count);
2117 }
2118
2119 boolean_t
2120 pmap_is_modified(vm_page_t m)
2121 {
2122         struct tte *tp;
2123         boolean_t rv;
2124
2125         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2126             ("pmap_is_modified: page %p is not managed", m));
2127         rv = FALSE;
2128
2129         /*
2130          * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
2131          * concurrently set while the object is locked.  Thus, if PGA_WRITEABLE
2132          * is clear, no TTEs can have TD_W set.
2133          */
2134         VM_OBJECT_ASSERT_WLOCKED(m->object);
2135         if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
2136                 return (rv);
2137         rw_wlock(&tte_list_global_lock);
2138         TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
2139                 if ((tp->tte_data & TD_PV) == 0)
2140                         continue;
2141                 if ((tp->tte_data & TD_W) != 0) {
2142                         rv = TRUE;
2143                         break;
2144                 }
2145         }
2146         rw_wunlock(&tte_list_global_lock);
2147         return (rv);
2148 }
2149
2150 /*
2151  *      pmap_is_prefaultable:
2152  *
2153  *      Return whether or not the specified virtual address is elgible
2154  *      for prefault.
2155  */
2156 boolean_t
2157 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
2158 {
2159         boolean_t rv;
2160
2161         PMAP_LOCK(pmap);
2162         rv = tsb_tte_lookup(pmap, addr) == NULL;
2163         PMAP_UNLOCK(pmap);
2164         return (rv);
2165 }
2166
2167 /*
2168  * Return whether or not the specified physical page was referenced
2169  * in any physical maps.
2170  */
2171 boolean_t
2172 pmap_is_referenced(vm_page_t m)
2173 {
2174         struct tte *tp;
2175         boolean_t rv;
2176
2177         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2178             ("pmap_is_referenced: page %p is not managed", m));
2179         rv = FALSE;
2180         rw_wlock(&tte_list_global_lock);
2181         TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
2182                 if ((tp->tte_data & TD_PV) == 0)
2183                         continue;
2184                 if ((tp->tte_data & TD_REF) != 0) {
2185                         rv = TRUE;
2186                         break;
2187                 }
2188         }
2189         rw_wunlock(&tte_list_global_lock);
2190         return (rv);
2191 }
2192
2193 /*
2194  * This function is advisory.
2195  */
2196 void
2197 pmap_advise(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, int advice)
2198 {
2199 }
2200
2201 void
2202 pmap_clear_modify(vm_page_t m)
2203 {
2204         struct tte *tp;
2205         u_long data;
2206
2207         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2208             ("pmap_clear_modify: page %p is not managed", m));
2209         VM_OBJECT_ASSERT_WLOCKED(m->object);
2210         KASSERT(!vm_page_xbusied(m),
2211             ("pmap_clear_modify: page %p is exclusive busied", m));
2212
2213         /*
2214          * If the page is not PGA_WRITEABLE, then no TTEs can have TD_W set.
2215          * If the object containing the page is locked and the page is not
2216          * exclusive busied, then PGA_WRITEABLE cannot be concurrently set.
2217          */
2218         if ((m->aflags & PGA_WRITEABLE) == 0)
2219                 return;
2220         rw_wlock(&tte_list_global_lock);
2221         TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
2222                 if ((tp->tte_data & TD_PV) == 0)
2223                         continue;
2224                 data = atomic_clear_long(&tp->tte_data, TD_W);
2225                 if ((data & TD_W) != 0)
2226                         tlb_page_demap(TTE_GET_PMAP(tp), TTE_GET_VA(tp));
2227         }
2228         rw_wunlock(&tte_list_global_lock);
2229 }
2230
2231 void
2232 pmap_remove_write(vm_page_t m)
2233 {
2234         struct tte *tp;
2235         u_long data;
2236
2237         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2238             ("pmap_remove_write: page %p is not managed", m));
2239
2240         /*
2241          * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
2242          * set by another thread while the object is locked.  Thus,
2243          * if PGA_WRITEABLE is clear, no page table entries need updating.
2244          */
2245         VM_OBJECT_ASSERT_WLOCKED(m->object);
2246         if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
2247                 return;
2248         rw_wlock(&tte_list_global_lock);
2249         TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
2250                 if ((tp->tte_data & TD_PV) == 0)
2251                         continue;
2252                 data = atomic_clear_long(&tp->tte_data, TD_SW | TD_W);
2253                 if ((data & TD_W) != 0) {
2254                         vm_page_dirty(m);
2255                         tlb_page_demap(TTE_GET_PMAP(tp), TTE_GET_VA(tp));
2256                 }
2257         }
2258         vm_page_aflag_clear(m, PGA_WRITEABLE);
2259         rw_wunlock(&tte_list_global_lock);
2260 }
2261
2262 int
2263 pmap_mincore(pmap_t pm, vm_offset_t addr, vm_paddr_t *locked_pa)
2264 {
2265
2266         /* TODO; */
2267         return (0);
2268 }
2269
2270 /*
2271  * Activate a user pmap.  The pmap must be activated before its address space
2272  * can be accessed in any way.
2273  */
2274 void
2275 pmap_activate(struct thread *td)
2276 {
2277         struct vmspace *vm;
2278         struct pmap *pm;
2279         int context;
2280
2281         critical_enter();
2282         vm = td->td_proc->p_vmspace;
2283         pm = vmspace_pmap(vm);
2284
2285         context = PCPU_GET(tlb_ctx);
2286         if (context == PCPU_GET(tlb_ctx_max)) {
2287                 tlb_flush_user();
2288                 context = PCPU_GET(tlb_ctx_min);
2289         }
2290         PCPU_SET(tlb_ctx, context + 1);
2291
2292         pm->pm_context[curcpu] = context;
2293 #ifdef SMP
2294         CPU_SET_ATOMIC(PCPU_GET(cpuid), &pm->pm_active);
2295         atomic_store_acq_ptr((uintptr_t *)PCPU_PTR(pmap), (uintptr_t)pm);
2296 #else
2297         CPU_SET(PCPU_GET(cpuid), &pm->pm_active);
2298         PCPU_SET(pmap, pm);
2299 #endif
2300
2301         stxa(AA_DMMU_TSB, ASI_DMMU, pm->pm_tsb);
2302         stxa(AA_IMMU_TSB, ASI_IMMU, pm->pm_tsb);
2303         stxa(AA_DMMU_PCXR, ASI_DMMU, (ldxa(AA_DMMU_PCXR, ASI_DMMU) &
2304             TLB_CXR_PGSZ_MASK) | context);
2305         flush(KERNBASE);
2306         critical_exit();
2307 }
2308
2309 void
2310 pmap_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
2311 {
2312
2313 }
2314
2315 /*
2316  * Increase the starting virtual address of the given mapping if a
2317  * different alignment might result in more superpage mappings.
2318  */
2319 void
2320 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
2321     vm_offset_t *addr, vm_size_t size)
2322 {
2323
2324 }