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/config.h>
12 #include <linux/init.h>
13 #include <linux/sched.h>
17 #include <asm/bootinfo.h>
18 #include <asm/mmu_context.h>
19 #include <asm/pgtable.h>
20 #include <asm/system.h>
22 extern void build_tlb_refill_handler(void);
25 * Make sure all entries differ. If they're not different
26 * MIPS32 will take revenge ...
28 #define UNIQUE_ENTRYHI(idx) (CKSEG0 + ((idx) << (PAGE_SHIFT + 1)))
30 /* CP0 hazard avoidance. */
31 #define BARRIER __asm__ __volatile__(".set noreorder\n\t" \
32 "nop; nop; nop; nop; nop; nop;\n\t" \
35 /* Atomicity and interruptability */
36 #ifdef CONFIG_MIPS_MT_SMTC
39 #include <asm/mipsmtregs.h>
41 #define ENTER_CRITICAL(flags) \
43 unsigned int mvpflags; \
44 local_irq_save(flags);\
46 #define EXIT_CRITICAL(flags) \
48 local_irq_restore(flags); \
52 #define ENTER_CRITICAL(flags) local_irq_save(flags)
53 #define EXIT_CRITICAL(flags) local_irq_restore(flags)
55 #endif /* CONFIG_MIPS_MT_SMTC */
57 void local_flush_tlb_all(void)
60 unsigned long old_ctx;
63 ENTER_CRITICAL(flags);
64 /* Save old context and create impossible VPN2 value */
65 old_ctx = read_c0_entryhi();
69 entry = read_c0_wired();
71 /* Blast 'em all away. */
72 while (entry < current_cpu_data.tlbsize) {
73 /* Make sure all entries differ. */
74 write_c0_entryhi(UNIQUE_ENTRYHI(entry));
75 write_c0_index(entry);
81 write_c0_entryhi(old_ctx);
85 /* All entries common to a mm share an asid. To effectively flush
86 these entries, we just bump the asid. */
87 void local_flush_tlb_mm(struct mm_struct *mm)
93 cpu = smp_processor_id();
95 if (cpu_context(cpu, mm) != 0) {
96 drop_mmu_context(mm, cpu);
102 void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
105 struct mm_struct *mm = vma->vm_mm;
106 int cpu = smp_processor_id();
108 if (cpu_context(cpu, mm) != 0) {
112 ENTER_CRITICAL(flags);
113 size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
114 size = (size + 1) >> 1;
115 local_irq_save(flags);
116 if (size <= current_cpu_data.tlbsize/2) {
117 int oldpid = read_c0_entryhi();
118 int newpid = cpu_asid(cpu, mm);
120 start &= (PAGE_MASK << 1);
121 end += ((PAGE_SIZE << 1) - 1);
122 end &= (PAGE_MASK << 1);
123 while (start < end) {
126 write_c0_entryhi(start | newpid);
127 start += (PAGE_SIZE << 1);
131 idx = read_c0_index();
132 write_c0_entrylo0(0);
133 write_c0_entrylo1(0);
136 /* Make sure all entries differ. */
137 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
142 write_c0_entryhi(oldpid);
144 drop_mmu_context(mm, cpu);
146 EXIT_CRITICAL(flags);
150 void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
155 ENTER_CRITICAL(flags);
156 size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
157 size = (size + 1) >> 1;
158 if (size <= current_cpu_data.tlbsize / 2) {
159 int pid = read_c0_entryhi();
161 start &= (PAGE_MASK << 1);
162 end += ((PAGE_SIZE << 1) - 1);
163 end &= (PAGE_MASK << 1);
165 while (start < end) {
168 write_c0_entryhi(start);
169 start += (PAGE_SIZE << 1);
173 idx = read_c0_index();
174 write_c0_entrylo0(0);
175 write_c0_entrylo1(0);
178 /* Make sure all entries differ. */
179 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
184 write_c0_entryhi(pid);
186 local_flush_tlb_all();
188 EXIT_CRITICAL(flags);
191 void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
193 int cpu = smp_processor_id();
195 if (cpu_context(cpu, vma->vm_mm) != 0) {
197 int oldpid, newpid, idx;
199 newpid = cpu_asid(cpu, vma->vm_mm);
200 page &= (PAGE_MASK << 1);
201 ENTER_CRITICAL(flags);
202 oldpid = read_c0_entryhi();
203 write_c0_entryhi(page | newpid);
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));
219 write_c0_entryhi(oldpid);
220 EXIT_CRITICAL(flags);
225 * This one is only used for pages with the global bit set so we don't care
226 * much about the ASID.
228 void local_flush_tlb_one(unsigned long page)
233 ENTER_CRITICAL(flags);
234 oldpid = read_c0_entryhi();
235 page &= (PAGE_MASK << 1);
236 write_c0_entryhi(page);
240 idx = read_c0_index();
241 write_c0_entrylo0(0);
242 write_c0_entrylo1(0);
244 /* Make sure all entries differ. */
245 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
250 write_c0_entryhi(oldpid);
252 EXIT_CRITICAL(flags);
256 * We will need multiple versions of update_mmu_cache(), one that just
257 * updates the TLB with the new pte(s), and another which also checks
258 * for the R4k "end of page" hardware bug and does the needy.
260 void __update_tlb(struct vm_area_struct * vma, unsigned long address, pte_t pte)
270 * Handle debugger faulting in for debugee.
272 if (current->active_mm != vma->vm_mm)
275 ENTER_CRITICAL(flags);
277 pid = read_c0_entryhi() & ASID_MASK;
278 address &= (PAGE_MASK << 1);
279 write_c0_entryhi(address | pid);
280 pgdp = pgd_offset(vma->vm_mm, address);
284 pudp = pud_offset(pgdp, address);
285 pmdp = pmd_offset(pudp, address);
286 idx = read_c0_index();
287 ptep = pte_offset_map(pmdp, address);
289 #if defined(CONFIG_64BIT_PHYS_ADDR) && defined(CONFIG_CPU_MIPS32_R1)
290 write_c0_entrylo0(ptep->pte_high);
292 write_c0_entrylo1(ptep->pte_high);
294 write_c0_entrylo0(pte_val(*ptep++) >> 6);
295 write_c0_entrylo1(pte_val(*ptep) >> 6);
303 EXIT_CRITICAL(flags);
307 static void r4k_update_mmu_cache_hwbug(struct vm_area_struct * vma,
308 unsigned long address, pte_t pte)
317 ENTER_CRITICAL(flags);
318 address &= (PAGE_MASK << 1);
319 asid = read_c0_entryhi() & ASID_MASK;
320 write_c0_entryhi(address | asid);
321 pgdp = pgd_offset(vma->vm_mm, address);
325 pmdp = pmd_offset(pgdp, address);
326 idx = read_c0_index();
327 ptep = pte_offset_map(pmdp, address);
328 write_c0_entrylo0(pte_val(*ptep++) >> 6);
329 write_c0_entrylo1(pte_val(*ptep) >> 6);
336 EXIT_CRITICAL(flags);
340 void __init add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
341 unsigned long entryhi, unsigned long pagemask)
345 unsigned long old_pagemask;
346 unsigned long old_ctx;
348 ENTER_CRITICAL(flags);
349 /* Save old context and create impossible VPN2 value */
350 old_ctx = read_c0_entryhi();
351 old_pagemask = read_c0_pagemask();
352 wired = read_c0_wired();
353 write_c0_wired(wired + 1);
354 write_c0_index(wired);
356 write_c0_pagemask(pagemask);
357 write_c0_entryhi(entryhi);
358 write_c0_entrylo0(entrylo0);
359 write_c0_entrylo1(entrylo1);
364 write_c0_entryhi(old_ctx);
366 write_c0_pagemask(old_pagemask);
367 local_flush_tlb_all();
368 EXIT_CRITICAL(flags);
372 * Used for loading TLB entries before trap_init() has started, when we
373 * don't actually want to add a wired entry which remains throughout the
374 * lifetime of the system
377 static int temp_tlb_entry __initdata;
379 __init int add_temporary_entry(unsigned long entrylo0, unsigned long entrylo1,
380 unsigned long entryhi, unsigned long pagemask)
385 unsigned long old_pagemask;
386 unsigned long old_ctx;
388 ENTER_CRITICAL(flags);
389 /* Save old context and create impossible VPN2 value */
390 old_ctx = read_c0_entryhi();
391 old_pagemask = read_c0_pagemask();
392 wired = read_c0_wired();
393 if (--temp_tlb_entry < wired) {
395 "No TLB space left for add_temporary_entry\n");
400 write_c0_index(temp_tlb_entry);
401 write_c0_pagemask(pagemask);
402 write_c0_entryhi(entryhi);
403 write_c0_entrylo0(entrylo0);
404 write_c0_entrylo1(entrylo1);
409 write_c0_entryhi(old_ctx);
410 write_c0_pagemask(old_pagemask);
412 EXIT_CRITICAL(flags);
416 extern void __init sanitize_tlb_entries(void);
417 static void __init probe_tlb(unsigned long config)
419 struct cpuinfo_mips *c = ¤t_cpu_data;
423 * If this isn't a MIPS32 / MIPS64 compliant CPU. Config 1 register
424 * is not supported, we assume R4k style. Cpu probing already figured
425 * out the number of tlb entries.
427 if ((c->processor_id & 0xff0000) == PRID_COMP_LEGACY)
429 #ifdef CONFIG_MIPS_MT_SMTC
431 * If TLB is shared in SMTC system, total size already
432 * has been calculated and written into cpu_data tlbsize
434 if((smtc_status & SMTC_TLB_SHARED) == SMTC_TLB_SHARED)
436 #endif /* CONFIG_MIPS_MT_SMTC */
438 reg = read_c0_config1();
439 if (!((config >> 7) & 3))
440 panic("No TLB present");
442 c->tlbsize = ((reg >> 25) & 0x3f) + 1;
445 static int __initdata ntlb = 0;
446 static int __init set_ntlb(char *str)
448 get_option(&str, &ntlb);
452 __setup("ntlb=", set_ntlb);
454 void __init tlb_init(void)
456 unsigned int config = read_c0_config();
459 * You should never change this register:
460 * - On R4600 1.7 the tlbp never hits for pages smaller than
461 * the value in the c0_pagemask register.
462 * - The entire mm handling assumes the c0_pagemask register to
463 * be set for 4kb pages.
466 write_c0_pagemask(PM_DEFAULT_MASK);
468 write_c0_framemask(0);
469 temp_tlb_entry = current_cpu_data.tlbsize - 1;
471 /* From this point on the ARC firmware is dead. */
472 local_flush_tlb_all();
474 /* Did I tell you that ARC SUCKS? */
477 if (ntlb > 1 && ntlb <= current_cpu_data.tlbsize) {
478 int wired = current_cpu_data.tlbsize - ntlb;
479 write_c0_wired(wired);
480 write_c0_index(wired-1);
481 printk ("Restricting TLB to %d entries\n", ntlb);
483 printk("Ignoring invalid argument ntlb=%d\n", ntlb);
486 build_tlb_refill_handler();