]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sparc64/sparc64/pmap.c
Import DTS files from Linux 5.3
[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 && !vm_page_xbusied(m))
1504                 VM_OBJECT_ASSERT_LOCKED(m->object);
1505         PMAP_STATS_INC(pmap_nenter);
1506         pa = VM_PAGE_TO_PHYS(m);
1507         wired = (flags & PMAP_ENTER_WIRED) != 0;
1508
1509         /*
1510          * If this is a fake page from the device_pager, but it covers actual
1511          * physical memory, convert to the real backing page.
1512          */
1513         if ((m->flags & PG_FICTITIOUS) != 0) {
1514                 real = vm_phys_paddr_to_vm_page(pa);
1515                 if (real != NULL)
1516                         m = real;
1517         }
1518
1519         CTR6(KTR_PMAP,
1520             "pmap_enter_locked: ctx=%p m=%p va=%#lx pa=%#lx prot=%#x wired=%d",
1521             pm->pm_context[curcpu], m, va, pa, prot, wired);
1522
1523         /*
1524          * If there is an existing mapping, and the physical address has not
1525          * changed, must be protection or wiring change.
1526          */
1527         if ((tp = tsb_tte_lookup(pm, va)) != NULL && TTE_GET_PA(tp) == pa) {
1528                 CTR0(KTR_PMAP, "pmap_enter_locked: update");
1529                 PMAP_STATS_INC(pmap_nenter_update);
1530
1531                 /*
1532                  * Wiring change, just update stats.
1533                  */
1534                 if (wired) {
1535                         if ((tp->tte_data & TD_WIRED) == 0) {
1536                                 tp->tte_data |= TD_WIRED;
1537                                 pm->pm_stats.wired_count++;
1538                         }
1539                 } else {
1540                         if ((tp->tte_data & TD_WIRED) != 0) {
1541                                 tp->tte_data &= ~TD_WIRED;
1542                                 pm->pm_stats.wired_count--;
1543                         }
1544                 }
1545
1546                 /*
1547                  * Save the old bits and clear the ones we're interested in.
1548                  */
1549                 data = tp->tte_data;
1550                 tp->tte_data &= ~(TD_EXEC | TD_SW | TD_W);
1551
1552                 /*
1553                  * If we're turning off write permissions, sense modify status.
1554                  */
1555                 if ((prot & VM_PROT_WRITE) != 0) {
1556                         tp->tte_data |= TD_SW;
1557                         if (wired)
1558                                 tp->tte_data |= TD_W;
1559                         if ((m->oflags & VPO_UNMANAGED) == 0)
1560                                 vm_page_aflag_set(m, PGA_WRITEABLE);
1561                 } else if ((data & TD_W) != 0)
1562                         vm_page_dirty(m);
1563
1564                 /*
1565                  * If we're turning on execute permissions, flush the icache.
1566                  */
1567                 if ((prot & VM_PROT_EXECUTE) != 0) {
1568                         if ((data & TD_EXEC) == 0)
1569                                 icache_page_inval(pa);
1570                         tp->tte_data |= TD_EXEC;
1571                 }
1572
1573                 /*
1574                  * Delete the old mapping.
1575                  */
1576                 tlb_page_demap(pm, TTE_GET_VA(tp));
1577         } else {
1578                 /*
1579                  * If there is an existing mapping, but its for a different
1580                  * physical address, delete the old mapping.
1581                  */
1582                 if (tp != NULL) {
1583                         CTR0(KTR_PMAP, "pmap_enter_locked: replace");
1584                         PMAP_STATS_INC(pmap_nenter_replace);
1585                         pmap_remove_tte(pm, NULL, tp, va);
1586                         tlb_page_demap(pm, va);
1587                 } else {
1588                         CTR0(KTR_PMAP, "pmap_enter_locked: new");
1589                         PMAP_STATS_INC(pmap_nenter_new);
1590                 }
1591
1592                 /*
1593                  * Now set up the data and install the new mapping.
1594                  */
1595                 data = TD_V | TD_8K | TD_PA(pa);
1596                 if (pm == kernel_pmap)
1597                         data |= TD_P;
1598                 if ((prot & VM_PROT_WRITE) != 0) {
1599                         data |= TD_SW;
1600                         if ((m->oflags & VPO_UNMANAGED) == 0)
1601                                 vm_page_aflag_set(m, PGA_WRITEABLE);
1602                 }
1603                 if (prot & VM_PROT_EXECUTE) {
1604                         data |= TD_EXEC;
1605                         icache_page_inval(pa);
1606                 }
1607
1608                 /*
1609                  * If its wired update stats.  We also don't need reference or
1610                  * modify tracking for wired mappings, so set the bits now.
1611                  */
1612                 if (wired) {
1613                         pm->pm_stats.wired_count++;
1614                         data |= TD_REF | TD_WIRED;
1615                         if ((prot & VM_PROT_WRITE) != 0)
1616                                 data |= TD_W;
1617                 }
1618
1619                 tsb_tte_enter(pm, m, va, TS_8K, data);
1620         }
1621
1622         return (KERN_SUCCESS);
1623 }
1624
1625 /*
1626  * Maps a sequence of resident pages belonging to the same object.
1627  * The sequence begins with the given page m_start.  This page is
1628  * mapped at the given virtual address start.  Each subsequent page is
1629  * mapped at a virtual address that is offset from start by the same
1630  * amount as the page is offset from m_start within the object.  The
1631  * last page in the sequence is the page with the largest offset from
1632  * m_start that can be mapped at a virtual address less than the given
1633  * virtual address end.  Not every virtual page between start and end
1634  * is mapped; only those for which a resident page exists with the
1635  * corresponding offset from m_start are mapped.
1636  */
1637 void
1638 pmap_enter_object(pmap_t pm, vm_offset_t start, vm_offset_t end,
1639     vm_page_t m_start, vm_prot_t prot)
1640 {
1641         vm_page_t m;
1642         vm_pindex_t diff, psize;
1643
1644         VM_OBJECT_ASSERT_LOCKED(m_start->object);
1645
1646         psize = atop(end - start);
1647         m = m_start;
1648         rw_wlock(&tte_list_global_lock);
1649         PMAP_LOCK(pm);
1650         while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1651                 pmap_enter_locked(pm, start + ptoa(diff), m, prot &
1652                     (VM_PROT_READ | VM_PROT_EXECUTE), 0, 0);
1653                 m = TAILQ_NEXT(m, listq);
1654         }
1655         rw_wunlock(&tte_list_global_lock);
1656         PMAP_UNLOCK(pm);
1657 }
1658
1659 void
1660 pmap_enter_quick(pmap_t pm, vm_offset_t va, vm_page_t m, vm_prot_t prot)
1661 {
1662
1663         rw_wlock(&tte_list_global_lock);
1664         PMAP_LOCK(pm);
1665         pmap_enter_locked(pm, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
1666             0, 0);
1667         rw_wunlock(&tte_list_global_lock);
1668         PMAP_UNLOCK(pm);
1669 }
1670
1671 void
1672 pmap_object_init_pt(pmap_t pm, vm_offset_t addr, vm_object_t object,
1673     vm_pindex_t pindex, vm_size_t size)
1674 {
1675
1676         VM_OBJECT_ASSERT_WLOCKED(object);
1677         KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
1678             ("pmap_object_init_pt: non-device object"));
1679 }
1680
1681 static int
1682 pmap_unwire_tte(pmap_t pm, pmap_t pm2, struct tte *tp, vm_offset_t va)
1683 {
1684
1685         PMAP_LOCK_ASSERT(pm, MA_OWNED);
1686         if ((tp->tte_data & TD_WIRED) == 0)
1687                 panic("pmap_unwire_tte: tp %p is missing TD_WIRED", tp);
1688         atomic_clear_long(&tp->tte_data, TD_WIRED);
1689         pm->pm_stats.wired_count--;
1690         return (1);
1691 }
1692
1693 /*
1694  * Clear the wired attribute from the mappings for the specified range of
1695  * addresses in the given pmap.  Every valid mapping within that range must
1696  * have the wired attribute set.  In contrast, invalid mappings cannot have
1697  * the wired attribute set, so they are ignored.
1698  *
1699  * The wired attribute of the translation table entry is not a hardware
1700  * feature, so there is no need to invalidate any TLB entries.
1701  */
1702 void
1703 pmap_unwire(pmap_t pm, vm_offset_t sva, vm_offset_t eva)
1704 {
1705         vm_offset_t va;
1706         struct tte *tp;
1707
1708         PMAP_LOCK(pm);
1709         if (eva - sva > PMAP_TSB_THRESH)
1710                 tsb_foreach(pm, NULL, sva, eva, pmap_unwire_tte);
1711         else {
1712                 for (va = sva; va < eva; va += PAGE_SIZE)
1713                         if ((tp = tsb_tte_lookup(pm, va)) != NULL)
1714                                 pmap_unwire_tte(pm, NULL, tp, va);
1715         }
1716         PMAP_UNLOCK(pm);
1717 }
1718
1719 static int
1720 pmap_copy_tte(pmap_t src_pmap, pmap_t dst_pmap, struct tte *tp,
1721     vm_offset_t va)
1722 {
1723         vm_page_t m;
1724         u_long data;
1725
1726         if ((tp->tte_data & TD_FAKE) != 0)
1727                 return (1);
1728         if (tsb_tte_lookup(dst_pmap, va) == NULL) {
1729                 data = tp->tte_data &
1730                     ~(TD_PV | TD_REF | TD_SW | TD_CV | TD_W);
1731                 m = PHYS_TO_VM_PAGE(TTE_GET_PA(tp));
1732                 tsb_tte_enter(dst_pmap, m, va, TS_8K, data);
1733         }
1734         return (1);
1735 }
1736
1737 void
1738 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
1739     vm_size_t len, vm_offset_t src_addr)
1740 {
1741         struct tte *tp;
1742         vm_offset_t va;
1743
1744         if (dst_addr != src_addr)
1745                 return;
1746         rw_wlock(&tte_list_global_lock);
1747         if (dst_pmap < src_pmap) {
1748                 PMAP_LOCK(dst_pmap);
1749                 PMAP_LOCK(src_pmap);
1750         } else {
1751                 PMAP_LOCK(src_pmap);
1752                 PMAP_LOCK(dst_pmap);
1753         }
1754         if (len > PMAP_TSB_THRESH) {
1755                 tsb_foreach(src_pmap, dst_pmap, src_addr, src_addr + len,
1756                     pmap_copy_tte);
1757                 tlb_context_demap(dst_pmap);
1758         } else {
1759                 for (va = src_addr; va < src_addr + len; va += PAGE_SIZE)
1760                         if ((tp = tsb_tte_lookup(src_pmap, va)) != NULL)
1761                                 pmap_copy_tte(src_pmap, dst_pmap, tp, va);
1762                 tlb_range_demap(dst_pmap, src_addr, src_addr + len - 1);
1763         }
1764         rw_wunlock(&tte_list_global_lock);
1765         PMAP_UNLOCK(src_pmap);
1766         PMAP_UNLOCK(dst_pmap);
1767 }
1768
1769 void
1770 pmap_zero_page(vm_page_t m)
1771 {
1772         struct tte *tp;
1773         vm_offset_t va;
1774         vm_paddr_t pa;
1775
1776         KASSERT((m->flags & PG_FICTITIOUS) == 0,
1777             ("pmap_zero_page: fake page"));
1778         PMAP_STATS_INC(pmap_nzero_page);
1779         pa = VM_PAGE_TO_PHYS(m);
1780         if (dcache_color_ignore != 0 || m->md.color == DCACHE_COLOR(pa)) {
1781                 PMAP_STATS_INC(pmap_nzero_page_c);
1782                 va = TLB_PHYS_TO_DIRECT(pa);
1783                 cpu_block_zero((void *)va, PAGE_SIZE);
1784         } else if (m->md.color == -1) {
1785                 PMAP_STATS_INC(pmap_nzero_page_nc);
1786                 aszero(ASI_PHYS_USE_EC, pa, PAGE_SIZE);
1787         } else {
1788                 PMAP_STATS_INC(pmap_nzero_page_oc);
1789                 PMAP_LOCK(kernel_pmap);
1790                 va = pmap_temp_map_1 + (m->md.color * PAGE_SIZE);
1791                 tp = tsb_kvtotte(va);
1792                 tp->tte_data = TD_V | TD_8K | TD_PA(pa) | TD_CP | TD_CV | TD_W;
1793                 tp->tte_vpn = TV_VPN(va, TS_8K);
1794                 cpu_block_zero((void *)va, PAGE_SIZE);
1795                 tlb_page_demap(kernel_pmap, va);
1796                 PMAP_UNLOCK(kernel_pmap);
1797         }
1798 }
1799
1800 void
1801 pmap_zero_page_area(vm_page_t m, int off, int size)
1802 {
1803         struct tte *tp;
1804         vm_offset_t va;
1805         vm_paddr_t pa;
1806
1807         KASSERT((m->flags & PG_FICTITIOUS) == 0,
1808             ("pmap_zero_page_area: fake page"));
1809         KASSERT(off + size <= PAGE_SIZE, ("pmap_zero_page_area: bad off/size"));
1810         PMAP_STATS_INC(pmap_nzero_page_area);
1811         pa = VM_PAGE_TO_PHYS(m);
1812         if (dcache_color_ignore != 0 || m->md.color == DCACHE_COLOR(pa)) {
1813                 PMAP_STATS_INC(pmap_nzero_page_area_c);
1814                 va = TLB_PHYS_TO_DIRECT(pa);
1815                 bzero((void *)(va + off), size);
1816         } else if (m->md.color == -1) {
1817                 PMAP_STATS_INC(pmap_nzero_page_area_nc);
1818                 aszero(ASI_PHYS_USE_EC, pa + off, size);
1819         } else {
1820                 PMAP_STATS_INC(pmap_nzero_page_area_oc);
1821                 PMAP_LOCK(kernel_pmap);
1822                 va = pmap_temp_map_1 + (m->md.color * PAGE_SIZE);
1823                 tp = tsb_kvtotte(va);
1824                 tp->tte_data = TD_V | TD_8K | TD_PA(pa) | TD_CP | TD_CV | TD_W;
1825                 tp->tte_vpn = TV_VPN(va, TS_8K);
1826                 bzero((void *)(va + off), size);
1827                 tlb_page_demap(kernel_pmap, va);
1828                 PMAP_UNLOCK(kernel_pmap);
1829         }
1830 }
1831
1832 void
1833 pmap_copy_page(vm_page_t msrc, vm_page_t mdst)
1834 {
1835         vm_offset_t vdst;
1836         vm_offset_t vsrc;
1837         vm_paddr_t pdst;
1838         vm_paddr_t psrc;
1839         struct tte *tp;
1840
1841         KASSERT((mdst->flags & PG_FICTITIOUS) == 0,
1842             ("pmap_copy_page: fake dst page"));
1843         KASSERT((msrc->flags & PG_FICTITIOUS) == 0,
1844             ("pmap_copy_page: fake src page"));
1845         PMAP_STATS_INC(pmap_ncopy_page);
1846         pdst = VM_PAGE_TO_PHYS(mdst);
1847         psrc = VM_PAGE_TO_PHYS(msrc);
1848         if (dcache_color_ignore != 0 ||
1849             (msrc->md.color == DCACHE_COLOR(psrc) &&
1850             mdst->md.color == DCACHE_COLOR(pdst))) {
1851                 PMAP_STATS_INC(pmap_ncopy_page_c);
1852                 vdst = TLB_PHYS_TO_DIRECT(pdst);
1853                 vsrc = TLB_PHYS_TO_DIRECT(psrc);
1854                 cpu_block_copy((void *)vsrc, (void *)vdst, PAGE_SIZE);
1855         } else if (msrc->md.color == -1 && mdst->md.color == -1) {
1856                 PMAP_STATS_INC(pmap_ncopy_page_nc);
1857                 ascopy(ASI_PHYS_USE_EC, psrc, pdst, PAGE_SIZE);
1858         } else if (msrc->md.color == -1) {
1859                 if (mdst->md.color == DCACHE_COLOR(pdst)) {
1860                         PMAP_STATS_INC(pmap_ncopy_page_dc);
1861                         vdst = TLB_PHYS_TO_DIRECT(pdst);
1862                         ascopyfrom(ASI_PHYS_USE_EC, psrc, (void *)vdst,
1863                             PAGE_SIZE);
1864                 } else {
1865                         PMAP_STATS_INC(pmap_ncopy_page_doc);
1866                         PMAP_LOCK(kernel_pmap);
1867                         vdst = pmap_temp_map_1 + (mdst->md.color * PAGE_SIZE);
1868                         tp = tsb_kvtotte(vdst);
1869                         tp->tte_data =
1870                             TD_V | TD_8K | TD_PA(pdst) | TD_CP | TD_CV | TD_W;
1871                         tp->tte_vpn = TV_VPN(vdst, TS_8K);
1872                         ascopyfrom(ASI_PHYS_USE_EC, psrc, (void *)vdst,
1873                             PAGE_SIZE);
1874                         tlb_page_demap(kernel_pmap, vdst);
1875                         PMAP_UNLOCK(kernel_pmap);
1876                 }
1877         } else if (mdst->md.color == -1) {
1878                 if (msrc->md.color == DCACHE_COLOR(psrc)) {
1879                         PMAP_STATS_INC(pmap_ncopy_page_sc);
1880                         vsrc = TLB_PHYS_TO_DIRECT(psrc);
1881                         ascopyto((void *)vsrc, ASI_PHYS_USE_EC, pdst,
1882                             PAGE_SIZE);
1883                 } else {
1884                         PMAP_STATS_INC(pmap_ncopy_page_soc);
1885                         PMAP_LOCK(kernel_pmap);
1886                         vsrc = pmap_temp_map_1 + (msrc->md.color * PAGE_SIZE);
1887                         tp = tsb_kvtotte(vsrc);
1888                         tp->tte_data =
1889                             TD_V | TD_8K | TD_PA(psrc) | TD_CP | TD_CV | TD_W;
1890                         tp->tte_vpn = TV_VPN(vsrc, TS_8K);
1891                         ascopyto((void *)vsrc, ASI_PHYS_USE_EC, pdst,
1892                             PAGE_SIZE);
1893                         tlb_page_demap(kernel_pmap, vsrc);
1894                         PMAP_UNLOCK(kernel_pmap);
1895                 }
1896         } else {
1897                 PMAP_STATS_INC(pmap_ncopy_page_oc);
1898                 PMAP_LOCK(kernel_pmap);
1899                 vdst = pmap_temp_map_1 + (mdst->md.color * PAGE_SIZE);
1900                 tp = tsb_kvtotte(vdst);
1901                 tp->tte_data =
1902                     TD_V | TD_8K | TD_PA(pdst) | TD_CP | TD_CV | TD_W;
1903                 tp->tte_vpn = TV_VPN(vdst, TS_8K);
1904                 vsrc = pmap_temp_map_2 + (msrc->md.color * PAGE_SIZE);
1905                 tp = tsb_kvtotte(vsrc);
1906                 tp->tte_data =
1907                     TD_V | TD_8K | TD_PA(psrc) | TD_CP | TD_CV | TD_W;
1908                 tp->tte_vpn = TV_VPN(vsrc, TS_8K);
1909                 cpu_block_copy((void *)vsrc, (void *)vdst, PAGE_SIZE);
1910                 tlb_page_demap(kernel_pmap, vdst);
1911                 tlb_page_demap(kernel_pmap, vsrc);
1912                 PMAP_UNLOCK(kernel_pmap);
1913         }
1914 }
1915
1916 vm_offset_t
1917 pmap_quick_enter_page(vm_page_t m)
1918 {
1919         vm_paddr_t pa;
1920         vm_offset_t qaddr;
1921         struct tte *tp;
1922
1923         pa = VM_PAGE_TO_PHYS(m);
1924         if (dcache_color_ignore != 0 || m->md.color == DCACHE_COLOR(pa))
1925                 return (TLB_PHYS_TO_DIRECT(pa));
1926
1927         critical_enter();
1928         qaddr = PCPU_GET(qmap_addr);
1929         qaddr += (PAGE_SIZE * ((DCACHE_COLORS + DCACHE_COLOR(pa) -
1930             DCACHE_COLOR(qaddr)) % DCACHE_COLORS));
1931         tp = tsb_kvtotte(qaddr);
1932
1933         KASSERT(tp->tte_data == 0, ("pmap_quick_enter_page: PTE busy"));
1934         
1935         tp->tte_data = TD_V | TD_8K | TD_PA(pa) | TD_CP | TD_CV | TD_W;
1936         tp->tte_vpn = TV_VPN(qaddr, TS_8K);
1937
1938         return (qaddr);
1939 }
1940
1941 void
1942 pmap_quick_remove_page(vm_offset_t addr)
1943 {
1944         vm_offset_t qaddr;
1945         struct tte *tp;
1946
1947         if (addr >= VM_MIN_DIRECT_ADDRESS)
1948                 return;
1949
1950         tp = tsb_kvtotte(addr);
1951         qaddr = PCPU_GET(qmap_addr);
1952         
1953         KASSERT((addr >= qaddr) && (addr < (qaddr + (PAGE_SIZE * DCACHE_COLORS))),
1954             ("pmap_quick_remove_page: invalid address"));
1955         KASSERT(tp->tte_data != 0, ("pmap_quick_remove_page: PTE not in use"));
1956         
1957         stxa(TLB_DEMAP_VA(addr) | TLB_DEMAP_NUCLEUS | TLB_DEMAP_PAGE, ASI_DMMU_DEMAP, 0);
1958         stxa(TLB_DEMAP_VA(addr) | TLB_DEMAP_NUCLEUS | TLB_DEMAP_PAGE, ASI_IMMU_DEMAP, 0);
1959         flush(KERNBASE);
1960         TTE_ZERO(tp);
1961         critical_exit();
1962 }
1963
1964 int unmapped_buf_allowed;
1965
1966 void
1967 pmap_copy_pages(vm_page_t ma[], vm_offset_t a_offset, vm_page_t mb[],
1968     vm_offset_t b_offset, int xfersize)
1969 {
1970
1971         panic("pmap_copy_pages: not implemented");
1972 }
1973
1974 /*
1975  * Returns true if the pmap's pv is one of the first
1976  * 16 pvs linked to from this page.  This count may
1977  * be changed upwards or downwards in the future; it
1978  * is only necessary that true be returned for a small
1979  * subset of pmaps for proper page aging.
1980  */
1981 boolean_t
1982 pmap_page_exists_quick(pmap_t pm, vm_page_t m)
1983 {
1984         struct tte *tp;
1985         int loops;
1986         boolean_t rv;
1987
1988         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1989             ("pmap_page_exists_quick: page %p is not managed", m));
1990         loops = 0;
1991         rv = FALSE;
1992         rw_wlock(&tte_list_global_lock);
1993         TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
1994                 if ((tp->tte_data & TD_PV) == 0)
1995                         continue;
1996                 if (TTE_GET_PMAP(tp) == pm) {
1997                         rv = TRUE;
1998                         break;
1999                 }
2000                 if (++loops >= 16)
2001                         break;
2002         }
2003         rw_wunlock(&tte_list_global_lock);
2004         return (rv);
2005 }
2006
2007 /*
2008  * Return the number of managed mappings to the given physical page
2009  * that are wired.
2010  */
2011 int
2012 pmap_page_wired_mappings(vm_page_t m)
2013 {
2014         struct tte *tp;
2015         int count;
2016
2017         count = 0;
2018         if ((m->oflags & VPO_UNMANAGED) != 0)
2019                 return (count);
2020         rw_wlock(&tte_list_global_lock);
2021         TAILQ_FOREACH(tp, &m->md.tte_list, tte_link)
2022                 if ((tp->tte_data & (TD_PV | TD_WIRED)) == (TD_PV | TD_WIRED))
2023                         count++;
2024         rw_wunlock(&tte_list_global_lock);
2025         return (count);
2026 }
2027
2028 /*
2029  * Remove all pages from specified address space, this aids process exit
2030  * speeds.  This is much faster than pmap_remove in the case of running down
2031  * an entire address space.  Only works for the current pmap.
2032  */
2033 void
2034 pmap_remove_pages(pmap_t pm)
2035 {
2036
2037 }
2038
2039 /*
2040  * Returns TRUE if the given page has a managed mapping.
2041  */
2042 boolean_t
2043 pmap_page_is_mapped(vm_page_t m)
2044 {
2045         struct tte *tp;
2046         boolean_t rv;
2047
2048         rv = FALSE;
2049         if ((m->oflags & VPO_UNMANAGED) != 0)
2050                 return (rv);
2051         rw_wlock(&tte_list_global_lock);
2052         TAILQ_FOREACH(tp, &m->md.tte_list, tte_link)
2053                 if ((tp->tte_data & TD_PV) != 0) {
2054                         rv = TRUE;
2055                         break;
2056                 }
2057         rw_wunlock(&tte_list_global_lock);
2058         return (rv);
2059 }
2060
2061 /*
2062  * Return a count of reference bits for a page, clearing those bits.
2063  * It is not necessary for every reference bit to be cleared, but it
2064  * is necessary that 0 only be returned when there are truly no
2065  * reference bits set.
2066  *
2067  * As an optimization, update the page's dirty field if a modified bit is
2068  * found while counting reference bits.  This opportunistic update can be
2069  * performed at low cost and can eliminate the need for some future calls
2070  * to pmap_is_modified().  However, since this function stops after
2071  * finding PMAP_TS_REFERENCED_MAX reference bits, it may not detect some
2072  * dirty pages.  Those dirty pages will only be detected by a future call
2073  * to pmap_is_modified().
2074  */
2075 int
2076 pmap_ts_referenced(vm_page_t m)
2077 {
2078         struct tte *tpf;
2079         struct tte *tpn;
2080         struct tte *tp;
2081         u_long data;
2082         int count;
2083
2084         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2085             ("pmap_ts_referenced: page %p is not managed", m));
2086         count = 0;
2087         rw_wlock(&tte_list_global_lock);
2088         if ((tp = TAILQ_FIRST(&m->md.tte_list)) != NULL) {
2089                 tpf = tp;
2090                 do {
2091                         tpn = TAILQ_NEXT(tp, tte_link);
2092                         TAILQ_REMOVE(&m->md.tte_list, tp, tte_link);
2093                         TAILQ_INSERT_TAIL(&m->md.tte_list, tp, tte_link);
2094                         if ((tp->tte_data & TD_PV) == 0)
2095                                 continue;
2096                         data = atomic_clear_long(&tp->tte_data, TD_REF);
2097                         if ((data & TD_W) != 0)
2098                                 vm_page_dirty(m);
2099                         if ((data & TD_REF) != 0 && ++count >=
2100                             PMAP_TS_REFERENCED_MAX)
2101                                 break;
2102                 } while ((tp = tpn) != NULL && tp != tpf);
2103         }
2104         rw_wunlock(&tte_list_global_lock);
2105         return (count);
2106 }
2107
2108 boolean_t
2109 pmap_is_modified(vm_page_t m)
2110 {
2111         struct tte *tp;
2112         boolean_t rv;
2113
2114         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2115             ("pmap_is_modified: page %p is not managed", m));
2116         rv = FALSE;
2117
2118         /*
2119          * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
2120          * concurrently set while the object is locked.  Thus, if PGA_WRITEABLE
2121          * is clear, no TTEs can have TD_W set.
2122          */
2123         VM_OBJECT_ASSERT_WLOCKED(m->object);
2124         if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
2125                 return (rv);
2126         rw_wlock(&tte_list_global_lock);
2127         TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
2128                 if ((tp->tte_data & TD_PV) == 0)
2129                         continue;
2130                 if ((tp->tte_data & TD_W) != 0) {
2131                         rv = TRUE;
2132                         break;
2133                 }
2134         }
2135         rw_wunlock(&tte_list_global_lock);
2136         return (rv);
2137 }
2138
2139 /*
2140  *      pmap_is_prefaultable:
2141  *
2142  *      Return whether or not the specified virtual address is elgible
2143  *      for prefault.
2144  */
2145 boolean_t
2146 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
2147 {
2148         boolean_t rv;
2149
2150         PMAP_LOCK(pmap);
2151         rv = tsb_tte_lookup(pmap, addr) == NULL;
2152         PMAP_UNLOCK(pmap);
2153         return (rv);
2154 }
2155
2156 /*
2157  * Return whether or not the specified physical page was referenced
2158  * in any physical maps.
2159  */
2160 boolean_t
2161 pmap_is_referenced(vm_page_t m)
2162 {
2163         struct tte *tp;
2164         boolean_t rv;
2165
2166         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2167             ("pmap_is_referenced: page %p is not managed", m));
2168         rv = FALSE;
2169         rw_wlock(&tte_list_global_lock);
2170         TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
2171                 if ((tp->tte_data & TD_PV) == 0)
2172                         continue;
2173                 if ((tp->tte_data & TD_REF) != 0) {
2174                         rv = TRUE;
2175                         break;
2176                 }
2177         }
2178         rw_wunlock(&tte_list_global_lock);
2179         return (rv);
2180 }
2181
2182 /*
2183  * This function is advisory.
2184  */
2185 void
2186 pmap_advise(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, int advice)
2187 {
2188 }
2189
2190 void
2191 pmap_clear_modify(vm_page_t m)
2192 {
2193         struct tte *tp;
2194         u_long data;
2195
2196         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2197             ("pmap_clear_modify: page %p is not managed", m));
2198         VM_OBJECT_ASSERT_WLOCKED(m->object);
2199         KASSERT(!vm_page_xbusied(m),
2200             ("pmap_clear_modify: page %p is exclusive busied", m));
2201
2202         /*
2203          * If the page is not PGA_WRITEABLE, then no TTEs can have TD_W set.
2204          * If the object containing the page is locked and the page is not
2205          * exclusive busied, then PGA_WRITEABLE cannot be concurrently set.
2206          */
2207         if ((m->aflags & PGA_WRITEABLE) == 0)
2208                 return;
2209         rw_wlock(&tte_list_global_lock);
2210         TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
2211                 if ((tp->tte_data & TD_PV) == 0)
2212                         continue;
2213                 data = atomic_clear_long(&tp->tte_data, TD_W);
2214                 if ((data & TD_W) != 0)
2215                         tlb_page_demap(TTE_GET_PMAP(tp), TTE_GET_VA(tp));
2216         }
2217         rw_wunlock(&tte_list_global_lock);
2218 }
2219
2220 void
2221 pmap_remove_write(vm_page_t m)
2222 {
2223         struct tte *tp;
2224         u_long data;
2225
2226         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2227             ("pmap_remove_write: page %p is not managed", m));
2228
2229         /*
2230          * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
2231          * set by another thread while the object is locked.  Thus,
2232          * if PGA_WRITEABLE is clear, no page table entries need updating.
2233          */
2234         VM_OBJECT_ASSERT_WLOCKED(m->object);
2235         if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
2236                 return;
2237         rw_wlock(&tte_list_global_lock);
2238         TAILQ_FOREACH(tp, &m->md.tte_list, tte_link) {
2239                 if ((tp->tte_data & TD_PV) == 0)
2240                         continue;
2241                 data = atomic_clear_long(&tp->tte_data, TD_SW | TD_W);
2242                 if ((data & TD_W) != 0) {
2243                         vm_page_dirty(m);
2244                         tlb_page_demap(TTE_GET_PMAP(tp), TTE_GET_VA(tp));
2245                 }
2246         }
2247         vm_page_aflag_clear(m, PGA_WRITEABLE);
2248         rw_wunlock(&tte_list_global_lock);
2249 }
2250
2251 int
2252 pmap_mincore(pmap_t pm, vm_offset_t addr, vm_paddr_t *locked_pa)
2253 {
2254
2255         /* TODO; */
2256         return (0);
2257 }
2258
2259 /*
2260  * Activate a user pmap.  The pmap must be activated before its address space
2261  * can be accessed in any way.
2262  */
2263 void
2264 pmap_activate(struct thread *td)
2265 {
2266         struct vmspace *vm;
2267         struct pmap *pm;
2268         int context;
2269
2270         critical_enter();
2271         vm = td->td_proc->p_vmspace;
2272         pm = vmspace_pmap(vm);
2273
2274         context = PCPU_GET(tlb_ctx);
2275         if (context == PCPU_GET(tlb_ctx_max)) {
2276                 tlb_flush_user();
2277                 context = PCPU_GET(tlb_ctx_min);
2278         }
2279         PCPU_SET(tlb_ctx, context + 1);
2280
2281         pm->pm_context[curcpu] = context;
2282 #ifdef SMP
2283         CPU_SET_ATOMIC(PCPU_GET(cpuid), &pm->pm_active);
2284         atomic_store_acq_ptr((uintptr_t *)PCPU_PTR(pmap), (uintptr_t)pm);
2285 #else
2286         CPU_SET(PCPU_GET(cpuid), &pm->pm_active);
2287         PCPU_SET(pmap, pm);
2288 #endif
2289
2290         stxa(AA_DMMU_TSB, ASI_DMMU, pm->pm_tsb);
2291         stxa(AA_IMMU_TSB, ASI_IMMU, pm->pm_tsb);
2292         stxa(AA_DMMU_PCXR, ASI_DMMU, (ldxa(AA_DMMU_PCXR, ASI_DMMU) &
2293             TLB_CXR_PGSZ_MASK) | context);
2294         flush(KERNBASE);
2295         critical_exit();
2296 }
2297
2298 void
2299 pmap_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
2300 {
2301
2302 }
2303
2304 /*
2305  * Increase the starting virtual address of the given mapping if a
2306  * different alignment might result in more superpage mappings.
2307  */
2308 void
2309 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
2310     vm_offset_t *addr, vm_size_t size)
2311 {
2312
2313 }
2314
2315 boolean_t
2316 pmap_is_valid_memattr(pmap_t pmap __unused, vm_memattr_t mode)
2317 {
2318
2319         return (mode == VM_MEMATTR_DEFAULT);
2320 }