Fedora Core 2.6.11-1.35_FC3 kernel
[linux-2.6.git] / arch / mips / mm / tlbex32-r4k.S
1 /*
2  * TLB exception handling code for r4k.
3  *
4  * Copyright (C) 1994, 1995, 1996 by Ralf Baechle and Andreas Busse
5  *
6  * Multi-cpu abstraction and reworking:
7  * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
8  *
9  * Carsten Langgaard, carstenl@mips.com
10  * Copyright (C) 2000 MIPS Technologies, Inc.  All rights reserved.
11  */
12 #include <linux/init.h>
13 #include <linux/config.h>
14
15 #include <asm/asm.h>
16 #include <asm/offset.h>
17 #include <asm/cachectl.h>
18 #include <asm/fpregdef.h>
19 #include <asm/mipsregs.h>
20 #include <asm/page.h>
21 #include <asm/pgtable-bits.h>
22 #include <asm/regdef.h>
23 #include <asm/stackframe.h>
24 #include <asm/war.h>
25
26 #define TLB_OPTIMIZE /* If you are paranoid, disable this. */
27
28 #ifdef CONFIG_64BIT_PHYS_ADDR
29 #define PTE_L           ld
30 #define PTE_S           sd
31 #define PTE_SRL         dsrl
32 #define P_MTC0          dmtc0
33 #define PTE_SIZE        8
34 #define PTEP_INDX_MSK   0xff0
35 #define PTE_INDX_MSK    0xff8
36 #define PTE_INDX_SHIFT  9
37 #else
38 #define PTE_L           lw
39 #define PTE_S           sw
40 #define PTE_SRL         srl
41 #define P_MTC0          mtc0
42 #define PTE_SIZE        4
43 #define PTEP_INDX_MSK   0xff8
44 #define PTE_INDX_MSK    0xffc
45 #define PTE_INDX_SHIFT  10
46 #endif
47
48 /*
49  * ABUSE of CPP macros 101.
50  *
51  * After this macro runs, the pte faulted on is
52  * in register PTE, a ptr into the table in which
53  * the pte belongs is in PTR.
54  */
55
56 #ifdef CONFIG_SMP
57 #define GET_PGD(scratch, ptr)        \
58         mfc0    ptr, CP0_CONTEXT;    \
59         la      scratch, pgd_current;\
60         srl     ptr, 23;             \
61         sll     ptr, 2;              \
62         addu    ptr, scratch, ptr;   \
63         lw      ptr, (ptr);
64 #else
65 #define GET_PGD(scratch, ptr)    \
66         lw      ptr, pgd_current;
67 #endif
68
69 #define LOAD_PTE(pte, ptr) \
70         GET_PGD(pte, ptr)          \
71         mfc0    pte, CP0_BADVADDR; \
72         srl     pte, pte, _PGDIR_SHIFT; \
73         sll     pte, pte, 2; \
74         addu    ptr, ptr, pte; \
75         mfc0    pte, CP0_BADVADDR; \
76         lw      ptr, (ptr); \
77         srl     pte, pte, PTE_INDX_SHIFT; \
78         and     pte, pte, PTE_INDX_MSK; \
79         addu    ptr, ptr, pte; \
80         PTE_L   pte, (ptr);
81
82         /* This places the even/odd pte pair in the page
83          * table at PTR into ENTRYLO0 and ENTRYLO1 using
84          * TMP as a scratch register.
85          */
86 #define PTE_RELOAD(ptr, tmp) \
87         ori     ptr, ptr, PTE_SIZE; \
88         xori    ptr, ptr, PTE_SIZE; \
89         PTE_L   tmp, PTE_SIZE(ptr); \
90         PTE_L   ptr, 0(ptr); \
91         PTE_SRL tmp, tmp, 6; \
92         P_MTC0  tmp, CP0_ENTRYLO1; \
93         PTE_SRL ptr, ptr, 6; \
94         P_MTC0  ptr, CP0_ENTRYLO0;
95
96 #define DO_FAULT(write) \
97         .set    noat; \
98         SAVE_ALL; \
99         mfc0    a2, CP0_BADVADDR; \
100         KMODE; \
101         .set    at; \
102         move    a0, sp; \
103         jal     do_page_fault; \
104          li     a1, write; \
105         j       ret_from_exception; \
106          nop; \
107         .set    noat;
108
109         /* Check is PTE is present, if not then jump to LABEL.
110          * PTR points to the page table where this PTE is located,
111          * when the macro is done executing PTE will be restored
112          * with it's original value.
113          */
114 #define PTE_PRESENT(pte, ptr, label) \
115         andi    pte, pte, (_PAGE_PRESENT | _PAGE_READ); \
116         xori    pte, pte, (_PAGE_PRESENT | _PAGE_READ); \
117         bnez    pte, label; \
118          PTE_L  pte, (ptr);
119
120         /* Make PTE valid, store result in PTR. */
121 #define PTE_MAKEVALID(pte, ptr) \
122         ori     pte, pte, (_PAGE_VALID | _PAGE_ACCESSED); \
123         PTE_S   pte, (ptr);
124
125         /* Check if PTE can be written to, if not branch to LABEL.
126          * Regardless restore PTE with value from PTR when done.
127          */
128 #define PTE_WRITABLE(pte, ptr, label) \
129         andi    pte, pte, (_PAGE_PRESENT | _PAGE_WRITE); \
130         xori    pte, pte, (_PAGE_PRESENT | _PAGE_WRITE); \
131         bnez    pte, label; \
132          PTE_L  pte, (ptr);
133
134         /* Make PTE writable, update software status bits as well,
135          * then store at PTR.
136          */
137 #define PTE_MAKEWRITE(pte, ptr) \
138         ori     pte, pte, (_PAGE_ACCESSED | _PAGE_MODIFIED | \
139                            _PAGE_VALID | _PAGE_DIRTY); \
140         PTE_S   pte, (ptr);
141
142
143         .set    noreorder
144
145 /*
146  * From the IDT errata for the QED RM5230 (Nevada), processor revision 1.0:
147  * 2. A timing hazard exists for the TLBP instruction.
148  *
149  *      stalling_instruction
150  *      TLBP
151  *
152  * The JTLB is being read for the TLBP throughout the stall generated by the
153  * previous instruction. This is not really correct as the stalling instruction
154  * can modify the address used to access the JTLB.  The failure symptom is that
155  * the TLBP instruction will use an address created for the stalling instruction
156  * and not the address held in C0_ENHI and thus report the wrong results.
157  *
158  * The software work-around is to not allow the instruction preceding the TLBP
159  * to stall - make it an NOP or some other instruction guaranteed not to stall.
160  *
161  * Errata 2 will not be fixed.  This errata is also on the R5000.
162  *
163  * As if we MIPS hackers wouldn't know how to nop pipelines happy ...
164  */
165 #define R5K_HAZARD nop
166
167         /*
168          * Note for many R4k variants tlb probes cannot be executed out
169          * of the instruction cache else you get bogus results.
170          */
171         .align  5
172         NESTED(handle_tlbl, PT_SIZE, sp)
173         .set    noat
174 #if BCM1250_M3_WAR
175         mfc0    k0, CP0_BADVADDR
176         mfc0    k1, CP0_ENTRYHI
177         xor     k0, k1
178         srl     k0, k0, PAGE_SHIFT+1
179         beqz    k0, 1f
180          nop
181         .set    mips3
182         eret
183         .set    mips0
184 1:
185 #endif
186 invalid_tlbl:
187 #ifdef TLB_OPTIMIZE
188         .set    mips3
189         /* Test present bit in entry. */
190         LOAD_PTE(k0, k1)
191         R5K_HAZARD
192         tlbp
193         PTE_PRESENT(k0, k1, nopage_tlbl)
194         PTE_MAKEVALID(k0, k1)
195         PTE_RELOAD(k1, k0)
196         mtc0_tlbw_hazard
197         tlbwi
198         nop
199         tlbw_eret_hazard
200         .set    mips3
201         eret
202         .set    mips0
203 #endif
204
205 nopage_tlbl:
206         DO_FAULT(0)
207         END(handle_tlbl)
208
209         .align  5
210         NESTED(handle_tlbs, PT_SIZE, sp)
211         .set    noat
212 #ifdef TLB_OPTIMIZE
213         .set    mips3
214         li      k0,0
215         LOAD_PTE(k0, k1)
216         R5K_HAZARD
217         tlbp                            # find faulting entry
218         PTE_WRITABLE(k0, k1, nopage_tlbs)
219         PTE_MAKEWRITE(k0, k1)
220         PTE_RELOAD(k1, k0)
221         mtc0_tlbw_hazard
222         tlbwi
223         nop
224         tlbw_eret_hazard
225         .set    mips3
226         eret
227         .set    mips0
228 #endif
229
230 nopage_tlbs:
231         DO_FAULT(1)
232         END(handle_tlbs)
233
234         .align  5
235         NESTED(handle_mod, PT_SIZE, sp)
236         .set    noat
237 #ifdef TLB_OPTIMIZE
238         .set    mips3
239         LOAD_PTE(k0, k1)
240         R5K_HAZARD
241         tlbp                                    # find faulting entry
242         andi    k0, k0, _PAGE_WRITE
243         beqz    k0, nowrite_mod
244          PTE_L  k0, (k1)
245
246         /* Present and writable bits set, set accessed and dirty bits. */
247         PTE_MAKEWRITE(k0, k1)
248
249         /* Now reload the entry into the tlb. */
250         PTE_RELOAD(k1, k0)
251         mtc0_tlbw_hazard
252         tlbwi
253         nop
254         tlbw_eret_hazard
255         .set    mips3
256         eret
257         .set    mips0
258 #endif
259
260 nowrite_mod:
261         DO_FAULT(1)
262         END(handle_mod)