blob: 08702202758d4a20ef3de5645aba6e91afa96d1e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
22extern void build_tlb_refill_handler(void);
23
24/* CP0 hazard avoidance. */
25#define BARRIER __asm__ __volatile__(".set noreorder\n\t" \
26 "nop; nop; nop; nop; nop; nop;\n\t" \
27 ".set reorder\n\t")
28
29void local_flush_tlb_all(void)
30{
31 unsigned long flags;
32 unsigned long old_ctx;
33 int entry;
34
35 local_irq_save(flags);
36 /* Save old context and create impossible VPN2 value */
37 old_ctx = read_c0_entryhi();
38 write_c0_entrylo0(0);
39 write_c0_entrylo1(0);
40
41 entry = read_c0_wired();
42
43 /* Blast 'em all away. */
44 while (entry < current_cpu_data.tlbsize) {
45 /*
46 * Make sure all entries differ. If they're not different
47 * MIPS32 will take revenge ...
48 */
49 write_c0_entryhi(CKSEG0 + (entry << (PAGE_SHIFT + 1)));
50 write_c0_index(entry);
51 mtc0_tlbw_hazard();
52 tlb_write_indexed();
53 entry++;
54 }
55 tlbw_use_hazard();
56 write_c0_entryhi(old_ctx);
57 local_irq_restore(flags);
58}
59
60void local_flush_tlb_mm(struct mm_struct *mm)
61{
62 int cpu = smp_processor_id();
63
64 if (cpu_context(cpu, mm) != 0)
65 drop_mmu_context(mm,cpu);
66}
67
68void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
69 unsigned long end)
70{
71 struct mm_struct *mm = vma->vm_mm;
72 int cpu = smp_processor_id();
73
74 if (cpu_context(cpu, mm) != 0) {
75 unsigned long flags;
76 int size;
77
78 local_irq_save(flags);
79 size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
80 size = (size + 1) >> 1;
81 if (size <= current_cpu_data.tlbsize/2) {
82 int oldpid = read_c0_entryhi();
83 int newpid = cpu_asid(cpu, mm);
84
85 start &= (PAGE_MASK << 1);
86 end += ((PAGE_SIZE << 1) - 1);
87 end &= (PAGE_MASK << 1);
88 while (start < end) {
89 int idx;
90
91 write_c0_entryhi(start | newpid);
92 start += (PAGE_SIZE << 1);
93 mtc0_tlbw_hazard();
94 tlb_probe();
95 BARRIER;
96 idx = read_c0_index();
97 write_c0_entrylo0(0);
98 write_c0_entrylo1(0);
99 if (idx < 0)
100 continue;
101 /* Make sure all entries differ. */
102 write_c0_entryhi(CKSEG0 +
Maciej W. Rozycki30442992005-02-01 23:02:12 +0000103 (idx << (PAGE_SHIFT + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 mtc0_tlbw_hazard();
105 tlb_write_indexed();
106 }
107 tlbw_use_hazard();
108 write_c0_entryhi(oldpid);
109 } else {
110 drop_mmu_context(mm, cpu);
111 }
112 local_irq_restore(flags);
113 }
114}
115
116void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
117{
118 unsigned long flags;
119 int size;
120
121 local_irq_save(flags);
122 size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
123 size = (size + 1) >> 1;
124 if (size <= current_cpu_data.tlbsize / 2) {
125 int pid = read_c0_entryhi();
126
127 start &= (PAGE_MASK << 1);
128 end += ((PAGE_SIZE << 1) - 1);
129 end &= (PAGE_MASK << 1);
130
131 while (start < end) {
132 int idx;
133
134 write_c0_entryhi(start);
135 start += (PAGE_SIZE << 1);
136 mtc0_tlbw_hazard();
137 tlb_probe();
138 BARRIER;
139 idx = read_c0_index();
140 write_c0_entrylo0(0);
141 write_c0_entrylo1(0);
142 if (idx < 0)
143 continue;
144 /* Make sure all entries differ. */
145 write_c0_entryhi(CKSEG0 + (idx << (PAGE_SHIFT + 1)));
146 mtc0_tlbw_hazard();
147 tlb_write_indexed();
148 }
149 tlbw_use_hazard();
150 write_c0_entryhi(pid);
151 } else {
152 local_flush_tlb_all();
153 }
154 local_irq_restore(flags);
155}
156
157void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
158{
159 int cpu = smp_processor_id();
160
161 if (cpu_context(cpu, vma->vm_mm) != 0) {
162 unsigned long flags;
163 int oldpid, newpid, idx;
164
165 newpid = cpu_asid(cpu, vma->vm_mm);
166 page &= (PAGE_MASK << 1);
167 local_irq_save(flags);
168 oldpid = read_c0_entryhi();
169 write_c0_entryhi(page | newpid);
170 mtc0_tlbw_hazard();
171 tlb_probe();
172 BARRIER;
173 idx = read_c0_index();
174 write_c0_entrylo0(0);
175 write_c0_entrylo1(0);
176 if (idx < 0)
177 goto finish;
178 /* Make sure all entries differ. */
179 write_c0_entryhi(CKSEG0 + (idx << (PAGE_SHIFT + 1)));
180 mtc0_tlbw_hazard();
181 tlb_write_indexed();
182 tlbw_use_hazard();
183
184 finish:
185 write_c0_entryhi(oldpid);
186 local_irq_restore(flags);
187 }
188}
189
190/*
191 * This one is only used for pages with the global bit set so we don't care
192 * much about the ASID.
193 */
194void local_flush_tlb_one(unsigned long page)
195{
196 unsigned long flags;
197 int oldpid, idx;
198
199 local_irq_save(flags);
200 page &= (PAGE_MASK << 1);
201 oldpid = read_c0_entryhi();
202 write_c0_entryhi(page);
203 mtc0_tlbw_hazard();
204 tlb_probe();
205 BARRIER;
206 idx = read_c0_index();
207 write_c0_entrylo0(0);
208 write_c0_entrylo1(0);
209 if (idx >= 0) {
210 /* Make sure all entries differ. */
211 write_c0_entryhi(CKSEG0 + (idx << (PAGE_SHIFT + 1)));
212 mtc0_tlbw_hazard();
213 tlb_write_indexed();
214 tlbw_use_hazard();
215 }
216 write_c0_entryhi(oldpid);
217
218 local_irq_restore(flags);
219}
220
221/*
222 * We will need multiple versions of update_mmu_cache(), one that just
223 * updates the TLB with the new pte(s), and another which also checks
224 * for the R4k "end of page" hardware bug and does the needy.
225 */
226void __update_tlb(struct vm_area_struct * vma, unsigned long address, pte_t pte)
227{
228 unsigned long flags;
229 pgd_t *pgdp;
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000230 pud_t *pudp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 pmd_t *pmdp;
232 pte_t *ptep;
233 int idx, pid;
234
235 /*
236 * Handle debugger faulting in for debugee.
237 */
238 if (current->active_mm != vma->vm_mm)
239 return;
240
241 pid = read_c0_entryhi() & ASID_MASK;
242
243 local_irq_save(flags);
244 address &= (PAGE_MASK << 1);
245 write_c0_entryhi(address | pid);
246 pgdp = pgd_offset(vma->vm_mm, address);
247 mtc0_tlbw_hazard();
248 tlb_probe();
249 BARRIER;
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000250 pudp = pud_offset(pgdp, address);
251 pmdp = pmd_offset(pudp, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 idx = read_c0_index();
253 ptep = pte_offset_map(pmdp, address);
254
Maciej W. Rozycki30442992005-02-01 23:02:12 +0000255#if defined(CONFIG_64BIT_PHYS_ADDR) && defined(CONFIG_CPU_MIPS32)
256 write_c0_entrylo0(ptep->pte_high);
257 ptep++;
258 write_c0_entrylo1(ptep->pte_high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259#else
Maciej W. Rozycki30442992005-02-01 23:02:12 +0000260 write_c0_entrylo0(pte_val(*ptep++) >> 6);
261 write_c0_entrylo1(pte_val(*ptep) >> 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262#endif
263 write_c0_entryhi(address | pid);
264 mtc0_tlbw_hazard();
265 if (idx < 0)
266 tlb_write_random();
267 else
268 tlb_write_indexed();
269 tlbw_use_hazard();
270 write_c0_entryhi(pid);
271 local_irq_restore(flags);
272}
273
274#if 0
275static void r4k_update_mmu_cache_hwbug(struct vm_area_struct * vma,
276 unsigned long address, pte_t pte)
277{
278 unsigned long flags;
279 unsigned int asid;
280 pgd_t *pgdp;
281 pmd_t *pmdp;
282 pte_t *ptep;
283 int idx;
284
285 local_irq_save(flags);
286 address &= (PAGE_MASK << 1);
287 asid = read_c0_entryhi() & ASID_MASK;
288 write_c0_entryhi(address | asid);
289 pgdp = pgd_offset(vma->vm_mm, address);
290 mtc0_tlbw_hazard();
291 tlb_probe();
292 BARRIER;
293 pmdp = pmd_offset(pgdp, address);
294 idx = read_c0_index();
295 ptep = pte_offset_map(pmdp, address);
296 write_c0_entrylo0(pte_val(*ptep++) >> 6);
297 write_c0_entrylo1(pte_val(*ptep) >> 6);
298 mtc0_tlbw_hazard();
299 if (idx < 0)
300 tlb_write_random();
301 else
302 tlb_write_indexed();
303 tlbw_use_hazard();
304 local_irq_restore(flags);
305}
306#endif
307
308void __init add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
309 unsigned long entryhi, unsigned long pagemask)
310{
311 unsigned long flags;
312 unsigned long wired;
313 unsigned long old_pagemask;
314 unsigned long old_ctx;
315
316 local_irq_save(flags);
317 /* Save old context and create impossible VPN2 value */
318 old_ctx = read_c0_entryhi();
319 old_pagemask = read_c0_pagemask();
320 wired = read_c0_wired();
321 write_c0_wired(wired + 1);
322 write_c0_index(wired);
323 BARRIER;
324 write_c0_pagemask(pagemask);
325 write_c0_entryhi(entryhi);
326 write_c0_entrylo0(entrylo0);
327 write_c0_entrylo1(entrylo1);
328 mtc0_tlbw_hazard();
329 tlb_write_indexed();
330 tlbw_use_hazard();
331
332 write_c0_entryhi(old_ctx);
333 BARRIER;
334 write_c0_pagemask(old_pagemask);
335 local_flush_tlb_all();
336 local_irq_restore(flags);
337}
338
339/*
340 * Used for loading TLB entries before trap_init() has started, when we
341 * don't actually want to add a wired entry which remains throughout the
342 * lifetime of the system
343 */
344
345static int temp_tlb_entry __initdata;
346
347__init int add_temporary_entry(unsigned long entrylo0, unsigned long entrylo1,
348 unsigned long entryhi, unsigned long pagemask)
349{
350 int ret = 0;
351 unsigned long flags;
352 unsigned long wired;
353 unsigned long old_pagemask;
354 unsigned long old_ctx;
355
356 local_irq_save(flags);
357 /* Save old context and create impossible VPN2 value */
358 old_ctx = read_c0_entryhi();
359 old_pagemask = read_c0_pagemask();
360 wired = read_c0_wired();
361 if (--temp_tlb_entry < wired) {
Maciej W. Rozycki30442992005-02-01 23:02:12 +0000362 printk(KERN_WARNING
363 "No TLB space left for add_temporary_entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 ret = -ENOSPC;
365 goto out;
366 }
367
368 write_c0_index(temp_tlb_entry);
369 write_c0_pagemask(pagemask);
370 write_c0_entryhi(entryhi);
371 write_c0_entrylo0(entrylo0);
372 write_c0_entrylo1(entrylo1);
373 mtc0_tlbw_hazard();
374 tlb_write_indexed();
375 tlbw_use_hazard();
376
377 write_c0_entryhi(old_ctx);
378 write_c0_pagemask(old_pagemask);
379out:
380 local_irq_restore(flags);
381 return ret;
382}
383
384static void __init probe_tlb(unsigned long config)
385{
386 struct cpuinfo_mips *c = &current_cpu_data;
387 unsigned int reg;
388
389 /*
390 * If this isn't a MIPS32 / MIPS64 compliant CPU. Config 1 register
391 * is not supported, we assume R4k style. Cpu probing already figured
392 * out the number of tlb entries.
393 */
Maciej W. Rozycki30442992005-02-01 23:02:12 +0000394 if ((c->processor_id & 0xff0000) == PRID_COMP_LEGACY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return;
396
397 reg = read_c0_config1();
398 if (!((config >> 7) & 3))
399 panic("No TLB present");
400
401 c->tlbsize = ((reg >> 25) & 0x3f) + 1;
402}
403
404void __init tlb_init(void)
405{
406 unsigned int config = read_c0_config();
407
408 /*
409 * You should never change this register:
410 * - On R4600 1.7 the tlbp never hits for pages smaller than
411 * the value in the c0_pagemask register.
412 * - The entire mm handling assumes the c0_pagemask register to
413 * be set for 4kb pages.
414 */
415 probe_tlb(config);
416 write_c0_pagemask(PM_DEFAULT_MASK);
417 write_c0_wired(0);
418 temp_tlb_entry = current_cpu_data.tlbsize - 1;
419 local_flush_tlb_all();
420
421 build_tlb_refill_handler();
422}