]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/sparc64/sparc64/tsb.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / sparc64 / sparc64 / tsb.c
1 /*-
2  * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. Berkeley Software Design Inc's name may not be used to endorse or
13  *    promote products derived from this software without specific prior
14  *    written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *      from BSDI: pmap.c,v 1.28.2.15 2000/04/27 03:10:31 cp Exp
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include "opt_ddb.h"
35 #include "opt_pmap.h"
36
37 #include <sys/param.h>
38 #include <sys/queue.h>
39 #include <sys/ktr.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/proc.h>
43 #include <sys/rwlock.h>
44 #include <sys/smp.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
47
48 #include <vm/vm.h>
49 #include <vm/vm_param.h>
50 #include <vm/vm_kern.h>
51 #include <vm/vm_page.h>
52 #include <vm/vm_map.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_extern.h>
55 #include <vm/vm_pageout.h>
56 #include <vm/vm_pager.h>
57
58 #include <machine/cpufunc.h>
59 #include <machine/frame.h>
60 #include <machine/trap.h>
61 #include <machine/pmap.h>
62 #include <machine/smp.h>
63 #include <machine/tlb.h>
64 #include <machine/tsb.h>
65 #include <machine/tte.h>
66
67 CTASSERT((1 << TTE_SHIFT) == sizeof(struct tte));
68 CTASSERT(TSB_BUCKET_MASK < (1 << 12));
69
70 PMAP_STATS_VAR(tsb_nrepl);
71 PMAP_STATS_VAR(tsb_nlookup_k);
72 PMAP_STATS_VAR(tsb_nlookup_u);
73 PMAP_STATS_VAR(tsb_nenter_k);
74 PMAP_STATS_VAR(tsb_nenter_k_oc);
75 PMAP_STATS_VAR(tsb_nenter_u);
76 PMAP_STATS_VAR(tsb_nenter_u_oc);
77 PMAP_STATS_VAR(tsb_nforeach);
78
79 struct tte *tsb_kernel;
80 vm_size_t tsb_kernel_mask;
81 vm_size_t tsb_kernel_size;
82 vm_paddr_t tsb_kernel_phys;
83 u_int tsb_kernel_ldd_phys;
84
85 struct tte *
86 tsb_tte_lookup(pmap_t pm, vm_offset_t va)
87 {
88         struct tte *bucket;
89         struct tte *tp;
90         u_long sz;
91         u_int i;
92
93         if (pm == kernel_pmap) {
94                 PMAP_STATS_INC(tsb_nlookup_k);
95                 tp = tsb_kvtotte(va);
96                 if (tte_match(tp, va))
97                         return (tp);
98         } else {
99                 PMAP_LOCK_ASSERT(pm, MA_OWNED);
100                 PMAP_STATS_INC(tsb_nlookup_u);
101                 for (sz = TS_MIN; sz <= TS_MAX; sz++) {
102                         bucket = tsb_vtobucket(pm, sz, va);
103                         for (i = 0; i < TSB_BUCKET_SIZE; i++) {
104                                 tp = &bucket[i];
105                                 if (tte_match(tp, va))
106                                         return (tp);
107                         }
108                 }
109         }
110         return (NULL);
111 }
112
113 struct tte *
114 tsb_tte_enter(pmap_t pm, vm_page_t m, vm_offset_t va, u_long sz, u_long data)
115 {
116         struct tte *bucket;
117         struct tte *rtp;
118         struct tte *tp;
119         vm_offset_t ova;
120         int b0;
121         int i;
122
123         if (DCACHE_COLOR(VM_PAGE_TO_PHYS(m)) != DCACHE_COLOR(va)) {
124                 CTR5(KTR_SPARE2,
125         "tsb_tte_enter: off colour va=%#lx pa=%#lx o=%p ot=%d pi=%#lx",
126                     va, VM_PAGE_TO_PHYS(m), m->object,
127                     m->object ? m->object->type : -1,
128                     m->pindex);
129                 if (pm == kernel_pmap)
130                         PMAP_STATS_INC(tsb_nenter_k_oc);
131                 else
132                         PMAP_STATS_INC(tsb_nenter_u_oc);
133         }
134
135         rw_assert(&tte_list_global_lock, RA_WLOCKED);
136         PMAP_LOCK_ASSERT(pm, MA_OWNED);
137         if (pm == kernel_pmap) {
138                 PMAP_STATS_INC(tsb_nenter_k);
139                 tp = tsb_kvtotte(va);
140                 KASSERT((tp->tte_data & TD_V) == 0,
141                     ("tsb_tte_enter: replacing valid kernel mapping"));
142                 goto enter;
143         }
144         PMAP_STATS_INC(tsb_nenter_u);
145
146         bucket = tsb_vtobucket(pm, sz, va);
147
148         tp = NULL;
149         rtp = NULL;
150         b0 = rd(tick) & (TSB_BUCKET_SIZE - 1);
151         i = b0;
152         do {
153                 if ((bucket[i].tte_data & TD_V) == 0) {
154                         tp = &bucket[i];
155                         break;
156                 }
157                 if (tp == NULL) {
158                         if ((bucket[i].tte_data & TD_REF) == 0)
159                                 tp = &bucket[i];
160                         else if (rtp == NULL)
161                                 rtp = &bucket[i];
162                 }
163         } while ((i = (i + 1) & (TSB_BUCKET_SIZE - 1)) != b0);
164
165         if (tp == NULL)
166                 tp = rtp;
167         if ((tp->tte_data & TD_V) != 0) {
168                 PMAP_STATS_INC(tsb_nrepl);
169                 ova = TTE_GET_VA(tp);
170                 pmap_remove_tte(pm, NULL, tp, ova);
171                 tlb_page_demap(pm, ova);
172         }
173
174 enter:
175         if ((m->flags & PG_FICTITIOUS) == 0) {
176                 data |= TD_CP;
177                 if ((m->oflags & VPO_UNMANAGED) == 0) {
178                         pm->pm_stats.resident_count++;
179                         data |= TD_PV;
180                 }
181                 if (pmap_cache_enter(m, va) != 0)
182                         data |= TD_CV;
183                 TAILQ_INSERT_TAIL(&m->md.tte_list, tp, tte_link);
184         } else
185                 data |= TD_FAKE | TD_E;
186
187         tp->tte_vpn = TV_VPN(va, sz);
188         tp->tte_data = data;
189
190         return (tp);
191 }
192
193 /*
194  * Traverse the tsb of a pmap, calling the callback function for any tte entry
195  * that has a virtual address between start and end. If this function returns 0,
196  * tsb_foreach() terminates.
197  * This is used by pmap_remove(), pmap_protect(), and pmap_copy() in the case
198  * that the number of pages in the range given to them reaches the
199  * dimensions of the tsb size as an optimization.
200  */
201 void
202 tsb_foreach(pmap_t pm1, pmap_t pm2, vm_offset_t start, vm_offset_t end,
203     tsb_callback_t *callback)
204 {
205         vm_offset_t va;
206         struct tte *tp;
207         struct tte *tsbp;
208         uintptr_t i;
209         uintptr_t n;
210
211         PMAP_STATS_INC(tsb_nforeach);
212         if (pm1 == kernel_pmap) {
213                 tsbp = tsb_kernel;
214                 n = tsb_kernel_size / sizeof(struct tte);
215         } else {
216                 tsbp = pm1->pm_tsb;
217                 n = TSB_SIZE;
218         }
219         for (i = 0; i < n; i++) {
220                 tp = &tsbp[i];
221                 if ((tp->tte_data & TD_V) != 0) {
222                         va = TTE_GET_VA(tp);
223                         if (va >= start && va < end) {
224                                 if (!callback(pm1, pm2, tp, va))
225                                         break;
226                         }
227                 }
228         }
229 }