MIPS: Mask out limit field when calculating wired entry count

Since MIPSr6 the Wired register is split into 2 fields, with the upper
16 bits of the register indicating a limit on the value that the wired
entry count in the bottom 16 bits of the register can take. This means
that simply reading the wired register doesn't get us a valid TLB entry
index any longer, and we instead need to retrieve only the lower 16 bits
of the register. Introduce a new num_wired_entries() function which does
this on MIPSr6 or higher and simply returns the value of the wired
register on older architecture revisions, and make use of it when
reading the number of wired entries.

Since commit e710d6668309 ("MIPS: tlb-r4k: If there are wired entries,
don't use TLBINVF") we have been using a non-zero number of wired
entries to determine whether we should avoid use of the tlbinvf
instruction (which would invalidate wired entries) and instead loop over
TLB entries in local_flush_tlb_all(). This loop begins with the number
of wired entries, or before this patch some large bogus TLB index on
MIPSr6 systems. Thus since the aforementioned commit some MIPSr6 systems
with FTLBs have been prone to leaving stale address translations in the
FTLB & crashing in various weird & wonderful ways when we later observe
the wrong memory.

Signed-off-by: Paul Burton <paul.burton@imgtec.com>
Cc: Matt Redfearn <matt.redfearn@imgtec.com>
Cc: linux-mips@linux-mips.org
Patchwork: https://patchwork.linux-mips.org/patch/14557/
Signed-off-by: Ralf Baechle <ralf@linux-mips.org>

authored by Paul Burton and committed by Ralf Baechle 10313980 9c763584

Changed files
+24 -5
arch
mips
include
mm
+6
arch/mips/include/asm/mipsregs.h
··· 215 215 #endif 216 216 217 217 /* 218 + * Wired register bits 219 + */ 220 + #define MIPSR6_WIRED_LIMIT (_ULCAST_(0xffff) << 16) 221 + #define MIPSR6_WIRED_WIRED (_ULCAST_(0xffff) << 0) 222 + 223 + /* 218 224 * Values used for computation of new tlb entries 219 225 */ 220 226 #define PL_4K 12
+13
arch/mips/include/asm/tlb.h
··· 1 1 #ifndef __ASM_TLB_H 2 2 #define __ASM_TLB_H 3 3 4 + #include <asm/cpu-features.h> 5 + #include <asm/mipsregs.h> 6 + 4 7 /* 5 8 * MIPS doesn't need any special per-pte or per-vma handling, except 6 9 * we need to flush cache for area to be unmapped. ··· 24 21 #define UNIQUE_ENTRYHI(idx) \ 25 22 ((CKSEG0 + ((idx) << (PAGE_SHIFT + 1))) | \ 26 23 (cpu_has_tlbinv ? MIPS_ENTRYHI_EHINV : 0)) 24 + 25 + static inline unsigned int num_wired_entries(void) 26 + { 27 + unsigned int wired = read_c0_wired(); 28 + 29 + if (cpu_has_mips_r6) 30 + wired &= MIPSR6_WIRED_WIRED; 31 + 32 + return wired; 33 + } 27 34 28 35 #include <asm-generic/tlb.h> 29 36
+2 -2
arch/mips/mm/init.c
··· 118 118 writex_c0_entrylo1(entrylo); 119 119 } 120 120 #endif 121 - tlbidx = read_c0_wired(); 121 + tlbidx = num_wired_entries(); 122 122 write_c0_wired(tlbidx + 1); 123 123 write_c0_index(tlbidx); 124 124 mtc0_tlbw_hazard(); ··· 147 147 148 148 local_irq_save(flags); 149 149 old_ctx = read_c0_entryhi(); 150 - wired = read_c0_wired() - 1; 150 + wired = num_wired_entries() - 1; 151 151 write_c0_wired(wired); 152 152 write_c0_index(wired); 153 153 write_c0_entryhi(UNIQUE_ENTRYHI(wired));
+3 -3
arch/mips/mm/tlb-r4k.c
··· 65 65 write_c0_entrylo0(0); 66 66 write_c0_entrylo1(0); 67 67 68 - entry = read_c0_wired(); 68 + entry = num_wired_entries(); 69 69 70 70 /* 71 71 * Blast 'em all away. ··· 385 385 old_ctx = read_c0_entryhi(); 386 386 htw_stop(); 387 387 old_pagemask = read_c0_pagemask(); 388 - wired = read_c0_wired(); 388 + wired = num_wired_entries(); 389 389 write_c0_wired(wired + 1); 390 390 write_c0_index(wired); 391 391 tlbw_use_hazard(); /* What is the hazard here? */ ··· 449 449 htw_stop(); 450 450 old_ctx = read_c0_entryhi(); 451 451 old_pagemask = read_c0_pagemask(); 452 - wired = read_c0_wired(); 452 + wired = num_wired_entries(); 453 453 if (--temp_tlb_entry < wired) { 454 454 printk(KERN_WARNING 455 455 "No TLB space left for add_temporary_entry\n");