This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / mips / mm / tlbex.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Synthesize TLB refill handlers at runtime.
7  *
8  * Copyright (C) 2004 by Thiemo Seufer
9  */
10
11 #include <stdarg.h>
12
13 #include <linux/config.h>
14 #include <linux/mm.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/string.h>
18 #include <linux/init.h>
19
20 #include <asm/pgtable.h>
21 #include <asm/cacheflush.h>
22 #include <asm/cacheflush.h>
23 #include <asm/mmu_context.h>
24 #include <asm/inst.h>
25 #include <asm/elf.h>
26 #include <asm/smp.h>
27
28 /* #define DEBUG_TLB */
29
30 static __init int __attribute__((unused)) r45k_bvahwbug(void)
31 {
32         /* XXX: We should probe for the presence of this bug, but we don't. */
33         return 0;
34 }
35
36 static __init int __attribute__((unused)) r4k_250MHZhwbug(void)
37 {
38         /* XXX: We should probe for the presence of this bug, but we don't. */
39         return 0;
40 }
41
42 static __init int __attribute__((unused)) bcm1250_m3_war(void)
43 {
44         return BCM1250_M3_WAR;
45 }
46
47 /*
48  * A little micro-assembler, intended for TLB refill handler
49  * synthesizing. It is intentionally kept simple, does only support
50  * a subset of instructions, and does not try to hide pipeline effects
51  * like branch delay slots.
52  */
53
54 enum fields
55 {
56         RS = 0x001,
57         RT = 0x002,
58         RD = 0x004,
59         RE = 0x008,
60         SIMM = 0x010,
61         UIMM = 0x020,
62         BIMM = 0x040,
63         JIMM = 0x080,
64         FUNC = 0x100,
65 };
66
67 #define OP_MASK         0x2f
68 #define OP_SH           26
69 #define RS_MASK         0x1f
70 #define RS_SH           21
71 #define RT_MASK         0x1f
72 #define RT_SH           16
73 #define RD_MASK         0x1f
74 #define RD_SH           11
75 #define RE_MASK         0x1f
76 #define RE_SH           6
77 #define IMM_MASK        0xffff
78 #define IMM_SH          0
79 #define JIMM_MASK       0x3ffffff
80 #define JIMM_SH         0
81 #define FUNC_MASK       0x2f
82 #define FUNC_SH         0
83
84 enum opcode {
85         insn_invalid,
86         insn_addu, insn_addiu, insn_and, insn_andi, insn_beq,
87         insn_bgez, insn_bgezl, insn_bltz, insn_bltzl, insn_bne,
88         insn_daddu, insn_daddiu, insn_dmfc0, insn_dmtc0,
89         insn_dsll, insn_dsll32, insn_dsra, insn_dsrl, insn_dsrl32,
90         insn_dsubu, insn_eret, insn_j, insn_jal, insn_jr, insn_ld,
91         insn_lui, insn_lw, insn_mfc0, insn_mtc0, insn_ori, insn_rfe,
92         insn_sd, insn_sll, insn_sra, insn_srl, insn_subu, insn_sw,
93         insn_tlbp, insn_tlbwi, insn_tlbwr, insn_xor, insn_xori
94 };
95
96 struct insn {
97         enum opcode opcode;
98         u32 match;
99         enum fields fields;
100 };
101
102 /* This macro sets the non-variable bits of an instruction. */
103 #define M(a, b, c, d, e, f)                                     \
104         ((a) << OP_SH                                           \
105          | (b) << RS_SH                                         \
106          | (c) << RT_SH                                         \
107          | (d) << RD_SH                                         \
108          | (e) << RE_SH                                         \
109          | (f) << FUNC_SH)
110
111 static __initdata struct insn insn_table[] = {
112         { insn_addiu, M(addiu_op,0,0,0,0,0), RS | RT | SIMM },
113         { insn_addu, M(spec_op,0,0,0,0,addu_op), RS | RT | RD },
114         { insn_and, M(spec_op,0,0,0,0,and_op), RS | RT | RD },
115         { insn_andi, M(andi_op,0,0,0,0,0), RS | RT | UIMM },
116         { insn_beq, M(beq_op,0,0,0,0,0), RS | RT | BIMM },
117         { insn_bgez, M(bcond_op,0,bgez_op,0,0,0), RS | BIMM },
118         { insn_bgezl, M(bcond_op,0,bgezl_op,0,0,0), RS | BIMM },
119         { insn_bltz, M(bcond_op,0,bltz_op,0,0,0), RS | BIMM },
120         { insn_bltzl, M(bcond_op,0,bltzl_op,0,0,0), RS | BIMM },
121         { insn_bne, M(bne_op,0,0,0,0,0), RS | RT | BIMM },
122         { insn_daddiu, M(daddiu_op,0,0,0,0,0), RS | RT | SIMM },
123         { insn_daddu, M(spec_op,0,0,0,0,daddu_op), RS | RT | RD },
124         { insn_dmfc0, M(cop0_op,dmfc_op,0,0,0,0), RT | RD },
125         { insn_dmtc0, M(cop0_op,dmtc_op,0,0,0,0), RT | RD },
126         { insn_dsll, M(spec_op,0,0,0,0,dsll_op), RT | RD | RE },
127         { insn_dsll32, M(spec_op,0,0,0,0,dsll32_op), RT | RD | RE },
128         { insn_dsra, M(spec_op,0,0,0,0,dsra_op), RT | RD | RE },
129         { insn_dsrl, M(spec_op,0,0,0,0,dsrl_op), RT | RD | RE },
130         { insn_dsrl32, M(spec_op,0,0,0,0,dsrl32_op), RT | RD | RE },
131         { insn_dsubu, M(spec_op,0,0,0,0,dsubu_op), RS | RT | RD },
132         { insn_eret, M(cop0_op,cop_op,0,0,0,eret_op), 0 },
133         { insn_j, M(j_op,0,0,0,0,0), JIMM },
134         { insn_jal, M(jal_op,0,0,0,0,0), JIMM },
135         { insn_jr, M(spec_op,0,0,0,0,jr_op), RS },
136         { insn_ld, M(ld_op,0,0,0,0,0), RS | RT | SIMM },
137         { insn_lui, M(lui_op,0,0,0,0,0), RT | SIMM },
138         { insn_lw, M(lw_op,0,0,0,0,0), RS | RT | SIMM },
139         { insn_mfc0, M(cop0_op,mfc_op,0,0,0,0), RT | RD },
140         { insn_mtc0, M(cop0_op,mtc_op,0,0,0,0), RT | RD },
141         { insn_ori, M(ori_op,0,0,0,0,0), RS | RT | UIMM },
142         { insn_rfe, M(cop0_op,cop_op,0,0,0,rfe_op), 0 },
143         { insn_sd, M(sd_op,0,0,0,0,0), RS | RT | SIMM },
144         { insn_sll, M(spec_op,0,0,0,0,sll_op), RT | RD | RE },
145         { insn_sra, M(spec_op,0,0,0,0,sra_op), RT | RD | RE },
146         { insn_srl, M(spec_op,0,0,0,0,srl_op), RT | RD | RE },
147         { insn_subu, M(spec_op,0,0,0,0,subu_op), RS | RT | RD },
148         { insn_sw, M(sw_op,0,0,0,0,0), RS | RT | SIMM },
149         { insn_tlbp, M(cop0_op,cop_op,0,0,0,tlbp_op), 0 },
150         { insn_tlbwi, M(cop0_op,cop_op,0,0,0,tlbwi_op), 0 },
151         { insn_tlbwr, M(cop0_op,cop_op,0,0,0,tlbwr_op), 0 },
152         { insn_xor, M(spec_op,0,0,0,0,xor_op), RS | RT | RD },
153         { insn_xori, M(xori_op,0,0,0,0,0), RS | RT | UIMM },
154         { insn_invalid, 0, 0 }
155 };
156
157 #undef M
158
159 static __init u32 build_rs(u32 arg)
160 {
161         if (arg & ~RS_MASK)
162                 printk(KERN_WARNING "TLB synthesizer field overflow\n");
163
164         return (arg & RS_MASK) << RS_SH;
165 }
166
167 static __init u32 build_rt(u32 arg)
168 {
169         if (arg & ~RT_MASK)
170                 printk(KERN_WARNING "TLB synthesizer field overflow\n");
171
172         return (arg & RT_MASK) << RT_SH;
173 }
174
175 static __init u32 build_rd(u32 arg)
176 {
177         if (arg & ~RD_MASK)
178                 printk(KERN_WARNING "TLB synthesizer field overflow\n");
179
180         return (arg & RD_MASK) << RD_SH;
181 }
182
183 static __init u32 build_re(u32 arg)
184 {
185         if (arg & ~RE_MASK)
186                 printk(KERN_WARNING "TLB synthesizer field overflow\n");
187
188         return (arg & RE_MASK) << RE_SH;
189 }
190
191 static __init u32 build_simm(s32 arg)
192 {
193         if (arg > 0x7fff || arg < -0x8000)
194                 printk(KERN_WARNING "TLB synthesizer field overflow\n");
195
196         return arg & 0xffff;
197 }
198
199 static __init u32 build_uimm(u32 arg)
200 {
201         if (arg & ~IMM_MASK)
202                 printk(KERN_WARNING "TLB synthesizer field overflow\n");
203
204         return arg & IMM_MASK;
205 }
206
207 static __init u32 build_bimm(s32 arg)
208 {
209         if (arg > 0x1ffff || arg < -0x20000)
210                 printk(KERN_WARNING "TLB synthesizer field overflow\n");
211
212         if (arg & 0x3)
213                 printk(KERN_WARNING "Invalid TLB synthesizer branch target\n");
214
215         return ((arg < 0) ? (1 << 15) : 0) | ((arg >> 2) & 0x7fff);
216 }
217
218 static __init u32 build_jimm(u32 arg)
219 {
220         if (arg & ~((JIMM_MASK) << 2))
221                 printk(KERN_WARNING "TLB synthesizer field overflow\n");
222
223         return (arg >> 2) & JIMM_MASK;
224 }
225
226 static __init u32 build_func(u32 arg)
227 {
228         if (arg & ~FUNC_MASK)
229                 printk(KERN_WARNING "TLB synthesizer field overflow\n");
230
231         return arg & FUNC_MASK;
232 }
233
234 /*
235  * The order of opcode arguments is implicitly left to right,
236  * starting with RS and ending with FUNC or IMM.
237  */
238 static void __init build_insn(u32 **buf, enum opcode opc, ...)
239 {
240         struct insn *ip = NULL;
241         unsigned int i;
242         va_list ap;
243         u32 op;
244
245         for (i = 0; insn_table[i].opcode != insn_invalid; i++)
246                 if (insn_table[i].opcode == opc) {
247                         ip = &insn_table[i];
248                         break;
249                 }
250
251         if (!ip)
252                 panic("Unsupported TLB synthesizer instruction %d", opc);
253
254         op = ip->match;
255         va_start(ap, opc);
256         if (ip->fields & RS) op |= build_rs(va_arg(ap, u32));
257         if (ip->fields & RT) op |= build_rt(va_arg(ap, u32));
258         if (ip->fields & RD) op |= build_rd(va_arg(ap, u32));
259         if (ip->fields & RE) op |= build_re(va_arg(ap, u32));
260         if (ip->fields & SIMM) op |= build_simm(va_arg(ap, s32));
261         if (ip->fields & UIMM) op |= build_uimm(va_arg(ap, u32));
262         if (ip->fields & BIMM) op |= build_bimm(va_arg(ap, s32));
263         if (ip->fields & JIMM) op |= build_jimm(va_arg(ap, u32));
264         if (ip->fields & FUNC) op |= build_func(va_arg(ap, u32));
265         va_end(ap);
266
267         **buf = op;
268         (*buf)++;
269 }
270
271 #define I_u1u2u3(op)                                            \
272         static inline void i##op(u32 **buf, unsigned int a,     \
273                 unsigned int b, unsigned int c)                 \
274         {                                                       \
275                 build_insn(buf, insn##op, a, b, c);             \
276         }
277
278 #define I_u2u1u3(op)                                            \
279         static inline void i##op(u32 **buf, unsigned int a,     \
280                 unsigned int b, unsigned int c)                 \
281         {                                                       \
282                 build_insn(buf, insn##op, b, a, c);             \
283         }
284
285 #define I_u3u1u2(op)                                            \
286         static inline void i##op(u32 **buf, unsigned int a,     \
287                 unsigned int b, unsigned int c)                 \
288         {                                                       \
289                 build_insn(buf, insn##op, b, c, a);             \
290         }
291
292 #define I_u1u2s3(op)                                            \
293         static inline void i##op(u32 **buf, unsigned int a,     \
294                 unsigned int b, signed int c)                   \
295         {                                                       \
296                 build_insn(buf, insn##op, a, b, c);             \
297         }
298
299 #define I_u2s3u1(op)                                            \
300         static inline void i##op(u32 **buf, unsigned int a,     \
301                 signed int b, unsigned int c)                   \
302         {                                                       \
303                 build_insn(buf, insn##op, c, a, b);             \
304         }
305
306 #define I_u2u1s3(op)                                            \
307         static inline void i##op(u32 **buf, unsigned int a,     \
308                 unsigned int b, signed int c)                   \
309         {                                                       \
310                 build_insn(buf, insn##op, b, a, c);             \
311         }
312
313 #define I_u1u2(op)                                              \
314         static inline void i##op(u32 **buf, unsigned int a,     \
315                 unsigned int b)                                 \
316         {                                                       \
317                 build_insn(buf, insn##op, a, b);                \
318         }
319
320 #define I_u1s2(op)                                              \
321         static inline void i##op(u32 **buf, unsigned int a,     \
322                 signed int b)                                   \
323         {                                                       \
324                 build_insn(buf, insn##op, a, b);                \
325         }
326
327 #define I_u1(op)                                                \
328         static inline void i##op(u32 **buf, unsigned int a)     \
329         {                                                       \
330                 build_insn(buf, insn##op, a);                   \
331         }
332
333 #define I_0(op)                                                 \
334         static inline void i##op(u32 **buf)                     \
335         {                                                       \
336                 build_insn(buf, insn##op);                      \
337         }
338
339 I_u2u1s3(_addiu);
340 I_u3u1u2(_addu);
341 I_u2u1u3(_andi);
342 I_u3u1u2(_and);
343 I_u1u2s3(_beq);
344 I_u1s2(_bgez);
345 I_u1s2(_bgezl);
346 I_u1s2(_bltz);
347 I_u1s2(_bltzl);
348 I_u1u2s3(_bne);
349 I_u1u2(_dmfc0);
350 I_u1u2(_dmtc0);
351 I_u2u1s3(_daddiu);
352 I_u3u1u2(_daddu);
353 I_u2u1u3(_dsll);
354 I_u2u1u3(_dsll32);
355 I_u2u1u3(_dsra);
356 I_u2u1u3(_dsrl);
357 I_u2u1u3(_dsrl32);
358 I_u3u1u2(_dsubu);
359 I_0(_eret);
360 I_u1(_j);
361 I_u1(_jal);
362 I_u1(_jr);
363 I_u2s3u1(_ld);
364 I_u1s2(_lui);
365 I_u2s3u1(_lw);
366 I_u1u2(_mfc0);
367 I_u1u2(_mtc0);
368 I_u2u1u3(_ori);
369 I_0(_rfe);
370 I_u2s3u1(_sd);
371 I_u2u1u3(_sll);
372 I_u2u1u3(_sra);
373 I_u2u1u3(_srl);
374 I_u3u1u2(_subu);
375 I_u2s3u1(_sw);
376 I_0(_tlbp);
377 I_0(_tlbwi);
378 I_0(_tlbwr);
379 I_u3u1u2(_xor)
380 I_u2u1u3(_xori);
381
382 /*
383  * handling labels
384  */
385
386 enum label_id {
387         label_invalid,
388         label_second_part,
389         label_leave,
390         label_vmalloc,
391         label_vmalloc_done,
392         label_tlbwr_hazard,
393         label_split
394 };
395
396 struct label {
397         u32 *addr;
398         enum label_id lab;
399 };
400
401 static __init void build_label(struct label **lab, u32 *addr,
402                                enum label_id l)
403 {
404         (*lab)->addr = addr;
405         (*lab)->lab = l;
406         (*lab)++;
407 }
408
409 #define L_LA(lb)                                                \
410         static inline void l##lb(struct label **lab, u32 *addr) \
411         {                                                       \
412                 build_label(lab, addr, label##lb);              \
413         }
414
415 L_LA(_second_part)
416 L_LA(_leave)
417 L_LA(_vmalloc)
418 L_LA(_vmalloc_done)
419 L_LA(_tlbwr_hazard)
420 L_LA(_split)
421
422 /* convenience macros for instructions */
423 #ifdef CONFIG_MIPS64
424 # define i_LW(buf, rs, rt, off) i_ld(buf, rs, rt, off)
425 # define i_SW(buf, rs, rt, off) i_sd(buf, rs, rt, off)
426 # define i_SLL(buf, rs, rt, sh) i_dsll(buf, rs, rt, sh)
427 # define i_SRA(buf, rs, rt, sh) i_dsra(buf, rs, rt, sh)
428 # define i_SRL(buf, rs, rt, sh) i_dsrl(buf, rs, rt, sh)
429 # define i_MFC0(buf, rt, rd) i_dmfc0(buf, rt, rd)
430 # define i_MTC0(buf, rt, rd) i_dmtc0(buf, rt, rd)
431 # define i_ADDIU(buf, rs, rt, val) i_daddiu(buf, rs, rt, val)
432 # define i_ADDU(buf, rs, rt, rd) i_daddu(buf, rs, rt, rd)
433 # define i_SUBU(buf, rs, rt, rd) i_dsubu(buf, rs, rt, rd)
434 #else
435 # define i_LW(buf, rs, rt, off) i_lw(buf, rs, rt, off)
436 # define i_SW(buf, rs, rt, off) i_sw(buf, rs, rt, off)
437 # define i_SLL(buf, rs, rt, sh) i_sll(buf, rs, rt, sh)
438 # define i_SRA(buf, rs, rt, sh) i_sra(buf, rs, rt, sh)
439 # define i_SRL(buf, rs, rt, sh) i_srl(buf, rs, rt, sh)
440 # define i_MFC0(buf, rt, rd) i_mfc0(buf, rt, rd)
441 # define i_MTC0(buf, rt, rd) i_mtc0(buf, rt, rd)
442 # define i_ADDIU(buf, rs, rt, val) i_addiu(buf, rs, rt, val)
443 # define i_ADDU(buf, rs, rt, rd) i_addu(buf, rs, rt, rd)
444 # define i_SUBU(buf, rs, rt, rd) i_subu(buf, rs, rt, rd)
445 #endif
446
447 #define i_b(buf, off) i_beq(buf, 0, 0, off)
448 #define i_bnez(buf, rs, off) i_bne(buf, rs, 0, off)
449 #define i_move(buf, a, b) i_ADDU(buf, a, 0, b)
450 #define i_nop(buf) i_sll(buf, 0, 0, 0)
451 #define i_ssnop(buf) i_sll(buf, 0, 0, 1)
452 #define i_ehb(buf) i_sll(buf, 0, 0, 3)
453
454 #if CONFIG_MIPS64
455 static __init int in_compat_space_p(long addr)
456 {
457         /* Is this address in 32bit compat space? */
458         return (((addr) & 0xffffffff00000000) == 0xffffffff00000000);
459 }
460
461 static __init int rel_highest(long val)
462 {
463         return ((((val + 0x800080008000L) >> 48) & 0xffff) ^ 0x8000) - 0x8000;
464 }
465
466 static __init int rel_higher(long val)
467 {
468         return ((((val + 0x80008000L) >> 32) & 0xffff) ^ 0x8000) - 0x8000;
469 }
470 #endif
471
472 static __init int rel_hi(long val)
473 {
474         return ((((val + 0x8000L) >> 16) & 0xffff) ^ 0x8000) - 0x8000;
475 }
476
477 static __init int rel_lo(long val)
478 {
479         return ((val & 0xffff) ^ 0x8000) - 0x8000;
480 }
481
482 static __init void i_LA_mostly(u32 **buf, unsigned int rs, long addr)
483 {
484 #if CONFIG_MIPS64
485         if (!in_compat_space_p(addr)) {
486                 i_lui(buf, rs, rel_highest(addr));
487                 if (rel_higher(addr))
488                         i_daddiu(buf, rs, rs, rel_higher(addr));
489                 if (rel_hi(addr)) {
490                         i_dsll(buf, rs, rs, 16);
491                         i_daddiu(buf, rs, rs, rel_hi(addr));
492                         i_dsll(buf, rs, rs, 16);
493                 } else
494                         i_dsll32(buf, rs, rs, 0);
495         } else
496 #endif
497                 i_lui(buf, rs, rel_hi(addr));
498 }
499
500 static __init void __attribute__((unused)) i_LA(u32 **buf, unsigned int rs,
501                                                 long addr)
502 {
503         i_LA_mostly(buf, rs, addr);
504         if (rel_lo(addr))
505                 i_ADDIU(buf, rs, rs, rel_lo(addr));
506 }
507
508 /*
509  * handle relocations
510  */
511
512 struct reloc {
513         u32 *addr;
514         unsigned int type;
515         enum label_id lab;
516 };
517
518 static __init void r_mips_pc16(struct reloc **rel, u32 *addr,
519                                enum label_id l)
520 {
521         (*rel)->addr = addr;
522         (*rel)->type = R_MIPS_PC16;
523         (*rel)->lab = l;
524         (*rel)++;
525 }
526
527 static inline void __resolve_relocs(struct reloc *rel, struct label *lab)
528 {
529         long laddr = (long)lab->addr;
530         long raddr = (long)rel->addr;
531
532         switch (rel->type) {
533         case R_MIPS_PC16:
534                 *rel->addr |= build_bimm(laddr - (raddr + 4));
535                 break;
536
537         default:
538                 panic("Unsupported TLB synthesizer relocation %d",
539                       rel->type);
540         }
541 }
542
543 static __init void resolve_relocs(struct reloc *rel, struct label *lab)
544 {
545         struct label *l;
546
547         for (; rel->lab != label_invalid; rel++)
548                 for (l = lab; l->lab != label_invalid; l++)
549                         if (rel->lab == l->lab)
550                                 __resolve_relocs(rel, l);
551 }
552
553 static __init void copy_handler(struct reloc *rel, struct label *lab,
554                                 u32 *first, u32 *end, u32* target)
555 {
556         long off = (long)(target - first);
557
558         memcpy(target, first, (end - first) * sizeof(u32));
559
560         for (; rel->lab != label_invalid; rel++)
561                 if (rel->addr >= first && rel->addr < end)
562                         rel->addr += off;
563
564         for (; lab->lab != label_invalid; lab++)
565                 if (lab->addr >= first && lab->addr < end)
566                         lab->addr += off;
567 }
568
569 static __init int __attribute__((unused)) insn_has_bdelay(struct reloc *rel,
570                                                           u32 *addr)
571 {
572         for (; rel->lab != label_invalid; rel++) {
573                 if (rel->addr == addr
574                     && (rel->type == R_MIPS_PC16
575                         || rel->type == R_MIPS_26))
576                         return 1;
577         }
578
579         return 0;
580 }
581
582 /* convenience functions for labeled branches */
583 static void __attribute__((unused)) il_bltz(u32 **p, struct reloc **r,
584                                             unsigned int reg, enum label_id l)
585 {
586         r_mips_pc16(r, *p, l);
587         i_bltz(p, reg, 0);
588 }
589
590 static void __attribute__((unused)) il_b(u32 **p, struct reloc **r,
591                                          enum label_id l)
592 {
593         r_mips_pc16(r, *p, l);
594         i_b(p, 0);
595 }
596
597 static void il_bnez(u32 **p, struct reloc **r, unsigned int reg,
598                     enum label_id l)
599 {
600         r_mips_pc16(r, *p, l);
601         i_bnez(p, reg, 0);
602 }
603
604 static void il_bgezl(u32 **p, struct reloc **r, unsigned int reg,
605                      enum label_id l)
606 {
607         r_mips_pc16(r, *p, l);
608         i_bgezl(p, reg, 0);
609 }
610
611 /* The only registers allowed in TLB handlers. */
612 #define K0              26
613 #define K1              27
614
615 /* Some CP0 registers */
616 #define C0_INDEX        0
617 #define C0_ENTRYLO0     2
618 #define C0_ENTRYLO1     3
619 #define C0_CONTEXT      4
620 #define C0_BADVADDR     8
621 #define C0_ENTRYHI      10
622 #define C0_EPC          14
623 #define C0_XCONTEXT     20
624
625 #ifdef CONFIG_MIPS64
626 # define GET_CONTEXT(buf, reg) i_MFC0(buf, reg, C0_XCONTEXT)
627 #else
628 # define GET_CONTEXT(buf, reg) i_MFC0(buf, reg, C0_CONTEXT)
629 #endif
630
631 /* The worst case length of the handler is around 18 instructions for
632  * R3000-style TLBs and up to 63 instructions for R4000-style TLBs.
633  * Maximum space available is 32 instructions for R3000 and 64
634  * instructions for R4000.
635  *
636  * We deliberately chose a buffer size of 128, so we won't scribble
637  * over anything important on overflow before we panic.
638  */
639 static __initdata u32 tlb_handler[128];
640
641 /* simply assume worst case size for labels and relocs */
642 static __initdata struct label labels[128];
643 static __initdata struct reloc relocs[128];
644
645 #ifdef CONFIG_MIPS32
646 /*
647  * The R3000 TLB handler is simple.
648  */
649 static void __init build_r3000_tlb_refill_handler(void)
650 {
651         long pgdc = (long)pgd_current;
652         u32 *p;
653
654         memset(tlb_handler, 0, sizeof(tlb_handler));
655         p = tlb_handler;
656
657         i_mfc0(&p, K0, C0_BADVADDR);
658         i_lui(&p, K1, rel_hi(pgdc)); /* cp0 delay */
659         i_lw(&p, K1, rel_lo(pgdc), K1);
660         i_srl(&p, K0, K0, 22); /* load delay */
661         i_sll(&p, K0, K0, 2);
662         i_addu(&p, K1, K1, K0);
663         i_mfc0(&p, K0, C0_CONTEXT);
664         i_lw(&p, K1, 0, K1); /* cp0 delay */
665         i_andi(&p, K0, K0, 0xffc); /* load delay */
666         i_addu(&p, K1, K1, K0);
667         i_lw(&p, K0, 0, K1);
668         i_nop(&p); /* load delay */
669         i_mtc0(&p, K0, C0_ENTRYLO0);
670         i_mfc0(&p, K1, C0_EPC); /* cp0 delay */
671         i_tlbwr(&p); /* cp0 delay */
672         i_jr(&p, K1);
673         i_rfe(&p); /* branch delay */
674
675         if (p > tlb_handler + 32)
676                 panic("TLB refill handler space exceeded");
677
678         printk("Synthesized TLB handler (%u instructions).\n",
679                p - tlb_handler);
680 #ifdef DEBUG_TLB
681         {
682                 int i;
683                 for (i = 0; i < (p - tlb_handler); i++)
684                         printk("%08x\n", tlb_handler[i]);
685         }
686 #endif
687
688         memcpy((void *)CAC_BASE, tlb_handler, 0x80);
689         flush_icache_range(CAC_BASE, CAC_BASE + 0x80);
690 }
691 #endif /* CONFIG_MIPS32 */
692
693 /*
694  * The R4000 TLB handler is much more complicated. We have two
695  * consecutive handler areas with 32 instructions space each.
696  * Since they aren't used at the same time, we can overflow in the
697  * other one.To keep things simple, we first assume linear space,
698  * then we relocate it to the final handler layout as needed.
699  */
700 static __initdata u32 final_handler[64];
701
702 /*
703  * Hazards
704  *
705  * From the IDT errata for the QED RM5230 (Nevada), processor revision 1.0:
706  * 2. A timing hazard exists for the TLBP instruction.
707  *
708  *      stalling_instruction
709  *      TLBP
710  *
711  * The JTLB is being read for the TLBP throughout the stall generated by the
712  * previous instruction. This is not really correct as the stalling instruction
713  * can modify the address used to access the JTLB.  The failure symptom is that
714  * the TLBP instruction will use an address created for the stalling instruction
715  * and not the address held in C0_ENHI and thus report the wrong results.
716  *
717  * The software work-around is to not allow the instruction preceding the TLBP
718  * to stall - make it an NOP or some other instruction guaranteed not to stall.
719  *
720  * Errata 2 will not be fixed.  This errata is also on the R5000.
721  *
722  * As if we MIPS hackers wouldn't know how to nop pipelines happy ...
723  */
724 static __init void __attribute__((unused)) build_tlb_probe_entry(u32 **p)
725 {
726         switch (current_cpu_data.cputype) {
727         case CPU_R5000:
728         case CPU_R5000A:
729         case CPU_NEVADA:
730                 i_nop(p);
731                 i_tlbp(p);
732                 break;
733
734         default:
735                 i_tlbp(p);
736                 break;
737         }
738 }
739
740 /*
741  * Write random TLB entry, and care about the hazards from the
742  * preceeding mtc0 and for the following eret.
743  */
744 static __init void build_tlb_write_random_entry(u32 **p, struct label **l,
745                                                 struct reloc **r)
746 {
747         switch (current_cpu_data.cputype) {
748         case CPU_R4000PC:
749         case CPU_R4000SC:
750         case CPU_R4000MC:
751         case CPU_R4400PC:
752         case CPU_R4400SC:
753         case CPU_R4400MC:
754                 /*
755                  * This branch uses up a mtc0 hazard nop slot and saves
756                  * two nops after the tlbwr.
757                  */
758                 il_bgezl(p, r, 0, label_tlbwr_hazard);
759                 i_tlbwr(p);
760                 l_tlbwr_hazard(l, *p);
761                 i_nop(p);
762                 break;
763
764         case CPU_R4600:
765         case CPU_R4700:
766         case CPU_R5000:
767         case CPU_R5000A:
768         case CPU_5KC:
769         case CPU_AU1000:
770         case CPU_AU1100:
771         case CPU_AU1500:
772         case CPU_AU1550:
773                 i_nop(p);
774                 i_tlbwr(p);
775                 break;
776
777         case CPU_R10000:
778         case CPU_R12000:
779         case CPU_4KC:
780         case CPU_SB1:
781         case CPU_4KSC:
782         case CPU_20KC:
783         case CPU_25KF:
784                 i_tlbwr(p);
785                 break;
786
787         case CPU_NEVADA:
788                 i_nop(p); /* QED specifies 2 nops hazard */
789                 /*
790                  * This branch uses up a mtc0 hazard nop slot and saves
791                  * a nop after the tlbwr.
792                  */
793                 il_bgezl(p, r, 0, label_tlbwr_hazard);
794                 i_tlbwr(p);
795                 l_tlbwr_hazard(l, *p);
796                 break;
797
798         case CPU_4KEC:
799         case CPU_24K:
800                 i_ehb(p);
801                 i_tlbwr(p);
802                 break;
803
804         case CPU_RM9000:
805                 /*
806                  * When the JTLB is updated by tlbwi or tlbwr, a subsequent
807                  * use of the JTLB for instructions should not occur for 4
808                  * cpu cycles and use for data translations should not occur
809                  * for 3 cpu cycles.
810                  */
811                 i_ssnop(p);
812                 i_ssnop(p);
813                 i_ssnop(p);
814                 i_ssnop(p);
815                 i_tlbwr(p);
816                 i_ssnop(p);
817                 i_ssnop(p);
818                 i_ssnop(p);
819                 i_ssnop(p);
820                 break;
821
822         default:
823                 panic("No TLB refill handler yet (CPU type: %d)",
824                       current_cpu_data.cputype);
825                 break;
826         }
827 }
828
829 #if CONFIG_MIPS64
830 /*
831  * TMP and PTR are scratch.
832  * TMP will be clobbered, PTR will hold the pmd entry.
833  */
834 static __init void
835 build_get_pmde64(u32 **p, struct label **l, struct reloc **r,
836                  unsigned int tmp, unsigned int ptr)
837 {
838         long pgdc = (long)pgd_current;
839
840         /*
841          * The vmalloc handling is not in the hotpath.
842          */
843         i_dmfc0(p, tmp, C0_BADVADDR);
844         il_bltz(p, r, tmp, label_vmalloc);
845         /* No i_nop needed here, since the next insn doesn't touch TMP. */
846
847 # ifdef CONFIG_SMP
848         /*
849          * 64 bit SMP has the lower part of &pgd_current[smp_processor_id()]
850          * stored in CONTEXT.
851          */
852         if (in_compat_space_p(pgdc)) {
853                 i_dmfc0(p, ptr, C0_CONTEXT);
854                 i_dsra(p, ptr, ptr, 23);
855         } else {
856                 i_dmfc0(p, ptr, C0_CONTEXT);
857                 i_lui(p, tmp, rel_highest(pgdc));
858                 i_dsll(p, ptr, ptr, 9);
859                 i_daddiu(p, tmp, tmp, rel_higher(pgdc));
860                 i_dsrl32(p, ptr, ptr, 0);
861                 i_and(p, ptr, ptr, tmp);
862                 i_dmfc0(p, tmp, C0_BADVADDR);
863         }
864         i_ld(p, ptr, 0, ptr);
865 # else
866         i_LA_mostly(p, ptr, pgdc);
867         i_ld(p, ptr, rel_lo(pgdc), ptr);
868 # endif
869
870         l_vmalloc_done(l, *p);
871         i_dsrl(p, tmp, tmp, PGDIR_SHIFT-3); /* get pgd offset in bytes */
872         i_andi(p, tmp, tmp, (PTRS_PER_PGD - 1)<<3);
873         i_daddu(p, ptr, ptr, tmp); /* add in pgd offset */
874         i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
875         i_ld(p, ptr, 0, ptr); /* get pmd pointer */
876         i_dsrl(p, tmp, tmp, PMD_SHIFT-3); /* get pmd offset in bytes */
877         i_andi(p, tmp, tmp, (PTRS_PER_PMD - 1)<<3);
878         i_daddu(p, ptr, ptr, tmp); /* add in pmd offset */
879 }
880
881 /*
882  * BVADDR is the faulting address, PTR is scratch.
883  * PTR will hold the pgd for vmalloc.
884  */
885 static __init void
886 build_get_pgd_vmalloc64(u32 **p, struct label **l, struct reloc **r,
887                         unsigned int bvaddr, unsigned int ptr)
888 {
889         long swpd = (long)swapper_pg_dir;
890
891         l_vmalloc(l, *p);
892         i_LA(p, ptr, VMALLOC_START);
893         i_dsubu(p, bvaddr, bvaddr, ptr);
894
895         if (in_compat_space_p(swpd) && !rel_lo(swpd)) {
896                 il_b(p, r, label_vmalloc_done);
897                 i_lui(p, ptr, rel_hi(swpd));
898         } else {
899                 i_LA_mostly(p, ptr, swpd);
900                 il_b(p, r, label_vmalloc_done);
901                 i_daddiu(p, ptr, ptr, rel_lo(swpd));
902         }
903 }
904
905 #else /* CONFIG_MIPS32 */
906
907 /*
908  * TMP and PTR are scratch.
909  * TMP will be clobbered, PTR will hold the pgd entry.
910  */
911 static __init void build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
912 {
913         long pgdc = (long)pgd_current;
914
915         /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
916 #ifdef CONFIG_SMP
917         i_mfc0(p, ptr, C0_CONTEXT);
918         i_LA_mostly(p, tmp, pgdc);
919         i_srl(p, ptr, ptr, 23);
920         i_sll(p, ptr, ptr, 2);
921         i_addu(p, ptr, tmp, ptr);
922 #else
923         i_LA_mostly(p, ptr, pgdc);
924 #endif
925         i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
926         i_lw(p, ptr, rel_lo(pgdc), ptr);
927         i_srl(p, tmp, tmp, PGDIR_SHIFT); /* get pgd only bits */
928         i_sll(p, tmp, tmp, PGD_T_LOG2);
929         i_addu(p, ptr, ptr, tmp); /* add in pgd offset */
930 }
931 #endif /* CONFIG_MIPS32 */
932
933 static __init void build_adjust_context(u32 **p, unsigned int ctx)
934 {
935         unsigned int shift = 0;
936         unsigned int mask = 0xff0;
937
938 #if !defined(CONFIG_MIPS64) && !defined(CONFIG_64BIT_PHYS_ADDR)
939         shift++;
940         mask |= 0x008;
941 #endif
942
943         switch (current_cpu_data.cputype) {
944         case CPU_VR41XX:
945         case CPU_VR4111:
946         case CPU_VR4121:
947         case CPU_VR4122:
948         case CPU_VR4131:
949         case CPU_VR4181:
950         case CPU_VR4181A:
951         case CPU_VR4133:
952                 shift += 2;
953                 break;
954
955         default:
956                 break;
957         }
958
959         if (shift)
960                 i_SRL(p, ctx, ctx, shift);
961         i_andi(p, ctx, ctx, mask);
962 }
963
964 static __init void build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
965 {
966         /*
967          * Bug workaround for the Nevada. It seems as if under certain
968          * circumstances the move from cp0_context might produce a
969          * bogus result when the mfc0 instruction and its consumer are
970          * in a different cacheline or a load instruction, probably any
971          * memory reference, is between them.
972          */
973         switch (current_cpu_data.cputype) {
974         case CPU_NEVADA:
975                 i_LW(p, ptr, 0, ptr);
976                 GET_CONTEXT(p, tmp); /* get context reg */
977                 break;
978
979         default:
980                 GET_CONTEXT(p, tmp); /* get context reg */
981                 i_LW(p, ptr, 0, ptr);
982                 break;
983         }
984
985         build_adjust_context(p, tmp);
986         i_ADDU(p, ptr, ptr, tmp); /* add in offset */
987 }
988
989 static __init void build_update_entries(u32 **p, unsigned int tmp,
990                                         unsigned int ptep)
991 {
992         /*
993          * 64bit address support (36bit on a 32bit CPU) in a 32bit
994          * Kernel is a special case. Only a few CPUs use it.
995          */
996 #ifdef CONFIG_64BIT_PHYS_ADDR
997         if (cpu_has_64bit_gp_regs) {
998                 i_ld(p, tmp, 0, ptep); /* get even pte */
999                 i_ld(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
1000                 i_dsrl(p, tmp, tmp, 6); /* convert to entrylo0 */
1001                 i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
1002                 i_dsrl(p, ptep, ptep, 6); /* convert to entrylo1 */
1003                 i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
1004         } else {
1005                 int pte_off_even = sizeof(pte_t) / 2;
1006                 int pte_off_odd = pte_off_even + sizeof(pte_t);
1007
1008                 /* The pte entries are pre-shifted */
1009                 i_lw(p, tmp, pte_off_even, ptep); /* get even pte */
1010                 i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
1011                 i_lw(p, ptep, pte_off_odd, ptep); /* get odd pte */
1012                 i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
1013         }
1014 #else
1015         i_LW(p, tmp, 0, ptep); /* get even pte */
1016         i_LW(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
1017         if (r45k_bvahwbug())
1018                 build_tlb_probe_entry(p);
1019         i_SRL(p, tmp, tmp, 6); /* convert to entrylo0 */
1020         if (r4k_250MHZhwbug())
1021                 i_mtc0(p, 0, C0_ENTRYLO0);
1022         i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
1023         i_SRL(p, ptep, ptep, 6); /* convert to entrylo1 */
1024         if (r45k_bvahwbug())
1025                 i_mfc0(p, tmp, C0_INDEX);
1026         if (r4k_250MHZhwbug())
1027                 i_mtc0(p, 0, C0_ENTRYLO1);
1028         i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
1029 #endif
1030 }
1031
1032 static void __init build_r4000_tlb_refill_handler(void)
1033 {
1034         u32 *p = tlb_handler;
1035         struct label *l = labels;
1036         struct reloc *r = relocs;
1037         u32 *f;
1038         unsigned int final_len;
1039
1040         memset(tlb_handler, 0, sizeof(tlb_handler));
1041         memset(labels, 0, sizeof(labels));
1042         memset(relocs, 0, sizeof(relocs));
1043         memset(final_handler, 0, sizeof(final_handler));
1044
1045         /*
1046          * create the plain linear handler
1047          */
1048         if (bcm1250_m3_war()) {
1049                 i_MFC0(&p, K0, C0_BADVADDR);
1050                 i_MFC0(&p, K1, C0_ENTRYHI);
1051                 i_xor(&p, K0, K0, K1);
1052                 i_SRL(&p, K0, K0, PAGE_SHIFT+1);
1053                 il_bnez(&p, &r, K0, label_leave);
1054                 /* No need for i_nop */
1055         }
1056
1057 #ifdef CONFIG_MIPS64
1058         build_get_pmde64(&p, &l, &r, K0, K1); /* get pmd ptr in K1 */
1059 #else
1060         build_get_pgde32(&p, K0, K1); /* get pgd ptr in K1 */
1061 #endif
1062
1063         build_get_ptep(&p, K0, K1);
1064         build_update_entries(&p, K0, K1);
1065         build_tlb_write_random_entry(&p, &l, &r);
1066         l_leave(&l, p);
1067         i_eret(&p); /* return from trap */
1068
1069 #ifdef CONFIG_MIPS64
1070         build_get_pgd_vmalloc64(&p, &l, &r, K0, K1);
1071 #endif
1072
1073         /*
1074          * Overflow check: For the 64bit handler, we need at least one
1075          * free instruction slot for the wrap-around branch. In worst
1076          * case, if the intended insertion point is a delay slot, we
1077          * need three, with the the second nop'ed and the third being
1078          * unused.
1079          */
1080 #ifdef CONFIG_MIPS32
1081         if ((p - tlb_handler) > 64)
1082                 panic("TLB refill handler space exceeded");
1083 #else
1084         if (((p - tlb_handler) > 63)
1085             || (((p - tlb_handler) > 61)
1086                 && insn_has_bdelay(relocs, tlb_handler + 29)))
1087                 panic("TLB refill handler space exceeded");
1088 #endif
1089
1090         /*
1091          * Now fold the handler in the TLB refill handler space.
1092          */
1093 #ifdef CONFIG_MIPS32
1094         f = final_handler;
1095         /* Simplest case, just copy the handler. */
1096         copy_handler(relocs, labels, tlb_handler, p, f);
1097         final_len = p - tlb_handler;
1098 #else /* CONFIG_MIPS64 */
1099         f = final_handler + 32;
1100         if ((p - tlb_handler) <= 32) {
1101                 /* Just copy the handler. */
1102                 copy_handler(relocs, labels, tlb_handler, p, f);
1103                 final_len = p - tlb_handler;
1104         } else {
1105                 u32 *split = tlb_handler + 30;
1106
1107                 /*
1108                  * Find the split point.
1109                  */
1110                 if (insn_has_bdelay(relocs, split - 1))
1111                         split--;
1112
1113                 /* Copy first part of the handler. */
1114                 copy_handler(relocs, labels, tlb_handler, split, f);
1115                 f += split - tlb_handler;
1116
1117                 /* Insert branch. */
1118                 l_split(&l, final_handler);
1119                 il_b(&f, &r, label_split);
1120                 if (insn_has_bdelay(relocs, split))
1121                         i_nop(&f);
1122                 else {
1123                         copy_handler(relocs, labels, split, split + 1, f);
1124                         f++;
1125                         split++;
1126                 }
1127
1128                 /* Copy the rest of the handler. */
1129                 copy_handler(relocs, labels, split, p, final_handler);
1130                 final_len = (f - (final_handler + 32)) + (p - split);
1131         }
1132 #endif /* CONFIG_MIPS64 */
1133
1134         resolve_relocs(relocs, labels);
1135         printk("Synthesized TLB handler (%u instructions).\n", final_len);
1136
1137 #ifdef DEBUG_TLB
1138         {
1139                 int i;
1140
1141                 for (i = 0; i < 64; i++)
1142                         printk("%08x\n", final_handler[i]);
1143         }
1144 #endif
1145
1146         memcpy((void *)CAC_BASE, final_handler, 0x100);
1147         flush_icache_range(CAC_BASE, CAC_BASE + 0x100);
1148 }
1149
1150 void __init build_tlb_refill_handler(void)
1151 {
1152         switch (current_cpu_data.cputype) {
1153 #ifdef CONFIG_MIPS32
1154         case CPU_R2000:
1155         case CPU_R3000:
1156         case CPU_R3000A:
1157         case CPU_R3081E:
1158         case CPU_TX3912:
1159         case CPU_TX3922:
1160         case CPU_TX3927:
1161                 build_r3000_tlb_refill_handler();
1162                 break;
1163
1164         case CPU_R6000:
1165         case CPU_R6000A:
1166                 panic("No R6000 TLB refill handler yet");
1167                 break;
1168 #endif
1169
1170         case CPU_R8000:
1171                 panic("No R8000 TLB refill handler yet");
1172                 break;
1173
1174         default:
1175                 build_r4000_tlb_refill_handler();
1176         }
1177 }