]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/mips/tlb.c
Move MIPS_MAX_TLB_ENTRIES definition from cpuregs.h to tlb.c
[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         } entry[MIPS_MAX_TLB_ENTRIES];
58 };
59
60 static struct tlb_state tlb_state[MAXCPU];
61
62 #if 0
63 /*
64  * PageMask must increment in steps of 2 bits.
65  */
66 COMPILE_TIME_ASSERT(POPCNT(TLBMASK_MASK) % 2 == 0);
67 #endif
68
69 static inline void
70 tlb_probe(void)
71 {
72         __asm __volatile ("tlbp" : : : "memory");
73         mips_cp0_sync();
74 }
75
76 static inline void
77 tlb_read(void)
78 {
79         __asm __volatile ("tlbr" : : : "memory");
80         mips_cp0_sync();
81 }
82
83 static inline void
84 tlb_write_indexed(void)
85 {
86         __asm __volatile ("tlbwi" : : : "memory");
87         mips_cp0_sync();
88 }
89
90 static inline void
91 tlb_write_random(void)
92 {
93         __asm __volatile ("tlbwr" : : : "memory");
94         mips_cp0_sync();
95 }
96
97 static void tlb_invalidate_one(unsigned);
98
99 void
100 tlb_insert_wired(unsigned i, vm_offset_t va, pt_entry_t pte0, pt_entry_t pte1)
101 {
102         register_t asid;
103         register_t s;
104
105         va &= ~PAGE_MASK;
106
107         s = intr_disable();
108         asid = mips_rd_entryhi() & TLBHI_ASID_MASK;
109
110         mips_wr_index(i);
111         mips_wr_pagemask(0);
112         mips_wr_entryhi(TLBHI_ENTRY(va, 0));
113         mips_wr_entrylo0(pte0);
114         mips_wr_entrylo1(pte1);
115         tlb_write_indexed();
116
117         mips_wr_entryhi(asid);
118         intr_restore(s);
119 }
120
121 void
122 tlb_invalidate_address(struct pmap *pmap, vm_offset_t va)
123 {
124         register_t asid;
125         register_t s;
126         int i;
127
128         va &= ~PAGE_MASK;
129
130         s = intr_disable();
131         asid = mips_rd_entryhi() & TLBHI_ASID_MASK;
132
133         mips_wr_pagemask(0);
134         mips_wr_entryhi(TLBHI_ENTRY(va, pmap_asid(pmap)));
135         tlb_probe();
136         i = mips_rd_index();
137         if (i >= 0)
138                 tlb_invalidate_one(i);
139
140         mips_wr_entryhi(asid);
141         intr_restore(s);
142 }
143
144 void
145 tlb_invalidate_all(void)
146 {
147         register_t asid;
148         register_t s;
149         unsigned i;
150
151         s = intr_disable();
152         asid = mips_rd_entryhi() & TLBHI_ASID_MASK;
153
154         for (i = mips_rd_wired(); i < num_tlbentries; i++)
155                 tlb_invalidate_one(i);
156
157         mips_wr_entryhi(asid);
158         intr_restore(s);
159 }
160
161 void
162 tlb_invalidate_all_user(struct pmap *pmap)
163 {
164         register_t asid;
165         register_t s;
166         unsigned i;
167
168         s = intr_disable();
169         asid = mips_rd_entryhi() & TLBHI_ASID_MASK;
170
171         for (i = mips_rd_wired(); i < num_tlbentries; i++) {
172                 register_t uasid;
173
174                 mips_wr_index(i);
175                 tlb_read();
176
177                 uasid = mips_rd_entryhi() & TLBHI_ASID_MASK;
178                 if (pmap == NULL) {
179                         /*
180                          * Invalidate all non-kernel entries.
181                          */
182                         if (uasid == 0)
183                                 continue;
184                 } else {
185                         /*
186                          * Invalidate this pmap's entries.
187                          */
188                         if (uasid != pmap_asid(pmap))
189                                 continue;
190                 }
191                 tlb_invalidate_one(i);
192         }
193
194         mips_wr_entryhi(asid);
195         intr_restore(s);
196 }
197
198 /*
199  * Invalidates any TLB entries that map a virtual page from the specified
200  * address range.  If "end" is zero, then every virtual page is considered to
201  * be within the address range's upper bound.
202  */
203 void
204 tlb_invalidate_range(pmap_t pmap, vm_offset_t start, vm_offset_t end)
205 {
206         register_t asid, end_hi, hi, hi_pagemask, s, save_asid, start_hi;
207         int i;
208
209         KASSERT(start < end || (end == 0 && start > 0),
210             ("tlb_invalidate_range: invalid range"));
211
212         /*
213          * Truncate the virtual address "start" to an even page frame number,
214          * and round the virtual address "end" to an even page frame number.
215          */
216         start &= ~((1 << TLBMASK_SHIFT) - 1);
217         end = (end + (1 << TLBMASK_SHIFT) - 1) & ~((1 << TLBMASK_SHIFT) - 1);
218
219         s = intr_disable();
220         save_asid = mips_rd_entryhi() & TLBHI_ASID_MASK;
221
222         asid = pmap_asid(pmap);
223         start_hi = TLBHI_ENTRY(start, asid);
224         end_hi = TLBHI_ENTRY(end, asid);
225
226         /*
227          * Select the fastest method for invalidating the TLB entries.
228          */
229         if (end - start < num_tlbentries << TLBMASK_SHIFT || (end == 0 &&
230             start >= -(num_tlbentries << TLBMASK_SHIFT))) {
231                 /*
232                  * The virtual address range is small compared to the size of
233                  * the TLB.  Probe the TLB for each even numbered page frame
234                  * within the virtual address range.
235                  */
236                 for (hi = start_hi; hi != end_hi; hi += 1 << TLBMASK_SHIFT) {
237                         mips_wr_pagemask(0);
238                         mips_wr_entryhi(hi);
239                         tlb_probe();
240                         i = mips_rd_index();
241                         if (i >= 0)
242                                 tlb_invalidate_one(i);
243                 }
244         } else {
245                 /*
246                  * The virtual address range is large compared to the size of
247                  * the TLB.  Test every non-wired TLB entry.
248                  */
249                 for (i = mips_rd_wired(); i < num_tlbentries; i++) {
250                         mips_wr_index(i);
251                         tlb_read();
252                         hi = mips_rd_entryhi();
253                         if ((hi & TLBHI_ASID_MASK) == asid && (hi < end_hi ||
254                             end == 0)) {
255                                 /*
256                                  * If "hi" is a large page that spans
257                                  * "start_hi", then it must be invalidated.
258                                  */
259                                 hi_pagemask = mips_rd_pagemask();
260                                 if (hi >= (start_hi & ~(hi_pagemask <<
261                                     TLBMASK_SHIFT)))
262                                         tlb_invalidate_one(i);
263                         }
264                 }
265         }
266
267         mips_wr_entryhi(save_asid);
268         intr_restore(s);
269 }
270
271 /* XXX Only if DDB?  */
272 void
273 tlb_save(void)
274 {
275         unsigned ntlb, i, cpu;
276
277         cpu = PCPU_GET(cpuid);
278         if (num_tlbentries > MIPS_MAX_TLB_ENTRIES)
279                 ntlb = MIPS_MAX_TLB_ENTRIES;
280         else
281                 ntlb = num_tlbentries;
282         tlb_state[cpu].wired = mips_rd_wired();
283         for (i = 0; i < ntlb; i++) {
284                 mips_wr_index(i);
285                 tlb_read();
286
287                 tlb_state[cpu].entry[i].entryhi = mips_rd_entryhi();
288                 tlb_state[cpu].entry[i].entrylo0 = mips_rd_entrylo0();
289                 tlb_state[cpu].entry[i].entrylo1 = mips_rd_entrylo1();
290         }
291 }
292
293 void
294 tlb_update(struct pmap *pmap, vm_offset_t va, pt_entry_t pte)
295 {
296         register_t asid;
297         register_t s;
298         int i;
299
300         va &= ~PAGE_MASK;
301         pte &= ~TLBLO_SWBITS_MASK;
302
303         s = intr_disable();
304         asid = mips_rd_entryhi() & TLBHI_ASID_MASK;
305
306         mips_wr_pagemask(0);
307         mips_wr_entryhi(TLBHI_ENTRY(va, pmap_asid(pmap)));
308         tlb_probe();
309         i = mips_rd_index();
310         if (i >= 0) {
311                 tlb_read();
312
313                 if ((va & PAGE_SIZE) == 0) {
314                         mips_wr_entrylo0(pte);
315                 } else {
316                         mips_wr_entrylo1(pte);
317                 }
318                 tlb_write_indexed();
319         }
320
321         mips_wr_entryhi(asid);
322         intr_restore(s);
323 }
324
325 static void
326 tlb_invalidate_one(unsigned i)
327 {
328         /* XXX an invalid ASID? */
329         mips_wr_entryhi(TLBHI_ENTRY(MIPS_KSEG0_START + (2 * i * PAGE_SIZE), 0));
330         mips_wr_entrylo0(0);
331         mips_wr_entrylo1(0);
332         mips_wr_pagemask(0);
333         mips_wr_index(i);
334         tlb_write_indexed();
335 }
336
337 #ifdef DDB
338 #include <ddb/ddb.h>
339
340 DB_SHOW_COMMAND(tlb, ddb_dump_tlb)
341 {
342         register_t ehi, elo0, elo1;
343         unsigned i, cpu, ntlb;
344
345         /*
346          * XXX
347          * The worst conversion from hex to decimal ever.
348          */
349         if (have_addr)
350                 cpu = ((addr >> 4) % 16) * 10 + (addr % 16);
351         else
352                 cpu = PCPU_GET(cpuid);
353
354         if (cpu < 0 || cpu >= mp_ncpus) {
355                 db_printf("Invalid CPU %u\n", cpu);
356                 return;
357         }
358         if (num_tlbentries > MIPS_MAX_TLB_ENTRIES) {
359                 ntlb = MIPS_MAX_TLB_ENTRIES;
360                 db_printf("Warning: Only %d of %d TLB entries saved!\n",
361                     ntlb, num_tlbentries);
362         } else
363                 ntlb = num_tlbentries;
364
365         if (cpu == PCPU_GET(cpuid))
366                 tlb_save();
367
368         db_printf("Beginning TLB dump for CPU %u...\n", cpu);
369         for (i = 0; i < ntlb; i++) {
370                 if (i == tlb_state[cpu].wired) {
371                         if (i != 0)
372                                 db_printf("^^^ WIRED ENTRIES ^^^\n");
373                         else
374                                 db_printf("(No wired entries.)\n");
375                 }
376
377                 /* XXX PageMask.  */
378                 ehi = tlb_state[cpu].entry[i].entryhi;
379                 elo0 = tlb_state[cpu].entry[i].entrylo0;
380                 elo1 = tlb_state[cpu].entry[i].entrylo1;
381
382                 if (elo0 == 0 && elo1 == 0)
383                         continue;
384
385                 db_printf("#%u\t=> %jx\n", i, (intmax_t)ehi);
386                 db_printf(" Lo0\t%jx\t(%#jx)\n", (intmax_t)elo0, (intmax_t)TLBLO_PTE_TO_PA(elo0));
387                 db_printf(" Lo1\t%jx\t(%#jx)\n", (intmax_t)elo1, (intmax_t)TLBLO_PTE_TO_PA(elo1));
388         }
389         db_printf("Finished.\n");
390 }
391 #endif