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/cpu_pm.h>
12 #include <linux/init.h>
13 #include <linux/sched.h>
14 #include <linux/smp.h>
16 #include <linux/hugetlb.h>
17 #include <linux/module.h>
20 #include <asm/cpu-type.h>
21 #include <asm/bootinfo.h>
22 #include <asm/hazards.h>
23 #include <asm/mmu_context.h>
24 #include <asm/pgtable.h>
26 #include <asm/tlbmisc.h>
28 extern void build_tlb_refill_handler(void);
31 * LOONGSON-2 has a 4 entry itlb which is a subset of jtlb, LOONGSON-3 has
32 * a 4 entry itlb and a 4 entry dtlb which are subsets of jtlb. Unfortunately,
33 * itlb/dtlb are not totally transparent to software.
35 static inline void flush_micro_tlb(void)
37 switch (current_cpu_type()) {
39 write_c0_diag(LOONGSON_DIAG_ITLB
);
42 write_c0_diag(LOONGSON_DIAG_ITLB
| LOONGSON_DIAG_DTLB
);
49 static inline void flush_micro_tlb_vm(struct vm_area_struct
*vma
)
51 if (vma
->vm_flags
& VM_EXEC
)
55 void local_flush_tlb_all(void)
58 unsigned long old_ctx
;
59 int entry
, ftlbhighset
;
61 local_irq_save(flags
);
62 /* Save old context and create impossible VPN2 value */
63 old_ctx
= read_c0_entryhi();
68 entry
= read_c0_wired();
70 /* Blast 'em all away. */
72 if (current_cpu_data
.tlbsizevtlb
) {
75 tlbinvf(); /* invalidate VTLB */
77 ftlbhighset
= current_cpu_data
.tlbsizevtlb
+
78 current_cpu_data
.tlbsizeftlbsets
;
79 for (entry
= current_cpu_data
.tlbsizevtlb
;
82 write_c0_index(entry
);
84 tlbinvf(); /* invalidate one FTLB set */
87 while (entry
< current_cpu_data
.tlbsize
) {
88 /* Make sure all entries differ. */
89 write_c0_entryhi(UNIQUE_ENTRYHI(entry
));
90 write_c0_index(entry
);
97 write_c0_entryhi(old_ctx
);
100 local_irq_restore(flags
);
102 EXPORT_SYMBOL(local_flush_tlb_all
);
104 /* All entries common to a mm share an asid. To effectively flush
105 these entries, we just bump the asid. */
106 void local_flush_tlb_mm(struct mm_struct
*mm
)
112 cpu
= smp_processor_id();
114 if (cpu_context(cpu
, mm
) != 0) {
115 drop_mmu_context(mm
, cpu
);
121 void local_flush_tlb_range(struct vm_area_struct
*vma
, unsigned long start
,
124 struct mm_struct
*mm
= vma
->vm_mm
;
125 int cpu
= smp_processor_id();
127 if (cpu_context(cpu
, mm
) != 0) {
128 unsigned long size
, flags
;
130 local_irq_save(flags
);
131 start
= round_down(start
, PAGE_SIZE
<< 1);
132 end
= round_up(end
, PAGE_SIZE
<< 1);
133 size
= (end
- start
) >> (PAGE_SHIFT
+ 1);
134 if (size
<= (current_cpu_data
.tlbsizeftlbsets
?
135 current_cpu_data
.tlbsize
/ 8 :
136 current_cpu_data
.tlbsize
/ 2)) {
137 int oldpid
= read_c0_entryhi();
138 int newpid
= cpu_asid(cpu
, mm
);
141 while (start
< end
) {
144 write_c0_entryhi(start
| newpid
);
145 start
+= (PAGE_SIZE
<< 1);
149 idx
= read_c0_index();
150 write_c0_entrylo0(0);
151 write_c0_entrylo1(0);
154 /* Make sure all entries differ. */
155 write_c0_entryhi(UNIQUE_ENTRYHI(idx
));
160 write_c0_entryhi(oldpid
);
163 drop_mmu_context(mm
, cpu
);
166 local_irq_restore(flags
);
170 void local_flush_tlb_kernel_range(unsigned long start
, unsigned long end
)
172 unsigned long size
, flags
;
174 local_irq_save(flags
);
175 size
= (end
- start
+ (PAGE_SIZE
- 1)) >> PAGE_SHIFT
;
176 size
= (size
+ 1) >> 1;
177 if (size
<= (current_cpu_data
.tlbsizeftlbsets
?
178 current_cpu_data
.tlbsize
/ 8 :
179 current_cpu_data
.tlbsize
/ 2)) {
180 int pid
= read_c0_entryhi();
182 start
&= (PAGE_MASK
<< 1);
183 end
+= ((PAGE_SIZE
<< 1) - 1);
184 end
&= (PAGE_MASK
<< 1);
187 while (start
< end
) {
190 write_c0_entryhi(start
);
191 start
+= (PAGE_SIZE
<< 1);
195 idx
= read_c0_index();
196 write_c0_entrylo0(0);
197 write_c0_entrylo1(0);
200 /* Make sure all entries differ. */
201 write_c0_entryhi(UNIQUE_ENTRYHI(idx
));
206 write_c0_entryhi(pid
);
209 local_flush_tlb_all();
212 local_irq_restore(flags
);
215 void local_flush_tlb_page(struct vm_area_struct
*vma
, unsigned long page
)
217 int cpu
= smp_processor_id();
219 if (cpu_context(cpu
, vma
->vm_mm
) != 0) {
221 int oldpid
, newpid
, idx
;
223 newpid
= cpu_asid(cpu
, vma
->vm_mm
);
224 page
&= (PAGE_MASK
<< 1);
225 local_irq_save(flags
);
226 oldpid
= read_c0_entryhi();
228 write_c0_entryhi(page
| newpid
);
232 idx
= read_c0_index();
233 write_c0_entrylo0(0);
234 write_c0_entrylo1(0);
237 /* Make sure all entries differ. */
238 write_c0_entryhi(UNIQUE_ENTRYHI(idx
));
244 write_c0_entryhi(oldpid
);
246 flush_micro_tlb_vm(vma
);
247 local_irq_restore(flags
);
252 * This one is only used for pages with the global bit set so we don't care
253 * much about the ASID.
255 void local_flush_tlb_one(unsigned long page
)
260 local_irq_save(flags
);
261 oldpid
= read_c0_entryhi();
263 page
&= (PAGE_MASK
<< 1);
264 write_c0_entryhi(page
);
268 idx
= read_c0_index();
269 write_c0_entrylo0(0);
270 write_c0_entrylo1(0);
272 /* Make sure all entries differ. */
273 write_c0_entryhi(UNIQUE_ENTRYHI(idx
));
278 write_c0_entryhi(oldpid
);
281 local_irq_restore(flags
);
285 * We will need multiple versions of update_mmu_cache(), one that just
286 * updates the TLB with the new pte(s), and another which also checks
287 * for the R4k "end of page" hardware bug and does the needy.
289 void __update_tlb(struct vm_area_struct
* vma
, unsigned long address
, pte_t pte
)
299 * Handle debugger faulting in for debugee.
301 if (current
->active_mm
!= vma
->vm_mm
)
304 local_irq_save(flags
);
307 pid
= read_c0_entryhi() & cpu_asid_mask(¤t_cpu_data
);
308 address
&= (PAGE_MASK
<< 1);
309 write_c0_entryhi(address
| pid
);
310 pgdp
= pgd_offset(vma
->vm_mm
, address
);
314 pudp
= pud_offset(pgdp
, address
);
315 pmdp
= pmd_offset(pudp
, address
);
316 idx
= read_c0_index();
317 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
318 /* this could be a huge page */
319 if (pmd_huge(*pmdp
)) {
321 write_c0_pagemask(PM_HUGE_MASK
);
322 ptep
= (pte_t
*)pmdp
;
323 lo
= pte_to_entrylo(pte_val(*ptep
));
324 write_c0_entrylo0(lo
);
325 write_c0_entrylo1(lo
+ (HPAGE_SIZE
>> 7));
333 write_c0_pagemask(PM_DEFAULT_MASK
);
337 ptep
= pte_offset_map(pmdp
, address
);
339 #if defined(CONFIG_PHYS_ADDR_T_64BIT) && defined(CONFIG_CPU_MIPS32)
341 write_c0_entrylo0(pte_to_entrylo(ptep
->pte_high
));
343 writex_c0_entrylo0(ptep
->pte_low
& _PFNX_MASK
);
345 write_c0_entrylo1(pte_to_entrylo(ptep
->pte_high
));
347 writex_c0_entrylo1(ptep
->pte_low
& _PFNX_MASK
);
349 write_c0_entrylo0(ptep
->pte_high
);
351 write_c0_entrylo1(ptep
->pte_high
);
354 write_c0_entrylo0(pte_to_entrylo(pte_val(*ptep
++)));
355 write_c0_entrylo1(pte_to_entrylo(pte_val(*ptep
)));
365 flush_micro_tlb_vm(vma
);
366 local_irq_restore(flags
);
369 void add_wired_entry(unsigned long entrylo0
, unsigned long entrylo1
,
370 unsigned long entryhi
, unsigned long pagemask
)
373 panic("Broken for XPA kernels");
377 unsigned long old_pagemask
;
378 unsigned long old_ctx
;
380 local_irq_save(flags
);
381 /* Save old context and create impossible VPN2 value */
382 old_ctx
= read_c0_entryhi();
384 old_pagemask
= read_c0_pagemask();
385 wired
= read_c0_wired();
386 write_c0_wired(wired
+ 1);
387 write_c0_index(wired
);
388 tlbw_use_hazard(); /* What is the hazard here? */
389 write_c0_pagemask(pagemask
);
390 write_c0_entryhi(entryhi
);
391 write_c0_entrylo0(entrylo0
);
392 write_c0_entrylo1(entrylo1
);
397 write_c0_entryhi(old_ctx
);
398 tlbw_use_hazard(); /* What is the hazard here? */
400 write_c0_pagemask(old_pagemask
);
401 local_flush_tlb_all();
402 local_irq_restore(flags
);
406 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
408 int has_transparent_hugepage(void)
410 static unsigned int mask
= -1;
412 if (mask
== -1) { /* first call comes during __init */
415 local_irq_save(flags
);
416 write_c0_pagemask(PM_HUGE_MASK
);
417 back_to_back_c0_hazard();
418 mask
= read_c0_pagemask();
419 write_c0_pagemask(PM_DEFAULT_MASK
);
420 local_irq_restore(flags
);
422 return mask
== PM_HUGE_MASK
;
425 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
428 * Used for loading TLB entries before trap_init() has started, when we
429 * don't actually want to add a wired entry which remains throughout the
430 * lifetime of the system
435 __init
int add_temporary_entry(unsigned long entrylo0
, unsigned long entrylo1
,
436 unsigned long entryhi
, unsigned long pagemask
)
441 unsigned long old_pagemask
;
442 unsigned long old_ctx
;
444 local_irq_save(flags
);
445 /* Save old context and create impossible VPN2 value */
447 old_ctx
= read_c0_entryhi();
448 old_pagemask
= read_c0_pagemask();
449 wired
= read_c0_wired();
450 if (--temp_tlb_entry
< wired
) {
452 "No TLB space left for add_temporary_entry\n");
457 write_c0_index(temp_tlb_entry
);
458 write_c0_pagemask(pagemask
);
459 write_c0_entryhi(entryhi
);
460 write_c0_entrylo0(entrylo0
);
461 write_c0_entrylo1(entrylo1
);
466 write_c0_entryhi(old_ctx
);
467 write_c0_pagemask(old_pagemask
);
470 local_irq_restore(flags
);
475 static int __init
set_ntlb(char *str
)
477 get_option(&str
, &ntlb
);
481 __setup("ntlb=", set_ntlb
);
484 * Configure TLB (for init or after a CPU has been powered off).
486 static void r4k_tlb_configure(void)
489 * You should never change this register:
490 * - On R4600 1.7 the tlbp never hits for pages smaller than
491 * the value in the c0_pagemask register.
492 * - The entire mm handling assumes the c0_pagemask register to
493 * be set to fixed-size pages.
495 write_c0_pagemask(PM_DEFAULT_MASK
);
496 back_to_back_c0_hazard();
497 if (read_c0_pagemask() != PM_DEFAULT_MASK
)
498 panic("MMU doesn't support PAGE_SIZE=0x%lx", PAGE_SIZE
);
501 if (current_cpu_type() == CPU_R10000
||
502 current_cpu_type() == CPU_R12000
||
503 current_cpu_type() == CPU_R14000
||
504 current_cpu_type() == CPU_R16000
)
505 write_c0_framemask(0);
509 * Enable the no read, no exec bits, and enable large physical
513 set_c0_pagegrain(PG_RIE
| PG_XIE
| PG_ELPA
);
515 set_c0_pagegrain(PG_RIE
| PG_XIE
);
519 temp_tlb_entry
= current_cpu_data
.tlbsize
- 1;
521 /* From this point on the ARC firmware is dead. */
522 local_flush_tlb_all();
524 /* Did I tell you that ARC SUCKS? */
532 if (ntlb
> 1 && ntlb
<= current_cpu_data
.tlbsize
) {
533 int wired
= current_cpu_data
.tlbsize
- ntlb
;
534 write_c0_wired(wired
);
535 write_c0_index(wired
-1);
536 printk("Restricting TLB to %d entries\n", ntlb
);
538 printk("Ignoring invalid argument ntlb=%d\n", ntlb
);
541 build_tlb_refill_handler();
544 static int r4k_tlb_pm_notifier(struct notifier_block
*self
, unsigned long cmd
,
548 case CPU_PM_ENTER_FAILED
:
557 static struct notifier_block r4k_tlb_pm_notifier_block
= {
558 .notifier_call
= r4k_tlb_pm_notifier
,
561 static int __init
r4k_tlb_init_pm(void)
563 return cpu_pm_register_notifier(&r4k_tlb_pm_notifier_block
);
565 arch_initcall(r4k_tlb_init_pm
);