1 // SPDX-License-Identifier: GPL-2.0
3 * r2300.c: R2000 and R3000 specific mmu/cache code.
5 * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
7 * with a lot of changes to make this thing work for R3000s
8 * Tx39XX R4k style caches added. HK
9 * Copyright (C) 1998, 1999, 2000 Harald Koerfgen
10 * Copyright (C) 1998 Gleb Raiko & Vladimir Roganov
11 * Copyright (C) 2002 Ralf Baechle
12 * Copyright (C) 2002 Maciej W. Rozycki
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/smp.h>
20 #include <asm/pgtable.h>
21 #include <asm/mmu_context.h>
22 #include <asm/tlbmisc.h>
23 #include <asm/isadep.h>
25 #include <asm/bootinfo.h>
30 extern void build_tlb_refill_handler(void);
32 /* CP0 hazard avoidance. */
34 __asm__ __volatile__( \
36 ".set noreorder\n\t" \
40 int r3k_have_wired_reg
; /* Should be in cpu_data? */
43 static void local_flush_tlb_from(int entry
)
45 unsigned long old_ctx
;
47 old_ctx
= read_c0_entryhi() & cpu_asid_mask(¤t_cpu_data
);
49 while (entry
< current_cpu_data
.tlbsize
) {
50 write_c0_index(entry
<< 8);
51 write_c0_entryhi((entry
| 0x80000) << 12);
52 entry
++; /* BARRIER */
55 write_c0_entryhi(old_ctx
);
58 void local_flush_tlb_all(void)
65 local_irq_save(flags
);
66 local_flush_tlb_from(r3k_have_wired_reg
? read_c0_wired() : 8);
67 local_irq_restore(flags
);
70 void local_flush_tlb_range(struct vm_area_struct
*vma
, unsigned long start
,
73 unsigned long asid_mask
= cpu_asid_mask(¤t_cpu_data
);
74 struct mm_struct
*mm
= vma
->vm_mm
;
75 int cpu
= smp_processor_id();
77 if (cpu_context(cpu
, mm
) != 0) {
78 unsigned long size
, flags
;
81 printk("[tlbrange<%lu,0x%08lx,0x%08lx>]",
82 cpu_context(cpu
, mm
) & asid_mask
, start
, end
);
84 local_irq_save(flags
);
85 size
= (end
- start
+ (PAGE_SIZE
- 1)) >> PAGE_SHIFT
;
86 if (size
<= current_cpu_data
.tlbsize
) {
87 int oldpid
= read_c0_entryhi() & asid_mask
;
88 int newpid
= cpu_context(cpu
, mm
) & asid_mask
;
96 write_c0_entryhi(start
| newpid
);
97 start
+= PAGE_SIZE
; /* BARRIER */
99 idx
= read_c0_index();
100 write_c0_entrylo0(0);
101 write_c0_entryhi(KSEG0
);
102 if (idx
< 0) /* BARRIER */
106 write_c0_entryhi(oldpid
);
108 drop_mmu_context(mm
);
110 local_irq_restore(flags
);
114 void local_flush_tlb_kernel_range(unsigned long start
, unsigned long end
)
116 unsigned long size
, flags
;
119 printk("[tlbrange<%lu,0x%08lx,0x%08lx>]", start
, end
);
121 local_irq_save(flags
);
122 size
= (end
- start
+ (PAGE_SIZE
- 1)) >> PAGE_SHIFT
;
123 if (size
<= current_cpu_data
.tlbsize
) {
124 int pid
= read_c0_entryhi();
127 end
+= PAGE_SIZE
- 1;
130 while (start
< end
) {
133 write_c0_entryhi(start
);
134 start
+= PAGE_SIZE
; /* BARRIER */
136 idx
= read_c0_index();
137 write_c0_entrylo0(0);
138 write_c0_entryhi(KSEG0
);
139 if (idx
< 0) /* BARRIER */
143 write_c0_entryhi(pid
);
145 local_flush_tlb_all();
147 local_irq_restore(flags
);
150 void local_flush_tlb_page(struct vm_area_struct
*vma
, unsigned long page
)
152 unsigned long asid_mask
= cpu_asid_mask(¤t_cpu_data
);
153 int cpu
= smp_processor_id();
155 if (cpu_context(cpu
, vma
->vm_mm
) != 0) {
157 int oldpid
, newpid
, idx
;
160 printk("[tlbpage<%lu,0x%08lx>]", cpu_context(cpu
, vma
->vm_mm
), page
);
162 newpid
= cpu_context(cpu
, vma
->vm_mm
) & asid_mask
;
164 local_irq_save(flags
);
165 oldpid
= read_c0_entryhi() & asid_mask
;
166 write_c0_entryhi(page
| newpid
);
169 idx
= read_c0_index();
170 write_c0_entrylo0(0);
171 write_c0_entryhi(KSEG0
);
172 if (idx
< 0) /* BARRIER */
177 write_c0_entryhi(oldpid
);
178 local_irq_restore(flags
);
182 void __update_tlb(struct vm_area_struct
*vma
, unsigned long address
, pte_t pte
)
184 unsigned long asid_mask
= cpu_asid_mask(¤t_cpu_data
);
189 * Handle debugger faulting in for debugee.
191 if (current
->active_mm
!= vma
->vm_mm
)
194 pid
= read_c0_entryhi() & asid_mask
;
197 if ((pid
!= (cpu_context(cpu
, vma
->vm_mm
) & asid_mask
)) || (cpu_context(cpu
, vma
->vm_mm
) == 0)) {
198 printk("update_mmu_cache: Wheee, bogus tlbpid mmpid=%lu tlbpid=%d\n",
199 (cpu_context(cpu
, vma
->vm_mm
)), pid
);
203 local_irq_save(flags
);
204 address
&= PAGE_MASK
;
205 write_c0_entryhi(address
| pid
);
208 idx
= read_c0_index();
209 write_c0_entrylo0(pte_val(pte
));
210 write_c0_entryhi(address
| pid
);
211 if (idx
< 0) { /* BARRIER */
216 write_c0_entryhi(pid
);
217 local_irq_restore(flags
);
220 void add_wired_entry(unsigned long entrylo0
, unsigned long entrylo1
,
221 unsigned long entryhi
, unsigned long pagemask
)
223 unsigned long asid_mask
= cpu_asid_mask(¤t_cpu_data
);
225 unsigned long old_ctx
;
226 static unsigned long wired
= 0;
228 if (r3k_have_wired_reg
) { /* TX39XX */
229 unsigned long old_pagemask
;
233 printk("[tlbwired<entry lo0 %8x, hi %8x\n, pagemask %8x>]\n",
234 entrylo0
, entryhi
, pagemask
);
237 local_irq_save(flags
);
238 /* Save old context and create impossible VPN2 value */
239 old_ctx
= read_c0_entryhi() & asid_mask
;
240 old_pagemask
= read_c0_pagemask();
242 write_c0_wired(w
+ 1);
243 write_c0_index(w
<< 8);
244 write_c0_pagemask(pagemask
);
245 write_c0_entryhi(entryhi
);
246 write_c0_entrylo0(entrylo0
);
250 write_c0_entryhi(old_ctx
);
251 write_c0_pagemask(old_pagemask
);
252 local_flush_tlb_all();
253 local_irq_restore(flags
);
255 } else if (wired
< 8) {
257 printk("[tlbwired<entry lo0 %8x, hi %8x\n>]\n",
261 local_irq_save(flags
);
262 old_ctx
= read_c0_entryhi() & asid_mask
;
263 write_c0_entrylo0(entrylo0
);
264 write_c0_entryhi(entryhi
);
265 write_c0_index(wired
);
266 wired
++; /* BARRIER */
268 write_c0_entryhi(old_ctx
);
269 local_flush_tlb_all();
270 local_irq_restore(flags
);
276 switch (current_cpu_type()) {
279 r3k_have_wired_reg
= 1;
280 write_c0_wired(0); /* Set to 8 on reset... */
283 local_flush_tlb_from(0);
284 build_tlb_refill_handler();