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