[MTD] mtdchar: Return EINVAL for bad seeks instead of fixing up to valid byte
[sfrench/cifs-2.6.git] / arch / mips / mm / tlb-sb1.c
1 /*
2  * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
3  * Copyright (C) 1997, 2001 Ralf Baechle (ralf@gnu.org)
4  * Copyright (C) 2000, 2001, 2002, 2003 Broadcom Corporation
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  */
20 #include <linux/init.h>
21 #include <asm/mmu_context.h>
22 #include <asm/bootinfo.h>
23 #include <asm/cpu.h>
24
25 extern void build_tlb_refill_handler(void);
26
27 #define UNIQUE_ENTRYHI(idx) (CKSEG0 + ((idx) << (PAGE_SHIFT + 1)))
28
29 /* Dump the current entry* and pagemask registers */
30 static inline void dump_cur_tlb_regs(void)
31 {
32         unsigned int entryhihi, entryhilo, entrylo0hi, entrylo0lo, entrylo1hi;
33         unsigned int entrylo1lo, pagemask;
34
35         __asm__ __volatile__ (
36                 ".set push             \n"
37                 ".set noreorder        \n"
38                 ".set mips64           \n"
39                 ".set noat             \n"
40                 "     tlbr             \n"
41                 "     dmfc0  $1, $10   \n"
42                 "     dsrl32 %0, $1, 0 \n"
43                 "     sll    %1, $1, 0 \n"
44                 "     dmfc0  $1, $2    \n"
45                 "     dsrl32 %2, $1, 0 \n"
46                 "     sll    %3, $1, 0 \n"
47                 "     dmfc0  $1, $3    \n"
48                 "     dsrl32 %4, $1, 0 \n"
49                 "     sll    %5, $1, 0 \n"
50                 "     mfc0   %6, $5    \n"
51                 ".set pop              \n"
52                 : "=r" (entryhihi), "=r" (entryhilo),
53                   "=r" (entrylo0hi), "=r" (entrylo0lo),
54                   "=r" (entrylo1hi), "=r" (entrylo1lo),
55                   "=r" (pagemask));
56
57         printk("%08X%08X %08X%08X %08X%08X %08X",
58                entryhihi, entryhilo,
59                entrylo0hi, entrylo0lo,
60                entrylo1hi, entrylo1lo,
61                pagemask);
62 }
63
64 void sb1_dump_tlb(void)
65 {
66         unsigned long old_ctx;
67         unsigned long flags;
68         int entry;
69         local_irq_save(flags);
70         old_ctx = read_c0_entryhi();
71         printk("Current TLB registers state:\n"
72                "      EntryHi       EntryLo0          EntryLo1     PageMask  Index\n"
73                "--------------------------------------------------------------------\n");
74         dump_cur_tlb_regs();
75         printk(" %08X\n", read_c0_index());
76         printk("\n\nFull TLB Dump:\n"
77                "Idx      EntryHi       EntryLo0          EntryLo1     PageMask\n"
78                "--------------------------------------------------------------\n");
79         for (entry = 0; entry < current_cpu_data.tlbsize; entry++) {
80                 write_c0_index(entry);
81                 printk("\n%02i ", entry);
82                 dump_cur_tlb_regs();
83         }
84         printk("\n");
85         write_c0_entryhi(old_ctx);
86         local_irq_restore(flags);
87 }
88
89 void local_flush_tlb_all(void)
90 {
91         unsigned long flags;
92         unsigned long old_ctx;
93         int entry;
94
95         local_irq_save(flags);
96         /* Save old context and create impossible VPN2 value */
97         old_ctx = read_c0_entryhi() & ASID_MASK;
98         write_c0_entrylo0(0);
99         write_c0_entrylo1(0);
100
101         entry = read_c0_wired();
102         while (entry < current_cpu_data.tlbsize) {
103                 write_c0_entryhi(UNIQUE_ENTRYHI(entry));
104                 write_c0_index(entry);
105                 tlb_write_indexed();
106                 entry++;
107         }
108         write_c0_entryhi(old_ctx);
109         local_irq_restore(flags);
110 }
111
112
113 /*
114  * Use a bogus region of memory (starting at 0) to sanitize the TLB's.
115  * Use increments of the maximum page size (16MB), and check for duplicate
116  * entries before doing a given write.  Then, when we're safe from collisions
117  * with the firmware, go back and give all the entries invalid addresses with
118  * the normal flush routine.  Wired entries will be killed as well!
119  */
120 static void __init sb1_sanitize_tlb(void)
121 {
122         int entry;
123         long addr = 0;
124
125         long inc = 1<<24;  /* 16MB */
126         /* Save old context and create impossible VPN2 value */
127         write_c0_entrylo0(0);
128         write_c0_entrylo1(0);
129         for (entry = 0; entry < current_cpu_data.tlbsize; entry++) {
130                 do {
131                         addr += inc;
132                         write_c0_entryhi(addr);
133                         tlb_probe();
134                 } while ((int)(read_c0_index()) >= 0);
135                 write_c0_index(entry);
136                 tlb_write_indexed();
137         }
138         /* Now that we know we're safe from collisions, we can safely flush
139            the TLB with the "normal" routine. */
140         local_flush_tlb_all();
141 }
142
143 void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
144         unsigned long end)
145 {
146         struct mm_struct *mm = vma->vm_mm;
147         unsigned long flags;
148         int cpu;
149
150         local_irq_save(flags);
151         cpu = smp_processor_id();
152         if (cpu_context(cpu, mm) != 0) {
153                 int size;
154                 size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
155                 size = (size + 1) >> 1;
156                 if (size <= (current_cpu_data.tlbsize/2)) {
157                         int oldpid = read_c0_entryhi() & ASID_MASK;
158                         int newpid = cpu_asid(cpu, mm);
159
160                         start &= (PAGE_MASK << 1);
161                         end += ((PAGE_SIZE << 1) - 1);
162                         end &= (PAGE_MASK << 1);
163                         while (start < end) {
164                                 int idx;
165
166                                 write_c0_entryhi(start | newpid);
167                                 start += (PAGE_SIZE << 1);
168                                 tlb_probe();
169                                 idx = read_c0_index();
170                                 write_c0_entrylo0(0);
171                                 write_c0_entrylo1(0);
172                                 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
173                                 if (idx < 0)
174                                         continue;
175                                 tlb_write_indexed();
176                         }
177                         write_c0_entryhi(oldpid);
178                 } else {
179                         drop_mmu_context(mm, cpu);
180                 }
181         }
182         local_irq_restore(flags);
183 }
184
185 void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
186 {
187         unsigned long flags;
188         int size;
189
190         size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
191         size = (size + 1) >> 1;
192
193         local_irq_save(flags);
194         if (size <= (current_cpu_data.tlbsize/2)) {
195                 int pid = read_c0_entryhi();
196
197                 start &= (PAGE_MASK << 1);
198                 end += ((PAGE_SIZE << 1) - 1);
199                 end &= (PAGE_MASK << 1);
200
201                 while (start < end) {
202                         int idx;
203
204                         write_c0_entryhi(start);
205                         start += (PAGE_SIZE << 1);
206                         tlb_probe();
207                         idx = read_c0_index();
208                         write_c0_entrylo0(0);
209                         write_c0_entrylo1(0);
210                         write_c0_entryhi(UNIQUE_ENTRYHI(idx));
211                         if (idx < 0)
212                                 continue;
213                         tlb_write_indexed();
214                 }
215                 write_c0_entryhi(pid);
216         } else {
217                 local_flush_tlb_all();
218         }
219         local_irq_restore(flags);
220 }
221
222 void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
223 {
224         unsigned long flags;
225         int cpu = smp_processor_id();
226
227         local_irq_save(flags);
228         if (cpu_context(cpu, vma->vm_mm) != 0) {
229                 int oldpid, newpid, idx;
230                 newpid = cpu_asid(cpu, vma->vm_mm);
231                 page &= (PAGE_MASK << 1);
232                 oldpid = read_c0_entryhi() & ASID_MASK;
233                 write_c0_entryhi(page | newpid);
234                 tlb_probe();
235                 idx = read_c0_index();
236                 write_c0_entrylo0(0);
237                 write_c0_entrylo1(0);
238                 if (idx < 0)
239                         goto finish;
240                 /* Make sure all entries differ. */
241                 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
242                 tlb_write_indexed();
243         finish:
244                 write_c0_entryhi(oldpid);
245         }
246         local_irq_restore(flags);
247 }
248
249 /*
250  * Remove one kernel space TLB entry.  This entry is assumed to be marked
251  * global so we don't do the ASID thing.
252  */
253 void local_flush_tlb_one(unsigned long page)
254 {
255         unsigned long flags;
256         int oldpid, idx;
257
258         page &= (PAGE_MASK << 1);
259         oldpid = read_c0_entryhi() & ASID_MASK;
260
261         local_irq_save(flags);
262         write_c0_entryhi(page);
263         tlb_probe();
264         idx = read_c0_index();
265         if (idx >= 0) {
266                 /* Make sure all entries differ. */
267                 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
268                 write_c0_entrylo0(0);
269                 write_c0_entrylo1(0);
270                 tlb_write_indexed();
271         }
272
273         write_c0_entryhi(oldpid);
274         local_irq_restore(flags);
275 }
276
277 /* All entries common to a mm share an asid.  To effectively flush
278    these entries, we just bump the asid. */
279 void local_flush_tlb_mm(struct mm_struct *mm)
280 {
281         int cpu;
282
283         preempt_disable();
284
285         cpu = smp_processor_id();
286
287         if (cpu_context(cpu, mm) != 0) {
288                 drop_mmu_context(mm, cpu);
289         }
290
291         preempt_enable();
292 }
293
294 /* Stolen from mips32 routines */
295
296 void __update_tlb(struct vm_area_struct *vma, unsigned long address, pte_t pte)
297 {
298         unsigned long flags;
299         pgd_t *pgdp;
300         pmd_t *pmdp;
301         pte_t *ptep;
302         int idx, pid;
303
304         /*
305          * Handle debugger faulting in for debugee.
306          */
307         if (current->active_mm != vma->vm_mm)
308                 return;
309
310         local_irq_save(flags);
311
312         pid = read_c0_entryhi() & ASID_MASK;
313         address &= (PAGE_MASK << 1);
314         write_c0_entryhi(address | (pid));
315         pgdp = pgd_offset(vma->vm_mm, address);
316         tlb_probe();
317         pmdp = pmd_offset(pgdp, address);
318         idx = read_c0_index();
319         ptep = pte_offset_map(pmdp, address);
320         write_c0_entrylo0(pte_val(*ptep++) >> 6);
321         write_c0_entrylo1(pte_val(*ptep) >> 6);
322         if (idx < 0) {
323                 tlb_write_random();
324         } else {
325                 tlb_write_indexed();
326         }
327         local_irq_restore(flags);
328 }
329
330 void __init add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
331         unsigned long entryhi, unsigned long pagemask)
332 {
333         unsigned long flags;
334         unsigned long wired;
335         unsigned long old_pagemask;
336         unsigned long old_ctx;
337
338         local_irq_save(flags);
339         old_ctx = read_c0_entryhi() & 0xff;
340         old_pagemask = read_c0_pagemask();
341         wired = read_c0_wired();
342         write_c0_wired(wired + 1);
343         write_c0_index(wired);
344
345         write_c0_pagemask(pagemask);
346         write_c0_entryhi(entryhi);
347         write_c0_entrylo0(entrylo0);
348         write_c0_entrylo1(entrylo1);
349         tlb_write_indexed();
350
351         write_c0_entryhi(old_ctx);
352         write_c0_pagemask(old_pagemask);
353
354         local_flush_tlb_all();
355         local_irq_restore(flags);
356 }
357
358 /*
359  * This is called from loadmmu.c.  We have to set up all the
360  * memory management function pointers, as well as initialize
361  * the caches and tlbs
362  */
363 void tlb_init(void)
364 {
365         write_c0_pagemask(PM_DEFAULT_MASK);
366         write_c0_wired(0);
367
368         /*
369          * We don't know what state the firmware left the TLB's in, so this is
370          * the ultra-conservative way to flush the TLB's and avoid machine
371          * check exceptions due to duplicate TLB entries
372          */
373         sb1_sanitize_tlb();
374
375         build_tlb_refill_handler();
376 }