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