2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
7 * Copyright (C) 1997, 1998, 1999, 2000 Ralf Baechle ralf@gnu.org
8 * Carsten Langgaard, carstenl@mips.com
9 * Copyright (C) 2002 MIPS Technologies, Inc. All rights reserved.
11 #include <linux/init.h>
12 #include <linux/sched.h>
13 #include <linux/smp.h>
15 #include <linux/hugetlb.h>
16 #include <linux/module.h>
19 #include <asm/cpu-type.h>
20 #include <asm/bootinfo.h>
21 #include <asm/mmu_context.h>
22 #include <asm/pgtable.h>
24 #include <asm/tlbmisc.h>
26 extern void build_tlb_refill_handler(void);
28 /* Atomicity and interruptability */
29 #ifdef CONFIG_MIPS_MT_SMTC
32 #include <asm/mipsmtregs.h>
34 #define ENTER_CRITICAL(flags) \
36 unsigned int mvpflags; \
37 local_irq_save(flags);\
39 #define EXIT_CRITICAL(flags) \
41 local_irq_restore(flags); \
45 #define ENTER_CRITICAL(flags) local_irq_save(flags)
46 #define EXIT_CRITICAL(flags) local_irq_restore(flags)
48 #endif /* CONFIG_MIPS_MT_SMTC */
51 * LOONGSON2/3 has a 4 entry itlb which is a subset of dtlb,
52 * unfortunately, itlb is not totally transparent to software.
54 static inline void flush_itlb(void)
56 switch (current_cpu_type()) {
66 static inline void flush_itlb_vm(struct vm_area_struct
*vma
)
68 if (vma
->vm_flags
& VM_EXEC
)
72 void local_flush_tlb_all(void)
75 unsigned long old_ctx
;
76 int entry
, ftlbhighset
;
78 ENTER_CRITICAL(flags
);
79 /* Save old context and create impossible VPN2 value */
80 old_ctx
= read_c0_entryhi();
84 entry
= read_c0_wired();
86 /* Blast 'em all away. */
88 if (current_cpu_data
.tlbsizevtlb
) {
91 tlbinvf(); /* invalidate VTLB */
93 ftlbhighset
= current_cpu_data
.tlbsizevtlb
+
94 current_cpu_data
.tlbsizeftlbsets
;
95 for (entry
= current_cpu_data
.tlbsizevtlb
;
98 write_c0_index(entry
);
100 tlbinvf(); /* invalidate one FTLB set */
103 while (entry
< current_cpu_data
.tlbsize
) {
104 /* Make sure all entries differ. */
105 write_c0_entryhi(UNIQUE_ENTRYHI(entry
));
106 write_c0_index(entry
);
113 write_c0_entryhi(old_ctx
);
115 EXIT_CRITICAL(flags
);
117 EXPORT_SYMBOL(local_flush_tlb_all
);
119 /* All entries common to a mm share an asid. To effectively flush
120 these entries, we just bump the asid. */
121 void local_flush_tlb_mm(struct mm_struct
*mm
)
127 cpu
= smp_processor_id();
129 if (cpu_context(cpu
, mm
) != 0) {
130 drop_mmu_context(mm
, cpu
);
136 void local_flush_tlb_range(struct vm_area_struct
*vma
, unsigned long start
,
139 struct mm_struct
*mm
= vma
->vm_mm
;
140 int cpu
= smp_processor_id();
142 if (cpu_context(cpu
, mm
) != 0) {
143 unsigned long size
, flags
;
145 ENTER_CRITICAL(flags
);
146 start
= round_down(start
, PAGE_SIZE
<< 1);
147 end
= round_up(end
, PAGE_SIZE
<< 1);
148 size
= (end
- start
) >> (PAGE_SHIFT
+ 1);
149 if (size
<= (current_cpu_data
.tlbsizeftlbsets
?
150 current_cpu_data
.tlbsize
/ 8 :
151 current_cpu_data
.tlbsize
/ 2)) {
152 int oldpid
= read_c0_entryhi();
153 int newpid
= cpu_asid(cpu
, mm
);
155 while (start
< end
) {
158 write_c0_entryhi(start
| newpid
);
159 start
+= (PAGE_SIZE
<< 1);
163 idx
= read_c0_index();
164 write_c0_entrylo0(0);
165 write_c0_entrylo1(0);
168 /* Make sure all entries differ. */
169 write_c0_entryhi(UNIQUE_ENTRYHI(idx
));
174 write_c0_entryhi(oldpid
);
176 drop_mmu_context(mm
, cpu
);
179 EXIT_CRITICAL(flags
);
183 void local_flush_tlb_kernel_range(unsigned long start
, unsigned long end
)
185 unsigned long size
, flags
;
187 ENTER_CRITICAL(flags
);
188 size
= (end
- start
+ (PAGE_SIZE
- 1)) >> PAGE_SHIFT
;
189 size
= (size
+ 1) >> 1;
190 if (size
<= (current_cpu_data
.tlbsizeftlbsets
?
191 current_cpu_data
.tlbsize
/ 8 :
192 current_cpu_data
.tlbsize
/ 2)) {
193 int pid
= read_c0_entryhi();
195 start
&= (PAGE_MASK
<< 1);
196 end
+= ((PAGE_SIZE
<< 1) - 1);
197 end
&= (PAGE_MASK
<< 1);
199 while (start
< end
) {
202 write_c0_entryhi(start
);
203 start
+= (PAGE_SIZE
<< 1);
207 idx
= read_c0_index();
208 write_c0_entrylo0(0);
209 write_c0_entrylo1(0);
212 /* Make sure all entries differ. */
213 write_c0_entryhi(UNIQUE_ENTRYHI(idx
));
218 write_c0_entryhi(pid
);
220 local_flush_tlb_all();
223 EXIT_CRITICAL(flags
);
226 void local_flush_tlb_page(struct vm_area_struct
*vma
, unsigned long page
)
228 int cpu
= smp_processor_id();
230 if (cpu_context(cpu
, vma
->vm_mm
) != 0) {
232 int oldpid
, newpid
, idx
;
234 newpid
= cpu_asid(cpu
, vma
->vm_mm
);
235 page
&= (PAGE_MASK
<< 1);
236 ENTER_CRITICAL(flags
);
237 oldpid
= read_c0_entryhi();
238 write_c0_entryhi(page
| newpid
);
242 idx
= read_c0_index();
243 write_c0_entrylo0(0);
244 write_c0_entrylo1(0);
247 /* Make sure all entries differ. */
248 write_c0_entryhi(UNIQUE_ENTRYHI(idx
));
254 write_c0_entryhi(oldpid
);
256 EXIT_CRITICAL(flags
);
261 * This one is only used for pages with the global bit set so we don't care
262 * much about the ASID.
264 void local_flush_tlb_one(unsigned long page
)
269 ENTER_CRITICAL(flags
);
270 oldpid
= read_c0_entryhi();
271 page
&= (PAGE_MASK
<< 1);
272 write_c0_entryhi(page
);
276 idx
= read_c0_index();
277 write_c0_entrylo0(0);
278 write_c0_entrylo1(0);
280 /* Make sure all entries differ. */
281 write_c0_entryhi(UNIQUE_ENTRYHI(idx
));
286 write_c0_entryhi(oldpid
);
288 EXIT_CRITICAL(flags
);
292 * We will need multiple versions of update_mmu_cache(), one that just
293 * updates the TLB with the new pte(s), and another which also checks
294 * for the R4k "end of page" hardware bug and does the needy.
296 void __update_tlb(struct vm_area_struct
* vma
, unsigned long address
, pte_t pte
)
306 * Handle debugger faulting in for debugee.
308 if (current
->active_mm
!= vma
->vm_mm
)
311 ENTER_CRITICAL(flags
);
313 pid
= read_c0_entryhi() & ASID_MASK
;
314 address
&= (PAGE_MASK
<< 1);
315 write_c0_entryhi(address
| pid
);
316 pgdp
= pgd_offset(vma
->vm_mm
, address
);
320 pudp
= pud_offset(pgdp
, address
);
321 pmdp
= pmd_offset(pudp
, address
);
322 idx
= read_c0_index();
323 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
324 /* this could be a huge page */
325 if (pmd_huge(*pmdp
)) {
327 write_c0_pagemask(PM_HUGE_MASK
);
328 ptep
= (pte_t
*)pmdp
;
329 lo
= pte_to_entrylo(pte_val(*ptep
));
330 write_c0_entrylo0(lo
);
331 write_c0_entrylo1(lo
+ (HPAGE_SIZE
>> 7));
339 write_c0_pagemask(PM_DEFAULT_MASK
);
343 ptep
= pte_offset_map(pmdp
, address
);
345 #if defined(CONFIG_64BIT_PHYS_ADDR) && defined(CONFIG_CPU_MIPS32)
346 write_c0_entrylo0(ptep
->pte_high
);
348 write_c0_entrylo1(ptep
->pte_high
);
350 write_c0_entrylo0(pte_to_entrylo(pte_val(*ptep
++)));
351 write_c0_entrylo1(pte_to_entrylo(pte_val(*ptep
)));
361 EXIT_CRITICAL(flags
);
364 void add_wired_entry(unsigned long entrylo0
, unsigned long entrylo1
,
365 unsigned long entryhi
, unsigned long pagemask
)
369 unsigned long old_pagemask
;
370 unsigned long old_ctx
;
372 ENTER_CRITICAL(flags
);
373 /* Save old context and create impossible VPN2 value */
374 old_ctx
= read_c0_entryhi();
375 old_pagemask
= read_c0_pagemask();
376 wired
= read_c0_wired();
377 write_c0_wired(wired
+ 1);
378 write_c0_index(wired
);
379 tlbw_use_hazard(); /* What is the hazard here? */
380 write_c0_pagemask(pagemask
);
381 write_c0_entryhi(entryhi
);
382 write_c0_entrylo0(entrylo0
);
383 write_c0_entrylo1(entrylo1
);
388 write_c0_entryhi(old_ctx
);
389 tlbw_use_hazard(); /* What is the hazard here? */
390 write_c0_pagemask(old_pagemask
);
391 local_flush_tlb_all();
392 EXIT_CRITICAL(flags
);
395 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
397 int __init
has_transparent_hugepage(void)
402 ENTER_CRITICAL(flags
);
403 write_c0_pagemask(PM_HUGE_MASK
);
404 back_to_back_c0_hazard();
405 mask
= read_c0_pagemask();
406 write_c0_pagemask(PM_DEFAULT_MASK
);
408 EXIT_CRITICAL(flags
);
410 return mask
== PM_HUGE_MASK
;
413 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
416 static int __init
set_ntlb(char *str
)
418 get_option(&str
, &ntlb
);
422 __setup("ntlb=", set_ntlb
);
427 * You should never change this register:
428 * - On R4600 1.7 the tlbp never hits for pages smaller than
429 * the value in the c0_pagemask register.
430 * - The entire mm handling assumes the c0_pagemask register to
431 * be set to fixed-size pages.
433 write_c0_pagemask(PM_DEFAULT_MASK
);
435 if (current_cpu_type() == CPU_R10000
||
436 current_cpu_type() == CPU_R12000
||
437 current_cpu_type() == CPU_R14000
)
438 write_c0_framemask(0);
442 * Enable the no read, no exec bits, and enable large virtual
445 u32 pg
= PG_RIE
| PG_XIE
;
449 write_c0_pagegrain(pg
);
452 /* From this point on the ARC firmware is dead. */
453 local_flush_tlb_all();
455 /* Did I tell you that ARC SUCKS? */
458 if (ntlb
> 1 && ntlb
<= current_cpu_data
.tlbsize
) {
459 int wired
= current_cpu_data
.tlbsize
- ntlb
;
460 write_c0_wired(wired
);
461 write_c0_index(wired
-1);
462 printk("Restricting TLB to %d entries\n", ntlb
);
464 printk("Ignoring invalid argument ntlb=%d\n", ntlb
);
467 build_tlb_refill_handler();