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