blob: ef3e4d477864f3d2060f884a3df3e461b0c347c0 [file] [log] [blame]
Paul Mundt26ff6c12006-09-27 15:13:36 +09001/*
2 * TLB flushing operations for SH with an MMU.
3 *
4 * Copyright (C) 1999 Niibe Yutaka
Paul Mundt26b7a782006-12-28 10:31:48 +09005 * Copyright (C) 2003 - 2006 Paul Mundt
Paul Mundt26ff6c12006-09-27 15:13:36 +09006 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
9 * for more details.
10 */
11#include <linux/mm.h>
Paul Mundt26b7a782006-12-28 10:31:48 +090012#include <linux/io.h>
Paul Mundt26ff6c12006-09-27 15:13:36 +090013#include <asm/mmu_context.h>
14#include <asm/tlbflush.h>
Paul Mundt26b7a782006-12-28 10:31:48 +090015#include <asm/cacheflush.h>
Paul Mundt26ff6c12006-09-27 15:13:36 +090016
17void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
18{
Paul Mundt19f9a342006-09-27 18:33:49 +090019 if (vma->vm_mm && vma->vm_mm->context.id != NO_CONTEXT) {
Paul Mundt26ff6c12006-09-27 15:13:36 +090020 unsigned long flags;
21 unsigned long asid;
22 unsigned long saved_asid = MMU_NO_ASID;
23
Paul Mundt19f9a342006-09-27 18:33:49 +090024 asid = vma->vm_mm->context.id & MMU_CONTEXT_ASID_MASK;
Paul Mundt26ff6c12006-09-27 15:13:36 +090025 page &= PAGE_MASK;
26
27 local_irq_save(flags);
28 if (vma->vm_mm != current->mm) {
29 saved_asid = get_asid();
30 set_asid(asid);
31 }
32 __flush_tlb_page(asid, page);
33 if (saved_asid != MMU_NO_ASID)
34 set_asid(saved_asid);
35 local_irq_restore(flags);
36 }
37}
38
39void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
40 unsigned long end)
41{
42 struct mm_struct *mm = vma->vm_mm;
43
Paul Mundt19f9a342006-09-27 18:33:49 +090044 if (mm->context.id != NO_CONTEXT) {
Paul Mundt26ff6c12006-09-27 15:13:36 +090045 unsigned long flags;
46 int size;
47
48 local_irq_save(flags);
49 size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
50 if (size > (MMU_NTLB_ENTRIES/4)) { /* Too many TLB to flush */
Paul Mundt19f9a342006-09-27 18:33:49 +090051 mm->context.id = NO_CONTEXT;
Paul Mundt26ff6c12006-09-27 15:13:36 +090052 if (mm == current->mm)
53 activate_context(mm);
54 } else {
Paul Mundt19f9a342006-09-27 18:33:49 +090055 unsigned long asid;
Paul Mundt26ff6c12006-09-27 15:13:36 +090056 unsigned long saved_asid = MMU_NO_ASID;
57
Paul Mundt19f9a342006-09-27 18:33:49 +090058 asid = mm->context.id & MMU_CONTEXT_ASID_MASK;
Paul Mundt26ff6c12006-09-27 15:13:36 +090059 start &= PAGE_MASK;
60 end += (PAGE_SIZE - 1);
61 end &= PAGE_MASK;
62 if (mm != current->mm) {
63 saved_asid = get_asid();
64 set_asid(asid);
65 }
66 while (start < end) {
67 __flush_tlb_page(asid, start);
68 start += PAGE_SIZE;
69 }
70 if (saved_asid != MMU_NO_ASID)
71 set_asid(saved_asid);
72 }
73 local_irq_restore(flags);
74 }
75}
76
77void flush_tlb_kernel_range(unsigned long start, unsigned long end)
78{
79 unsigned long flags;
80 int size;
81
82 local_irq_save(flags);
83 size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
84 if (size > (MMU_NTLB_ENTRIES/4)) { /* Too many TLB to flush */
85 flush_tlb_all();
86 } else {
Paul Mundt19f9a342006-09-27 18:33:49 +090087 unsigned long asid;
Paul Mundt26ff6c12006-09-27 15:13:36 +090088 unsigned long saved_asid = get_asid();
89
Paul Mundt19f9a342006-09-27 18:33:49 +090090 asid = init_mm.context.id & MMU_CONTEXT_ASID_MASK;
Paul Mundt26ff6c12006-09-27 15:13:36 +090091 start &= PAGE_MASK;
92 end += (PAGE_SIZE - 1);
93 end &= PAGE_MASK;
94 set_asid(asid);
95 while (start < end) {
96 __flush_tlb_page(asid, start);
97 start += PAGE_SIZE;
98 }
99 set_asid(saved_asid);
100 }
101 local_irq_restore(flags);
102}
103
104void flush_tlb_mm(struct mm_struct *mm)
105{
106 /* Invalidate all TLB of this process. */
107 /* Instead of invalidating each TLB, we get new MMU context. */
Paul Mundt19f9a342006-09-27 18:33:49 +0900108 if (mm->context.id != NO_CONTEXT) {
Paul Mundt26ff6c12006-09-27 15:13:36 +0900109 unsigned long flags;
110
111 local_irq_save(flags);
Paul Mundt19f9a342006-09-27 18:33:49 +0900112 mm->context.id = NO_CONTEXT;
Paul Mundt26ff6c12006-09-27 15:13:36 +0900113 if (mm == current->mm)
114 activate_context(mm);
115 local_irq_restore(flags);
116 }
117}
118
119void flush_tlb_all(void)
120{
121 unsigned long flags, status;
122
123 /*
124 * Flush all the TLB.
125 *
126 * Write to the MMU control register's bit:
127 * TF-bit for SH-3, TI-bit for SH-4.
128 * It's same position, bit #2.
129 */
130 local_irq_save(flags);
131 status = ctrl_inl(MMUCR);
132 status |= 0x04;
133 ctrl_outl(status, MMUCR);
134 ctrl_barrier();
135 local_irq_restore(flags);
136}
Paul Mundt26b7a782006-12-28 10:31:48 +0900137
138void update_mmu_cache(struct vm_area_struct *vma,
139 unsigned long address, pte_t pte)
140{
141 unsigned long flags;
142 unsigned long pteval;
143 unsigned long vpn;
144 struct page *page;
145 unsigned long pfn = pte_pfn(pte);
146 struct address_space *mapping;
147
148 if (!pfn_valid(pfn))
149 return;
150
151 page = pfn_to_page(pfn);
152 mapping = page_mapping(page);
153 if (mapping) {
154 unsigned long phys = pte_val(pte) & PTE_PHYS_MASK;
155 int dirty = test_and_clear_bit(PG_dcache_dirty, &page->flags);
156
157 if (dirty)
158 __flush_wback_region((void *)P1SEGADDR(phys),
159 PAGE_SIZE);
160 }
161
162 local_irq_save(flags);
163
164 /* Set PTEH register */
165 vpn = (address & MMU_VPN_MASK) | get_asid();
166 ctrl_outl(vpn, MMU_PTEH);
167
168 pteval = pte_val(pte);
169
170#ifdef CONFIG_CPU_HAS_PTEA
171 /* Set PTEA register */
172 /* TODO: make this look less hacky */
173 ctrl_outl(((pteval >> 28) & 0xe) | (pteval & 0x1), MMU_PTEA);
174#endif
175
176 /* Set PTEL register */
177 pteval &= _PAGE_FLAGS_HARDWARE_MASK; /* drop software flags */
Paul Mundtadac9572006-12-28 10:54:01 +0900178#if defined(CONFIG_SH_WRITETHROUGH) && defined(CONFIG_CPU_SH4)
Paul Mundt26b7a782006-12-28 10:31:48 +0900179 pteval |= _PAGE_WT;
180#endif
181 /* conveniently, we want all the software flags to be 0 anyway */
182 ctrl_outl(pteval, MMU_PTEL);
183
184 /* Load the TLB */
185 asm volatile("ldtlb": /* no output */ : /* no input */ : "memory");
186 local_irq_restore(flags);
187}