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 (dm@engr.sgi.com)
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>
16 #include <asm/bootinfo.h>
17 #include <asm/mmu_context.h>
18 #include <asm/pgtable.h>
19 #include <asm/system.h>
21 extern void build_tlb_refill_handler(void);
24 * Make sure all entries differ. If they're not different
25 * MIPS32 will take revenge ...
27 #define UNIQUE_ENTRYHI(idx) (CKSEG0 + ((idx) << (PAGE_SHIFT + 1)))
29 /* CP0 hazard avoidance. */
30 #define BARRIER __asm__ __volatile__(".set noreorder\n\t" \
31 "nop; nop; nop; nop; nop; nop;\n\t" \
34 /* Atomicity and interruptability */
35 #ifdef CONFIG_MIPS_MT_SMTC
38 #include <asm/mipsmtregs.h>
40 #define ENTER_CRITICAL(flags) \
42 unsigned int mvpflags; \
43 local_irq_save(flags);\
45 #define EXIT_CRITICAL(flags) \
47 local_irq_restore(flags); \
51 #define ENTER_CRITICAL(flags) local_irq_save(flags)
52 #define EXIT_CRITICAL(flags) local_irq_restore(flags)
54 #endif /* CONFIG_MIPS_MT_SMTC */
56 void local_flush_tlb_all(void)
59 unsigned long old_ctx;
62 ENTER_CRITICAL(flags);
63 /* Save old context and create impossible VPN2 value */
64 old_ctx = read_c0_entryhi();
68 entry = read_c0_wired();
70 /* Blast 'em all away. */
71 while (entry < current_cpu_data.tlbsize) {
72 /* Make sure all entries differ. */
73 write_c0_entryhi(UNIQUE_ENTRYHI(entry));
74 write_c0_index(entry);
80 write_c0_entryhi(old_ctx);
84 /* All entries common to a mm share an asid. To effectively flush
85 these entries, we just bump the asid. */
86 void local_flush_tlb_mm(struct mm_struct *mm)
92 cpu = smp_processor_id();
94 if (cpu_context(cpu, mm) != 0) {
95 drop_mmu_context(mm, cpu);
101 void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
104 struct mm_struct *mm = vma->vm_mm;
105 int cpu = smp_processor_id();
107 if (cpu_context(cpu, mm) != 0) {
111 ENTER_CRITICAL(flags);
112 size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
113 size = (size + 1) >> 1;
114 local_irq_save(flags);
115 if (size <= current_cpu_data.tlbsize/2) {
116 int oldpid = read_c0_entryhi();
117 int newpid = cpu_asid(cpu, mm);
119 start &= (PAGE_MASK << 1);
120 end += ((PAGE_SIZE << 1) - 1);
121 end &= (PAGE_MASK << 1);
122 while (start < end) {
125 write_c0_entryhi(start | newpid);
126 start += (PAGE_SIZE << 1);
130 idx = read_c0_index();
131 write_c0_entrylo0(0);
132 write_c0_entrylo1(0);
135 /* Make sure all entries differ. */
136 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
141 write_c0_entryhi(oldpid);
143 drop_mmu_context(mm, cpu);
145 EXIT_CRITICAL(flags);
149 void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
154 ENTER_CRITICAL(flags);
155 size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
156 size = (size + 1) >> 1;
157 if (size <= current_cpu_data.tlbsize / 2) {
158 int pid = read_c0_entryhi();
160 start &= (PAGE_MASK << 1);
161 end += ((PAGE_SIZE << 1) - 1);
162 end &= (PAGE_MASK << 1);
164 while (start < end) {
167 write_c0_entryhi(start);
168 start += (PAGE_SIZE << 1);
172 idx = read_c0_index();
173 write_c0_entrylo0(0);
174 write_c0_entrylo1(0);
177 /* Make sure all entries differ. */
178 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
183 write_c0_entryhi(pid);
185 local_flush_tlb_all();
187 EXIT_CRITICAL(flags);
190 void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
192 int cpu = smp_processor_id();
194 if (cpu_context(cpu, vma->vm_mm) != 0) {
196 int oldpid, newpid, idx;
198 newpid = cpu_asid(cpu, vma->vm_mm);
199 page &= (PAGE_MASK << 1);
200 ENTER_CRITICAL(flags);
201 oldpid = read_c0_entryhi();
202 write_c0_entryhi(page | newpid);
206 idx = read_c0_index();
207 write_c0_entrylo0(0);
208 write_c0_entrylo1(0);
211 /* Make sure all entries differ. */
212 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
218 write_c0_entryhi(oldpid);
219 EXIT_CRITICAL(flags);
224 * This one is only used for pages with the global bit set so we don't care
225 * much about the ASID.
227 void local_flush_tlb_one(unsigned long page)
232 ENTER_CRITICAL(flags);
233 oldpid = read_c0_entryhi();
234 page &= (PAGE_MASK << 1);
235 write_c0_entryhi(page);
239 idx = read_c0_index();
240 write_c0_entrylo0(0);
241 write_c0_entrylo1(0);
243 /* Make sure all entries differ. */
244 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
249 write_c0_entryhi(oldpid);
251 EXIT_CRITICAL(flags);
255 * We will need multiple versions of update_mmu_cache(), one that just
256 * updates the TLB with the new pte(s), and another which also checks
257 * for the R4k "end of page" hardware bug and does the needy.
259 void __update_tlb(struct vm_area_struct * vma, unsigned long address, pte_t pte)
269 * Handle debugger faulting in for debugee.
271 if (current->active_mm != vma->vm_mm)
274 ENTER_CRITICAL(flags);
276 pid = read_c0_entryhi() & ASID_MASK;
277 address &= (PAGE_MASK << 1);
278 write_c0_entryhi(address | pid);
279 pgdp = pgd_offset(vma->vm_mm, address);
283 pudp = pud_offset(pgdp, address);
284 pmdp = pmd_offset(pudp, address);
285 idx = read_c0_index();
286 ptep = pte_offset_map(pmdp, address);
288 #if defined(CONFIG_64BIT_PHYS_ADDR) && defined(CONFIG_CPU_MIPS32_R1)
289 write_c0_entrylo0(ptep->pte_high);
291 write_c0_entrylo1(ptep->pte_high);
293 write_c0_entrylo0(pte_val(*ptep++) >> 6);
294 write_c0_entrylo1(pte_val(*ptep) >> 6);
302 EXIT_CRITICAL(flags);
306 static void r4k_update_mmu_cache_hwbug(struct vm_area_struct * vma,
307 unsigned long address, pte_t pte)
316 ENTER_CRITICAL(flags);
317 address &= (PAGE_MASK << 1);
318 asid = read_c0_entryhi() & ASID_MASK;
319 write_c0_entryhi(address | asid);
320 pgdp = pgd_offset(vma->vm_mm, address);
324 pmdp = pmd_offset(pgdp, address);
325 idx = read_c0_index();
326 ptep = pte_offset_map(pmdp, address);
327 write_c0_entrylo0(pte_val(*ptep++) >> 6);
328 write_c0_entrylo1(pte_val(*ptep) >> 6);
335 EXIT_CRITICAL(flags);
339 void __init add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
340 unsigned long entryhi, unsigned long pagemask)
344 unsigned long old_pagemask;
345 unsigned long old_ctx;
347 ENTER_CRITICAL(flags);
348 /* Save old context and create impossible VPN2 value */
349 old_ctx = read_c0_entryhi();
350 old_pagemask = read_c0_pagemask();
351 wired = read_c0_wired();
352 write_c0_wired(wired + 1);
353 write_c0_index(wired);
355 write_c0_pagemask(pagemask);
356 write_c0_entryhi(entryhi);
357 write_c0_entrylo0(entrylo0);
358 write_c0_entrylo1(entrylo1);
363 write_c0_entryhi(old_ctx);
365 write_c0_pagemask(old_pagemask);
366 local_flush_tlb_all();
367 EXIT_CRITICAL(flags);
371 * Used for loading TLB entries before trap_init() has started, when we
372 * don't actually want to add a wired entry which remains throughout the
373 * lifetime of the system
376 static int temp_tlb_entry __initdata;
378 __init int add_temporary_entry(unsigned long entrylo0, unsigned long entrylo1,
379 unsigned long entryhi, unsigned long pagemask)
384 unsigned long old_pagemask;
385 unsigned long old_ctx;
387 ENTER_CRITICAL(flags);
388 /* Save old context and create impossible VPN2 value */
389 old_ctx = read_c0_entryhi();
390 old_pagemask = read_c0_pagemask();
391 wired = read_c0_wired();
392 if (--temp_tlb_entry < wired) {
394 "No TLB space left for add_temporary_entry\n");
399 write_c0_index(temp_tlb_entry);
400 write_c0_pagemask(pagemask);
401 write_c0_entryhi(entryhi);
402 write_c0_entrylo0(entrylo0);
403 write_c0_entrylo1(entrylo1);
408 write_c0_entryhi(old_ctx);
409 write_c0_pagemask(old_pagemask);
411 EXIT_CRITICAL(flags);
415 static void __init probe_tlb(unsigned long config)
417 struct cpuinfo_mips *c = ¤t_cpu_data;
421 * If this isn't a MIPS32 / MIPS64 compliant CPU. Config 1 register
422 * is not supported, we assume R4k style. Cpu probing already figured
423 * out the number of tlb entries.
425 if ((c->processor_id & 0xff0000) == PRID_COMP_LEGACY)
427 #ifdef CONFIG_MIPS_MT_SMTC
429 * If TLB is shared in SMTC system, total size already
430 * has been calculated and written into cpu_data tlbsize
432 if((smtc_status & SMTC_TLB_SHARED) == SMTC_TLB_SHARED)
434 #endif /* CONFIG_MIPS_MT_SMTC */
436 reg = read_c0_config1();
437 if (!((config >> 7) & 3))
438 panic("No TLB present");
440 c->tlbsize = ((reg >> 25) & 0x3f) + 1;
443 static int __initdata ntlb = 0;
444 static int __init set_ntlb(char *str)
446 get_option(&str, &ntlb);
450 __setup("ntlb=", set_ntlb);
452 void __init tlb_init(void)
454 unsigned int config = read_c0_config();
457 * You should never change this register:
458 * - On R4600 1.7 the tlbp never hits for pages smaller than
459 * the value in the c0_pagemask register.
460 * - The entire mm handling assumes the c0_pagemask register to
461 * be set for 4kb pages.
464 write_c0_pagemask(PM_DEFAULT_MASK);
466 write_c0_framemask(0);
467 temp_tlb_entry = current_cpu_data.tlbsize - 1;
469 /* From this point on the ARC firmware is dead. */
470 local_flush_tlb_all();
472 /* Did I tell you that ARC SUCKS? */
475 if (ntlb > 1 && ntlb <= current_cpu_data.tlbsize) {
476 int wired = current_cpu_data.tlbsize - ntlb;
477 write_c0_wired(wired);
478 write_c0_index(wired-1);
479 printk ("Restricting TLB to %d entries\n", ntlb);
481 printk("Ignoring invalid argument ntlb=%d\n", ntlb);
484 build_tlb_refill_handler();