MIPS: TXx9: Add ACLC support
[deliverable/linux.git] / arch / mips / mm / tlbex.c
CommitLineData
1da177e4
LT
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 *
e30ec452 8 * Copyright (C) 2004, 2005, 2006, 2008 Thiemo Seufer
619b6e18 9 * Copyright (C) 2005, 2007 Maciej W. Rozycki
41c594ab
RB
10 * 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)
1da177e4
LT
20 */
21
1da177e4
LT
22#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/init.h>
26
1da177e4 27#include <asm/mmu_context.h>
1da177e4
LT
28#include <asm/war.h>
29
e30ec452
TS
30#include "uasm.h"
31
aeffdbba 32static inline int r45k_bvahwbug(void)
1da177e4
LT
33{
34 /* XXX: We should probe for the presence of this bug, but we don't. */
35 return 0;
36}
37
aeffdbba 38static inline int r4k_250MHZhwbug(void)
1da177e4
LT
39{
40 /* XXX: We should probe for the presence of this bug, but we don't. */
41 return 0;
42}
43
aeffdbba 44static inline int __maybe_unused bcm1250_m3_war(void)
1da177e4
LT
45{
46 return BCM1250_M3_WAR;
47}
48
aeffdbba 49static inline int __maybe_unused r10000_llsc_war(void)
1da177e4
LT
50{
51 return R10000_LLSC_WAR;
52}
53
8df5beac
MR
54/*
55 * Found by experiment: At least some revisions of the 4kc throw under
56 * some circumstances a machine check exception, triggered by invalid
57 * values in the index register. Delaying the tlbp instruction until
58 * after the next branch, plus adding an additional nop in front of
59 * tlbwi/tlbwr avoids the invalid index register values. Nobody knows
60 * why; it's not an issue caused by the core RTL.
61 *
62 */
234fcd14 63static int __cpuinit m4kc_tlbp_war(void)
8df5beac
MR
64{
65 return (current_cpu_data.processor_id & 0xffff00) ==
66 (PRID_COMP_MIPS | PRID_IMP_4KC);
67}
68
e30ec452 69/* Handle labels (which must be positive integers). */
1da177e4 70enum label_id {
e30ec452 71 label_second_part = 1,
1da177e4 72 label_leave,
656be92f
AN
73#ifdef MODULE_START
74 label_module_alloc,
75#endif
1da177e4
LT
76 label_vmalloc,
77 label_vmalloc_done,
78 label_tlbw_hazard,
79 label_split,
80 label_nopage_tlbl,
81 label_nopage_tlbs,
82 label_nopage_tlbm,
83 label_smp_pgtable_change,
84 label_r3000_write_probe_fail,
1da177e4
LT
85};
86
e30ec452
TS
87UASM_L_LA(_second_part)
88UASM_L_LA(_leave)
656be92f 89#ifdef MODULE_START
e30ec452 90UASM_L_LA(_module_alloc)
619b6e18 91#endif
e30ec452
TS
92UASM_L_LA(_vmalloc)
93UASM_L_LA(_vmalloc_done)
94UASM_L_LA(_tlbw_hazard)
95UASM_L_LA(_split)
96UASM_L_LA(_nopage_tlbl)
97UASM_L_LA(_nopage_tlbs)
98UASM_L_LA(_nopage_tlbm)
99UASM_L_LA(_smp_pgtable_change)
100UASM_L_LA(_r3000_write_probe_fail)
656be92f 101
92b1e6a6
FBH
102/*
103 * For debug purposes.
104 */
105static inline void dump_handler(const u32 *handler, int count)
106{
107 int i;
108
109 pr_debug("\t.set push\n");
110 pr_debug("\t.set noreorder\n");
111
112 for (i = 0; i < count; i++)
113 pr_debug("\t%p\t.word 0x%08x\n", &handler[i], handler[i]);
114
115 pr_debug("\t.set pop\n");
116}
117
1da177e4
LT
118/* The only general purpose registers allowed in TLB handlers. */
119#define K0 26
120#define K1 27
121
122/* Some CP0 registers */
41c594ab
RB
123#define C0_INDEX 0, 0
124#define C0_ENTRYLO0 2, 0
125#define C0_TCBIND 2, 2
126#define C0_ENTRYLO1 3, 0
127#define C0_CONTEXT 4, 0
128#define C0_BADVADDR 8, 0
129#define C0_ENTRYHI 10, 0
130#define C0_EPC 14, 0
131#define C0_XCONTEXT 20, 0
1da177e4 132
875d43e7 133#ifdef CONFIG_64BIT
e30ec452 134# define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_XCONTEXT)
1da177e4 135#else
e30ec452 136# define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_CONTEXT)
1da177e4
LT
137#endif
138
139/* The worst case length of the handler is around 18 instructions for
140 * R3000-style TLBs and up to 63 instructions for R4000-style TLBs.
141 * Maximum space available is 32 instructions for R3000 and 64
142 * instructions for R4000.
143 *
144 * We deliberately chose a buffer size of 128, so we won't scribble
145 * over anything important on overflow before we panic.
146 */
234fcd14 147static u32 tlb_handler[128] __cpuinitdata;
1da177e4
LT
148
149/* simply assume worst case size for labels and relocs */
234fcd14
RB
150static struct uasm_label labels[128] __cpuinitdata;
151static struct uasm_reloc relocs[128] __cpuinitdata;
1da177e4
LT
152
153/*
154 * The R3000 TLB handler is simple.
155 */
234fcd14 156static void __cpuinit build_r3000_tlb_refill_handler(void)
1da177e4
LT
157{
158 long pgdc = (long)pgd_current;
159 u32 *p;
160
161 memset(tlb_handler, 0, sizeof(tlb_handler));
162 p = tlb_handler;
163
e30ec452
TS
164 uasm_i_mfc0(&p, K0, C0_BADVADDR);
165 uasm_i_lui(&p, K1, uasm_rel_hi(pgdc)); /* cp0 delay */
166 uasm_i_lw(&p, K1, uasm_rel_lo(pgdc), K1);
167 uasm_i_srl(&p, K0, K0, 22); /* load delay */
168 uasm_i_sll(&p, K0, K0, 2);
169 uasm_i_addu(&p, K1, K1, K0);
170 uasm_i_mfc0(&p, K0, C0_CONTEXT);
171 uasm_i_lw(&p, K1, 0, K1); /* cp0 delay */
172 uasm_i_andi(&p, K0, K0, 0xffc); /* load delay */
173 uasm_i_addu(&p, K1, K1, K0);
174 uasm_i_lw(&p, K0, 0, K1);
175 uasm_i_nop(&p); /* load delay */
176 uasm_i_mtc0(&p, K0, C0_ENTRYLO0);
177 uasm_i_mfc0(&p, K1, C0_EPC); /* cp0 delay */
178 uasm_i_tlbwr(&p); /* cp0 delay */
179 uasm_i_jr(&p, K1);
180 uasm_i_rfe(&p); /* branch delay */
1da177e4
LT
181
182 if (p > tlb_handler + 32)
183 panic("TLB refill handler space exceeded");
184
e30ec452
TS
185 pr_debug("Wrote TLB refill handler (%u instructions).\n",
186 (unsigned int)(p - tlb_handler));
1da177e4 187
91b05e67 188 memcpy((void *)ebase, tlb_handler, 0x80);
92b1e6a6
FBH
189
190 dump_handler((u32 *)ebase, 32);
1da177e4
LT
191}
192
193/*
194 * The R4000 TLB handler is much more complicated. We have two
195 * consecutive handler areas with 32 instructions space each.
196 * Since they aren't used at the same time, we can overflow in the
197 * other one.To keep things simple, we first assume linear space,
198 * then we relocate it to the final handler layout as needed.
199 */
234fcd14 200static u32 final_handler[64] __cpuinitdata;
1da177e4
LT
201
202/*
203 * Hazards
204 *
205 * From the IDT errata for the QED RM5230 (Nevada), processor revision 1.0:
206 * 2. A timing hazard exists for the TLBP instruction.
207 *
208 * stalling_instruction
209 * TLBP
210 *
211 * The JTLB is being read for the TLBP throughout the stall generated by the
212 * previous instruction. This is not really correct as the stalling instruction
213 * can modify the address used to access the JTLB. The failure symptom is that
214 * the TLBP instruction will use an address created for the stalling instruction
215 * and not the address held in C0_ENHI and thus report the wrong results.
216 *
217 * The software work-around is to not allow the instruction preceding the TLBP
218 * to stall - make it an NOP or some other instruction guaranteed not to stall.
219 *
220 * Errata 2 will not be fixed. This errata is also on the R5000.
221 *
222 * As if we MIPS hackers wouldn't know how to nop pipelines happy ...
223 */
234fcd14 224static void __cpuinit __maybe_unused build_tlb_probe_entry(u32 **p)
1da177e4 225{
10cc3529 226 switch (current_cpu_type()) {
326e2e1a 227 /* Found by experiment: R4600 v2.0/R4700 needs this, too. */
f5b4d956 228 case CPU_R4600:
326e2e1a 229 case CPU_R4700:
1da177e4
LT
230 case CPU_R5000:
231 case CPU_R5000A:
232 case CPU_NEVADA:
e30ec452
TS
233 uasm_i_nop(p);
234 uasm_i_tlbp(p);
1da177e4
LT
235 break;
236
237 default:
e30ec452 238 uasm_i_tlbp(p);
1da177e4
LT
239 break;
240 }
241}
242
243/*
244 * Write random or indexed TLB entry, and care about the hazards from
245 * the preceeding mtc0 and for the following eret.
246 */
247enum tlb_write_entry { tlb_random, tlb_indexed };
248
234fcd14 249static void __cpuinit build_tlb_write_entry(u32 **p, struct uasm_label **l,
e30ec452 250 struct uasm_reloc **r,
1da177e4
LT
251 enum tlb_write_entry wmode)
252{
253 void(*tlbw)(u32 **) = NULL;
254
255 switch (wmode) {
e30ec452
TS
256 case tlb_random: tlbw = uasm_i_tlbwr; break;
257 case tlb_indexed: tlbw = uasm_i_tlbwi; break;
1da177e4
LT
258 }
259
161548bf 260 if (cpu_has_mips_r2) {
e30ec452 261 uasm_i_ehb(p);
161548bf
RB
262 tlbw(p);
263 return;
264 }
265
10cc3529 266 switch (current_cpu_type()) {
1da177e4
LT
267 case CPU_R4000PC:
268 case CPU_R4000SC:
269 case CPU_R4000MC:
270 case CPU_R4400PC:
271 case CPU_R4400SC:
272 case CPU_R4400MC:
273 /*
274 * This branch uses up a mtc0 hazard nop slot and saves
275 * two nops after the tlbw instruction.
276 */
e30ec452 277 uasm_il_bgezl(p, r, 0, label_tlbw_hazard);
1da177e4 278 tlbw(p);
e30ec452
TS
279 uasm_l_tlbw_hazard(l, *p);
280 uasm_i_nop(p);
1da177e4
LT
281 break;
282
283 case CPU_R4600:
284 case CPU_R4700:
285 case CPU_R5000:
286 case CPU_R5000A:
e30ec452 287 uasm_i_nop(p);
2c93e12c 288 tlbw(p);
e30ec452 289 uasm_i_nop(p);
2c93e12c
MR
290 break;
291
292 case CPU_R4300:
1da177e4
LT
293 case CPU_5KC:
294 case CPU_TX49XX:
bdf21b18 295 case CPU_PR4450:
e30ec452 296 uasm_i_nop(p);
1da177e4
LT
297 tlbw(p);
298 break;
299
300 case CPU_R10000:
301 case CPU_R12000:
44d921b2 302 case CPU_R14000:
1da177e4 303 case CPU_4KC:
b1ec4c8e 304 case CPU_4KEC:
1da177e4 305 case CPU_SB1:
93ce2f52 306 case CPU_SB1A:
1da177e4
LT
307 case CPU_4KSC:
308 case CPU_20KC:
309 case CPU_25KF:
1c0c13eb
AJ
310 case CPU_BCM3302:
311 case CPU_BCM4710:
2a21c730 312 case CPU_LOONGSON2:
ec454d8c 313 case CPU_CAVIUM_OCTEON:
a644b277 314 case CPU_R5500:
8df5beac 315 if (m4kc_tlbp_war())
e30ec452 316 uasm_i_nop(p);
2f794d09 317 case CPU_ALCHEMY:
1da177e4
LT
318 tlbw(p);
319 break;
320
321 case CPU_NEVADA:
e30ec452 322 uasm_i_nop(p); /* QED specifies 2 nops hazard */
1da177e4
LT
323 /*
324 * This branch uses up a mtc0 hazard nop slot and saves
325 * a nop after the tlbw instruction.
326 */
e30ec452 327 uasm_il_bgezl(p, r, 0, label_tlbw_hazard);
1da177e4 328 tlbw(p);
e30ec452 329 uasm_l_tlbw_hazard(l, *p);
1da177e4
LT
330 break;
331
332 case CPU_RM7000:
e30ec452
TS
333 uasm_i_nop(p);
334 uasm_i_nop(p);
335 uasm_i_nop(p);
336 uasm_i_nop(p);
1da177e4
LT
337 tlbw(p);
338 break;
339
1da177e4
LT
340 case CPU_RM9000:
341 /*
342 * When the JTLB is updated by tlbwi or tlbwr, a subsequent
343 * use of the JTLB for instructions should not occur for 4
344 * cpu cycles and use for data translations should not occur
345 * for 3 cpu cycles.
346 */
e30ec452
TS
347 uasm_i_ssnop(p);
348 uasm_i_ssnop(p);
349 uasm_i_ssnop(p);
350 uasm_i_ssnop(p);
1da177e4 351 tlbw(p);
e30ec452
TS
352 uasm_i_ssnop(p);
353 uasm_i_ssnop(p);
354 uasm_i_ssnop(p);
355 uasm_i_ssnop(p);
1da177e4
LT
356 break;
357
358 case CPU_VR4111:
359 case CPU_VR4121:
360 case CPU_VR4122:
361 case CPU_VR4181:
362 case CPU_VR4181A:
e30ec452
TS
363 uasm_i_nop(p);
364 uasm_i_nop(p);
1da177e4 365 tlbw(p);
e30ec452
TS
366 uasm_i_nop(p);
367 uasm_i_nop(p);
1da177e4
LT
368 break;
369
370 case CPU_VR4131:
371 case CPU_VR4133:
7623debf 372 case CPU_R5432:
e30ec452
TS
373 uasm_i_nop(p);
374 uasm_i_nop(p);
1da177e4
LT
375 tlbw(p);
376 break;
377
378 default:
379 panic("No TLB refill handler yet (CPU type: %d)",
380 current_cpu_data.cputype);
381 break;
382 }
383}
384
875d43e7 385#ifdef CONFIG_64BIT
1da177e4
LT
386/*
387 * TMP and PTR are scratch.
388 * TMP will be clobbered, PTR will hold the pmd entry.
389 */
234fcd14 390static void __cpuinit
e30ec452 391build_get_pmde64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
1da177e4
LT
392 unsigned int tmp, unsigned int ptr)
393{
394 long pgdc = (long)pgd_current;
395
396 /*
397 * The vmalloc handling is not in the hotpath.
398 */
e30ec452 399 uasm_i_dmfc0(p, tmp, C0_BADVADDR);
656be92f 400#ifdef MODULE_START
e30ec452 401 uasm_il_bltz(p, r, tmp, label_module_alloc);
656be92f 402#else
e30ec452 403 uasm_il_bltz(p, r, tmp, label_vmalloc);
656be92f 404#endif
e30ec452 405 /* No uasm_i_nop needed here, since the next insn doesn't touch TMP. */
1da177e4
LT
406
407#ifdef CONFIG_SMP
41c594ab
RB
408# ifdef CONFIG_MIPS_MT_SMTC
409 /*
410 * SMTC uses TCBind value as "CPU" index
411 */
e30ec452
TS
412 uasm_i_mfc0(p, ptr, C0_TCBIND);
413 uasm_i_dsrl(p, ptr, ptr, 19);
41c594ab 414# else
1da177e4 415 /*
1b3a6e97 416 * 64 bit SMP running in XKPHYS has smp_processor_id() << 3
1da177e4
LT
417 * stored in CONTEXT.
418 */
e30ec452
TS
419 uasm_i_dmfc0(p, ptr, C0_CONTEXT);
420 uasm_i_dsrl(p, ptr, ptr, 23);
41c594ab 421#endif
e30ec452
TS
422 UASM_i_LA_mostly(p, tmp, pgdc);
423 uasm_i_daddu(p, ptr, ptr, tmp);
424 uasm_i_dmfc0(p, tmp, C0_BADVADDR);
425 uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
1da177e4 426#else
e30ec452
TS
427 UASM_i_LA_mostly(p, ptr, pgdc);
428 uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
1da177e4
LT
429#endif
430
e30ec452 431 uasm_l_vmalloc_done(l, *p);
242954b5
RB
432
433 if (PGDIR_SHIFT - 3 < 32) /* get pgd offset in bytes */
e30ec452 434 uasm_i_dsrl(p, tmp, tmp, PGDIR_SHIFT-3);
242954b5 435 else
e30ec452
TS
436 uasm_i_dsrl32(p, tmp, tmp, PGDIR_SHIFT - 3 - 32);
437
438 uasm_i_andi(p, tmp, tmp, (PTRS_PER_PGD - 1)<<3);
439 uasm_i_daddu(p, ptr, ptr, tmp); /* add in pgd offset */
440 uasm_i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
441 uasm_i_ld(p, ptr, 0, ptr); /* get pmd pointer */
442 uasm_i_dsrl(p, tmp, tmp, PMD_SHIFT-3); /* get pmd offset in bytes */
443 uasm_i_andi(p, tmp, tmp, (PTRS_PER_PMD - 1)<<3);
444 uasm_i_daddu(p, ptr, ptr, tmp); /* add in pmd offset */
1da177e4
LT
445}
446
447/*
448 * BVADDR is the faulting address, PTR is scratch.
449 * PTR will hold the pgd for vmalloc.
450 */
234fcd14 451static void __cpuinit
e30ec452 452build_get_pgd_vmalloc64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
1da177e4
LT
453 unsigned int bvaddr, unsigned int ptr)
454{
455 long swpd = (long)swapper_pg_dir;
456
656be92f
AN
457#ifdef MODULE_START
458 long modd = (long)module_pg_dir;
459
e30ec452 460 uasm_l_module_alloc(l, *p);
656be92f
AN
461 /*
462 * Assumption:
463 * VMALLOC_START >= 0xc000000000000000UL
464 * MODULE_START >= 0xe000000000000000UL
465 */
e30ec452
TS
466 UASM_i_SLL(p, ptr, bvaddr, 2);
467 uasm_il_bgez(p, r, ptr, label_vmalloc);
656be92f 468
e30ec452
TS
469 if (uasm_in_compat_space_p(MODULE_START) &&
470 !uasm_rel_lo(MODULE_START)) {
471 uasm_i_lui(p, ptr, uasm_rel_hi(MODULE_START)); /* delay slot */
656be92f
AN
472 } else {
473 /* unlikely configuration */
e30ec452
TS
474 uasm_i_nop(p); /* delay slot */
475 UASM_i_LA(p, ptr, MODULE_START);
656be92f 476 }
e30ec452 477 uasm_i_dsubu(p, bvaddr, bvaddr, ptr);
656be92f 478
e30ec452
TS
479 if (uasm_in_compat_space_p(modd) && !uasm_rel_lo(modd)) {
480 uasm_il_b(p, r, label_vmalloc_done);
481 uasm_i_lui(p, ptr, uasm_rel_hi(modd));
656be92f 482 } else {
e30ec452
TS
483 UASM_i_LA_mostly(p, ptr, modd);
484 uasm_il_b(p, r, label_vmalloc_done);
485 if (uasm_in_compat_space_p(modd))
486 uasm_i_addiu(p, ptr, ptr, uasm_rel_lo(modd));
619b6e18 487 else
e30ec452 488 uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(modd));
656be92f
AN
489 }
490
e30ec452
TS
491 uasm_l_vmalloc(l, *p);
492 if (uasm_in_compat_space_p(MODULE_START) &&
493 !uasm_rel_lo(MODULE_START) &&
656be92f 494 MODULE_START << 32 == VMALLOC_START)
e30ec452 495 uasm_i_dsll32(p, ptr, ptr, 0); /* typical case */
656be92f 496 else
e30ec452 497 UASM_i_LA(p, ptr, VMALLOC_START);
656be92f 498#else
e30ec452
TS
499 uasm_l_vmalloc(l, *p);
500 UASM_i_LA(p, ptr, VMALLOC_START);
656be92f 501#endif
e30ec452 502 uasm_i_dsubu(p, bvaddr, bvaddr, ptr);
1da177e4 503
e30ec452
TS
504 if (uasm_in_compat_space_p(swpd) && !uasm_rel_lo(swpd)) {
505 uasm_il_b(p, r, label_vmalloc_done);
506 uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
1da177e4 507 } else {
e30ec452
TS
508 UASM_i_LA_mostly(p, ptr, swpd);
509 uasm_il_b(p, r, label_vmalloc_done);
510 if (uasm_in_compat_space_p(swpd))
511 uasm_i_addiu(p, ptr, ptr, uasm_rel_lo(swpd));
619b6e18 512 else
e30ec452 513 uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(swpd));
1da177e4
LT
514 }
515}
516
875d43e7 517#else /* !CONFIG_64BIT */
1da177e4
LT
518
519/*
520 * TMP and PTR are scratch.
521 * TMP will be clobbered, PTR will hold the pgd entry.
522 */
234fcd14 523static void __cpuinit __maybe_unused
1da177e4
LT
524build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
525{
526 long pgdc = (long)pgd_current;
527
528 /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
529#ifdef CONFIG_SMP
41c594ab
RB
530#ifdef CONFIG_MIPS_MT_SMTC
531 /*
532 * SMTC uses TCBind value as "CPU" index
533 */
e30ec452
TS
534 uasm_i_mfc0(p, ptr, C0_TCBIND);
535 UASM_i_LA_mostly(p, tmp, pgdc);
536 uasm_i_srl(p, ptr, ptr, 19);
41c594ab
RB
537#else
538 /*
539 * smp_processor_id() << 3 is stored in CONTEXT.
540 */
e30ec452
TS
541 uasm_i_mfc0(p, ptr, C0_CONTEXT);
542 UASM_i_LA_mostly(p, tmp, pgdc);
543 uasm_i_srl(p, ptr, ptr, 23);
41c594ab 544#endif
e30ec452 545 uasm_i_addu(p, ptr, tmp, ptr);
1da177e4 546#else
e30ec452 547 UASM_i_LA_mostly(p, ptr, pgdc);
1da177e4 548#endif
e30ec452
TS
549 uasm_i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
550 uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
551 uasm_i_srl(p, tmp, tmp, PGDIR_SHIFT); /* get pgd only bits */
552 uasm_i_sll(p, tmp, tmp, PGD_T_LOG2);
553 uasm_i_addu(p, ptr, ptr, tmp); /* add in pgd offset */
1da177e4
LT
554}
555
875d43e7 556#endif /* !CONFIG_64BIT */
1da177e4 557
234fcd14 558static void __cpuinit build_adjust_context(u32 **p, unsigned int ctx)
1da177e4 559{
242954b5 560 unsigned int shift = 4 - (PTE_T_LOG2 + 1) + PAGE_SHIFT - 12;
1da177e4
LT
561 unsigned int mask = (PTRS_PER_PTE / 2 - 1) << (PTE_T_LOG2 + 1);
562
10cc3529 563 switch (current_cpu_type()) {
1da177e4
LT
564 case CPU_VR41XX:
565 case CPU_VR4111:
566 case CPU_VR4121:
567 case CPU_VR4122:
568 case CPU_VR4131:
569 case CPU_VR4181:
570 case CPU_VR4181A:
571 case CPU_VR4133:
572 shift += 2;
573 break;
574
575 default:
576 break;
577 }
578
579 if (shift)
e30ec452
TS
580 UASM_i_SRL(p, ctx, ctx, shift);
581 uasm_i_andi(p, ctx, ctx, mask);
1da177e4
LT
582}
583
234fcd14 584static void __cpuinit build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
1da177e4
LT
585{
586 /*
587 * Bug workaround for the Nevada. It seems as if under certain
588 * circumstances the move from cp0_context might produce a
589 * bogus result when the mfc0 instruction and its consumer are
590 * in a different cacheline or a load instruction, probably any
591 * memory reference, is between them.
592 */
10cc3529 593 switch (current_cpu_type()) {
1da177e4 594 case CPU_NEVADA:
e30ec452 595 UASM_i_LW(p, ptr, 0, ptr);
1da177e4
LT
596 GET_CONTEXT(p, tmp); /* get context reg */
597 break;
598
599 default:
600 GET_CONTEXT(p, tmp); /* get context reg */
e30ec452 601 UASM_i_LW(p, ptr, 0, ptr);
1da177e4
LT
602 break;
603 }
604
605 build_adjust_context(p, tmp);
e30ec452 606 UASM_i_ADDU(p, ptr, ptr, tmp); /* add in offset */
1da177e4
LT
607}
608
234fcd14 609static void __cpuinit build_update_entries(u32 **p, unsigned int tmp,
1da177e4
LT
610 unsigned int ptep)
611{
612 /*
613 * 64bit address support (36bit on a 32bit CPU) in a 32bit
614 * Kernel is a special case. Only a few CPUs use it.
615 */
616#ifdef CONFIG_64BIT_PHYS_ADDR
617 if (cpu_has_64bits) {
e30ec452
TS
618 uasm_i_ld(p, tmp, 0, ptep); /* get even pte */
619 uasm_i_ld(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
620 uasm_i_dsrl(p, tmp, tmp, 6); /* convert to entrylo0 */
621 uasm_i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
622 uasm_i_dsrl(p, ptep, ptep, 6); /* convert to entrylo1 */
623 uasm_i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
1da177e4
LT
624 } else {
625 int pte_off_even = sizeof(pte_t) / 2;
626 int pte_off_odd = pte_off_even + sizeof(pte_t);
627
628 /* The pte entries are pre-shifted */
e30ec452
TS
629 uasm_i_lw(p, tmp, pte_off_even, ptep); /* get even pte */
630 uasm_i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
631 uasm_i_lw(p, ptep, pte_off_odd, ptep); /* get odd pte */
632 uasm_i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
1da177e4
LT
633 }
634#else
e30ec452
TS
635 UASM_i_LW(p, tmp, 0, ptep); /* get even pte */
636 UASM_i_LW(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
1da177e4
LT
637 if (r45k_bvahwbug())
638 build_tlb_probe_entry(p);
e30ec452 639 UASM_i_SRL(p, tmp, tmp, 6); /* convert to entrylo0 */
1da177e4 640 if (r4k_250MHZhwbug())
e30ec452
TS
641 uasm_i_mtc0(p, 0, C0_ENTRYLO0);
642 uasm_i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
643 UASM_i_SRL(p, ptep, ptep, 6); /* convert to entrylo1 */
1da177e4 644 if (r45k_bvahwbug())
e30ec452 645 uasm_i_mfc0(p, tmp, C0_INDEX);
1da177e4 646 if (r4k_250MHZhwbug())
e30ec452
TS
647 uasm_i_mtc0(p, 0, C0_ENTRYLO1);
648 uasm_i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
1da177e4
LT
649#endif
650}
651
234fcd14 652static void __cpuinit build_r4000_tlb_refill_handler(void)
1da177e4
LT
653{
654 u32 *p = tlb_handler;
e30ec452
TS
655 struct uasm_label *l = labels;
656 struct uasm_reloc *r = relocs;
1da177e4
LT
657 u32 *f;
658 unsigned int final_len;
659
660 memset(tlb_handler, 0, sizeof(tlb_handler));
661 memset(labels, 0, sizeof(labels));
662 memset(relocs, 0, sizeof(relocs));
663 memset(final_handler, 0, sizeof(final_handler));
664
665 /*
666 * create the plain linear handler
667 */
668 if (bcm1250_m3_war()) {
e30ec452
TS
669 UASM_i_MFC0(&p, K0, C0_BADVADDR);
670 UASM_i_MFC0(&p, K1, C0_ENTRYHI);
671 uasm_i_xor(&p, K0, K0, K1);
672 UASM_i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
673 uasm_il_bnez(&p, &r, K0, label_leave);
674 /* No need for uasm_i_nop */
1da177e4
LT
675 }
676
875d43e7 677#ifdef CONFIG_64BIT
1da177e4
LT
678 build_get_pmde64(&p, &l, &r, K0, K1); /* get pmd in K1 */
679#else
680 build_get_pgde32(&p, K0, K1); /* get pgd in K1 */
681#endif
682
683 build_get_ptep(&p, K0, K1);
684 build_update_entries(&p, K0, K1);
685 build_tlb_write_entry(&p, &l, &r, tlb_random);
e30ec452
TS
686 uasm_l_leave(&l, p);
687 uasm_i_eret(&p); /* return from trap */
1da177e4 688
875d43e7 689#ifdef CONFIG_64BIT
1da177e4
LT
690 build_get_pgd_vmalloc64(&p, &l, &r, K0, K1);
691#endif
692
693 /*
694 * Overflow check: For the 64bit handler, we need at least one
695 * free instruction slot for the wrap-around branch. In worst
696 * case, if the intended insertion point is a delay slot, we
4b3f686d 697 * need three, with the second nop'ed and the third being
1da177e4
LT
698 * unused.
699 */
2a21c730
FZ
700 /* Loongson2 ebase is different than r4k, we have more space */
701#if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
1da177e4
LT
702 if ((p - tlb_handler) > 64)
703 panic("TLB refill handler space exceeded");
704#else
705 if (((p - tlb_handler) > 63)
706 || (((p - tlb_handler) > 61)
e30ec452 707 && uasm_insn_has_bdelay(relocs, tlb_handler + 29)))
1da177e4
LT
708 panic("TLB refill handler space exceeded");
709#endif
710
711 /*
712 * Now fold the handler in the TLB refill handler space.
713 */
2a21c730 714#if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
1da177e4
LT
715 f = final_handler;
716 /* Simplest case, just copy the handler. */
e30ec452 717 uasm_copy_handler(relocs, labels, tlb_handler, p, f);
1da177e4 718 final_len = p - tlb_handler;
875d43e7 719#else /* CONFIG_64BIT */
1da177e4
LT
720 f = final_handler + 32;
721 if ((p - tlb_handler) <= 32) {
722 /* Just copy the handler. */
e30ec452 723 uasm_copy_handler(relocs, labels, tlb_handler, p, f);
1da177e4
LT
724 final_len = p - tlb_handler;
725 } else {
726 u32 *split = tlb_handler + 30;
727
728 /*
729 * Find the split point.
730 */
e30ec452 731 if (uasm_insn_has_bdelay(relocs, split - 1))
1da177e4
LT
732 split--;
733
734 /* Copy first part of the handler. */
e30ec452 735 uasm_copy_handler(relocs, labels, tlb_handler, split, f);
1da177e4
LT
736 f += split - tlb_handler;
737
738 /* Insert branch. */
e30ec452
TS
739 uasm_l_split(&l, final_handler);
740 uasm_il_b(&f, &r, label_split);
741 if (uasm_insn_has_bdelay(relocs, split))
742 uasm_i_nop(&f);
1da177e4 743 else {
e30ec452
TS
744 uasm_copy_handler(relocs, labels, split, split + 1, f);
745 uasm_move_labels(labels, f, f + 1, -1);
1da177e4
LT
746 f++;
747 split++;
748 }
749
750 /* Copy the rest of the handler. */
e30ec452 751 uasm_copy_handler(relocs, labels, split, p, final_handler);
1da177e4
LT
752 final_len = (f - (final_handler + 32)) + (p - split);
753 }
875d43e7 754#endif /* CONFIG_64BIT */
1da177e4 755
e30ec452
TS
756 uasm_resolve_relocs(relocs, labels);
757 pr_debug("Wrote TLB refill handler (%u instructions).\n",
758 final_len);
1da177e4 759
91b05e67 760 memcpy((void *)ebase, final_handler, 0x100);
92b1e6a6
FBH
761
762 dump_handler((u32 *)ebase, 64);
1da177e4
LT
763}
764
765/*
766 * TLB load/store/modify handlers.
767 *
768 * Only the fastpath gets synthesized at runtime, the slowpath for
769 * do_page_fault remains normal asm.
770 */
771extern void tlb_do_page_fault_0(void);
772extern void tlb_do_page_fault_1(void);
773
1da177e4
LT
774/*
775 * 128 instructions for the fastpath handler is generous and should
776 * never be exceeded.
777 */
778#define FASTPATH_SIZE 128
779
cbdbe07f
FBH
780u32 handle_tlbl[FASTPATH_SIZE] __cacheline_aligned;
781u32 handle_tlbs[FASTPATH_SIZE] __cacheline_aligned;
782u32 handle_tlbm[FASTPATH_SIZE] __cacheline_aligned;
1da177e4 783
234fcd14 784static void __cpuinit
e30ec452 785iPTE_LW(u32 **p, struct uasm_label **l, unsigned int pte, unsigned int ptr)
1da177e4
LT
786{
787#ifdef CONFIG_SMP
788# ifdef CONFIG_64BIT_PHYS_ADDR
789 if (cpu_has_64bits)
e30ec452 790 uasm_i_lld(p, pte, 0, ptr);
1da177e4
LT
791 else
792# endif
e30ec452 793 UASM_i_LL(p, pte, 0, ptr);
1da177e4
LT
794#else
795# ifdef CONFIG_64BIT_PHYS_ADDR
796 if (cpu_has_64bits)
e30ec452 797 uasm_i_ld(p, pte, 0, ptr);
1da177e4
LT
798 else
799# endif
e30ec452 800 UASM_i_LW(p, pte, 0, ptr);
1da177e4
LT
801#endif
802}
803
234fcd14 804static void __cpuinit
e30ec452 805iPTE_SW(u32 **p, struct uasm_reloc **r, unsigned int pte, unsigned int ptr,
63b2d2f4 806 unsigned int mode)
1da177e4 807{
63b2d2f4
TS
808#ifdef CONFIG_64BIT_PHYS_ADDR
809 unsigned int hwmode = mode & (_PAGE_VALID | _PAGE_DIRTY);
810#endif
811
e30ec452 812 uasm_i_ori(p, pte, pte, mode);
1da177e4
LT
813#ifdef CONFIG_SMP
814# ifdef CONFIG_64BIT_PHYS_ADDR
815 if (cpu_has_64bits)
e30ec452 816 uasm_i_scd(p, pte, 0, ptr);
1da177e4
LT
817 else
818# endif
e30ec452 819 UASM_i_SC(p, pte, 0, ptr);
1da177e4
LT
820
821 if (r10000_llsc_war())
e30ec452 822 uasm_il_beqzl(p, r, pte, label_smp_pgtable_change);
1da177e4 823 else
e30ec452 824 uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
1da177e4
LT
825
826# ifdef CONFIG_64BIT_PHYS_ADDR
827 if (!cpu_has_64bits) {
e30ec452
TS
828 /* no uasm_i_nop needed */
829 uasm_i_ll(p, pte, sizeof(pte_t) / 2, ptr);
830 uasm_i_ori(p, pte, pte, hwmode);
831 uasm_i_sc(p, pte, sizeof(pte_t) / 2, ptr);
832 uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
833 /* no uasm_i_nop needed */
834 uasm_i_lw(p, pte, 0, ptr);
1da177e4 835 } else
e30ec452 836 uasm_i_nop(p);
1da177e4 837# else
e30ec452 838 uasm_i_nop(p);
1da177e4
LT
839# endif
840#else
841# ifdef CONFIG_64BIT_PHYS_ADDR
842 if (cpu_has_64bits)
e30ec452 843 uasm_i_sd(p, pte, 0, ptr);
1da177e4
LT
844 else
845# endif
e30ec452 846 UASM_i_SW(p, pte, 0, ptr);
1da177e4
LT
847
848# ifdef CONFIG_64BIT_PHYS_ADDR
849 if (!cpu_has_64bits) {
e30ec452
TS
850 uasm_i_lw(p, pte, sizeof(pte_t) / 2, ptr);
851 uasm_i_ori(p, pte, pte, hwmode);
852 uasm_i_sw(p, pte, sizeof(pte_t) / 2, ptr);
853 uasm_i_lw(p, pte, 0, ptr);
1da177e4
LT
854 }
855# endif
856#endif
857}
858
859/*
860 * Check if PTE is present, if not then jump to LABEL. PTR points to
861 * the page table where this PTE is located, PTE will be re-loaded
862 * with it's original value.
863 */
234fcd14 864static void __cpuinit
e30ec452 865build_pte_present(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
1da177e4
LT
866 unsigned int pte, unsigned int ptr, enum label_id lid)
867{
e30ec452
TS
868 uasm_i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
869 uasm_i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
870 uasm_il_bnez(p, r, pte, lid);
63b2d2f4 871 iPTE_LW(p, l, pte, ptr);
1da177e4
LT
872}
873
874/* Make PTE valid, store result in PTR. */
234fcd14 875static void __cpuinit
e30ec452 876build_make_valid(u32 **p, struct uasm_reloc **r, unsigned int pte,
1da177e4
LT
877 unsigned int ptr)
878{
63b2d2f4
TS
879 unsigned int mode = _PAGE_VALID | _PAGE_ACCESSED;
880
881 iPTE_SW(p, r, pte, ptr, mode);
1da177e4
LT
882}
883
884/*
885 * Check if PTE can be written to, if not branch to LABEL. Regardless
886 * restore PTE with value from PTR when done.
887 */
234fcd14 888static void __cpuinit
e30ec452 889build_pte_writable(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
1da177e4
LT
890 unsigned int pte, unsigned int ptr, enum label_id lid)
891{
e30ec452
TS
892 uasm_i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
893 uasm_i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
894 uasm_il_bnez(p, r, pte, lid);
63b2d2f4 895 iPTE_LW(p, l, pte, ptr);
1da177e4
LT
896}
897
898/* Make PTE writable, update software status bits as well, then store
899 * at PTR.
900 */
234fcd14 901static void __cpuinit
e30ec452 902build_make_write(u32 **p, struct uasm_reloc **r, unsigned int pte,
1da177e4
LT
903 unsigned int ptr)
904{
63b2d2f4
TS
905 unsigned int mode = (_PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID
906 | _PAGE_DIRTY);
907
908 iPTE_SW(p, r, pte, ptr, mode);
1da177e4
LT
909}
910
911/*
912 * Check if PTE can be modified, if not branch to LABEL. Regardless
913 * restore PTE with value from PTR when done.
914 */
234fcd14 915static void __cpuinit
e30ec452 916build_pte_modifiable(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
1da177e4
LT
917 unsigned int pte, unsigned int ptr, enum label_id lid)
918{
e30ec452
TS
919 uasm_i_andi(p, pte, pte, _PAGE_WRITE);
920 uasm_il_beqz(p, r, pte, lid);
63b2d2f4 921 iPTE_LW(p, l, pte, ptr);
1da177e4
LT
922}
923
924/*
925 * R3000 style TLB load/store/modify handlers.
926 */
927
fded2e50
MR
928/*
929 * This places the pte into ENTRYLO0 and writes it with tlbwi.
930 * Then it returns.
931 */
234fcd14 932static void __cpuinit
fded2e50 933build_r3000_pte_reload_tlbwi(u32 **p, unsigned int pte, unsigned int tmp)
1da177e4 934{
e30ec452
TS
935 uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
936 uasm_i_mfc0(p, tmp, C0_EPC); /* cp0 delay */
937 uasm_i_tlbwi(p);
938 uasm_i_jr(p, tmp);
939 uasm_i_rfe(p); /* branch delay */
1da177e4
LT
940}
941
942/*
fded2e50
MR
943 * This places the pte into ENTRYLO0 and writes it with tlbwi
944 * or tlbwr as appropriate. This is because the index register
945 * may have the probe fail bit set as a result of a trap on a
946 * kseg2 access, i.e. without refill. Then it returns.
1da177e4 947 */
234fcd14 948static void __cpuinit
e30ec452
TS
949build_r3000_tlb_reload_write(u32 **p, struct uasm_label **l,
950 struct uasm_reloc **r, unsigned int pte,
951 unsigned int tmp)
952{
953 uasm_i_mfc0(p, tmp, C0_INDEX);
954 uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
955 uasm_il_bltz(p, r, tmp, label_r3000_write_probe_fail); /* cp0 delay */
956 uasm_i_mfc0(p, tmp, C0_EPC); /* branch delay */
957 uasm_i_tlbwi(p); /* cp0 delay */
958 uasm_i_jr(p, tmp);
959 uasm_i_rfe(p); /* branch delay */
960 uasm_l_r3000_write_probe_fail(l, *p);
961 uasm_i_tlbwr(p); /* cp0 delay */
962 uasm_i_jr(p, tmp);
963 uasm_i_rfe(p); /* branch delay */
1da177e4
LT
964}
965
234fcd14 966static void __cpuinit
1da177e4
LT
967build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
968 unsigned int ptr)
969{
970 long pgdc = (long)pgd_current;
971
e30ec452
TS
972 uasm_i_mfc0(p, pte, C0_BADVADDR);
973 uasm_i_lui(p, ptr, uasm_rel_hi(pgdc)); /* cp0 delay */
974 uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
975 uasm_i_srl(p, pte, pte, 22); /* load delay */
976 uasm_i_sll(p, pte, pte, 2);
977 uasm_i_addu(p, ptr, ptr, pte);
978 uasm_i_mfc0(p, pte, C0_CONTEXT);
979 uasm_i_lw(p, ptr, 0, ptr); /* cp0 delay */
980 uasm_i_andi(p, pte, pte, 0xffc); /* load delay */
981 uasm_i_addu(p, ptr, ptr, pte);
982 uasm_i_lw(p, pte, 0, ptr);
983 uasm_i_tlbp(p); /* load delay */
1da177e4
LT
984}
985
234fcd14 986static void __cpuinit build_r3000_tlb_load_handler(void)
1da177e4
LT
987{
988 u32 *p = handle_tlbl;
e30ec452
TS
989 struct uasm_label *l = labels;
990 struct uasm_reloc *r = relocs;
1da177e4
LT
991
992 memset(handle_tlbl, 0, sizeof(handle_tlbl));
993 memset(labels, 0, sizeof(labels));
994 memset(relocs, 0, sizeof(relocs));
995
996 build_r3000_tlbchange_handler_head(&p, K0, K1);
997 build_pte_present(&p, &l, &r, K0, K1, label_nopage_tlbl);
e30ec452 998 uasm_i_nop(&p); /* load delay */
1da177e4 999 build_make_valid(&p, &r, K0, K1);
fded2e50 1000 build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
1da177e4 1001
e30ec452
TS
1002 uasm_l_nopage_tlbl(&l, p);
1003 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
1004 uasm_i_nop(&p);
1da177e4
LT
1005
1006 if ((p - handle_tlbl) > FASTPATH_SIZE)
1007 panic("TLB load handler fastpath space exceeded");
1008
e30ec452
TS
1009 uasm_resolve_relocs(relocs, labels);
1010 pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
1011 (unsigned int)(p - handle_tlbl));
1da177e4 1012
92b1e6a6 1013 dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
1da177e4
LT
1014}
1015
234fcd14 1016static void __cpuinit build_r3000_tlb_store_handler(void)
1da177e4
LT
1017{
1018 u32 *p = handle_tlbs;
e30ec452
TS
1019 struct uasm_label *l = labels;
1020 struct uasm_reloc *r = relocs;
1da177e4
LT
1021
1022 memset(handle_tlbs, 0, sizeof(handle_tlbs));
1023 memset(labels, 0, sizeof(labels));
1024 memset(relocs, 0, sizeof(relocs));
1025
1026 build_r3000_tlbchange_handler_head(&p, K0, K1);
1027 build_pte_writable(&p, &l, &r, K0, K1, label_nopage_tlbs);
e30ec452 1028 uasm_i_nop(&p); /* load delay */
1da177e4 1029 build_make_write(&p, &r, K0, K1);
fded2e50 1030 build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
1da177e4 1031
e30ec452
TS
1032 uasm_l_nopage_tlbs(&l, p);
1033 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1034 uasm_i_nop(&p);
1da177e4
LT
1035
1036 if ((p - handle_tlbs) > FASTPATH_SIZE)
1037 panic("TLB store handler fastpath space exceeded");
1038
e30ec452
TS
1039 uasm_resolve_relocs(relocs, labels);
1040 pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
1041 (unsigned int)(p - handle_tlbs));
1da177e4 1042
92b1e6a6 1043 dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
1da177e4
LT
1044}
1045
234fcd14 1046static void __cpuinit build_r3000_tlb_modify_handler(void)
1da177e4
LT
1047{
1048 u32 *p = handle_tlbm;
e30ec452
TS
1049 struct uasm_label *l = labels;
1050 struct uasm_reloc *r = relocs;
1da177e4
LT
1051
1052 memset(handle_tlbm, 0, sizeof(handle_tlbm));
1053 memset(labels, 0, sizeof(labels));
1054 memset(relocs, 0, sizeof(relocs));
1055
1056 build_r3000_tlbchange_handler_head(&p, K0, K1);
1057 build_pte_modifiable(&p, &l, &r, K0, K1, label_nopage_tlbm);
e30ec452 1058 uasm_i_nop(&p); /* load delay */
1da177e4 1059 build_make_write(&p, &r, K0, K1);
fded2e50 1060 build_r3000_pte_reload_tlbwi(&p, K0, K1);
1da177e4 1061
e30ec452
TS
1062 uasm_l_nopage_tlbm(&l, p);
1063 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1064 uasm_i_nop(&p);
1da177e4
LT
1065
1066 if ((p - handle_tlbm) > FASTPATH_SIZE)
1067 panic("TLB modify handler fastpath space exceeded");
1068
e30ec452
TS
1069 uasm_resolve_relocs(relocs, labels);
1070 pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
1071 (unsigned int)(p - handle_tlbm));
1da177e4 1072
92b1e6a6 1073 dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
1da177e4
LT
1074}
1075
1076/*
1077 * R4000 style TLB load/store/modify handlers.
1078 */
234fcd14 1079static void __cpuinit
e30ec452
TS
1080build_r4000_tlbchange_handler_head(u32 **p, struct uasm_label **l,
1081 struct uasm_reloc **r, unsigned int pte,
1da177e4
LT
1082 unsigned int ptr)
1083{
875d43e7 1084#ifdef CONFIG_64BIT
1da177e4
LT
1085 build_get_pmde64(p, l, r, pte, ptr); /* get pmd in ptr */
1086#else
1087 build_get_pgde32(p, pte, ptr); /* get pgd in ptr */
1088#endif
1089
e30ec452
TS
1090 UASM_i_MFC0(p, pte, C0_BADVADDR);
1091 UASM_i_LW(p, ptr, 0, ptr);
1092 UASM_i_SRL(p, pte, pte, PAGE_SHIFT + PTE_ORDER - PTE_T_LOG2);
1093 uasm_i_andi(p, pte, pte, (PTRS_PER_PTE - 1) << PTE_T_LOG2);
1094 UASM_i_ADDU(p, ptr, ptr, pte);
1da177e4
LT
1095
1096#ifdef CONFIG_SMP
e30ec452
TS
1097 uasm_l_smp_pgtable_change(l, *p);
1098#endif
63b2d2f4 1099 iPTE_LW(p, l, pte, ptr); /* get even pte */
8df5beac
MR
1100 if (!m4kc_tlbp_war())
1101 build_tlb_probe_entry(p);
1da177e4
LT
1102}
1103
234fcd14 1104static void __cpuinit
e30ec452
TS
1105build_r4000_tlbchange_handler_tail(u32 **p, struct uasm_label **l,
1106 struct uasm_reloc **r, unsigned int tmp,
1da177e4
LT
1107 unsigned int ptr)
1108{
e30ec452
TS
1109 uasm_i_ori(p, ptr, ptr, sizeof(pte_t));
1110 uasm_i_xori(p, ptr, ptr, sizeof(pte_t));
1da177e4
LT
1111 build_update_entries(p, tmp, ptr);
1112 build_tlb_write_entry(p, l, r, tlb_indexed);
e30ec452
TS
1113 uasm_l_leave(l, *p);
1114 uasm_i_eret(p); /* return from trap */
1da177e4 1115
875d43e7 1116#ifdef CONFIG_64BIT
1da177e4
LT
1117 build_get_pgd_vmalloc64(p, l, r, tmp, ptr);
1118#endif
1119}
1120
234fcd14 1121static void __cpuinit build_r4000_tlb_load_handler(void)
1da177e4
LT
1122{
1123 u32 *p = handle_tlbl;
e30ec452
TS
1124 struct uasm_label *l = labels;
1125 struct uasm_reloc *r = relocs;
1da177e4
LT
1126
1127 memset(handle_tlbl, 0, sizeof(handle_tlbl));
1128 memset(labels, 0, sizeof(labels));
1129 memset(relocs, 0, sizeof(relocs));
1130
1131 if (bcm1250_m3_war()) {
e30ec452
TS
1132 UASM_i_MFC0(&p, K0, C0_BADVADDR);
1133 UASM_i_MFC0(&p, K1, C0_ENTRYHI);
1134 uasm_i_xor(&p, K0, K0, K1);
1135 UASM_i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
1136 uasm_il_bnez(&p, &r, K0, label_leave);
1137 /* No need for uasm_i_nop */
1da177e4
LT
1138 }
1139
1140 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
1141 build_pte_present(&p, &l, &r, K0, K1, label_nopage_tlbl);
8df5beac
MR
1142 if (m4kc_tlbp_war())
1143 build_tlb_probe_entry(&p);
1da177e4
LT
1144 build_make_valid(&p, &r, K0, K1);
1145 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1146
e30ec452
TS
1147 uasm_l_nopage_tlbl(&l, p);
1148 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
1149 uasm_i_nop(&p);
1da177e4
LT
1150
1151 if ((p - handle_tlbl) > FASTPATH_SIZE)
1152 panic("TLB load handler fastpath space exceeded");
1153
e30ec452
TS
1154 uasm_resolve_relocs(relocs, labels);
1155 pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
1156 (unsigned int)(p - handle_tlbl));
1da177e4 1157
92b1e6a6 1158 dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
1da177e4
LT
1159}
1160
234fcd14 1161static void __cpuinit build_r4000_tlb_store_handler(void)
1da177e4
LT
1162{
1163 u32 *p = handle_tlbs;
e30ec452
TS
1164 struct uasm_label *l = labels;
1165 struct uasm_reloc *r = relocs;
1da177e4
LT
1166
1167 memset(handle_tlbs, 0, sizeof(handle_tlbs));
1168 memset(labels, 0, sizeof(labels));
1169 memset(relocs, 0, sizeof(relocs));
1170
1171 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
1172 build_pte_writable(&p, &l, &r, K0, K1, label_nopage_tlbs);
8df5beac
MR
1173 if (m4kc_tlbp_war())
1174 build_tlb_probe_entry(&p);
1da177e4
LT
1175 build_make_write(&p, &r, K0, K1);
1176 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1177
e30ec452
TS
1178 uasm_l_nopage_tlbs(&l, p);
1179 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1180 uasm_i_nop(&p);
1da177e4
LT
1181
1182 if ((p - handle_tlbs) > FASTPATH_SIZE)
1183 panic("TLB store handler fastpath space exceeded");
1184
e30ec452
TS
1185 uasm_resolve_relocs(relocs, labels);
1186 pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
1187 (unsigned int)(p - handle_tlbs));
1da177e4 1188
92b1e6a6 1189 dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
1da177e4
LT
1190}
1191
234fcd14 1192static void __cpuinit build_r4000_tlb_modify_handler(void)
1da177e4
LT
1193{
1194 u32 *p = handle_tlbm;
e30ec452
TS
1195 struct uasm_label *l = labels;
1196 struct uasm_reloc *r = relocs;
1da177e4
LT
1197
1198 memset(handle_tlbm, 0, sizeof(handle_tlbm));
1199 memset(labels, 0, sizeof(labels));
1200 memset(relocs, 0, sizeof(relocs));
1201
1202 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
1203 build_pte_modifiable(&p, &l, &r, K0, K1, label_nopage_tlbm);
8df5beac
MR
1204 if (m4kc_tlbp_war())
1205 build_tlb_probe_entry(&p);
1da177e4
LT
1206 /* Present and writable bits set, set accessed and dirty bits. */
1207 build_make_write(&p, &r, K0, K1);
1208 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1209
e30ec452
TS
1210 uasm_l_nopage_tlbm(&l, p);
1211 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1212 uasm_i_nop(&p);
1da177e4
LT
1213
1214 if ((p - handle_tlbm) > FASTPATH_SIZE)
1215 panic("TLB modify handler fastpath space exceeded");
1216
e30ec452
TS
1217 uasm_resolve_relocs(relocs, labels);
1218 pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
1219 (unsigned int)(p - handle_tlbm));
115f2a44 1220
92b1e6a6 1221 dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
1da177e4
LT
1222}
1223
234fcd14 1224void __cpuinit build_tlb_refill_handler(void)
1da177e4
LT
1225{
1226 /*
1227 * The refill handler is generated per-CPU, multi-node systems
1228 * may have local storage for it. The other handlers are only
1229 * needed once.
1230 */
1231 static int run_once = 0;
1232
10cc3529 1233 switch (current_cpu_type()) {
1da177e4
LT
1234 case CPU_R2000:
1235 case CPU_R3000:
1236 case CPU_R3000A:
1237 case CPU_R3081E:
1238 case CPU_TX3912:
1239 case CPU_TX3922:
1240 case CPU_TX3927:
1241 build_r3000_tlb_refill_handler();
1242 if (!run_once) {
1243 build_r3000_tlb_load_handler();
1244 build_r3000_tlb_store_handler();
1245 build_r3000_tlb_modify_handler();
1246 run_once++;
1247 }
1248 break;
1249
1250 case CPU_R6000:
1251 case CPU_R6000A:
1252 panic("No R6000 TLB refill handler yet");
1253 break;
1254
1255 case CPU_R8000:
1256 panic("No R8000 TLB refill handler yet");
1257 break;
1258
1259 default:
1260 build_r4000_tlb_refill_handler();
1261 if (!run_once) {
1262 build_r4000_tlb_load_handler();
1263 build_r4000_tlb_store_handler();
1264 build_r4000_tlb_modify_handler();
1265 run_once++;
1266 }
1267 }
1268}
1d40cfcd 1269
234fcd14 1270void __cpuinit flush_tlb_handlers(void)
1d40cfcd 1271{
e0cee3ee 1272 local_flush_icache_range((unsigned long)handle_tlbl,
1d40cfcd 1273 (unsigned long)handle_tlbl + sizeof(handle_tlbl));
e0cee3ee 1274 local_flush_icache_range((unsigned long)handle_tlbs,
1d40cfcd 1275 (unsigned long)handle_tlbs + sizeof(handle_tlbs));
e0cee3ee 1276 local_flush_icache_range((unsigned long)handle_tlbm,
1d40cfcd
RB
1277 (unsigned long)handle_tlbm + sizeof(handle_tlbm));
1278}
This page took 0.47351 seconds and 5 git commands to generate.