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