2 * Copyright (c) 2004-2010 Juli Mallett <jmallett@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
40 #include <machine/pte.h>
41 #include <machine/tlb.h>
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)
48 #define MIPS_MAX_TLB_ENTRIES 64
57 } entry[MIPS_MAX_TLB_ENTRIES];
60 static struct tlb_state tlb_state[MAXCPU];
64 * PageMask must increment in steps of 2 bits.
66 COMPILE_TIME_ASSERT(POPCNT(TLBMASK_MASK) % 2 == 0);
72 __asm __volatile ("tlbp" : : : "memory");
79 __asm __volatile ("tlbr" : : : "memory");
84 tlb_write_indexed(void)
86 __asm __volatile ("tlbwi" : : : "memory");
91 tlb_write_random(void)
93 __asm __volatile ("tlbwr" : : : "memory");
97 static void tlb_invalidate_one(unsigned);
100 tlb_insert_wired(unsigned i, vm_offset_t va, pt_entry_t pte0, pt_entry_t pte1)
108 asid = mips_rd_entryhi() & TLBHI_ASID_MASK;
112 mips_wr_entryhi(TLBHI_ENTRY(va, 0));
113 mips_wr_entrylo0(pte0);
114 mips_wr_entrylo1(pte1);
117 mips_wr_entryhi(asid);
122 tlb_invalidate_address(struct pmap *pmap, vm_offset_t va)
131 asid = mips_rd_entryhi() & TLBHI_ASID_MASK;
134 mips_wr_entryhi(TLBHI_ENTRY(va, pmap_asid(pmap)));
138 tlb_invalidate_one(i);
140 mips_wr_entryhi(asid);
145 tlb_invalidate_all(void)
152 asid = mips_rd_entryhi() & TLBHI_ASID_MASK;
154 for (i = mips_rd_wired(); i < num_tlbentries; i++)
155 tlb_invalidate_one(i);
157 mips_wr_entryhi(asid);
162 tlb_invalidate_all_user(struct pmap *pmap)
169 asid = mips_rd_entryhi() & TLBHI_ASID_MASK;
171 for (i = mips_rd_wired(); i < num_tlbentries; i++) {
177 uasid = mips_rd_entryhi() & TLBHI_ASID_MASK;
180 * Invalidate all non-kernel entries.
186 * Invalidate this pmap's entries.
188 if (uasid != pmap_asid(pmap))
191 tlb_invalidate_one(i);
194 mips_wr_entryhi(asid);
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.
204 tlb_invalidate_range(pmap_t pmap, vm_offset_t start, vm_offset_t end)
206 register_t asid, end_hi, hi, hi_pagemask, s, save_asid, start_hi;
209 KASSERT(start < end || (end == 0 && start > 0),
210 ("tlb_invalidate_range: invalid range"));
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.
216 start &= ~((1 << TLBMASK_SHIFT) - 1);
217 end = (end + (1 << TLBMASK_SHIFT) - 1) & ~((1 << TLBMASK_SHIFT) - 1);
220 save_asid = mips_rd_entryhi() & TLBHI_ASID_MASK;
222 asid = pmap_asid(pmap);
223 start_hi = TLBHI_ENTRY(start, asid);
224 end_hi = TLBHI_ENTRY(end, asid);
227 * Select the fastest method for invalidating the TLB entries.
229 if (end - start < num_tlbentries << TLBMASK_SHIFT || (end == 0 &&
230 start >= -(num_tlbentries << TLBMASK_SHIFT))) {
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.
236 for (hi = start_hi; hi != end_hi; hi += 1 << TLBMASK_SHIFT) {
242 tlb_invalidate_one(i);
246 * The virtual address range is large compared to the size of
247 * the TLB. Test every non-wired TLB entry.
249 for (i = mips_rd_wired(); i < num_tlbentries; i++) {
252 hi = mips_rd_entryhi();
253 if ((hi & TLBHI_ASID_MASK) == asid && (hi < end_hi ||
256 * If "hi" is a large page that spans
257 * "start_hi", then it must be invalidated.
259 hi_pagemask = mips_rd_pagemask();
260 if (hi >= (start_hi & ~(hi_pagemask <<
262 tlb_invalidate_one(i);
267 mips_wr_entryhi(save_asid);
271 /* XXX Only if DDB? */
275 unsigned ntlb, i, cpu;
277 cpu = PCPU_GET(cpuid);
278 if (num_tlbentries > MIPS_MAX_TLB_ENTRIES)
279 ntlb = MIPS_MAX_TLB_ENTRIES;
281 ntlb = num_tlbentries;
282 tlb_state[cpu].wired = mips_rd_wired();
283 for (i = 0; i < ntlb; i++) {
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();
294 tlb_update(struct pmap *pmap, vm_offset_t va, pt_entry_t pte)
301 pte &= ~TLBLO_SWBITS_MASK;
304 asid = mips_rd_entryhi() & TLBHI_ASID_MASK;
307 mips_wr_entryhi(TLBHI_ENTRY(va, pmap_asid(pmap)));
313 if ((va & PAGE_SIZE) == 0) {
314 mips_wr_entrylo0(pte);
316 mips_wr_entrylo1(pte);
321 mips_wr_entryhi(asid);
326 tlb_invalidate_one(unsigned i)
328 /* XXX an invalid ASID? */
329 mips_wr_entryhi(TLBHI_ENTRY(MIPS_KSEG0_START + (2 * i * PAGE_SIZE), 0));
340 DB_SHOW_COMMAND(tlb, ddb_dump_tlb)
342 register_t ehi, elo0, elo1;
343 unsigned i, cpu, ntlb;
347 * The worst conversion from hex to decimal ever.
350 cpu = ((addr >> 4) % 16) * 10 + (addr % 16);
352 cpu = PCPU_GET(cpuid);
354 if (cpu < 0 || cpu >= mp_ncpus) {
355 db_printf("Invalid CPU %u\n", cpu);
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);
363 ntlb = num_tlbentries;
365 if (cpu == PCPU_GET(cpuid))
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) {
372 db_printf("^^^ WIRED ENTRIES ^^^\n");
374 db_printf("(No wired entries.)\n");
378 ehi = tlb_state[cpu].entry[i].entryhi;
379 elo0 = tlb_state[cpu].entry[i].entrylo0;
380 elo1 = tlb_state[cpu].entry[i].entrylo1;
382 if (elo0 == 0 && elo1 == 0)
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));
389 db_printf("Finished.\n");