]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/mips/tlb.c
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r306325, and update
[FreeBSD/FreeBSD.git] / sys / mips / mips / tlb.c
1 /*-
2  * Copyright (c) 2004-2010 Juli Mallett <jmallett@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include "opt_ddb.h"
30
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/pcpu.h>
35 #include <sys/smp.h>
36
37 #include <vm/vm.h>
38 #include <vm/pmap.h>
39
40 #include <machine/pte.h>
41 #include <machine/tlb.h>
42
43 #if defined(CPU_CNMIPS)
44 #define MIPS_MAX_TLB_ENTRIES    128
45 #elif defined(CPU_NLM)
46 #define MIPS_MAX_TLB_ENTRIES    (2048 + 128)
47 #else
48 #define MIPS_MAX_TLB_ENTRIES    64
49 #endif
50
51 struct tlb_state {
52         unsigned wired;
53         struct tlb_entry {
54                 register_t entryhi;
55                 register_t entrylo0;
56                 register_t entrylo1;
57                 register_t pagemask;
58         } entry[MIPS_MAX_TLB_ENTRIES];
59 };
60
61 static struct tlb_state tlb_state[MAXCPU];
62
63 #if 0
64 /*
65  * PageMask must increment in steps of 2 bits.
66  */
67 COMPILE_TIME_ASSERT(POPCNT(TLBMASK_MASK) % 2 == 0);
68 #endif
69
70 static inline void
71 tlb_probe(void)
72 {
73         __asm __volatile ("tlbp" : : : "memory");
74         mips_cp0_sync();
75 }
76
77 static inline void
78 tlb_read(void)
79 {
80         __asm __volatile ("tlbr" : : : "memory");
81         mips_cp0_sync();
82 }
83
84 static inline void
85 tlb_write_indexed(void)
86 {
87         __asm __volatile ("tlbwi" : : : "memory");
88         mips_cp0_sync();
89 }
90
91 static void tlb_invalidate_one(unsigned);
92
93 void
94 tlb_insert_wired(unsigned i, vm_offset_t va, pt_entry_t pte0, pt_entry_t pte1)
95 {
96         register_t asid;
97         register_t s;
98
99         va &= ~PAGE_MASK;
100
101         s = intr_disable();
102         asid = mips_rd_entryhi() & TLBHI_ASID_MASK;
103
104         mips_wr_index(i);
105         mips_wr_pagemask(0);
106         mips_wr_entryhi(TLBHI_ENTRY(va, 0));
107         mips_wr_entrylo0(pte0);
108         mips_wr_entrylo1(pte1);
109         tlb_write_indexed();
110
111         mips_wr_entryhi(asid);
112         intr_restore(s);
113 }
114
115 void
116 tlb_invalidate_address(struct pmap *pmap, vm_offset_t va)
117 {
118         register_t asid;
119         register_t s;
120         int i;
121
122         va &= ~PAGE_MASK;
123
124         s = intr_disable();
125         asid = mips_rd_entryhi() & TLBHI_ASID_MASK;
126
127         mips_wr_pagemask(0);
128         mips_wr_entryhi(TLBHI_ENTRY(va, pmap_asid(pmap)));
129         tlb_probe();
130         i = mips_rd_index();
131         if (i >= 0)
132                 tlb_invalidate_one(i);
133
134         mips_wr_entryhi(asid);
135         intr_restore(s);
136 }
137
138 void
139 tlb_invalidate_all(void)
140 {
141         register_t asid;
142         register_t s;
143         unsigned i;
144
145         s = intr_disable();
146         asid = mips_rd_entryhi() & TLBHI_ASID_MASK;
147
148         for (i = mips_rd_wired(); i < num_tlbentries; i++)
149                 tlb_invalidate_one(i);
150
151         mips_wr_entryhi(asid);
152         intr_restore(s);
153 }
154
155 void
156 tlb_invalidate_all_user(struct pmap *pmap)
157 {
158         register_t asid;
159         register_t s;
160         unsigned i;
161
162         s = intr_disable();
163         asid = mips_rd_entryhi() & TLBHI_ASID_MASK;
164
165         for (i = mips_rd_wired(); i < num_tlbentries; i++) {
166                 register_t uasid;
167
168                 mips_wr_index(i);
169                 tlb_read();
170
171                 uasid = mips_rd_entryhi() & TLBHI_ASID_MASK;
172                 if (pmap == NULL) {
173                         /*
174                          * Invalidate all non-kernel entries.
175                          */
176                         if (uasid == 0)
177                                 continue;
178                 } else {
179                         /*
180                          * Invalidate this pmap's entries.
181                          */
182                         if (uasid != pmap_asid(pmap))
183                                 continue;
184                 }
185                 tlb_invalidate_one(i);
186         }
187
188         mips_wr_entryhi(asid);
189         intr_restore(s);
190 }
191
192 /*
193  * Invalidates any TLB entries that map a virtual page from the specified
194  * address range.  If "end" is zero, then every virtual page is considered to
195  * be within the address range's upper bound.
196  */
197 void
198 tlb_invalidate_range(pmap_t pmap, vm_offset_t start, vm_offset_t end)
199 {
200         register_t asid, end_hi, hi, hi_pagemask, s, save_asid, start_hi;
201         int i;
202
203         KASSERT(start < end || (end == 0 && start > 0),
204             ("tlb_invalidate_range: invalid range"));
205
206         /*
207          * Truncate the virtual address "start" to an even page frame number,
208          * and round the virtual address "end" to an even page frame number.
209          */
210         start &= ~((1 << TLBMASK_SHIFT) - 1);
211         end = roundup2(end, 1 << TLBMASK_SHIFT);
212
213         s = intr_disable();
214         save_asid = mips_rd_entryhi() & TLBHI_ASID_MASK;
215
216         asid = pmap_asid(pmap);
217         start_hi = TLBHI_ENTRY(start, asid);
218         end_hi = TLBHI_ENTRY(end, asid);
219
220         /*
221          * Select the fastest method for invalidating the TLB entries.
222          */
223         if (end - start < num_tlbentries << TLBMASK_SHIFT || (end == 0 &&
224             start >= -(num_tlbentries << TLBMASK_SHIFT))) {
225                 /*
226                  * The virtual address range is small compared to the size of
227                  * the TLB.  Probe the TLB for each even numbered page frame
228                  * within the virtual address range.
229                  */
230                 for (hi = start_hi; hi != end_hi; hi += 1 << TLBMASK_SHIFT) {
231                         mips_wr_pagemask(0);
232                         mips_wr_entryhi(hi);
233                         tlb_probe();
234                         i = mips_rd_index();
235                         if (i >= 0)
236                                 tlb_invalidate_one(i);
237                 }
238         } else {
239                 /*
240                  * The virtual address range is large compared to the size of
241                  * the TLB.  Test every non-wired TLB entry.
242                  */
243                 for (i = mips_rd_wired(); i < num_tlbentries; i++) {
244                         mips_wr_index(i);
245                         tlb_read();
246                         hi = mips_rd_entryhi();
247                         if ((hi & TLBHI_ASID_MASK) == asid && (hi < end_hi ||
248                             end == 0)) {
249                                 /*
250                                  * If "hi" is a large page that spans
251                                  * "start_hi", then it must be invalidated.
252                                  */
253                                 hi_pagemask = mips_rd_pagemask();
254                                 if (hi >= (start_hi & ~(hi_pagemask <<
255                                     TLBMASK_SHIFT)))
256                                         tlb_invalidate_one(i);
257                         }
258                 }
259         }
260
261         mips_wr_entryhi(save_asid);
262         intr_restore(s);
263 }
264
265 /* XXX Only if DDB?  */
266 void
267 tlb_save(void)
268 {
269         unsigned ntlb, i, cpu;
270
271         cpu = PCPU_GET(cpuid);
272         if (num_tlbentries > MIPS_MAX_TLB_ENTRIES)
273                 ntlb = MIPS_MAX_TLB_ENTRIES;
274         else
275                 ntlb = num_tlbentries;
276         tlb_state[cpu].wired = mips_rd_wired();
277         for (i = 0; i < ntlb; i++) {
278                 mips_wr_index(i);
279                 tlb_read();
280
281                 tlb_state[cpu].entry[i].entryhi = mips_rd_entryhi();
282                 tlb_state[cpu].entry[i].pagemask = mips_rd_pagemask();
283                 tlb_state[cpu].entry[i].entrylo0 = mips_rd_entrylo0();
284                 tlb_state[cpu].entry[i].entrylo1 = mips_rd_entrylo1();
285         }
286 }
287
288 void
289 tlb_update(struct pmap *pmap, vm_offset_t va, pt_entry_t pte)
290 {
291         register_t asid;
292         register_t s;
293         int i;
294
295         va &= ~PAGE_MASK;
296         pte &= ~TLBLO_SWBITS_MASK;
297
298         s = intr_disable();
299         asid = mips_rd_entryhi() & TLBHI_ASID_MASK;
300
301         mips_wr_pagemask(0);
302         mips_wr_entryhi(TLBHI_ENTRY(va, pmap_asid(pmap)));
303         tlb_probe();
304         i = mips_rd_index();
305         if (i >= 0) {
306                 tlb_read();
307
308                 if ((va & PAGE_SIZE) == 0) {
309                         mips_wr_entrylo0(pte);
310                 } else {
311                         mips_wr_entrylo1(pte);
312                 }
313                 tlb_write_indexed();
314         }
315
316         mips_wr_entryhi(asid);
317         intr_restore(s);
318 }
319
320 static void
321 tlb_invalidate_one(unsigned i)
322 {
323         /* XXX an invalid ASID? */
324         mips_wr_entryhi(TLBHI_ENTRY(MIPS_KSEG0_START + (2 * i * PAGE_SIZE), 0));
325         mips_wr_entrylo0(0);
326         mips_wr_entrylo1(0);
327         mips_wr_pagemask(0);
328         mips_wr_index(i);
329         tlb_write_indexed();
330 }
331
332 #ifdef DDB
333 #include <ddb/ddb.h>
334
335 DB_SHOW_COMMAND(tlb, ddb_dump_tlb)
336 {
337         register_t ehi, elo0, elo1, epagemask;
338         unsigned i, cpu, ntlb;
339
340         /*
341          * XXX
342          * The worst conversion from hex to decimal ever.
343          */
344         if (have_addr)
345                 cpu = ((addr >> 4) % 16) * 10 + (addr % 16);
346         else
347                 cpu = PCPU_GET(cpuid);
348
349         if (cpu < 0 || cpu >= mp_ncpus) {
350                 db_printf("Invalid CPU %u\n", cpu);
351                 return;
352         }
353         if (num_tlbentries > MIPS_MAX_TLB_ENTRIES) {
354                 ntlb = MIPS_MAX_TLB_ENTRIES;
355                 db_printf("Warning: Only %d of %d TLB entries saved!\n",
356                     ntlb, num_tlbentries);
357         } else
358                 ntlb = num_tlbentries;
359
360         if (cpu == PCPU_GET(cpuid))
361                 tlb_save();
362
363         db_printf("Beginning TLB dump for CPU %u...\n", cpu);
364         for (i = 0; i < ntlb; i++) {
365                 if (i == tlb_state[cpu].wired) {
366                         if (i != 0)
367                                 db_printf("^^^ WIRED ENTRIES ^^^\n");
368                         else
369                                 db_printf("(No wired entries.)\n");
370                 }
371
372                 /* XXX PageMask.  */
373                 ehi = tlb_state[cpu].entry[i].entryhi;
374                 elo0 = tlb_state[cpu].entry[i].entrylo0;
375                 elo1 = tlb_state[cpu].entry[i].entrylo1;
376                 epagemask = tlb_state[cpu].entry[i].pagemask;
377
378                 if (elo0 == 0 && elo1 == 0)
379                         continue;
380
381                 db_printf("#%u\t=> %jx (pagemask %jx)\n", i, (intmax_t)ehi, (intmax_t) epagemask);
382                 db_printf(" Lo0\t%jx\t(%#jx)\n", (intmax_t)elo0, (intmax_t)TLBLO_PTE_TO_PA(elo0));
383                 db_printf(" Lo1\t%jx\t(%#jx)\n", (intmax_t)elo1, (intmax_t)TLBLO_PTE_TO_PA(elo1));
384         }
385         db_printf("Finished.\n");
386 }
387 #endif