blob: f8925ba0b39eef0c27111a80ce79f7f4ac4e2437 [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 * Synthesize TLB refill handlers at runtime.
7 *
Thiemo Seufer115f2a42006-07-09 01:47:06 +01008 * Copyright (C) 2004,2005,2006 by Thiemo Seufer
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +01009 * Copyright (C) 2005, 2007 Maciej W. Rozycki
Ralf Baechle41c594a2006-04-05 09:45:45 +010010 * Copyright (C) 2006 Ralf Baechle (ralf@linux-mips.org)
11 *
12 * ... and the days got worse and worse and now you see
13 * I've gone completly out of my mind.
14 *
15 * They're coming to take me a away haha
16 * they're coming to take me a away hoho hihi haha
17 * to the funny farm where code is beautiful all the time ...
18 *
19 * (Condolences to Napoleon XIV)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
22#include <stdarg.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/mm.h>
25#include <linux/kernel.h>
26#include <linux/types.h>
27#include <linux/string.h>
28#include <linux/init.h>
29
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +010030#include <asm/bugs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/pgtable.h>
32#include <asm/cacheflush.h>
33#include <asm/mmu_context.h>
34#include <asm/inst.h>
35#include <asm/elf.h>
36#include <asm/smp.h>
37#include <asm/war.h>
38
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010039static inline int r45k_bvahwbug(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 /* XXX: We should probe for the presence of this bug, but we don't. */
42 return 0;
43}
44
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010045static inline int r4k_250MHZhwbug(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 /* XXX: We should probe for the presence of this bug, but we don't. */
48 return 0;
49}
50
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010051static inline int __maybe_unused bcm1250_m3_war(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 return BCM1250_M3_WAR;
54}
55
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010056static inline int __maybe_unused r10000_llsc_war(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 return R10000_LLSC_WAR;
59}
60
61/*
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +010062 * Found by experiment: At least some revisions of the 4kc throw under
63 * some circumstances a machine check exception, triggered by invalid
64 * values in the index register. Delaying the tlbp instruction until
65 * after the next branch, plus adding an additional nop in front of
66 * tlbwi/tlbwr avoids the invalid index register values. Nobody knows
67 * why; it's not an issue caused by the core RTL.
68 *
69 */
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +010070static __init int __attribute__((unused)) m4kc_tlbp_war(void)
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +010071{
72 return (current_cpu_data.processor_id & 0xffff00) ==
73 (PRID_COMP_MIPS | PRID_IMP_4KC);
74}
75
76/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * A little micro-assembler, intended for TLB refill handler
78 * synthesizing. It is intentionally kept simple, does only support
79 * a subset of instructions, and does not try to hide pipeline effects
80 * like branch delay slots.
81 */
82
83enum fields
84{
85 RS = 0x001,
86 RT = 0x002,
87 RD = 0x004,
88 RE = 0x008,
89 SIMM = 0x010,
90 UIMM = 0x020,
91 BIMM = 0x040,
92 JIMM = 0x080,
93 FUNC = 0x100,
Ralf Baechle41c594a2006-04-05 09:45:45 +010094 SET = 0x200
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
Thiemo Seufer603c3382007-09-05 12:11:22 +010097#define OP_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#define OP_SH 26
99#define RS_MASK 0x1f
100#define RS_SH 21
101#define RT_MASK 0x1f
102#define RT_SH 16
103#define RD_MASK 0x1f
104#define RD_SH 11
105#define RE_MASK 0x1f
106#define RE_SH 6
107#define IMM_MASK 0xffff
108#define IMM_SH 0
109#define JIMM_MASK 0x3ffffff
110#define JIMM_SH 0
Thiemo Seufer603c3382007-09-05 12:11:22 +0100111#define FUNC_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define FUNC_SH 0
Ralf Baechle41c594a2006-04-05 09:45:45 +0100113#define SET_MASK 0x7
114#define SET_SH 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116enum opcode {
117 insn_invalid,
118 insn_addu, insn_addiu, insn_and, insn_andi, insn_beq,
119 insn_beql, insn_bgez, insn_bgezl, insn_bltz, insn_bltzl,
120 insn_bne, insn_daddu, insn_daddiu, insn_dmfc0, insn_dmtc0,
Ralf Baechle242954b2006-10-24 02:29:01 +0100121 insn_dsll, insn_dsll32, insn_dsra, insn_dsrl, insn_dsrl32,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 insn_dsubu, insn_eret, insn_j, insn_jal, insn_jr, insn_ld,
123 insn_ll, insn_lld, insn_lui, insn_lw, insn_mfc0, insn_mtc0,
124 insn_ori, insn_rfe, insn_sc, insn_scd, insn_sd, insn_sll,
125 insn_sra, insn_srl, insn_subu, insn_sw, insn_tlbp, insn_tlbwi,
126 insn_tlbwr, insn_xor, insn_xori
127};
128
129struct insn {
130 enum opcode opcode;
131 u32 match;
132 enum fields fields;
133};
134
135/* This macro sets the non-variable bits of an instruction. */
136#define M(a, b, c, d, e, f) \
137 ((a) << OP_SH \
138 | (b) << RS_SH \
139 | (c) << RT_SH \
140 | (d) << RD_SH \
141 | (e) << RE_SH \
142 | (f) << FUNC_SH)
143
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100144static __initdata struct insn insn_table[] = {
Ralf Baechle21a151d2007-10-11 23:46:15 +0100145 { insn_addiu, M(addiu_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
146 { insn_addu, M(spec_op, 0, 0, 0, 0, addu_op), RS | RT | RD },
147 { insn_and, M(spec_op, 0, 0, 0, 0, and_op), RS | RT | RD },
148 { insn_andi, M(andi_op, 0, 0, 0, 0, 0), RS | RT | UIMM },
149 { insn_beq, M(beq_op, 0, 0, 0, 0, 0), RS | RT | BIMM },
150 { insn_beql, M(beql_op, 0, 0, 0, 0, 0), RS | RT | BIMM },
151 { insn_bgez, M(bcond_op, 0, bgez_op, 0, 0, 0), RS | BIMM },
152 { insn_bgezl, M(bcond_op, 0, bgezl_op, 0, 0, 0), RS | BIMM },
153 { insn_bltz, M(bcond_op, 0, bltz_op, 0, 0, 0), RS | BIMM },
154 { insn_bltzl, M(bcond_op, 0, bltzl_op, 0, 0, 0), RS | BIMM },
155 { insn_bne, M(bne_op, 0, 0, 0, 0, 0), RS | RT | BIMM },
156 { insn_daddiu, M(daddiu_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
157 { insn_daddu, M(spec_op, 0, 0, 0, 0, daddu_op), RS | RT | RD },
158 { insn_dmfc0, M(cop0_op, dmfc_op, 0, 0, 0, 0), RT | RD | SET},
159 { insn_dmtc0, M(cop0_op, dmtc_op, 0, 0, 0, 0), RT | RD | SET},
160 { insn_dsll, M(spec_op, 0, 0, 0, 0, dsll_op), RT | RD | RE },
161 { insn_dsll32, M(spec_op, 0, 0, 0, 0, dsll32_op), RT | RD | RE },
162 { insn_dsra, M(spec_op, 0, 0, 0, 0, dsra_op), RT | RD | RE },
163 { insn_dsrl, M(spec_op, 0, 0, 0, 0, dsrl_op), RT | RD | RE },
164 { insn_dsrl32, M(spec_op, 0, 0, 0, 0, dsrl32_op), RT | RD | RE },
165 { insn_dsubu, M(spec_op, 0, 0, 0, 0, dsubu_op), RS | RT | RD },
166 { insn_eret, M(cop0_op, cop_op, 0, 0, 0, eret_op), 0 },
167 { insn_j, M(j_op, 0, 0, 0, 0, 0), JIMM },
168 { insn_jal, M(jal_op, 0, 0, 0, 0, 0), JIMM },
169 { insn_jr, M(spec_op, 0, 0, 0, 0, jr_op), RS },
170 { insn_ld, M(ld_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
171 { insn_ll, M(ll_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
172 { insn_lld, M(lld_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
173 { insn_lui, M(lui_op, 0, 0, 0, 0, 0), RT | SIMM },
174 { insn_lw, M(lw_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
175 { insn_mfc0, M(cop0_op, mfc_op, 0, 0, 0, 0), RT | RD | SET},
176 { insn_mtc0, M(cop0_op, mtc_op, 0, 0, 0, 0), RT | RD | SET},
177 { insn_ori, M(ori_op, 0, 0, 0, 0, 0), RS | RT | UIMM },
178 { insn_rfe, M(cop0_op, cop_op, 0, 0, 0, rfe_op), 0 },
179 { insn_sc, M(sc_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
180 { insn_scd, M(scd_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
181 { insn_sd, M(sd_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
182 { insn_sll, M(spec_op, 0, 0, 0, 0, sll_op), RT | RD | RE },
183 { insn_sra, M(spec_op, 0, 0, 0, 0, sra_op), RT | RD | RE },
184 { insn_srl, M(spec_op, 0, 0, 0, 0, srl_op), RT | RD | RE },
185 { insn_subu, M(spec_op, 0, 0, 0, 0, subu_op), RS | RT | RD },
186 { insn_sw, M(sw_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
187 { insn_tlbp, M(cop0_op, cop_op, 0, 0, 0, tlbp_op), 0 },
188 { insn_tlbwi, M(cop0_op, cop_op, 0, 0, 0, tlbwi_op), 0 },
189 { insn_tlbwr, M(cop0_op, cop_op, 0, 0, 0, tlbwr_op), 0 },
190 { insn_xor, M(spec_op, 0, 0, 0, 0, xor_op), RS | RT | RD },
191 { insn_xori, M(xori_op, 0, 0, 0, 0, 0), RS | RT | UIMM },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 { insn_invalid, 0, 0 }
193};
194
195#undef M
196
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100197static __init u32 build_rs(u32 arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 if (arg & ~RS_MASK)
200 printk(KERN_WARNING "TLB synthesizer field overflow\n");
201
202 return (arg & RS_MASK) << RS_SH;
203}
204
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100205static __init u32 build_rt(u32 arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 if (arg & ~RT_MASK)
208 printk(KERN_WARNING "TLB synthesizer field overflow\n");
209
210 return (arg & RT_MASK) << RT_SH;
211}
212
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100213static __init u32 build_rd(u32 arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 if (arg & ~RD_MASK)
216 printk(KERN_WARNING "TLB synthesizer field overflow\n");
217
218 return (arg & RD_MASK) << RD_SH;
219}
220
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100221static __init u32 build_re(u32 arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 if (arg & ~RE_MASK)
224 printk(KERN_WARNING "TLB synthesizer field overflow\n");
225
226 return (arg & RE_MASK) << RE_SH;
227}
228
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100229static __init u32 build_simm(s32 arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 if (arg > 0x7fff || arg < -0x8000)
232 printk(KERN_WARNING "TLB synthesizer field overflow\n");
233
234 return arg & 0xffff;
235}
236
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100237static __init u32 build_uimm(u32 arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 if (arg & ~IMM_MASK)
240 printk(KERN_WARNING "TLB synthesizer field overflow\n");
241
242 return arg & IMM_MASK;
243}
244
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100245static __init u32 build_bimm(s32 arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 if (arg > 0x1ffff || arg < -0x20000)
248 printk(KERN_WARNING "TLB synthesizer field overflow\n");
249
250 if (arg & 0x3)
251 printk(KERN_WARNING "Invalid TLB synthesizer branch target\n");
252
253 return ((arg < 0) ? (1 << 15) : 0) | ((arg >> 2) & 0x7fff);
254}
255
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100256static __init u32 build_jimm(u32 arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 if (arg & ~((JIMM_MASK) << 2))
259 printk(KERN_WARNING "TLB synthesizer field overflow\n");
260
261 return (arg >> 2) & JIMM_MASK;
262}
263
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100264static __init u32 build_func(u32 arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 if (arg & ~FUNC_MASK)
267 printk(KERN_WARNING "TLB synthesizer field overflow\n");
268
269 return arg & FUNC_MASK;
270}
271
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100272static __init u32 build_set(u32 arg)
Ralf Baechle41c594a2006-04-05 09:45:45 +0100273{
274 if (arg & ~SET_MASK)
275 printk(KERN_WARNING "TLB synthesizer field overflow\n");
276
277 return arg & SET_MASK;
278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280/*
281 * The order of opcode arguments is implicitly left to right,
282 * starting with RS and ending with FUNC or IMM.
283 */
284static void __init build_insn(u32 **buf, enum opcode opc, ...)
285{
286 struct insn *ip = NULL;
287 unsigned int i;
288 va_list ap;
289 u32 op;
290
291 for (i = 0; insn_table[i].opcode != insn_invalid; i++)
292 if (insn_table[i].opcode == opc) {
293 ip = &insn_table[i];
294 break;
295 }
296
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100297 if (!ip || (opc == insn_daddiu && r4k_daddiu_bug()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 panic("Unsupported TLB synthesizer instruction %d", opc);
299
300 op = ip->match;
301 va_start(ap, opc);
302 if (ip->fields & RS) op |= build_rs(va_arg(ap, u32));
303 if (ip->fields & RT) op |= build_rt(va_arg(ap, u32));
304 if (ip->fields & RD) op |= build_rd(va_arg(ap, u32));
305 if (ip->fields & RE) op |= build_re(va_arg(ap, u32));
306 if (ip->fields & SIMM) op |= build_simm(va_arg(ap, s32));
307 if (ip->fields & UIMM) op |= build_uimm(va_arg(ap, u32));
308 if (ip->fields & BIMM) op |= build_bimm(va_arg(ap, s32));
309 if (ip->fields & JIMM) op |= build_jimm(va_arg(ap, u32));
310 if (ip->fields & FUNC) op |= build_func(va_arg(ap, u32));
Ralf Baechle41c594a2006-04-05 09:45:45 +0100311 if (ip->fields & SET) op |= build_set(va_arg(ap, u32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 va_end(ap);
313
314 **buf = op;
315 (*buf)++;
316}
317
318#define I_u1u2u3(op) \
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100319 static inline void __init i##op(u32 **buf, unsigned int a, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 unsigned int b, unsigned int c) \
321 { \
322 build_insn(buf, insn##op, a, b, c); \
323 }
324
325#define I_u2u1u3(op) \
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100326 static inline void __init i##op(u32 **buf, unsigned int a, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 unsigned int b, unsigned int c) \
328 { \
329 build_insn(buf, insn##op, b, a, c); \
330 }
331
332#define I_u3u1u2(op) \
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100333 static inline void __init i##op(u32 **buf, unsigned int a, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 unsigned int b, unsigned int c) \
335 { \
336 build_insn(buf, insn##op, b, c, a); \
337 }
338
339#define I_u1u2s3(op) \
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100340 static inline void __init i##op(u32 **buf, unsigned int a, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 unsigned int b, signed int c) \
342 { \
343 build_insn(buf, insn##op, a, b, c); \
344 }
345
346#define I_u2s3u1(op) \
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100347 static inline void __init i##op(u32 **buf, unsigned int a, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 signed int b, unsigned int c) \
349 { \
350 build_insn(buf, insn##op, c, a, b); \
351 }
352
353#define I_u2u1s3(op) \
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100354 static inline void __init i##op(u32 **buf, unsigned int a, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 unsigned int b, signed int c) \
356 { \
357 build_insn(buf, insn##op, b, a, c); \
358 }
359
360#define I_u1u2(op) \
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100361 static inline void __init i##op(u32 **buf, unsigned int a, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 unsigned int b) \
363 { \
364 build_insn(buf, insn##op, a, b); \
365 }
366
367#define I_u1s2(op) \
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100368 static inline void __init i##op(u32 **buf, unsigned int a, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 signed int b) \
370 { \
371 build_insn(buf, insn##op, a, b); \
372 }
373
374#define I_u1(op) \
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100375 static inline void __init i##op(u32 **buf, unsigned int a) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 { \
377 build_insn(buf, insn##op, a); \
378 }
379
380#define I_0(op) \
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100381 static inline void __init i##op(u32 **buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 { \
383 build_insn(buf, insn##op); \
384 }
385
386I_u2u1s3(_addiu);
387I_u3u1u2(_addu);
388I_u2u1u3(_andi);
389I_u3u1u2(_and);
390I_u1u2s3(_beq);
391I_u1u2s3(_beql);
392I_u1s2(_bgez);
393I_u1s2(_bgezl);
394I_u1s2(_bltz);
395I_u1s2(_bltzl);
396I_u1u2s3(_bne);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100397I_u1u2u3(_dmfc0);
398I_u1u2u3(_dmtc0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399I_u2u1s3(_daddiu);
400I_u3u1u2(_daddu);
401I_u2u1u3(_dsll);
402I_u2u1u3(_dsll32);
403I_u2u1u3(_dsra);
404I_u2u1u3(_dsrl);
Ralf Baechle242954b2006-10-24 02:29:01 +0100405I_u2u1u3(_dsrl32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406I_u3u1u2(_dsubu);
407I_0(_eret);
408I_u1(_j);
409I_u1(_jal);
410I_u1(_jr);
411I_u2s3u1(_ld);
412I_u2s3u1(_ll);
413I_u2s3u1(_lld);
414I_u1s2(_lui);
415I_u2s3u1(_lw);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100416I_u1u2u3(_mfc0);
417I_u1u2u3(_mtc0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418I_u2u1u3(_ori);
419I_0(_rfe);
420I_u2s3u1(_sc);
421I_u2s3u1(_scd);
422I_u2s3u1(_sd);
423I_u2u1u3(_sll);
424I_u2u1u3(_sra);
425I_u2u1u3(_srl);
426I_u3u1u2(_subu);
427I_u2s3u1(_sw);
428I_0(_tlbp);
429I_0(_tlbwi);
430I_0(_tlbwr);
431I_u3u1u2(_xor)
432I_u2u1u3(_xori);
433
434/*
435 * handling labels
436 */
437
438enum label_id {
439 label_invalid,
440 label_second_part,
441 label_leave,
Atsushi Nemoto656be922006-10-26 00:08:31 +0900442#ifdef MODULE_START
443 label_module_alloc,
444#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 label_vmalloc,
446 label_vmalloc_done,
447 label_tlbw_hazard,
448 label_split,
449 label_nopage_tlbl,
450 label_nopage_tlbs,
451 label_nopage_tlbm,
452 label_smp_pgtable_change,
453 label_r3000_write_probe_fail,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454};
455
456struct label {
457 u32 *addr;
458 enum label_id lab;
459};
460
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100461static __init void build_label(struct label **lab, u32 *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 enum label_id l)
463{
464 (*lab)->addr = addr;
465 (*lab)->lab = l;
466 (*lab)++;
467}
468
469#define L_LA(lb) \
470 static inline void l##lb(struct label **lab, u32 *addr) \
471 { \
472 build_label(lab, addr, label##lb); \
473 }
474
475L_LA(_second_part)
476L_LA(_leave)
Atsushi Nemoto656be922006-10-26 00:08:31 +0900477#ifdef MODULE_START
478L_LA(_module_alloc)
479#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480L_LA(_vmalloc)
481L_LA(_vmalloc_done)
482L_LA(_tlbw_hazard)
483L_LA(_split)
484L_LA(_nopage_tlbl)
485L_LA(_nopage_tlbs)
486L_LA(_nopage_tlbm)
487L_LA(_smp_pgtable_change)
488L_LA(_r3000_write_probe_fail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490/* convenience macros for instructions */
Ralf Baechle875d43e2005-09-03 15:56:16 -0700491#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492# define i_LW(buf, rs, rt, off) i_ld(buf, rs, rt, off)
493# define i_SW(buf, rs, rt, off) i_sd(buf, rs, rt, off)
494# define i_SLL(buf, rs, rt, sh) i_dsll(buf, rs, rt, sh)
495# define i_SRA(buf, rs, rt, sh) i_dsra(buf, rs, rt, sh)
496# define i_SRL(buf, rs, rt, sh) i_dsrl(buf, rs, rt, sh)
Ralf Baechle41c594a2006-04-05 09:45:45 +0100497# define i_MFC0(buf, rt, rd...) i_dmfc0(buf, rt, rd)
498# define i_MTC0(buf, rt, rd...) i_dmtc0(buf, rt, rd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499# define i_ADDIU(buf, rs, rt, val) i_daddiu(buf, rs, rt, val)
500# define i_ADDU(buf, rs, rt, rd) i_daddu(buf, rs, rt, rd)
501# define i_SUBU(buf, rs, rt, rd) i_dsubu(buf, rs, rt, rd)
502# define i_LL(buf, rs, rt, off) i_lld(buf, rs, rt, off)
503# define i_SC(buf, rs, rt, off) i_scd(buf, rs, rt, off)
504#else
505# define i_LW(buf, rs, rt, off) i_lw(buf, rs, rt, off)
506# define i_SW(buf, rs, rt, off) i_sw(buf, rs, rt, off)
507# define i_SLL(buf, rs, rt, sh) i_sll(buf, rs, rt, sh)
508# define i_SRA(buf, rs, rt, sh) i_sra(buf, rs, rt, sh)
509# define i_SRL(buf, rs, rt, sh) i_srl(buf, rs, rt, sh)
Ralf Baechle41c594a2006-04-05 09:45:45 +0100510# define i_MFC0(buf, rt, rd...) i_mfc0(buf, rt, rd)
511# define i_MTC0(buf, rt, rd...) i_mtc0(buf, rt, rd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512# define i_ADDIU(buf, rs, rt, val) i_addiu(buf, rs, rt, val)
513# define i_ADDU(buf, rs, rt, rd) i_addu(buf, rs, rt, rd)
514# define i_SUBU(buf, rs, rt, rd) i_subu(buf, rs, rt, rd)
515# define i_LL(buf, rs, rt, off) i_ll(buf, rs, rt, off)
516# define i_SC(buf, rs, rt, off) i_sc(buf, rs, rt, off)
517#endif
518
519#define i_b(buf, off) i_beq(buf, 0, 0, off)
520#define i_beqz(buf, rs, off) i_beq(buf, rs, 0, off)
521#define i_beqzl(buf, rs, off) i_beql(buf, rs, 0, off)
522#define i_bnez(buf, rs, off) i_bne(buf, rs, 0, off)
523#define i_bnezl(buf, rs, off) i_bnel(buf, rs, 0, off)
524#define i_move(buf, a, b) i_ADDU(buf, a, 0, b)
525#define i_nop(buf) i_sll(buf, 0, 0, 0)
526#define i_ssnop(buf) i_sll(buf, 0, 0, 1)
527#define i_ehb(buf) i_sll(buf, 0, 0, 3)
528
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100529static __init int __maybe_unused in_compat_space_p(long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 /* Is this address in 32bit compat space? */
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100532#ifdef CONFIG_64BIT
Ralf Baechle3ef33e62005-07-08 20:10:17 +0000533 return (((addr) & 0xffffffff00000000L) == 0xffffffff00000000L);
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100534#else
535 return 1;
536#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100539static __init int __maybe_unused rel_highest(long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100541#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return ((((val + 0x800080008000L) >> 48) & 0xffff) ^ 0x8000) - 0x8000;
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100543#else
544 return 0;
545#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100548static __init int __maybe_unused rel_higher(long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100550#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return ((((val + 0x80008000L) >> 32) & 0xffff) ^ 0x8000) - 0x8000;
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100552#else
553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554#endif
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100555}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100557static __init int rel_hi(long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 return ((((val + 0x8000L) >> 16) & 0xffff) ^ 0x8000) - 0x8000;
560}
561
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100562static __init int rel_lo(long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 return ((val & 0xffff) ^ 0x8000) - 0x8000;
565}
566
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100567static __init void i_LA_mostly(u32 **buf, unsigned int rs, long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (!in_compat_space_p(addr)) {
570 i_lui(buf, rs, rel_highest(addr));
571 if (rel_higher(addr))
572 i_daddiu(buf, rs, rs, rel_higher(addr));
573 if (rel_hi(addr)) {
574 i_dsll(buf, rs, rs, 16);
575 i_daddiu(buf, rs, rs, rel_hi(addr));
576 i_dsll(buf, rs, rs, 16);
577 } else
578 i_dsll32(buf, rs, rs, 0);
579 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 i_lui(buf, rs, rel_hi(addr));
581}
582
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100583static __init void __maybe_unused i_LA(u32 **buf, unsigned int rs, long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 i_LA_mostly(buf, rs, addr);
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100586 if (rel_lo(addr)) {
587 if (!in_compat_space_p(addr))
588 i_daddiu(buf, rs, rs, rel_lo(addr));
589 else
590 i_addiu(buf, rs, rs, rel_lo(addr));
591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594/*
595 * handle relocations
596 */
597
598struct reloc {
599 u32 *addr;
600 unsigned int type;
601 enum label_id lab;
602};
603
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100604static __init void r_mips_pc16(struct reloc **rel, u32 *addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 enum label_id l)
606{
607 (*rel)->addr = addr;
608 (*rel)->type = R_MIPS_PC16;
609 (*rel)->lab = l;
610 (*rel)++;
611}
612
613static inline void __resolve_relocs(struct reloc *rel, struct label *lab)
614{
615 long laddr = (long)lab->addr;
616 long raddr = (long)rel->addr;
617
618 switch (rel->type) {
619 case R_MIPS_PC16:
620 *rel->addr |= build_bimm(laddr - (raddr + 4));
621 break;
622
623 default:
624 panic("Unsupported TLB synthesizer relocation %d",
625 rel->type);
626 }
627}
628
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100629static __init void resolve_relocs(struct reloc *rel, struct label *lab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 struct label *l;
632
633 for (; rel->lab != label_invalid; rel++)
634 for (l = lab; l->lab != label_invalid; l++)
635 if (rel->lab == l->lab)
636 __resolve_relocs(rel, l);
637}
638
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100639static __init void move_relocs(struct reloc *rel, u32 *first, u32 *end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 long off)
641{
642 for (; rel->lab != label_invalid; rel++)
643 if (rel->addr >= first && rel->addr < end)
644 rel->addr += off;
645}
646
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100647static __init void move_labels(struct label *lab, u32 *first, u32 *end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 long off)
649{
650 for (; lab->lab != label_invalid; lab++)
651 if (lab->addr >= first && lab->addr < end)
652 lab->addr += off;
653}
654
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100655static __init void copy_handler(struct reloc *rel, struct label *lab,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 u32 *first, u32 *end, u32 *target)
657{
658 long off = (long)(target - first);
659
660 memcpy(target, first, (end - first) * sizeof(u32));
661
662 move_relocs(rel, first, end, off);
663 move_labels(lab, first, end, off);
664}
665
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100666static __init int __maybe_unused insn_has_bdelay(struct reloc *rel,
David Rientjese8b6d402007-05-10 22:51:05 -0700667 u32 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 for (; rel->lab != label_invalid; rel++) {
670 if (rel->addr == addr
671 && (rel->type == R_MIPS_PC16
672 || rel->type == R_MIPS_26))
673 return 1;
674 }
675
676 return 0;
677}
678
679/* convenience functions for labeled branches */
David Rientjese8b6d402007-05-10 22:51:05 -0700680static void __init __maybe_unused
Ralf Baechle1443e482006-03-08 15:37:26 +0000681 il_bltz(u32 **p, struct reloc **r, unsigned int reg, enum label_id l)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 r_mips_pc16(r, *p, l);
684 i_bltz(p, reg, 0);
685}
686
David Rientjese8b6d402007-05-10 22:51:05 -0700687static void __init __maybe_unused il_b(u32 **p, struct reloc **r,
688 enum label_id l)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 r_mips_pc16(r, *p, l);
691 i_b(p, 0);
692}
693
Ralf Baechle1443e482006-03-08 15:37:26 +0000694static void __init il_beqz(u32 **p, struct reloc **r, unsigned int reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 enum label_id l)
696{
697 r_mips_pc16(r, *p, l);
698 i_beqz(p, reg, 0);
699}
700
David Rientjese8b6d402007-05-10 22:51:05 -0700701static void __init __maybe_unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702il_beqzl(u32 **p, struct reloc **r, unsigned int reg, enum label_id l)
703{
704 r_mips_pc16(r, *p, l);
705 i_beqzl(p, reg, 0);
706}
707
Ralf Baechle1443e482006-03-08 15:37:26 +0000708static void __init il_bnez(u32 **p, struct reloc **r, unsigned int reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 enum label_id l)
710{
711 r_mips_pc16(r, *p, l);
712 i_bnez(p, reg, 0);
713}
714
Ralf Baechle1443e482006-03-08 15:37:26 +0000715static void __init il_bgezl(u32 **p, struct reloc **r, unsigned int reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 enum label_id l)
717{
718 r_mips_pc16(r, *p, l);
719 i_bgezl(p, reg, 0);
720}
721
David Rientjese8b6d402007-05-10 22:51:05 -0700722static void __init __maybe_unused
Atsushi Nemoto656be922006-10-26 00:08:31 +0900723il_bgez(u32 **p, struct reloc **r, unsigned int reg, enum label_id l)
724{
725 r_mips_pc16(r, *p, l);
726 i_bgez(p, reg, 0);
727}
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729/* The only general purpose registers allowed in TLB handlers. */
730#define K0 26
731#define K1 27
732
733/* Some CP0 registers */
Ralf Baechle41c594a2006-04-05 09:45:45 +0100734#define C0_INDEX 0, 0
735#define C0_ENTRYLO0 2, 0
736#define C0_TCBIND 2, 2
737#define C0_ENTRYLO1 3, 0
738#define C0_CONTEXT 4, 0
739#define C0_BADVADDR 8, 0
740#define C0_ENTRYHI 10, 0
741#define C0_EPC 14, 0
742#define C0_XCONTEXT 20, 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Ralf Baechle875d43e2005-09-03 15:56:16 -0700744#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745# define GET_CONTEXT(buf, reg) i_MFC0(buf, reg, C0_XCONTEXT)
746#else
747# define GET_CONTEXT(buf, reg) i_MFC0(buf, reg, C0_CONTEXT)
748#endif
749
750/* The worst case length of the handler is around 18 instructions for
751 * R3000-style TLBs and up to 63 instructions for R4000-style TLBs.
752 * Maximum space available is 32 instructions for R3000 and 64
753 * instructions for R4000.
754 *
755 * We deliberately chose a buffer size of 128, so we won't scribble
756 * over anything important on overflow before we panic.
757 */
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100758static __initdata u32 tlb_handler[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760/* simply assume worst case size for labels and relocs */
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100761static __initdata struct label labels[128];
762static __initdata struct reloc relocs[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764/*
765 * The R3000 TLB handler is simple.
766 */
767static void __init build_r3000_tlb_refill_handler(void)
768{
769 long pgdc = (long)pgd_current;
770 u32 *p;
Thiemo Seufer115f2a42006-07-09 01:47:06 +0100771 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 memset(tlb_handler, 0, sizeof(tlb_handler));
774 p = tlb_handler;
775
776 i_mfc0(&p, K0, C0_BADVADDR);
777 i_lui(&p, K1, rel_hi(pgdc)); /* cp0 delay */
778 i_lw(&p, K1, rel_lo(pgdc), K1);
779 i_srl(&p, K0, K0, 22); /* load delay */
780 i_sll(&p, K0, K0, 2);
781 i_addu(&p, K1, K1, K0);
782 i_mfc0(&p, K0, C0_CONTEXT);
783 i_lw(&p, K1, 0, K1); /* cp0 delay */
784 i_andi(&p, K0, K0, 0xffc); /* load delay */
785 i_addu(&p, K1, K1, K0);
786 i_lw(&p, K0, 0, K1);
787 i_nop(&p); /* load delay */
788 i_mtc0(&p, K0, C0_ENTRYLO0);
789 i_mfc0(&p, K1, C0_EPC); /* cp0 delay */
790 i_tlbwr(&p); /* cp0 delay */
791 i_jr(&p, K1);
792 i_rfe(&p); /* branch delay */
793
794 if (p > tlb_handler + 32)
795 panic("TLB refill handler space exceeded");
796
Thiemo Seufer115f2a42006-07-09 01:47:06 +0100797 pr_info("Synthesized TLB refill handler (%u instructions).\n",
798 (unsigned int)(p - tlb_handler));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Thiemo Seufer115f2a42006-07-09 01:47:06 +0100800 pr_debug("\t.set push\n");
801 pr_debug("\t.set noreorder\n");
802 for (i = 0; i < (p - tlb_handler); i++)
803 pr_debug("\t.word 0x%08x\n", tlb_handler[i]);
804 pr_debug("\t.set pop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Ralf Baechle91b05e62006-03-29 18:53:00 +0100806 memcpy((void *)ebase, tlb_handler, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
809/*
810 * The R4000 TLB handler is much more complicated. We have two
811 * consecutive handler areas with 32 instructions space each.
812 * Since they aren't used at the same time, we can overflow in the
813 * other one.To keep things simple, we first assume linear space,
814 * then we relocate it to the final handler layout as needed.
815 */
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100816static __initdata u32 final_handler[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818/*
819 * Hazards
820 *
821 * From the IDT errata for the QED RM5230 (Nevada), processor revision 1.0:
822 * 2. A timing hazard exists for the TLBP instruction.
823 *
824 * stalling_instruction
825 * TLBP
826 *
827 * The JTLB is being read for the TLBP throughout the stall generated by the
828 * previous instruction. This is not really correct as the stalling instruction
829 * can modify the address used to access the JTLB. The failure symptom is that
830 * the TLBP instruction will use an address created for the stalling instruction
831 * and not the address held in C0_ENHI and thus report the wrong results.
832 *
833 * The software work-around is to not allow the instruction preceding the TLBP
834 * to stall - make it an NOP or some other instruction guaranteed not to stall.
835 *
836 * Errata 2 will not be fixed. This errata is also on the R5000.
837 *
838 * As if we MIPS hackers wouldn't know how to nop pipelines happy ...
839 */
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100840static __init void __maybe_unused build_tlb_probe_entry(u32 **p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Ralf Baechle10cc3522007-10-11 23:46:15 +0100842 switch (current_cpu_type()) {
Thiemo Seuferf5b4d952005-09-09 17:11:50 +0000843 /* Found by experiment: R4600 v2.0 needs this, too. */
844 case CPU_R4600:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 case CPU_R5000:
846 case CPU_R5000A:
847 case CPU_NEVADA:
848 i_nop(p);
849 i_tlbp(p);
850 break;
851
852 default:
853 i_tlbp(p);
854 break;
855 }
856}
857
858/*
859 * Write random or indexed TLB entry, and care about the hazards from
860 * the preceeding mtc0 and for the following eret.
861 */
862enum tlb_write_entry { tlb_random, tlb_indexed };
863
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +0100864static __init void build_tlb_write_entry(u32 **p, struct label **l,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 struct reloc **r,
866 enum tlb_write_entry wmode)
867{
868 void(*tlbw)(u32 **) = NULL;
869
870 switch (wmode) {
871 case tlb_random: tlbw = i_tlbwr; break;
872 case tlb_indexed: tlbw = i_tlbwi; break;
873 }
874
Ralf Baechle161548b2008-01-29 10:14:54 +0000875 if (cpu_has_mips_r2) {
876 i_ehb(p);
877 tlbw(p);
878 return;
879 }
880
Ralf Baechle10cc3522007-10-11 23:46:15 +0100881 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 case CPU_R4000PC:
883 case CPU_R4000SC:
884 case CPU_R4000MC:
885 case CPU_R4400PC:
886 case CPU_R4400SC:
887 case CPU_R4400MC:
888 /*
889 * This branch uses up a mtc0 hazard nop slot and saves
890 * two nops after the tlbw instruction.
891 */
892 il_bgezl(p, r, 0, label_tlbw_hazard);
893 tlbw(p);
894 l_tlbw_hazard(l, *p);
895 i_nop(p);
896 break;
897
898 case CPU_R4600:
899 case CPU_R4700:
900 case CPU_R5000:
901 case CPU_R5000A:
Maciej W. Rozycki2c93e122005-06-30 10:51:01 +0000902 i_nop(p);
903 tlbw(p);
904 i_nop(p);
905 break;
906
907 case CPU_R4300:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 case CPU_5KC:
909 case CPU_TX49XX:
910 case CPU_AU1000:
911 case CPU_AU1100:
912 case CPU_AU1500:
913 case CPU_AU1550:
Pete Popove3ad1c22005-03-01 06:33:16 +0000914 case CPU_AU1200:
Pete Popovbdf21b12005-07-14 17:47:57 +0000915 case CPU_PR4450:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 i_nop(p);
917 tlbw(p);
918 break;
919
920 case CPU_R10000:
921 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -0400922 case CPU_R14000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 case CPU_4KC:
924 case CPU_SB1:
Andrew Isaacson93ce2f522005-10-19 23:56:20 -0700925 case CPU_SB1A:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 case CPU_4KSC:
927 case CPU_20KC:
928 case CPU_25KF:
Aurelien Jarno1c0c13e2007-09-25 15:40:12 +0200929 case CPU_BCM3302:
930 case CPU_BCM4710:
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800931 case CPU_LOONGSON2:
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +0100932 if (m4kc_tlbp_war())
933 i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 tlbw(p);
935 break;
936
937 case CPU_NEVADA:
938 i_nop(p); /* QED specifies 2 nops hazard */
939 /*
940 * This branch uses up a mtc0 hazard nop slot and saves
941 * a nop after the tlbw instruction.
942 */
943 il_bgezl(p, r, 0, label_tlbw_hazard);
944 tlbw(p);
945 l_tlbw_hazard(l, *p);
946 break;
947
948 case CPU_RM7000:
949 i_nop(p);
950 i_nop(p);
951 i_nop(p);
952 i_nop(p);
953 tlbw(p);
954 break;
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 case CPU_RM9000:
957 /*
958 * When the JTLB is updated by tlbwi or tlbwr, a subsequent
959 * use of the JTLB for instructions should not occur for 4
960 * cpu cycles and use for data translations should not occur
961 * for 3 cpu cycles.
962 */
963 i_ssnop(p);
964 i_ssnop(p);
965 i_ssnop(p);
966 i_ssnop(p);
967 tlbw(p);
968 i_ssnop(p);
969 i_ssnop(p);
970 i_ssnop(p);
971 i_ssnop(p);
972 break;
973
974 case CPU_VR4111:
975 case CPU_VR4121:
976 case CPU_VR4122:
977 case CPU_VR4181:
978 case CPU_VR4181A:
979 i_nop(p);
980 i_nop(p);
981 tlbw(p);
982 i_nop(p);
983 i_nop(p);
984 break;
985
986 case CPU_VR4131:
987 case CPU_VR4133:
Ralf Baechle7623deb2005-08-29 16:49:55 +0000988 case CPU_R5432:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 i_nop(p);
990 i_nop(p);
991 tlbw(p);
992 break;
993
994 default:
995 panic("No TLB refill handler yet (CPU type: %d)",
996 current_cpu_data.cputype);
997 break;
998 }
999}
1000
Ralf Baechle875d43e2005-09-03 15:56:16 -07001001#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002/*
1003 * TMP and PTR are scratch.
1004 * TMP will be clobbered, PTR will hold the pmd entry.
1005 */
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +01001006static __init void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007build_get_pmde64(u32 **p, struct label **l, struct reloc **r,
1008 unsigned int tmp, unsigned int ptr)
1009{
1010 long pgdc = (long)pgd_current;
1011
1012 /*
1013 * The vmalloc handling is not in the hotpath.
1014 */
1015 i_dmfc0(p, tmp, C0_BADVADDR);
Atsushi Nemoto656be922006-10-26 00:08:31 +09001016#ifdef MODULE_START
1017 il_bltz(p, r, tmp, label_module_alloc);
1018#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 il_bltz(p, r, tmp, label_vmalloc);
Atsushi Nemoto656be922006-10-26 00:08:31 +09001020#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 /* No i_nop needed here, since the next insn doesn't touch TMP. */
1022
1023#ifdef CONFIG_SMP
Ralf Baechle41c594a2006-04-05 09:45:45 +01001024# ifdef CONFIG_MIPS_MT_SMTC
1025 /*
1026 * SMTC uses TCBind value as "CPU" index
1027 */
1028 i_mfc0(p, ptr, C0_TCBIND);
1029 i_dsrl(p, ptr, ptr, 19);
1030# else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /*
Thiemo Seufer1b3a6e92005-04-01 14:07:13 +00001032 * 64 bit SMP running in XKPHYS has smp_processor_id() << 3
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 * stored in CONTEXT.
1034 */
Thiemo Seufer1b3a6e92005-04-01 14:07:13 +00001035 i_dmfc0(p, ptr, C0_CONTEXT);
1036 i_dsrl(p, ptr, ptr, 23);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001037#endif
Thiemo Seufer1b3a6e92005-04-01 14:07:13 +00001038 i_LA_mostly(p, tmp, pgdc);
1039 i_daddu(p, ptr, ptr, tmp);
1040 i_dmfc0(p, tmp, C0_BADVADDR);
1041 i_ld(p, ptr, rel_lo(pgdc), ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042#else
1043 i_LA_mostly(p, ptr, pgdc);
1044 i_ld(p, ptr, rel_lo(pgdc), ptr);
1045#endif
1046
1047 l_vmalloc_done(l, *p);
Ralf Baechle242954b2006-10-24 02:29:01 +01001048
1049 if (PGDIR_SHIFT - 3 < 32) /* get pgd offset in bytes */
1050 i_dsrl(p, tmp, tmp, PGDIR_SHIFT-3);
1051 else
1052 i_dsrl32(p, tmp, tmp, PGDIR_SHIFT - 3 - 32);
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 i_andi(p, tmp, tmp, (PTRS_PER_PGD - 1)<<3);
1055 i_daddu(p, ptr, ptr, tmp); /* add in pgd offset */
1056 i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
1057 i_ld(p, ptr, 0, ptr); /* get pmd pointer */
1058 i_dsrl(p, tmp, tmp, PMD_SHIFT-3); /* get pmd offset in bytes */
1059 i_andi(p, tmp, tmp, (PTRS_PER_PMD - 1)<<3);
1060 i_daddu(p, ptr, ptr, tmp); /* add in pmd offset */
1061}
1062
1063/*
1064 * BVADDR is the faulting address, PTR is scratch.
1065 * PTR will hold the pgd for vmalloc.
1066 */
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +01001067static __init void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068build_get_pgd_vmalloc64(u32 **p, struct label **l, struct reloc **r,
1069 unsigned int bvaddr, unsigned int ptr)
1070{
1071 long swpd = (long)swapper_pg_dir;
1072
Atsushi Nemoto656be922006-10-26 00:08:31 +09001073#ifdef MODULE_START
1074 long modd = (long)module_pg_dir;
1075
1076 l_module_alloc(l, *p);
1077 /*
1078 * Assumption:
1079 * VMALLOC_START >= 0xc000000000000000UL
1080 * MODULE_START >= 0xe000000000000000UL
1081 */
1082 i_SLL(p, ptr, bvaddr, 2);
1083 il_bgez(p, r, ptr, label_vmalloc);
1084
1085 if (in_compat_space_p(MODULE_START) && !rel_lo(MODULE_START)) {
1086 i_lui(p, ptr, rel_hi(MODULE_START)); /* delay slot */
1087 } else {
1088 /* unlikely configuration */
1089 i_nop(p); /* delay slot */
1090 i_LA(p, ptr, MODULE_START);
1091 }
1092 i_dsubu(p, bvaddr, bvaddr, ptr);
1093
1094 if (in_compat_space_p(modd) && !rel_lo(modd)) {
1095 il_b(p, r, label_vmalloc_done);
1096 i_lui(p, ptr, rel_hi(modd));
1097 } else {
1098 i_LA_mostly(p, ptr, modd);
1099 il_b(p, r, label_vmalloc_done);
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +01001100 if (in_compat_space_p(modd))
1101 i_addiu(p, ptr, ptr, rel_lo(modd));
1102 else
1103 i_daddiu(p, ptr, ptr, rel_lo(modd));
Atsushi Nemoto656be922006-10-26 00:08:31 +09001104 }
1105
1106 l_vmalloc(l, *p);
1107 if (in_compat_space_p(MODULE_START) && !rel_lo(MODULE_START) &&
1108 MODULE_START << 32 == VMALLOC_START)
1109 i_dsll32(p, ptr, ptr, 0); /* typical case */
1110 else
1111 i_LA(p, ptr, VMALLOC_START);
1112#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 l_vmalloc(l, *p);
1114 i_LA(p, ptr, VMALLOC_START);
Atsushi Nemoto656be922006-10-26 00:08:31 +09001115#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 i_dsubu(p, bvaddr, bvaddr, ptr);
1117
1118 if (in_compat_space_p(swpd) && !rel_lo(swpd)) {
1119 il_b(p, r, label_vmalloc_done);
1120 i_lui(p, ptr, rel_hi(swpd));
1121 } else {
1122 i_LA_mostly(p, ptr, swpd);
1123 il_b(p, r, label_vmalloc_done);
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +01001124 if (in_compat_space_p(swpd))
1125 i_addiu(p, ptr, ptr, rel_lo(swpd));
1126 else
1127 i_daddiu(p, ptr, ptr, rel_lo(swpd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129}
1130
Ralf Baechle875d43e2005-09-03 15:56:16 -07001131#else /* !CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133/*
1134 * TMP and PTR are scratch.
1135 * TMP will be clobbered, PTR will hold the pgd entry.
1136 */
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +01001137static __init void __maybe_unused
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
1139{
1140 long pgdc = (long)pgd_current;
1141
1142 /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
1143#ifdef CONFIG_SMP
Ralf Baechle41c594a2006-04-05 09:45:45 +01001144#ifdef CONFIG_MIPS_MT_SMTC
1145 /*
1146 * SMTC uses TCBind value as "CPU" index
1147 */
1148 i_mfc0(p, ptr, C0_TCBIND);
1149 i_LA_mostly(p, tmp, pgdc);
1150 i_srl(p, ptr, ptr, 19);
1151#else
1152 /*
1153 * smp_processor_id() << 3 is stored in CONTEXT.
1154 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 i_mfc0(p, ptr, C0_CONTEXT);
1156 i_LA_mostly(p, tmp, pgdc);
1157 i_srl(p, ptr, ptr, 23);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001158#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 i_addu(p, ptr, tmp, ptr);
1160#else
1161 i_LA_mostly(p, ptr, pgdc);
1162#endif
1163 i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
1164 i_lw(p, ptr, rel_lo(pgdc), ptr);
1165 i_srl(p, tmp, tmp, PGDIR_SHIFT); /* get pgd only bits */
1166 i_sll(p, tmp, tmp, PGD_T_LOG2);
1167 i_addu(p, ptr, ptr, tmp); /* add in pgd offset */
1168}
1169
Ralf Baechle875d43e2005-09-03 15:56:16 -07001170#endif /* !CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +01001172static __init void build_adjust_context(u32 **p, unsigned int ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Ralf Baechle242954b2006-10-24 02:29:01 +01001174 unsigned int shift = 4 - (PTE_T_LOG2 + 1) + PAGE_SHIFT - 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 unsigned int mask = (PTRS_PER_PTE / 2 - 1) << (PTE_T_LOG2 + 1);
1176
Ralf Baechle10cc3522007-10-11 23:46:15 +01001177 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 case CPU_VR41XX:
1179 case CPU_VR4111:
1180 case CPU_VR4121:
1181 case CPU_VR4122:
1182 case CPU_VR4131:
1183 case CPU_VR4181:
1184 case CPU_VR4181A:
1185 case CPU_VR4133:
1186 shift += 2;
1187 break;
1188
1189 default:
1190 break;
1191 }
1192
1193 if (shift)
1194 i_SRL(p, ctx, ctx, shift);
1195 i_andi(p, ctx, ctx, mask);
1196}
1197
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +01001198static __init void build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
1200 /*
1201 * Bug workaround for the Nevada. It seems as if under certain
1202 * circumstances the move from cp0_context might produce a
1203 * bogus result when the mfc0 instruction and its consumer are
1204 * in a different cacheline or a load instruction, probably any
1205 * memory reference, is between them.
1206 */
Ralf Baechle10cc3522007-10-11 23:46:15 +01001207 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 case CPU_NEVADA:
1209 i_LW(p, ptr, 0, ptr);
1210 GET_CONTEXT(p, tmp); /* get context reg */
1211 break;
1212
1213 default:
1214 GET_CONTEXT(p, tmp); /* get context reg */
1215 i_LW(p, ptr, 0, ptr);
1216 break;
1217 }
1218
1219 build_adjust_context(p, tmp);
1220 i_ADDU(p, ptr, ptr, tmp); /* add in offset */
1221}
1222
Ralf Baechle6f1ca1d2007-10-12 15:39:45 +01001223static __init void build_update_entries(u32 **p, unsigned int tmp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 unsigned int ptep)
1225{
1226 /*
1227 * 64bit address support (36bit on a 32bit CPU) in a 32bit
1228 * Kernel is a special case. Only a few CPUs use it.
1229 */
1230#ifdef CONFIG_64BIT_PHYS_ADDR
1231 if (cpu_has_64bits) {
1232 i_ld(p, tmp, 0, ptep); /* get even pte */
1233 i_ld(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
1234 i_dsrl(p, tmp, tmp, 6); /* convert to entrylo0 */
1235 i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
1236 i_dsrl(p, ptep, ptep, 6); /* convert to entrylo1 */
1237 i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
1238 } else {
1239 int pte_off_even = sizeof(pte_t) / 2;
1240 int pte_off_odd = pte_off_even + sizeof(pte_t);
1241
1242 /* The pte entries are pre-shifted */
1243 i_lw(p, tmp, pte_off_even, ptep); /* get even pte */
1244 i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
1245 i_lw(p, ptep, pte_off_odd, ptep); /* get odd pte */
1246 i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
1247 }
1248#else
1249 i_LW(p, tmp, 0, ptep); /* get even pte */
1250 i_LW(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
1251 if (r45k_bvahwbug())
1252 build_tlb_probe_entry(p);
1253 i_SRL(p, tmp, tmp, 6); /* convert to entrylo0 */
1254 if (r4k_250MHZhwbug())
1255 i_mtc0(p, 0, C0_ENTRYLO0);
1256 i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
1257 i_SRL(p, ptep, ptep, 6); /* convert to entrylo1 */
1258 if (r45k_bvahwbug())
1259 i_mfc0(p, tmp, C0_INDEX);
1260 if (r4k_250MHZhwbug())
1261 i_mtc0(p, 0, C0_ENTRYLO1);
1262 i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
1263#endif
1264}
1265
1266static void __init build_r4000_tlb_refill_handler(void)
1267{
1268 u32 *p = tlb_handler;
1269 struct label *l = labels;
1270 struct reloc *r = relocs;
1271 u32 *f;
1272 unsigned int final_len;
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001273 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 memset(tlb_handler, 0, sizeof(tlb_handler));
1276 memset(labels, 0, sizeof(labels));
1277 memset(relocs, 0, sizeof(relocs));
1278 memset(final_handler, 0, sizeof(final_handler));
1279
1280 /*
1281 * create the plain linear handler
1282 */
1283 if (bcm1250_m3_war()) {
1284 i_MFC0(&p, K0, C0_BADVADDR);
1285 i_MFC0(&p, K1, C0_ENTRYHI);
1286 i_xor(&p, K0, K0, K1);
1287 i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
1288 il_bnez(&p, &r, K0, label_leave);
1289 /* No need for i_nop */
1290 }
1291
Ralf Baechle875d43e2005-09-03 15:56:16 -07001292#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 build_get_pmde64(&p, &l, &r, K0, K1); /* get pmd in K1 */
1294#else
1295 build_get_pgde32(&p, K0, K1); /* get pgd in K1 */
1296#endif
1297
1298 build_get_ptep(&p, K0, K1);
1299 build_update_entries(&p, K0, K1);
1300 build_tlb_write_entry(&p, &l, &r, tlb_random);
1301 l_leave(&l, p);
1302 i_eret(&p); /* return from trap */
1303
Ralf Baechle875d43e2005-09-03 15:56:16 -07001304#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 build_get_pgd_vmalloc64(&p, &l, &r, K0, K1);
1306#endif
1307
1308 /*
1309 * Overflow check: For the 64bit handler, we need at least one
1310 * free instruction slot for the wrap-around branch. In worst
1311 * case, if the intended insertion point is a delay slot, we
Matt LaPlante4b3f6862006-10-03 22:21:02 +02001312 * need three, with the second nop'ed and the third being
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 * unused.
1314 */
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001315 /* Loongson2 ebase is different than r4k, we have more space */
1316#if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if ((p - tlb_handler) > 64)
1318 panic("TLB refill handler space exceeded");
1319#else
1320 if (((p - tlb_handler) > 63)
1321 || (((p - tlb_handler) > 61)
1322 && insn_has_bdelay(relocs, tlb_handler + 29)))
1323 panic("TLB refill handler space exceeded");
1324#endif
1325
1326 /*
1327 * Now fold the handler in the TLB refill handler space.
1328 */
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001329#if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 f = final_handler;
1331 /* Simplest case, just copy the handler. */
1332 copy_handler(relocs, labels, tlb_handler, p, f);
1333 final_len = p - tlb_handler;
Ralf Baechle875d43e2005-09-03 15:56:16 -07001334#else /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 f = final_handler + 32;
1336 if ((p - tlb_handler) <= 32) {
1337 /* Just copy the handler. */
1338 copy_handler(relocs, labels, tlb_handler, p, f);
1339 final_len = p - tlb_handler;
1340 } else {
1341 u32 *split = tlb_handler + 30;
1342
1343 /*
1344 * Find the split point.
1345 */
1346 if (insn_has_bdelay(relocs, split - 1))
1347 split--;
1348
1349 /* Copy first part of the handler. */
1350 copy_handler(relocs, labels, tlb_handler, split, f);
1351 f += split - tlb_handler;
1352
1353 /* Insert branch. */
1354 l_split(&l, final_handler);
1355 il_b(&f, &r, label_split);
1356 if (insn_has_bdelay(relocs, split))
1357 i_nop(&f);
1358 else {
1359 copy_handler(relocs, labels, split, split + 1, f);
1360 move_labels(labels, f, f + 1, -1);
1361 f++;
1362 split++;
1363 }
1364
1365 /* Copy the rest of the handler. */
1366 copy_handler(relocs, labels, split, p, final_handler);
1367 final_len = (f - (final_handler + 32)) + (p - split);
1368 }
Ralf Baechle875d43e2005-09-03 15:56:16 -07001369#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 resolve_relocs(relocs, labels);
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001372 pr_info("Synthesized TLB refill handler (%u instructions).\n",
1373 final_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001375 f = final_handler;
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001376#if defined(CONFIG_64BIT) && !defined(CONFIG_CPU_LOONGSON2)
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001377 if (final_len > 32)
1378 final_len = 64;
1379 else
1380 f = final_handler + 32;
Maciej W. Rozycki4c0a2d42005-06-29 10:43:51 +00001381#endif /* CONFIG_64BIT */
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001382 pr_debug("\t.set push\n");
1383 pr_debug("\t.set noreorder\n");
1384 for (i = 0; i < final_len; i++)
1385 pr_debug("\t.word 0x%08x\n", f[i]);
1386 pr_debug("\t.set pop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Ralf Baechle91b05e62006-03-29 18:53:00 +01001388 memcpy((void *)ebase, final_handler, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
1391/*
1392 * TLB load/store/modify handlers.
1393 *
1394 * Only the fastpath gets synthesized at runtime, the slowpath for
1395 * do_page_fault remains normal asm.
1396 */
1397extern void tlb_do_page_fault_0(void);
1398extern void tlb_do_page_fault_1(void);
1399
1400#define __tlb_handler_align \
1401 __attribute__((__aligned__(1 << CONFIG_MIPS_L1_CACHE_SHIFT)))
1402
1403/*
1404 * 128 instructions for the fastpath handler is generous and should
1405 * never be exceeded.
1406 */
1407#define FASTPATH_SIZE 128
1408
1409u32 __tlb_handler_align handle_tlbl[FASTPATH_SIZE];
1410u32 __tlb_handler_align handle_tlbs[FASTPATH_SIZE];
1411u32 __tlb_handler_align handle_tlbm[FASTPATH_SIZE];
1412
1413static void __init
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001414iPTE_LW(u32 **p, struct label **l, unsigned int pte, unsigned int ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416#ifdef CONFIG_SMP
1417# ifdef CONFIG_64BIT_PHYS_ADDR
1418 if (cpu_has_64bits)
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001419 i_lld(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 else
1421# endif
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001422 i_LL(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423#else
1424# ifdef CONFIG_64BIT_PHYS_ADDR
1425 if (cpu_has_64bits)
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001426 i_ld(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 else
1428# endif
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001429 i_LW(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430#endif
1431}
1432
1433static void __init
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001434iPTE_SW(u32 **p, struct reloc **r, unsigned int pte, unsigned int ptr,
1435 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001437#ifdef CONFIG_64BIT_PHYS_ADDR
1438 unsigned int hwmode = mode & (_PAGE_VALID | _PAGE_DIRTY);
1439#endif
1440
1441 i_ori(p, pte, pte, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442#ifdef CONFIG_SMP
1443# ifdef CONFIG_64BIT_PHYS_ADDR
1444 if (cpu_has_64bits)
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001445 i_scd(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 else
1447# endif
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001448 i_SC(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 if (r10000_llsc_war())
1451 il_beqzl(p, r, pte, label_smp_pgtable_change);
1452 else
1453 il_beqz(p, r, pte, label_smp_pgtable_change);
1454
1455# ifdef CONFIG_64BIT_PHYS_ADDR
1456 if (!cpu_has_64bits) {
1457 /* no i_nop needed */
1458 i_ll(p, pte, sizeof(pte_t) / 2, ptr);
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001459 i_ori(p, pte, pte, hwmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 i_sc(p, pte, sizeof(pte_t) / 2, ptr);
1461 il_beqz(p, r, pte, label_smp_pgtable_change);
1462 /* no i_nop needed */
1463 i_lw(p, pte, 0, ptr);
1464 } else
1465 i_nop(p);
1466# else
1467 i_nop(p);
1468# endif
1469#else
1470# ifdef CONFIG_64BIT_PHYS_ADDR
1471 if (cpu_has_64bits)
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001472 i_sd(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 else
1474# endif
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001475 i_SW(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477# ifdef CONFIG_64BIT_PHYS_ADDR
1478 if (!cpu_has_64bits) {
1479 i_lw(p, pte, sizeof(pte_t) / 2, ptr);
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001480 i_ori(p, pte, pte, hwmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 i_sw(p, pte, sizeof(pte_t) / 2, ptr);
1482 i_lw(p, pte, 0, ptr);
1483 }
1484# endif
1485#endif
1486}
1487
1488/*
1489 * Check if PTE is present, if not then jump to LABEL. PTR points to
1490 * the page table where this PTE is located, PTE will be re-loaded
1491 * with it's original value.
1492 */
1493static void __init
1494build_pte_present(u32 **p, struct label **l, struct reloc **r,
1495 unsigned int pte, unsigned int ptr, enum label_id lid)
1496{
1497 i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
1498 i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
1499 il_bnez(p, r, pte, lid);
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001500 iPTE_LW(p, l, pte, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
1503/* Make PTE valid, store result in PTR. */
1504static void __init
1505build_make_valid(u32 **p, struct reloc **r, unsigned int pte,
1506 unsigned int ptr)
1507{
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001508 unsigned int mode = _PAGE_VALID | _PAGE_ACCESSED;
1509
1510 iPTE_SW(p, r, pte, ptr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
1513/*
1514 * Check if PTE can be written to, if not branch to LABEL. Regardless
1515 * restore PTE with value from PTR when done.
1516 */
1517static void __init
1518build_pte_writable(u32 **p, struct label **l, struct reloc **r,
1519 unsigned int pte, unsigned int ptr, enum label_id lid)
1520{
1521 i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
1522 i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
1523 il_bnez(p, r, pte, lid);
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001524 iPTE_LW(p, l, pte, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
1527/* Make PTE writable, update software status bits as well, then store
1528 * at PTR.
1529 */
1530static void __init
1531build_make_write(u32 **p, struct reloc **r, unsigned int pte,
1532 unsigned int ptr)
1533{
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001534 unsigned int mode = (_PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID
1535 | _PAGE_DIRTY);
1536
1537 iPTE_SW(p, r, pte, ptr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
1540/*
1541 * Check if PTE can be modified, if not branch to LABEL. Regardless
1542 * restore PTE with value from PTR when done.
1543 */
1544static void __init
1545build_pte_modifiable(u32 **p, struct label **l, struct reloc **r,
1546 unsigned int pte, unsigned int ptr, enum label_id lid)
1547{
1548 i_andi(p, pte, pte, _PAGE_WRITE);
1549 il_beqz(p, r, pte, lid);
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001550 iPTE_LW(p, l, pte, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
1553/*
1554 * R3000 style TLB load/store/modify handlers.
1555 */
1556
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001557/*
1558 * This places the pte into ENTRYLO0 and writes it with tlbwi.
1559 * Then it returns.
1560 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561static void __init
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001562build_r3000_pte_reload_tlbwi(u32 **p, unsigned int pte, unsigned int tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001564 i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
1565 i_mfc0(p, tmp, C0_EPC); /* cp0 delay */
1566 i_tlbwi(p);
1567 i_jr(p, tmp);
1568 i_rfe(p); /* branch delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
1571/*
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001572 * This places the pte into ENTRYLO0 and writes it with tlbwi
1573 * or tlbwr as appropriate. This is because the index register
1574 * may have the probe fail bit set as a result of a trap on a
1575 * kseg2 access, i.e. without refill. Then it returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 */
1577static void __init
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001578build_r3000_tlb_reload_write(u32 **p, struct label **l, struct reloc **r,
1579 unsigned int pte, unsigned int tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 i_mfc0(p, tmp, C0_INDEX);
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001582 i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
1583 il_bltz(p, r, tmp, label_r3000_write_probe_fail); /* cp0 delay */
1584 i_mfc0(p, tmp, C0_EPC); /* branch delay */
1585 i_tlbwi(p); /* cp0 delay */
1586 i_jr(p, tmp);
1587 i_rfe(p); /* branch delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 l_r3000_write_probe_fail(l, *p);
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001589 i_tlbwr(p); /* cp0 delay */
1590 i_jr(p, tmp);
1591 i_rfe(p); /* branch delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593
1594static void __init
1595build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
1596 unsigned int ptr)
1597{
1598 long pgdc = (long)pgd_current;
1599
1600 i_mfc0(p, pte, C0_BADVADDR);
1601 i_lui(p, ptr, rel_hi(pgdc)); /* cp0 delay */
1602 i_lw(p, ptr, rel_lo(pgdc), ptr);
1603 i_srl(p, pte, pte, 22); /* load delay */
1604 i_sll(p, pte, pte, 2);
1605 i_addu(p, ptr, ptr, pte);
1606 i_mfc0(p, pte, C0_CONTEXT);
1607 i_lw(p, ptr, 0, ptr); /* cp0 delay */
1608 i_andi(p, pte, pte, 0xffc); /* load delay */
1609 i_addu(p, ptr, ptr, pte);
1610 i_lw(p, pte, 0, ptr);
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001611 i_tlbp(p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
1614static void __init build_r3000_tlb_load_handler(void)
1615{
1616 u32 *p = handle_tlbl;
1617 struct label *l = labels;
1618 struct reloc *r = relocs;
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001619 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 memset(handle_tlbl, 0, sizeof(handle_tlbl));
1622 memset(labels, 0, sizeof(labels));
1623 memset(relocs, 0, sizeof(relocs));
1624
1625 build_r3000_tlbchange_handler_head(&p, K0, K1);
1626 build_pte_present(&p, &l, &r, K0, K1, label_nopage_tlbl);
Maciej W. Rozyckid925c262005-06-13 20:12:01 +00001627 i_nop(&p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 build_make_valid(&p, &r, K0, K1);
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001629 build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 l_nopage_tlbl(&l, p);
1632 i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
1633 i_nop(&p);
1634
1635 if ((p - handle_tlbl) > FASTPATH_SIZE)
1636 panic("TLB load handler fastpath space exceeded");
1637
1638 resolve_relocs(relocs, labels);
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001639 pr_info("Synthesized TLB load handler fastpath (%u instructions).\n",
1640 (unsigned int)(p - handle_tlbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001642 pr_debug("\t.set push\n");
1643 pr_debug("\t.set noreorder\n");
1644 for (i = 0; i < (p - handle_tlbl); i++)
1645 pr_debug("\t.word 0x%08x\n", handle_tlbl[i]);
1646 pr_debug("\t.set pop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
1649static void __init build_r3000_tlb_store_handler(void)
1650{
1651 u32 *p = handle_tlbs;
1652 struct label *l = labels;
1653 struct reloc *r = relocs;
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001654 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 memset(handle_tlbs, 0, sizeof(handle_tlbs));
1657 memset(labels, 0, sizeof(labels));
1658 memset(relocs, 0, sizeof(relocs));
1659
1660 build_r3000_tlbchange_handler_head(&p, K0, K1);
1661 build_pte_writable(&p, &l, &r, K0, K1, label_nopage_tlbs);
Maciej W. Rozyckid925c262005-06-13 20:12:01 +00001662 i_nop(&p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 build_make_write(&p, &r, K0, K1);
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001664 build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 l_nopage_tlbs(&l, p);
1667 i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1668 i_nop(&p);
1669
1670 if ((p - handle_tlbs) > FASTPATH_SIZE)
1671 panic("TLB store handler fastpath space exceeded");
1672
1673 resolve_relocs(relocs, labels);
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001674 pr_info("Synthesized TLB store handler fastpath (%u instructions).\n",
1675 (unsigned int)(p - handle_tlbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001677 pr_debug("\t.set push\n");
1678 pr_debug("\t.set noreorder\n");
1679 for (i = 0; i < (p - handle_tlbs); i++)
1680 pr_debug("\t.word 0x%08x\n", handle_tlbs[i]);
1681 pr_debug("\t.set pop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
1684static void __init build_r3000_tlb_modify_handler(void)
1685{
1686 u32 *p = handle_tlbm;
1687 struct label *l = labels;
1688 struct reloc *r = relocs;
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001689 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 memset(handle_tlbm, 0, sizeof(handle_tlbm));
1692 memset(labels, 0, sizeof(labels));
1693 memset(relocs, 0, sizeof(relocs));
1694
1695 build_r3000_tlbchange_handler_head(&p, K0, K1);
1696 build_pte_modifiable(&p, &l, &r, K0, K1, label_nopage_tlbm);
Maciej W. Rozyckid925c262005-06-13 20:12:01 +00001697 i_nop(&p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 build_make_write(&p, &r, K0, K1);
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001699 build_r3000_pte_reload_tlbwi(&p, K0, K1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 l_nopage_tlbm(&l, p);
1702 i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1703 i_nop(&p);
1704
1705 if ((p - handle_tlbm) > FASTPATH_SIZE)
1706 panic("TLB modify handler fastpath space exceeded");
1707
1708 resolve_relocs(relocs, labels);
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001709 pr_info("Synthesized TLB modify handler fastpath (%u instructions).\n",
1710 (unsigned int)(p - handle_tlbm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001712 pr_debug("\t.set push\n");
1713 pr_debug("\t.set noreorder\n");
1714 for (i = 0; i < (p - handle_tlbm); i++)
1715 pr_debug("\t.word 0x%08x\n", handle_tlbm[i]);
1716 pr_debug("\t.set pop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
1719/*
1720 * R4000 style TLB load/store/modify handlers.
1721 */
1722static void __init
1723build_r4000_tlbchange_handler_head(u32 **p, struct label **l,
1724 struct reloc **r, unsigned int pte,
1725 unsigned int ptr)
1726{
Ralf Baechle875d43e2005-09-03 15:56:16 -07001727#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 build_get_pmde64(p, l, r, pte, ptr); /* get pmd in ptr */
1729#else
1730 build_get_pgde32(p, pte, ptr); /* get pgd in ptr */
1731#endif
1732
1733 i_MFC0(p, pte, C0_BADVADDR);
1734 i_LW(p, ptr, 0, ptr);
1735 i_SRL(p, pte, pte, PAGE_SHIFT + PTE_ORDER - PTE_T_LOG2);
1736 i_andi(p, pte, pte, (PTRS_PER_PTE - 1) << PTE_T_LOG2);
1737 i_ADDU(p, ptr, ptr, pte);
1738
1739#ifdef CONFIG_SMP
1740 l_smp_pgtable_change(l, *p);
1741# endif
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001742 iPTE_LW(p, l, pte, ptr); /* get even pte */
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01001743 if (!m4kc_tlbp_war())
1744 build_tlb_probe_entry(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
1747static void __init
1748build_r4000_tlbchange_handler_tail(u32 **p, struct label **l,
1749 struct reloc **r, unsigned int tmp,
1750 unsigned int ptr)
1751{
1752 i_ori(p, ptr, ptr, sizeof(pte_t));
1753 i_xori(p, ptr, ptr, sizeof(pte_t));
1754 build_update_entries(p, tmp, ptr);
1755 build_tlb_write_entry(p, l, r, tlb_indexed);
1756 l_leave(l, *p);
1757 i_eret(p); /* return from trap */
1758
Ralf Baechle875d43e2005-09-03 15:56:16 -07001759#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 build_get_pgd_vmalloc64(p, l, r, tmp, ptr);
1761#endif
1762}
1763
1764static void __init build_r4000_tlb_load_handler(void)
1765{
1766 u32 *p = handle_tlbl;
1767 struct label *l = labels;
1768 struct reloc *r = relocs;
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001769 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 memset(handle_tlbl, 0, sizeof(handle_tlbl));
1772 memset(labels, 0, sizeof(labels));
1773 memset(relocs, 0, sizeof(relocs));
1774
1775 if (bcm1250_m3_war()) {
1776 i_MFC0(&p, K0, C0_BADVADDR);
1777 i_MFC0(&p, K1, C0_ENTRYHI);
1778 i_xor(&p, K0, K0, K1);
1779 i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
1780 il_bnez(&p, &r, K0, label_leave);
1781 /* No need for i_nop */
1782 }
1783
1784 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
1785 build_pte_present(&p, &l, &r, K0, K1, label_nopage_tlbl);
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01001786 if (m4kc_tlbp_war())
1787 build_tlb_probe_entry(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 build_make_valid(&p, &r, K0, K1);
1789 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1790
1791 l_nopage_tlbl(&l, p);
1792 i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
1793 i_nop(&p);
1794
1795 if ((p - handle_tlbl) > FASTPATH_SIZE)
1796 panic("TLB load handler fastpath space exceeded");
1797
1798 resolve_relocs(relocs, labels);
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001799 pr_info("Synthesized TLB load handler fastpath (%u instructions).\n",
1800 (unsigned int)(p - handle_tlbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001802 pr_debug("\t.set push\n");
1803 pr_debug("\t.set noreorder\n");
1804 for (i = 0; i < (p - handle_tlbl); i++)
1805 pr_debug("\t.word 0x%08x\n", handle_tlbl[i]);
1806 pr_debug("\t.set pop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
1809static void __init build_r4000_tlb_store_handler(void)
1810{
1811 u32 *p = handle_tlbs;
1812 struct label *l = labels;
1813 struct reloc *r = relocs;
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001814 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 memset(handle_tlbs, 0, sizeof(handle_tlbs));
1817 memset(labels, 0, sizeof(labels));
1818 memset(relocs, 0, sizeof(relocs));
1819
1820 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
1821 build_pte_writable(&p, &l, &r, K0, K1, label_nopage_tlbs);
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01001822 if (m4kc_tlbp_war())
1823 build_tlb_probe_entry(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 build_make_write(&p, &r, K0, K1);
1825 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1826
1827 l_nopage_tlbs(&l, p);
1828 i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1829 i_nop(&p);
1830
1831 if ((p - handle_tlbs) > FASTPATH_SIZE)
1832 panic("TLB store handler fastpath space exceeded");
1833
1834 resolve_relocs(relocs, labels);
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001835 pr_info("Synthesized TLB store handler fastpath (%u instructions).\n",
1836 (unsigned int)(p - handle_tlbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001838 pr_debug("\t.set push\n");
1839 pr_debug("\t.set noreorder\n");
1840 for (i = 0; i < (p - handle_tlbs); i++)
1841 pr_debug("\t.word 0x%08x\n", handle_tlbs[i]);
1842 pr_debug("\t.set pop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
1845static void __init build_r4000_tlb_modify_handler(void)
1846{
1847 u32 *p = handle_tlbm;
1848 struct label *l = labels;
1849 struct reloc *r = relocs;
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001850 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 memset(handle_tlbm, 0, sizeof(handle_tlbm));
1853 memset(labels, 0, sizeof(labels));
1854 memset(relocs, 0, sizeof(relocs));
1855
1856 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
1857 build_pte_modifiable(&p, &l, &r, K0, K1, label_nopage_tlbm);
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01001858 if (m4kc_tlbp_war())
1859 build_tlb_probe_entry(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 /* Present and writable bits set, set accessed and dirty bits. */
1861 build_make_write(&p, &r, K0, K1);
1862 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1863
1864 l_nopage_tlbm(&l, p);
1865 i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1866 i_nop(&p);
1867
1868 if ((p - handle_tlbm) > FASTPATH_SIZE)
1869 panic("TLB modify handler fastpath space exceeded");
1870
1871 resolve_relocs(relocs, labels);
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001872 pr_info("Synthesized TLB modify handler fastpath (%u instructions).\n",
1873 (unsigned int)(p - handle_tlbm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Thiemo Seufer115f2a42006-07-09 01:47:06 +01001875 pr_debug("\t.set push\n");
1876 pr_debug("\t.set noreorder\n");
1877 for (i = 0; i < (p - handle_tlbm); i++)
1878 pr_debug("\t.word 0x%08x\n", handle_tlbm[i]);
1879 pr_debug("\t.set pop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
1882void __init build_tlb_refill_handler(void)
1883{
1884 /*
1885 * The refill handler is generated per-CPU, multi-node systems
1886 * may have local storage for it. The other handlers are only
1887 * needed once.
1888 */
1889 static int run_once = 0;
1890
Ralf Baechle10cc3522007-10-11 23:46:15 +01001891 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 case CPU_R2000:
1893 case CPU_R3000:
1894 case CPU_R3000A:
1895 case CPU_R3081E:
1896 case CPU_TX3912:
1897 case CPU_TX3922:
1898 case CPU_TX3927:
1899 build_r3000_tlb_refill_handler();
1900 if (!run_once) {
1901 build_r3000_tlb_load_handler();
1902 build_r3000_tlb_store_handler();
1903 build_r3000_tlb_modify_handler();
1904 run_once++;
1905 }
1906 break;
1907
1908 case CPU_R6000:
1909 case CPU_R6000A:
1910 panic("No R6000 TLB refill handler yet");
1911 break;
1912
1913 case CPU_R8000:
1914 panic("No R8000 TLB refill handler yet");
1915 break;
1916
1917 default:
1918 build_r4000_tlb_refill_handler();
1919 if (!run_once) {
1920 build_r4000_tlb_load_handler();
1921 build_r4000_tlb_store_handler();
1922 build_r4000_tlb_modify_handler();
1923 run_once++;
1924 }
1925 }
1926}
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001927
1928void __init flush_tlb_handlers(void)
1929{
1930 flush_icache_range((unsigned long)handle_tlbl,
1931 (unsigned long)handle_tlbl + sizeof(handle_tlbl));
1932 flush_icache_range((unsigned long)handle_tlbs,
1933 (unsigned long)handle_tlbs + sizeof(handle_tlbs));
1934 flush_icache_range((unsigned long)handle_tlbm,
1935 (unsigned long)handle_tlbm + sizeof(handle_tlbm));
1936}