This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / mips / mm / tlb-r8k.c
1 /*
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
4  * for more details.
5  *
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.
10  */
11 #include <linux/config.h>
12 #include <linux/init.h>
13 #include <linux/sched.h>
14 #include <linux/mm.h>
15
16 #include <asm/cpu.h>
17 #include <asm/bootinfo.h>
18 #include <asm/mmu_context.h>
19 #include <asm/pgtable.h>
20 #include <asm/system.h>
21
22 extern void except_vec0_generic(void);
23 extern void except_vec1_r8k(void);
24
25 #define TFP_TLB_SIZE            384
26 #define TFP_TLB_SET_SHIFT       7
27
28 /* CP0 hazard avoidance. */
29 #define BARRIER __asm__ __volatile__(".set noreorder\n\t" \
30                                      "nop; nop; nop; nop; nop; nop;\n\t" \
31                                      ".set reorder\n\t")
32
33 void local_flush_tlb_all(void)
34 {
35         unsigned long flags;
36         unsigned long old_ctx;
37         int entry;
38
39         local_irq_save(flags);
40         /* Save old context and create impossible VPN2 value */
41         old_ctx = read_c0_entryhi();
42         write_c0_entrylo(0);
43
44         for (entry = 0; entry < TFP_TLB_SIZE; entry++) {
45                 write_c0_tlbset(entry >> TFP_TLB_SET_SHIFT);
46                 write_c0_vaddr(entry << PAGE_SHIFT);
47                 write_c0_entryhi(CKSEG0 + (entry << (PAGE_SHIFT + 1)));
48                 mtc0_tlbw_hazard();
49                 tlb_write();
50         }
51         tlbw_use_hazard();
52         write_c0_entryhi(old_ctx);
53         local_irq_restore(flags);
54 }
55
56 void local_flush_tlb_mm(struct mm_struct *mm)
57 {
58         int cpu = smp_processor_id();
59
60         if (cpu_context(cpu, mm) != 0)
61                 drop_mmu_context(mm,cpu);
62 }
63
64 void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
65         unsigned long end)
66 {
67         struct mm_struct *mm = vma->vm_mm;
68         int cpu = smp_processor_id();
69         unsigned long flags;
70         int oldpid, newpid, size;
71
72         if (!cpu_context(cpu, mm))
73                 return;
74
75         size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
76         size = (size + 1) >> 1;
77
78         local_irq_save(flags);
79
80         if (size > TFP_TLB_SIZE / 2) {
81                 drop_mmu_context(mm, cpu);
82                 goto out_restore;
83         }
84
85         oldpid = read_c0_entryhi();
86         newpid = cpu_asid(cpu, mm);
87
88         write_c0_entrylo(0);
89
90         start &= PAGE_MASK;
91         end += (PAGE_SIZE - 1);
92         end &= PAGE_MASK;
93         while (start < end) {
94                 signed long idx;
95
96                 write_c0_vaddr(start);
97                 write_c0_entryhi(start);
98                 start += PAGE_SIZE;
99                 tlb_probe();
100                 idx = read_c0_tlbset();
101                 if (idx < 0)
102                         continue;
103
104                 write_c0_entryhi(CKSEG0 + (idx << (PAGE_SHIFT + 1)));
105                 tlb_write();
106         }
107         write_c0_entryhi(oldpid);
108
109 out_restore:
110         local_irq_restore(flags);
111 }
112
113 /* Usable for KV1 addresses only! */
114 void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
115 {
116         unsigned long flags;
117         int size;
118
119         size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
120         size = (size + 1) >> 1;
121
122         if (size > TFP_TLB_SIZE / 2) {
123                 local_flush_tlb_all();
124                 return;
125         }
126
127         local_irq_save(flags);
128
129         write_c0_entrylo(0);
130
131         start &= PAGE_MASK;
132         end += (PAGE_SIZE - 1);
133         end &= PAGE_MASK;
134         while (start < end) {
135                 signed long idx;
136
137                 write_c0_vaddr(start);
138                 write_c0_entryhi(start);
139                 start += PAGE_SIZE;
140                 tlb_probe();
141                 idx = read_c0_tlbset();
142                 if (idx < 0)
143                         continue;
144
145                 write_c0_entryhi(CKSEG0 + (idx << (PAGE_SHIFT + 1)));
146                 tlb_write();
147         }
148
149         local_irq_restore(flags);
150 }
151
152 void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
153 {
154         int cpu = smp_processor_id();
155         unsigned long flags;
156         int oldpid, newpid;
157         signed long idx;
158
159         if (!cpu_context(cpu, vma->vm_mm))
160                 return;
161
162         newpid = cpu_asid(cpu, vma->vm_mm);
163         page &= PAGE_MASK;
164         local_irq_save(flags);
165         oldpid = read_c0_entryhi();
166         write_c0_vaddr(page);
167         write_c0_entryhi(newpid);
168         tlb_probe();
169         idx = read_c0_tlbset();
170         if (idx < 0)
171                 goto finish;
172
173         write_c0_entrylo(0);
174         write_c0_entryhi(CKSEG0 + (idx << (PAGE_SHIFT + 1)));
175         tlb_write();
176
177 finish:
178         write_c0_entryhi(oldpid);
179         local_irq_restore(flags);
180 }
181
182 /*
183  * We will need multiple versions of update_mmu_cache(), one that just
184  * updates the TLB with the new pte(s), and another which also checks
185  * for the R4k "end of page" hardware bug and does the needy.
186  */
187 void __update_tlb(struct vm_area_struct * vma, unsigned long address, pte_t pte)
188 {
189         unsigned long flags;
190         pgd_t *pgdp;
191         pmd_t *pmdp;
192         pte_t *ptep;
193         int pid;
194
195         /*
196          * Handle debugger faulting in for debugee.
197          */
198         if (current->active_mm != vma->vm_mm)
199                 return;
200
201         pid = read_c0_entryhi() & ASID_MASK;
202
203         local_irq_save(flags);
204         address &= PAGE_MASK;
205         write_c0_vaddr(address);
206         write_c0_entryhi(pid);
207         pgdp = pgd_offset(vma->vm_mm, address);
208         pmdp = pmd_offset(pgdp, address);
209         ptep = pte_offset_map(pmdp, address);
210         tlb_probe();
211
212         write_c0_entrylo(pte_val(*ptep++) >> 6);
213         tlb_write();
214
215         write_c0_entryhi(pid);
216         local_irq_restore(flags);
217 }
218
219 static void __init probe_tlb(unsigned long config)
220 {
221         struct cpuinfo_mips *c = &current_cpu_data;
222
223         c->tlbsize = 3 * 128;           /* 3 sets each 128 entries */
224 }
225
226 void __init tlb_init(void)
227 {
228         unsigned int config = read_c0_config();
229         unsigned long status;
230
231         probe_tlb(config);
232
233         status = read_c0_status();
234         status &= ~(ST0_UPS | ST0_KPS);
235 #ifdef CONFIG_PAGE_SIZE_4KB
236         status |= (TFP_PAGESIZE_4K << 32) | (TFP_PAGESIZE_4K << 36);
237 #elif defined(CONFIG_PAGE_SIZE_8KB)
238         status |= (TFP_PAGESIZE_8K << 32) | (TFP_PAGESIZE_8K << 36);
239 #elif defined(CONFIG_PAGE_SIZE_16KB)
240         status |= (TFP_PAGESIZE_16K << 32) | (TFP_PAGESIZE_16K << 36);
241 #elif defined(CONFIG_PAGE_SIZE_64KB)
242         status |= (TFP_PAGESIZE_64K << 32) | (TFP_PAGESIZE_64K << 36);
243 #endif
244         write_c0_status(status);
245
246         write_c0_wired(0);
247
248         local_flush_tlb_all();
249
250         memcpy((void *)(CKSEG0 + 0x00), &except_vec0_generic, 0x80);
251         memcpy((void *)(CKSEG0 + 0x80), except_vec1_r8k, 0x80);
252         flush_icache_range(CKSEG0 + 0x80, CKSEG0 + 0x100);
253 }