blob: 0e34faaadb5c6faec670daf7aea3ea88926b6bc2 [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 Seufere30ec452008-01-28 20:05:38 +00008 * Copyright (C) 2004, 2005, 2006, 2008 Thiemo Seufer
David Daney95affdd2009-05-20 11:40:59 -07009 * Copyright (C) 2005, 2007, 2008, 2009 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
David Daney95affdd2009-05-20 11:40:59 -070022#include <linux/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/kernel.h>
24#include <linux/types.h>
25#include <linux/string.h>
26#include <linux/init.h>
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/war.h>
30
Thiemo Seufere30ec452008-01-28 20:05:38 +000031#include "uasm.h"
32
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010033static inline int r45k_bvahwbug(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 /* XXX: We should probe for the presence of this bug, but we don't. */
36 return 0;
37}
38
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010039static inline int r4k_250MHZhwbug(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 __maybe_unused bcm1250_m3_war(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 return BCM1250_M3_WAR;
48}
49
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010050static inline int __maybe_unused r10000_llsc_war(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 return R10000_LLSC_WAR;
53}
54
55/*
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +010056 * Found by experiment: At least some revisions of the 4kc throw under
57 * some circumstances a machine check exception, triggered by invalid
58 * values in the index register. Delaying the tlbp instruction until
59 * after the next branch, plus adding an additional nop in front of
60 * tlbwi/tlbwr avoids the invalid index register values. Nobody knows
61 * why; it's not an issue caused by the core RTL.
62 *
63 */
Ralf Baechle234fcd12008-03-08 09:56:28 +000064static int __cpuinit m4kc_tlbp_war(void)
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +010065{
66 return (current_cpu_data.processor_id & 0xffff00) ==
67 (PRID_COMP_MIPS | PRID_IMP_4KC);
68}
69
Thiemo Seufere30ec452008-01-28 20:05:38 +000070/* Handle labels (which must be positive integers). */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071enum label_id {
Thiemo Seufere30ec452008-01-28 20:05:38 +000072 label_second_part = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 label_leave,
Atsushi Nemoto656be922006-10-26 00:08:31 +090074#ifdef MODULE_START
75 label_module_alloc,
76#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 label_vmalloc,
78 label_vmalloc_done,
79 label_tlbw_hazard,
80 label_split,
81 label_nopage_tlbl,
82 label_nopage_tlbs,
83 label_nopage_tlbm,
84 label_smp_pgtable_change,
85 label_r3000_write_probe_fail,
Linus Torvalds1da177e2005-04-16 15:20:36 -070086};
87
Thiemo Seufere30ec452008-01-28 20:05:38 +000088UASM_L_LA(_second_part)
89UASM_L_LA(_leave)
Atsushi Nemoto656be922006-10-26 00:08:31 +090090#ifdef MODULE_START
Thiemo Seufere30ec452008-01-28 20:05:38 +000091UASM_L_LA(_module_alloc)
Atsushi Nemoto656be922006-10-26 00:08:31 +090092#endif
Thiemo Seufere30ec452008-01-28 20:05:38 +000093UASM_L_LA(_vmalloc)
94UASM_L_LA(_vmalloc_done)
95UASM_L_LA(_tlbw_hazard)
96UASM_L_LA(_split)
97UASM_L_LA(_nopage_tlbl)
98UASM_L_LA(_nopage_tlbs)
99UASM_L_LA(_nopage_tlbm)
100UASM_L_LA(_smp_pgtable_change)
101UASM_L_LA(_r3000_write_probe_fail)
Atsushi Nemoto656be922006-10-26 00:08:31 +0900102
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +0200103/*
104 * For debug purposes.
105 */
106static inline void dump_handler(const u32 *handler, int count)
107{
108 int i;
109
110 pr_debug("\t.set push\n");
111 pr_debug("\t.set noreorder\n");
112
113 for (i = 0; i < count; i++)
114 pr_debug("\t%p\t.word 0x%08x\n", &handler[i], handler[i]);
115
116 pr_debug("\t.set pop\n");
117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* The only general purpose registers allowed in TLB handlers. */
120#define K0 26
121#define K1 27
122
123/* Some CP0 registers */
Ralf Baechle41c594a2006-04-05 09:45:45 +0100124#define C0_INDEX 0, 0
125#define C0_ENTRYLO0 2, 0
126#define C0_TCBIND 2, 2
127#define C0_ENTRYLO1 3, 0
128#define C0_CONTEXT 4, 0
129#define C0_BADVADDR 8, 0
130#define C0_ENTRYHI 10, 0
131#define C0_EPC 14, 0
132#define C0_XCONTEXT 20, 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Ralf Baechle875d43e2005-09-03 15:56:16 -0700134#ifdef CONFIG_64BIT
Thiemo Seufere30ec452008-01-28 20:05:38 +0000135# define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_XCONTEXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000137# define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_CONTEXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#endif
139
140/* The worst case length of the handler is around 18 instructions for
141 * R3000-style TLBs and up to 63 instructions for R4000-style TLBs.
142 * Maximum space available is 32 instructions for R3000 and 64
143 * instructions for R4000.
144 *
145 * We deliberately chose a buffer size of 128, so we won't scribble
146 * over anything important on overflow before we panic.
147 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000148static u32 tlb_handler[128] __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150/* simply assume worst case size for labels and relocs */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000151static struct uasm_label labels[128] __cpuinitdata;
152static struct uasm_reloc relocs[128] __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154/*
155 * The R3000 TLB handler is simple.
156 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000157static void __cpuinit build_r3000_tlb_refill_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 long pgdc = (long)pgd_current;
160 u32 *p;
161
162 memset(tlb_handler, 0, sizeof(tlb_handler));
163 p = tlb_handler;
164
Thiemo Seufere30ec452008-01-28 20:05:38 +0000165 uasm_i_mfc0(&p, K0, C0_BADVADDR);
166 uasm_i_lui(&p, K1, uasm_rel_hi(pgdc)); /* cp0 delay */
167 uasm_i_lw(&p, K1, uasm_rel_lo(pgdc), K1);
168 uasm_i_srl(&p, K0, K0, 22); /* load delay */
169 uasm_i_sll(&p, K0, K0, 2);
170 uasm_i_addu(&p, K1, K1, K0);
171 uasm_i_mfc0(&p, K0, C0_CONTEXT);
172 uasm_i_lw(&p, K1, 0, K1); /* cp0 delay */
173 uasm_i_andi(&p, K0, K0, 0xffc); /* load delay */
174 uasm_i_addu(&p, K1, K1, K0);
175 uasm_i_lw(&p, K0, 0, K1);
176 uasm_i_nop(&p); /* load delay */
177 uasm_i_mtc0(&p, K0, C0_ENTRYLO0);
178 uasm_i_mfc0(&p, K1, C0_EPC); /* cp0 delay */
179 uasm_i_tlbwr(&p); /* cp0 delay */
180 uasm_i_jr(&p, K1);
181 uasm_i_rfe(&p); /* branch delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 if (p > tlb_handler + 32)
184 panic("TLB refill handler space exceeded");
185
Thiemo Seufere30ec452008-01-28 20:05:38 +0000186 pr_debug("Wrote TLB refill handler (%u instructions).\n",
187 (unsigned int)(p - tlb_handler));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Ralf Baechle91b05e62006-03-29 18:53:00 +0100189 memcpy((void *)ebase, tlb_handler, 0x80);
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +0200190
191 dump_handler((u32 *)ebase, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/*
195 * The R4000 TLB handler is much more complicated. We have two
196 * consecutive handler areas with 32 instructions space each.
197 * Since they aren't used at the same time, we can overflow in the
198 * other one.To keep things simple, we first assume linear space,
199 * then we relocate it to the final handler layout as needed.
200 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000201static u32 final_handler[64] __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203/*
204 * Hazards
205 *
206 * From the IDT errata for the QED RM5230 (Nevada), processor revision 1.0:
207 * 2. A timing hazard exists for the TLBP instruction.
208 *
209 * stalling_instruction
210 * TLBP
211 *
212 * The JTLB is being read for the TLBP throughout the stall generated by the
213 * previous instruction. This is not really correct as the stalling instruction
214 * can modify the address used to access the JTLB. The failure symptom is that
215 * the TLBP instruction will use an address created for the stalling instruction
216 * and not the address held in C0_ENHI and thus report the wrong results.
217 *
218 * The software work-around is to not allow the instruction preceding the TLBP
219 * to stall - make it an NOP or some other instruction guaranteed not to stall.
220 *
221 * Errata 2 will not be fixed. This errata is also on the R5000.
222 *
223 * As if we MIPS hackers wouldn't know how to nop pipelines happy ...
224 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000225static void __cpuinit __maybe_unused build_tlb_probe_entry(u32 **p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Ralf Baechle10cc3522007-10-11 23:46:15 +0100227 switch (current_cpu_type()) {
Thomas Bogendoerfer326e2e12008-05-12 13:55:42 +0200228 /* Found by experiment: R4600 v2.0/R4700 needs this, too. */
Thiemo Seuferf5b4d952005-09-09 17:11:50 +0000229 case CPU_R4600:
Thomas Bogendoerfer326e2e12008-05-12 13:55:42 +0200230 case CPU_R4700:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 case CPU_R5000:
232 case CPU_R5000A:
233 case CPU_NEVADA:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000234 uasm_i_nop(p);
235 uasm_i_tlbp(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 break;
237
238 default:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000239 uasm_i_tlbp(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 break;
241 }
242}
243
244/*
245 * Write random or indexed TLB entry, and care about the hazards from
246 * the preceeding mtc0 and for the following eret.
247 */
248enum tlb_write_entry { tlb_random, tlb_indexed };
249
Ralf Baechle234fcd12008-03-08 09:56:28 +0000250static void __cpuinit build_tlb_write_entry(u32 **p, struct uasm_label **l,
Thiemo Seufere30ec452008-01-28 20:05:38 +0000251 struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 enum tlb_write_entry wmode)
253{
254 void(*tlbw)(u32 **) = NULL;
255
256 switch (wmode) {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000257 case tlb_random: tlbw = uasm_i_tlbwr; break;
258 case tlb_indexed: tlbw = uasm_i_tlbwi; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
Ralf Baechle161548b2008-01-29 10:14:54 +0000261 if (cpu_has_mips_r2) {
David Daney41f0e4d2009-05-12 12:41:53 -0700262 if (cpu_has_mips_r2_exec_hazard)
263 uasm_i_ehb(p);
Ralf Baechle161548b2008-01-29 10:14:54 +0000264 tlbw(p);
265 return;
266 }
267
Ralf Baechle10cc3522007-10-11 23:46:15 +0100268 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 case CPU_R4000PC:
270 case CPU_R4000SC:
271 case CPU_R4000MC:
272 case CPU_R4400PC:
273 case CPU_R4400SC:
274 case CPU_R4400MC:
275 /*
276 * This branch uses up a mtc0 hazard nop slot and saves
277 * two nops after the tlbw instruction.
278 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000279 uasm_il_bgezl(p, r, 0, label_tlbw_hazard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 tlbw(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000281 uasm_l_tlbw_hazard(l, *p);
282 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 break;
284
285 case CPU_R4600:
286 case CPU_R4700:
287 case CPU_R5000:
288 case CPU_R5000A:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000289 uasm_i_nop(p);
Maciej W. Rozycki2c93e122005-06-30 10:51:01 +0000290 tlbw(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000291 uasm_i_nop(p);
Maciej W. Rozycki2c93e122005-06-30 10:51:01 +0000292 break;
293
294 case CPU_R4300:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 case CPU_5KC:
296 case CPU_TX49XX:
Pete Popovbdf21b12005-07-14 17:47:57 +0000297 case CPU_PR4450:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000298 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 tlbw(p);
300 break;
301
302 case CPU_R10000:
303 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -0400304 case CPU_R14000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 case CPU_4KC:
Thomas Bogendoerferb1ec4c82008-03-26 16:42:54 +0100306 case CPU_4KEC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 case CPU_SB1:
Andrew Isaacson93ce2f522005-10-19 23:56:20 -0700308 case CPU_SB1A:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 case CPU_4KSC:
310 case CPU_20KC:
311 case CPU_25KF:
Aurelien Jarno1c0c13e2007-09-25 15:40:12 +0200312 case CPU_BCM3302:
313 case CPU_BCM4710:
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800314 case CPU_LOONGSON2:
David Daneyec454d82008-12-11 15:33:35 -0800315 case CPU_CAVIUM_OCTEON:
Shinya Kuribayashia644b272009-03-03 18:05:51 +0900316 case CPU_R5500:
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +0100317 if (m4kc_tlbp_war())
Thiemo Seufere30ec452008-01-28 20:05:38 +0000318 uasm_i_nop(p);
Manuel Lauss2f794d02009-03-25 17:49:30 +0100319 case CPU_ALCHEMY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 tlbw(p);
321 break;
322
323 case CPU_NEVADA:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000324 uasm_i_nop(p); /* QED specifies 2 nops hazard */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /*
326 * This branch uses up a mtc0 hazard nop slot and saves
327 * a nop after the tlbw instruction.
328 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000329 uasm_il_bgezl(p, r, 0, label_tlbw_hazard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 tlbw(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000331 uasm_l_tlbw_hazard(l, *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 break;
333
334 case CPU_RM7000:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000335 uasm_i_nop(p);
336 uasm_i_nop(p);
337 uasm_i_nop(p);
338 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 tlbw(p);
340 break;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 case CPU_RM9000:
343 /*
344 * When the JTLB is updated by tlbwi or tlbwr, a subsequent
345 * use of the JTLB for instructions should not occur for 4
346 * cpu cycles and use for data translations should not occur
347 * for 3 cpu cycles.
348 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000349 uasm_i_ssnop(p);
350 uasm_i_ssnop(p);
351 uasm_i_ssnop(p);
352 uasm_i_ssnop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 tlbw(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000354 uasm_i_ssnop(p);
355 uasm_i_ssnop(p);
356 uasm_i_ssnop(p);
357 uasm_i_ssnop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 break;
359
360 case CPU_VR4111:
361 case CPU_VR4121:
362 case CPU_VR4122:
363 case CPU_VR4181:
364 case CPU_VR4181A:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000365 uasm_i_nop(p);
366 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 tlbw(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000368 uasm_i_nop(p);
369 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 break;
371
372 case CPU_VR4131:
373 case CPU_VR4133:
Ralf Baechle7623deb2005-08-29 16:49:55 +0000374 case CPU_R5432:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000375 uasm_i_nop(p);
376 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 tlbw(p);
378 break;
379
380 default:
381 panic("No TLB refill handler yet (CPU type: %d)",
382 current_cpu_data.cputype);
383 break;
384 }
385}
386
Ralf Baechle875d43e2005-09-03 15:56:16 -0700387#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/*
389 * TMP and PTR are scratch.
390 * TMP will be clobbered, PTR will hold the pmd entry.
391 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000392static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000393build_get_pmde64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 unsigned int tmp, unsigned int ptr)
395{
396 long pgdc = (long)pgd_current;
397
398 /*
399 * The vmalloc handling is not in the hotpath.
400 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000401 uasm_i_dmfc0(p, tmp, C0_BADVADDR);
Atsushi Nemoto656be922006-10-26 00:08:31 +0900402#ifdef MODULE_START
Thiemo Seufere30ec452008-01-28 20:05:38 +0000403 uasm_il_bltz(p, r, tmp, label_module_alloc);
Atsushi Nemoto656be922006-10-26 00:08:31 +0900404#else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000405 uasm_il_bltz(p, r, tmp, label_vmalloc);
Atsushi Nemoto656be922006-10-26 00:08:31 +0900406#endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000407 /* No uasm_i_nop needed here, since the next insn doesn't touch TMP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409#ifdef CONFIG_SMP
Ralf Baechle41c594a2006-04-05 09:45:45 +0100410# ifdef CONFIG_MIPS_MT_SMTC
411 /*
412 * SMTC uses TCBind value as "CPU" index
413 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000414 uasm_i_mfc0(p, ptr, C0_TCBIND);
415 uasm_i_dsrl(p, ptr, ptr, 19);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100416# else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /*
Thiemo Seufer1b3a6e92005-04-01 14:07:13 +0000418 * 64 bit SMP running in XKPHYS has smp_processor_id() << 3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 * stored in CONTEXT.
420 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000421 uasm_i_dmfc0(p, ptr, C0_CONTEXT);
422 uasm_i_dsrl(p, ptr, ptr, 23);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100423#endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000424 UASM_i_LA_mostly(p, tmp, pgdc);
425 uasm_i_daddu(p, ptr, ptr, tmp);
426 uasm_i_dmfc0(p, tmp, C0_BADVADDR);
427 uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428#else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000429 UASM_i_LA_mostly(p, ptr, pgdc);
430 uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431#endif
432
Thiemo Seufere30ec452008-01-28 20:05:38 +0000433 uasm_l_vmalloc_done(l, *p);
Ralf Baechle242954b2006-10-24 02:29:01 +0100434
435 if (PGDIR_SHIFT - 3 < 32) /* get pgd offset in bytes */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000436 uasm_i_dsrl(p, tmp, tmp, PGDIR_SHIFT-3);
Ralf Baechle242954b2006-10-24 02:29:01 +0100437 else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000438 uasm_i_dsrl32(p, tmp, tmp, PGDIR_SHIFT - 3 - 32);
Ralf Baechle242954b2006-10-24 02:29:01 +0100439
Thiemo Seufere30ec452008-01-28 20:05:38 +0000440 uasm_i_andi(p, tmp, tmp, (PTRS_PER_PGD - 1)<<3);
441 uasm_i_daddu(p, ptr, ptr, tmp); /* add in pgd offset */
442 uasm_i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
443 uasm_i_ld(p, ptr, 0, ptr); /* get pmd pointer */
444 uasm_i_dsrl(p, tmp, tmp, PMD_SHIFT-3); /* get pmd offset in bytes */
445 uasm_i_andi(p, tmp, tmp, (PTRS_PER_PMD - 1)<<3);
446 uasm_i_daddu(p, ptr, ptr, tmp); /* add in pmd offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449/*
450 * BVADDR is the faulting address, PTR is scratch.
451 * PTR will hold the pgd for vmalloc.
452 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000453static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000454build_get_pgd_vmalloc64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 unsigned int bvaddr, unsigned int ptr)
456{
457 long swpd = (long)swapper_pg_dir;
458
Atsushi Nemoto656be922006-10-26 00:08:31 +0900459#ifdef MODULE_START
460 long modd = (long)module_pg_dir;
461
Thiemo Seufere30ec452008-01-28 20:05:38 +0000462 uasm_l_module_alloc(l, *p);
Atsushi Nemoto656be922006-10-26 00:08:31 +0900463 /*
464 * Assumption:
465 * VMALLOC_START >= 0xc000000000000000UL
466 * MODULE_START >= 0xe000000000000000UL
467 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000468 UASM_i_SLL(p, ptr, bvaddr, 2);
469 uasm_il_bgez(p, r, ptr, label_vmalloc);
Atsushi Nemoto656be922006-10-26 00:08:31 +0900470
Thiemo Seufere30ec452008-01-28 20:05:38 +0000471 if (uasm_in_compat_space_p(MODULE_START) &&
472 !uasm_rel_lo(MODULE_START)) {
473 uasm_i_lui(p, ptr, uasm_rel_hi(MODULE_START)); /* delay slot */
Atsushi Nemoto656be922006-10-26 00:08:31 +0900474 } else {
475 /* unlikely configuration */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000476 uasm_i_nop(p); /* delay slot */
477 UASM_i_LA(p, ptr, MODULE_START);
Atsushi Nemoto656be922006-10-26 00:08:31 +0900478 }
Thiemo Seufere30ec452008-01-28 20:05:38 +0000479 uasm_i_dsubu(p, bvaddr, bvaddr, ptr);
Atsushi Nemoto656be922006-10-26 00:08:31 +0900480
Thiemo Seufere30ec452008-01-28 20:05:38 +0000481 if (uasm_in_compat_space_p(modd) && !uasm_rel_lo(modd)) {
482 uasm_il_b(p, r, label_vmalloc_done);
483 uasm_i_lui(p, ptr, uasm_rel_hi(modd));
Atsushi Nemoto656be922006-10-26 00:08:31 +0900484 } else {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000485 UASM_i_LA_mostly(p, ptr, modd);
486 uasm_il_b(p, r, label_vmalloc_done);
487 if (uasm_in_compat_space_p(modd))
488 uasm_i_addiu(p, ptr, ptr, uasm_rel_lo(modd));
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100489 else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000490 uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(modd));
Atsushi Nemoto656be922006-10-26 00:08:31 +0900491 }
492
Thiemo Seufere30ec452008-01-28 20:05:38 +0000493 uasm_l_vmalloc(l, *p);
494 if (uasm_in_compat_space_p(MODULE_START) &&
495 !uasm_rel_lo(MODULE_START) &&
Atsushi Nemoto656be922006-10-26 00:08:31 +0900496 MODULE_START << 32 == VMALLOC_START)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000497 uasm_i_dsll32(p, ptr, ptr, 0); /* typical case */
Atsushi Nemoto656be922006-10-26 00:08:31 +0900498 else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000499 UASM_i_LA(p, ptr, VMALLOC_START);
Atsushi Nemoto656be922006-10-26 00:08:31 +0900500#else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000501 uasm_l_vmalloc(l, *p);
502 UASM_i_LA(p, ptr, VMALLOC_START);
Atsushi Nemoto656be922006-10-26 00:08:31 +0900503#endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000504 uasm_i_dsubu(p, bvaddr, bvaddr, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Thiemo Seufere30ec452008-01-28 20:05:38 +0000506 if (uasm_in_compat_space_p(swpd) && !uasm_rel_lo(swpd)) {
507 uasm_il_b(p, r, label_vmalloc_done);
508 uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 } else {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000510 UASM_i_LA_mostly(p, ptr, swpd);
511 uasm_il_b(p, r, label_vmalloc_done);
512 if (uasm_in_compat_space_p(swpd))
513 uasm_i_addiu(p, ptr, ptr, uasm_rel_lo(swpd));
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100514 else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000515 uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(swpd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517}
518
Ralf Baechle875d43e2005-09-03 15:56:16 -0700519#else /* !CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521/*
522 * TMP and PTR are scratch.
523 * TMP will be clobbered, PTR will hold the pgd entry.
524 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000525static void __cpuinit __maybe_unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
527{
528 long pgdc = (long)pgd_current;
529
530 /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
531#ifdef CONFIG_SMP
Ralf Baechle41c594a2006-04-05 09:45:45 +0100532#ifdef CONFIG_MIPS_MT_SMTC
533 /*
534 * SMTC uses TCBind value as "CPU" index
535 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000536 uasm_i_mfc0(p, ptr, C0_TCBIND);
537 UASM_i_LA_mostly(p, tmp, pgdc);
538 uasm_i_srl(p, ptr, ptr, 19);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100539#else
540 /*
541 * smp_processor_id() << 3 is stored in CONTEXT.
542 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000543 uasm_i_mfc0(p, ptr, C0_CONTEXT);
544 UASM_i_LA_mostly(p, tmp, pgdc);
545 uasm_i_srl(p, ptr, ptr, 23);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100546#endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000547 uasm_i_addu(p, ptr, tmp, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548#else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000549 UASM_i_LA_mostly(p, ptr, pgdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550#endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000551 uasm_i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
552 uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
553 uasm_i_srl(p, tmp, tmp, PGDIR_SHIFT); /* get pgd only bits */
554 uasm_i_sll(p, tmp, tmp, PGD_T_LOG2);
555 uasm_i_addu(p, ptr, ptr, tmp); /* add in pgd offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Ralf Baechle875d43e2005-09-03 15:56:16 -0700558#endif /* !CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Ralf Baechle234fcd12008-03-08 09:56:28 +0000560static void __cpuinit build_adjust_context(u32 **p, unsigned int ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Ralf Baechle242954b2006-10-24 02:29:01 +0100562 unsigned int shift = 4 - (PTE_T_LOG2 + 1) + PAGE_SHIFT - 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 unsigned int mask = (PTRS_PER_PTE / 2 - 1) << (PTE_T_LOG2 + 1);
564
Ralf Baechle10cc3522007-10-11 23:46:15 +0100565 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 case CPU_VR41XX:
567 case CPU_VR4111:
568 case CPU_VR4121:
569 case CPU_VR4122:
570 case CPU_VR4131:
571 case CPU_VR4181:
572 case CPU_VR4181A:
573 case CPU_VR4133:
574 shift += 2;
575 break;
576
577 default:
578 break;
579 }
580
581 if (shift)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000582 UASM_i_SRL(p, ctx, ctx, shift);
583 uasm_i_andi(p, ctx, ctx, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Ralf Baechle234fcd12008-03-08 09:56:28 +0000586static void __cpuinit build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 /*
589 * Bug workaround for the Nevada. It seems as if under certain
590 * circumstances the move from cp0_context might produce a
591 * bogus result when the mfc0 instruction and its consumer are
592 * in a different cacheline or a load instruction, probably any
593 * memory reference, is between them.
594 */
Ralf Baechle10cc3522007-10-11 23:46:15 +0100595 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 case CPU_NEVADA:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000597 UASM_i_LW(p, ptr, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 GET_CONTEXT(p, tmp); /* get context reg */
599 break;
600
601 default:
602 GET_CONTEXT(p, tmp); /* get context reg */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000603 UASM_i_LW(p, ptr, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 break;
605 }
606
607 build_adjust_context(p, tmp);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000608 UASM_i_ADDU(p, ptr, ptr, tmp); /* add in offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
Ralf Baechle234fcd12008-03-08 09:56:28 +0000611static void __cpuinit build_update_entries(u32 **p, unsigned int tmp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 unsigned int ptep)
613{
614 /*
615 * 64bit address support (36bit on a 32bit CPU) in a 32bit
616 * Kernel is a special case. Only a few CPUs use it.
617 */
618#ifdef CONFIG_64BIT_PHYS_ADDR
619 if (cpu_has_64bits) {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000620 uasm_i_ld(p, tmp, 0, ptep); /* get even pte */
621 uasm_i_ld(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
622 uasm_i_dsrl(p, tmp, tmp, 6); /* convert to entrylo0 */
623 uasm_i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
624 uasm_i_dsrl(p, ptep, ptep, 6); /* convert to entrylo1 */
625 uasm_i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 } else {
627 int pte_off_even = sizeof(pte_t) / 2;
628 int pte_off_odd = pte_off_even + sizeof(pte_t);
629
630 /* The pte entries are pre-shifted */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000631 uasm_i_lw(p, tmp, pte_off_even, ptep); /* get even pte */
632 uasm_i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
633 uasm_i_lw(p, ptep, pte_off_odd, ptep); /* get odd pte */
634 uasm_i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636#else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000637 UASM_i_LW(p, tmp, 0, ptep); /* get even pte */
638 UASM_i_LW(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (r45k_bvahwbug())
640 build_tlb_probe_entry(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000641 UASM_i_SRL(p, tmp, tmp, 6); /* convert to entrylo0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (r4k_250MHZhwbug())
Thiemo Seufere30ec452008-01-28 20:05:38 +0000643 uasm_i_mtc0(p, 0, C0_ENTRYLO0);
644 uasm_i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
645 UASM_i_SRL(p, ptep, ptep, 6); /* convert to entrylo1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (r45k_bvahwbug())
Thiemo Seufere30ec452008-01-28 20:05:38 +0000647 uasm_i_mfc0(p, tmp, C0_INDEX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (r4k_250MHZhwbug())
Thiemo Seufere30ec452008-01-28 20:05:38 +0000649 uasm_i_mtc0(p, 0, C0_ENTRYLO1);
650 uasm_i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651#endif
652}
653
David Daneye6f72d32009-05-20 11:40:58 -0700654/*
655 * For a 64-bit kernel, we are using the 64-bit XTLB refill exception
656 * because EXL == 0. If we wrap, we can also use the 32 instruction
657 * slots before the XTLB refill exception handler which belong to the
658 * unused TLB refill exception.
659 */
660#define MIPS64_REFILL_INSNS 32
661
Ralf Baechle234fcd12008-03-08 09:56:28 +0000662static void __cpuinit build_r4000_tlb_refill_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 u32 *p = tlb_handler;
Thiemo Seufere30ec452008-01-28 20:05:38 +0000665 struct uasm_label *l = labels;
666 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 u32 *f;
668 unsigned int final_len;
669
670 memset(tlb_handler, 0, sizeof(tlb_handler));
671 memset(labels, 0, sizeof(labels));
672 memset(relocs, 0, sizeof(relocs));
673 memset(final_handler, 0, sizeof(final_handler));
674
675 /*
676 * create the plain linear handler
677 */
678 if (bcm1250_m3_war()) {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000679 UASM_i_MFC0(&p, K0, C0_BADVADDR);
680 UASM_i_MFC0(&p, K1, C0_ENTRYHI);
681 uasm_i_xor(&p, K0, K0, K1);
682 UASM_i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
683 uasm_il_bnez(&p, &r, K0, label_leave);
684 /* No need for uasm_i_nop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686
Ralf Baechle875d43e2005-09-03 15:56:16 -0700687#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 build_get_pmde64(&p, &l, &r, K0, K1); /* get pmd in K1 */
689#else
690 build_get_pgde32(&p, K0, K1); /* get pgd in K1 */
691#endif
692
693 build_get_ptep(&p, K0, K1);
694 build_update_entries(&p, K0, K1);
695 build_tlb_write_entry(&p, &l, &r, tlb_random);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000696 uasm_l_leave(&l, p);
697 uasm_i_eret(&p); /* return from trap */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Ralf Baechle875d43e2005-09-03 15:56:16 -0700699#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 build_get_pgd_vmalloc64(&p, &l, &r, K0, K1);
701#endif
702
703 /*
704 * Overflow check: For the 64bit handler, we need at least one
705 * free instruction slot for the wrap-around branch. In worst
706 * case, if the intended insertion point is a delay slot, we
Matt LaPlante4b3f6862006-10-03 22:21:02 +0200707 * need three, with the second nop'ed and the third being
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 * unused.
709 */
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800710 /* Loongson2 ebase is different than r4k, we have more space */
711#if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if ((p - tlb_handler) > 64)
713 panic("TLB refill handler space exceeded");
714#else
David Daneye6f72d32009-05-20 11:40:58 -0700715 if (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 1)
716 || (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 3)
717 && uasm_insn_has_bdelay(relocs,
718 tlb_handler + MIPS64_REFILL_INSNS - 3)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 panic("TLB refill handler space exceeded");
720#endif
721
722 /*
723 * Now fold the handler in the TLB refill handler space.
724 */
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800725#if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 f = final_handler;
727 /* Simplest case, just copy the handler. */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000728 uasm_copy_handler(relocs, labels, tlb_handler, p, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 final_len = p - tlb_handler;
Ralf Baechle875d43e2005-09-03 15:56:16 -0700730#else /* CONFIG_64BIT */
David Daneye6f72d32009-05-20 11:40:58 -0700731 f = final_handler + MIPS64_REFILL_INSNS;
732 if ((p - tlb_handler) <= MIPS64_REFILL_INSNS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /* Just copy the handler. */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000734 uasm_copy_handler(relocs, labels, tlb_handler, p, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 final_len = p - tlb_handler;
736 } else {
David Daney95affdd2009-05-20 11:40:59 -0700737#ifdef MODULE_START
738 const enum label_id ls = label_module_alloc;
739#else
740 const enum label_id ls = label_vmalloc;
741#endif
742 u32 *split;
743 int ov = 0;
744 int i;
745
746 for (i = 0; i < ARRAY_SIZE(labels) && labels[i].lab != ls; i++)
747 ;
748 BUG_ON(i == ARRAY_SIZE(labels));
749 split = labels[i].addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 /*
David Daney95affdd2009-05-20 11:40:59 -0700752 * See if we have overflown one way or the other.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
David Daney95affdd2009-05-20 11:40:59 -0700754 if (split > tlb_handler + MIPS64_REFILL_INSNS ||
755 split < p - MIPS64_REFILL_INSNS)
756 ov = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
David Daney95affdd2009-05-20 11:40:59 -0700758 if (ov) {
759 /*
760 * Split two instructions before the end. One
761 * for the branch and one for the instruction
762 * in the delay slot.
763 */
764 split = tlb_handler + MIPS64_REFILL_INSNS - 2;
765
766 /*
767 * If the branch would fall in a delay slot,
768 * we must back up an additional instruction
769 * so that it is no longer in a delay slot.
770 */
771 if (uasm_insn_has_bdelay(relocs, split - 1))
772 split--;
773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 /* Copy first part of the handler. */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000775 uasm_copy_handler(relocs, labels, tlb_handler, split, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 f += split - tlb_handler;
777
David Daney95affdd2009-05-20 11:40:59 -0700778 if (ov) {
779 /* Insert branch. */
780 uasm_l_split(&l, final_handler);
781 uasm_il_b(&f, &r, label_split);
782 if (uasm_insn_has_bdelay(relocs, split))
783 uasm_i_nop(&f);
784 else {
785 uasm_copy_handler(relocs, labels,
786 split, split + 1, f);
787 uasm_move_labels(labels, f, f + 1, -1);
788 f++;
789 split++;
790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
793 /* Copy the rest of the handler. */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000794 uasm_copy_handler(relocs, labels, split, p, final_handler);
David Daneye6f72d32009-05-20 11:40:58 -0700795 final_len = (f - (final_handler + MIPS64_REFILL_INSNS)) +
796 (p - split);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Ralf Baechle875d43e2005-09-03 15:56:16 -0700798#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Thiemo Seufere30ec452008-01-28 20:05:38 +0000800 uasm_resolve_relocs(relocs, labels);
801 pr_debug("Wrote TLB refill handler (%u instructions).\n",
802 final_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Ralf Baechle91b05e62006-03-29 18:53:00 +0100804 memcpy((void *)ebase, final_handler, 0x100);
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +0200805
806 dump_handler((u32 *)ebase, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
809/*
810 * TLB load/store/modify handlers.
811 *
812 * Only the fastpath gets synthesized at runtime, the slowpath for
813 * do_page_fault remains normal asm.
814 */
815extern void tlb_do_page_fault_0(void);
816extern void tlb_do_page_fault_1(void);
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818/*
819 * 128 instructions for the fastpath handler is generous and should
820 * never be exceeded.
821 */
822#define FASTPATH_SIZE 128
823
Franck Bui-Huucbdbe072007-10-18 09:11:16 +0200824u32 handle_tlbl[FASTPATH_SIZE] __cacheline_aligned;
825u32 handle_tlbs[FASTPATH_SIZE] __cacheline_aligned;
826u32 handle_tlbm[FASTPATH_SIZE] __cacheline_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Ralf Baechle234fcd12008-03-08 09:56:28 +0000828static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000829iPTE_LW(u32 **p, struct uasm_label **l, unsigned int pte, unsigned int ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831#ifdef CONFIG_SMP
832# ifdef CONFIG_64BIT_PHYS_ADDR
833 if (cpu_has_64bits)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000834 uasm_i_lld(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 else
836# endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000837 UASM_i_LL(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838#else
839# ifdef CONFIG_64BIT_PHYS_ADDR
840 if (cpu_has_64bits)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000841 uasm_i_ld(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 else
843# endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000844 UASM_i_LW(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845#endif
846}
847
Ralf Baechle234fcd12008-03-08 09:56:28 +0000848static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000849iPTE_SW(u32 **p, struct uasm_reloc **r, unsigned int pte, unsigned int ptr,
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +0000850 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +0000852#ifdef CONFIG_64BIT_PHYS_ADDR
853 unsigned int hwmode = mode & (_PAGE_VALID | _PAGE_DIRTY);
854#endif
855
Thiemo Seufere30ec452008-01-28 20:05:38 +0000856 uasm_i_ori(p, pte, pte, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857#ifdef CONFIG_SMP
858# ifdef CONFIG_64BIT_PHYS_ADDR
859 if (cpu_has_64bits)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000860 uasm_i_scd(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 else
862# endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000863 UASM_i_SC(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 if (r10000_llsc_war())
Thiemo Seufere30ec452008-01-28 20:05:38 +0000866 uasm_il_beqzl(p, r, pte, label_smp_pgtable_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000868 uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870# ifdef CONFIG_64BIT_PHYS_ADDR
871 if (!cpu_has_64bits) {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000872 /* no uasm_i_nop needed */
873 uasm_i_ll(p, pte, sizeof(pte_t) / 2, ptr);
874 uasm_i_ori(p, pte, pte, hwmode);
875 uasm_i_sc(p, pte, sizeof(pte_t) / 2, ptr);
876 uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
877 /* no uasm_i_nop needed */
878 uasm_i_lw(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 } else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000880 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881# else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000882 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883# endif
884#else
885# ifdef CONFIG_64BIT_PHYS_ADDR
886 if (cpu_has_64bits)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000887 uasm_i_sd(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 else
889# endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000890 UASM_i_SW(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892# ifdef CONFIG_64BIT_PHYS_ADDR
893 if (!cpu_has_64bits) {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000894 uasm_i_lw(p, pte, sizeof(pte_t) / 2, ptr);
895 uasm_i_ori(p, pte, pte, hwmode);
896 uasm_i_sw(p, pte, sizeof(pte_t) / 2, ptr);
897 uasm_i_lw(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899# endif
900#endif
901}
902
903/*
904 * Check if PTE is present, if not then jump to LABEL. PTR points to
905 * the page table where this PTE is located, PTE will be re-loaded
906 * with it's original value.
907 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000908static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000909build_pte_present(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 unsigned int pte, unsigned int ptr, enum label_id lid)
911{
Thiemo Seufere30ec452008-01-28 20:05:38 +0000912 uasm_i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
913 uasm_i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
914 uasm_il_bnez(p, r, pte, lid);
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +0000915 iPTE_LW(p, l, pte, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918/* Make PTE valid, store result in PTR. */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000919static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000920build_make_valid(u32 **p, struct uasm_reloc **r, unsigned int pte,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 unsigned int ptr)
922{
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +0000923 unsigned int mode = _PAGE_VALID | _PAGE_ACCESSED;
924
925 iPTE_SW(p, r, pte, ptr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
928/*
929 * Check if PTE can be written to, if not branch to LABEL. Regardless
930 * restore PTE with value from PTR when done.
931 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000932static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000933build_pte_writable(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 unsigned int pte, unsigned int ptr, enum label_id lid)
935{
Thiemo Seufere30ec452008-01-28 20:05:38 +0000936 uasm_i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
937 uasm_i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
938 uasm_il_bnez(p, r, pte, lid);
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +0000939 iPTE_LW(p, l, pte, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
942/* Make PTE writable, update software status bits as well, then store
943 * at PTR.
944 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000945static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000946build_make_write(u32 **p, struct uasm_reloc **r, unsigned int pte,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 unsigned int ptr)
948{
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +0000949 unsigned int mode = (_PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID
950 | _PAGE_DIRTY);
951
952 iPTE_SW(p, r, pte, ptr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
955/*
956 * Check if PTE can be modified, if not branch to LABEL. Regardless
957 * restore PTE with value from PTR when done.
958 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000959static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000960build_pte_modifiable(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 unsigned int pte, unsigned int ptr, enum label_id lid)
962{
Thiemo Seufere30ec452008-01-28 20:05:38 +0000963 uasm_i_andi(p, pte, pte, _PAGE_WRITE);
964 uasm_il_beqz(p, r, pte, lid);
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +0000965 iPTE_LW(p, l, pte, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968/*
969 * R3000 style TLB load/store/modify handlers.
970 */
971
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +0000972/*
973 * This places the pte into ENTRYLO0 and writes it with tlbwi.
974 * Then it returns.
975 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000976static void __cpuinit
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +0000977build_r3000_pte_reload_tlbwi(u32 **p, unsigned int pte, unsigned int tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Thiemo Seufere30ec452008-01-28 20:05:38 +0000979 uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
980 uasm_i_mfc0(p, tmp, C0_EPC); /* cp0 delay */
981 uasm_i_tlbwi(p);
982 uasm_i_jr(p, tmp);
983 uasm_i_rfe(p); /* branch delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
986/*
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +0000987 * This places the pte into ENTRYLO0 and writes it with tlbwi
988 * or tlbwr as appropriate. This is because the index register
989 * may have the probe fail bit set as a result of a trap on a
990 * kseg2 access, i.e. without refill. Then it returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000992static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000993build_r3000_tlb_reload_write(u32 **p, struct uasm_label **l,
994 struct uasm_reloc **r, unsigned int pte,
995 unsigned int tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Thiemo Seufere30ec452008-01-28 20:05:38 +0000997 uasm_i_mfc0(p, tmp, C0_INDEX);
998 uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
999 uasm_il_bltz(p, r, tmp, label_r3000_write_probe_fail); /* cp0 delay */
1000 uasm_i_mfc0(p, tmp, C0_EPC); /* branch delay */
1001 uasm_i_tlbwi(p); /* cp0 delay */
1002 uasm_i_jr(p, tmp);
1003 uasm_i_rfe(p); /* branch delay */
1004 uasm_l_r3000_write_probe_fail(l, *p);
1005 uasm_i_tlbwr(p); /* cp0 delay */
1006 uasm_i_jr(p, tmp);
1007 uasm_i_rfe(p); /* branch delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Ralf Baechle234fcd12008-03-08 09:56:28 +00001010static void __cpuinit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
1012 unsigned int ptr)
1013{
1014 long pgdc = (long)pgd_current;
1015
Thiemo Seufere30ec452008-01-28 20:05:38 +00001016 uasm_i_mfc0(p, pte, C0_BADVADDR);
1017 uasm_i_lui(p, ptr, uasm_rel_hi(pgdc)); /* cp0 delay */
1018 uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
1019 uasm_i_srl(p, pte, pte, 22); /* load delay */
1020 uasm_i_sll(p, pte, pte, 2);
1021 uasm_i_addu(p, ptr, ptr, pte);
1022 uasm_i_mfc0(p, pte, C0_CONTEXT);
1023 uasm_i_lw(p, ptr, 0, ptr); /* cp0 delay */
1024 uasm_i_andi(p, pte, pte, 0xffc); /* load delay */
1025 uasm_i_addu(p, ptr, ptr, pte);
1026 uasm_i_lw(p, pte, 0, ptr);
1027 uasm_i_tlbp(p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
Ralf Baechle234fcd12008-03-08 09:56:28 +00001030static void __cpuinit build_r3000_tlb_load_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 u32 *p = handle_tlbl;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001033 struct uasm_label *l = labels;
1034 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 memset(handle_tlbl, 0, sizeof(handle_tlbl));
1037 memset(labels, 0, sizeof(labels));
1038 memset(relocs, 0, sizeof(relocs));
1039
1040 build_r3000_tlbchange_handler_head(&p, K0, K1);
1041 build_pte_present(&p, &l, &r, K0, K1, label_nopage_tlbl);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001042 uasm_i_nop(&p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 build_make_valid(&p, &r, K0, K1);
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001044 build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Thiemo Seufere30ec452008-01-28 20:05:38 +00001046 uasm_l_nopage_tlbl(&l, p);
1047 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
1048 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 if ((p - handle_tlbl) > FASTPATH_SIZE)
1051 panic("TLB load handler fastpath space exceeded");
1052
Thiemo Seufere30ec452008-01-28 20:05:38 +00001053 uasm_resolve_relocs(relocs, labels);
1054 pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
1055 (unsigned int)(p - handle_tlbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001057 dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
Ralf Baechle234fcd12008-03-08 09:56:28 +00001060static void __cpuinit build_r3000_tlb_store_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 u32 *p = handle_tlbs;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001063 struct uasm_label *l = labels;
1064 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 memset(handle_tlbs, 0, sizeof(handle_tlbs));
1067 memset(labels, 0, sizeof(labels));
1068 memset(relocs, 0, sizeof(relocs));
1069
1070 build_r3000_tlbchange_handler_head(&p, K0, K1);
1071 build_pte_writable(&p, &l, &r, K0, K1, label_nopage_tlbs);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001072 uasm_i_nop(&p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 build_make_write(&p, &r, K0, K1);
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001074 build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Thiemo Seufere30ec452008-01-28 20:05:38 +00001076 uasm_l_nopage_tlbs(&l, p);
1077 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1078 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 if ((p - handle_tlbs) > FASTPATH_SIZE)
1081 panic("TLB store handler fastpath space exceeded");
1082
Thiemo Seufere30ec452008-01-28 20:05:38 +00001083 uasm_resolve_relocs(relocs, labels);
1084 pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
1085 (unsigned int)(p - handle_tlbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001087 dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
Ralf Baechle234fcd12008-03-08 09:56:28 +00001090static void __cpuinit build_r3000_tlb_modify_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
1092 u32 *p = handle_tlbm;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001093 struct uasm_label *l = labels;
1094 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 memset(handle_tlbm, 0, sizeof(handle_tlbm));
1097 memset(labels, 0, sizeof(labels));
1098 memset(relocs, 0, sizeof(relocs));
1099
1100 build_r3000_tlbchange_handler_head(&p, K0, K1);
1101 build_pte_modifiable(&p, &l, &r, K0, K1, label_nopage_tlbm);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001102 uasm_i_nop(&p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 build_make_write(&p, &r, K0, K1);
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001104 build_r3000_pte_reload_tlbwi(&p, K0, K1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Thiemo Seufere30ec452008-01-28 20:05:38 +00001106 uasm_l_nopage_tlbm(&l, p);
1107 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1108 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 if ((p - handle_tlbm) > FASTPATH_SIZE)
1111 panic("TLB modify handler fastpath space exceeded");
1112
Thiemo Seufere30ec452008-01-28 20:05:38 +00001113 uasm_resolve_relocs(relocs, labels);
1114 pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
1115 (unsigned int)(p - handle_tlbm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001117 dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
1120/*
1121 * R4000 style TLB load/store/modify handlers.
1122 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001123static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +00001124build_r4000_tlbchange_handler_head(u32 **p, struct uasm_label **l,
1125 struct uasm_reloc **r, unsigned int pte,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 unsigned int ptr)
1127{
Ralf Baechle875d43e2005-09-03 15:56:16 -07001128#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 build_get_pmde64(p, l, r, pte, ptr); /* get pmd in ptr */
1130#else
1131 build_get_pgde32(p, pte, ptr); /* get pgd in ptr */
1132#endif
1133
Thiemo Seufere30ec452008-01-28 20:05:38 +00001134 UASM_i_MFC0(p, pte, C0_BADVADDR);
1135 UASM_i_LW(p, ptr, 0, ptr);
1136 UASM_i_SRL(p, pte, pte, PAGE_SHIFT + PTE_ORDER - PTE_T_LOG2);
1137 uasm_i_andi(p, pte, pte, (PTRS_PER_PTE - 1) << PTE_T_LOG2);
1138 UASM_i_ADDU(p, ptr, ptr, pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140#ifdef CONFIG_SMP
Thiemo Seufere30ec452008-01-28 20:05:38 +00001141 uasm_l_smp_pgtable_change(l, *p);
1142#endif
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001143 iPTE_LW(p, l, pte, ptr); /* get even pte */
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01001144 if (!m4kc_tlbp_war())
1145 build_tlb_probe_entry(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
Ralf Baechle234fcd12008-03-08 09:56:28 +00001148static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +00001149build_r4000_tlbchange_handler_tail(u32 **p, struct uasm_label **l,
1150 struct uasm_reloc **r, unsigned int tmp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 unsigned int ptr)
1152{
Thiemo Seufere30ec452008-01-28 20:05:38 +00001153 uasm_i_ori(p, ptr, ptr, sizeof(pte_t));
1154 uasm_i_xori(p, ptr, ptr, sizeof(pte_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 build_update_entries(p, tmp, ptr);
1156 build_tlb_write_entry(p, l, r, tlb_indexed);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001157 uasm_l_leave(l, *p);
1158 uasm_i_eret(p); /* return from trap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Ralf Baechle875d43e2005-09-03 15:56:16 -07001160#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 build_get_pgd_vmalloc64(p, l, r, tmp, ptr);
1162#endif
1163}
1164
Ralf Baechle234fcd12008-03-08 09:56:28 +00001165static void __cpuinit build_r4000_tlb_load_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 u32 *p = handle_tlbl;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001168 struct uasm_label *l = labels;
1169 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 memset(handle_tlbl, 0, sizeof(handle_tlbl));
1172 memset(labels, 0, sizeof(labels));
1173 memset(relocs, 0, sizeof(relocs));
1174
1175 if (bcm1250_m3_war()) {
Thiemo Seufere30ec452008-01-28 20:05:38 +00001176 UASM_i_MFC0(&p, K0, C0_BADVADDR);
1177 UASM_i_MFC0(&p, K1, C0_ENTRYHI);
1178 uasm_i_xor(&p, K0, K0, K1);
1179 UASM_i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
1180 uasm_il_bnez(&p, &r, K0, label_leave);
1181 /* No need for uasm_i_nop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183
1184 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
1185 build_pte_present(&p, &l, &r, K0, K1, label_nopage_tlbl);
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01001186 if (m4kc_tlbp_war())
1187 build_tlb_probe_entry(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 build_make_valid(&p, &r, K0, K1);
1189 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1190
Thiemo Seufere30ec452008-01-28 20:05:38 +00001191 uasm_l_nopage_tlbl(&l, p);
1192 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
1193 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 if ((p - handle_tlbl) > FASTPATH_SIZE)
1196 panic("TLB load handler fastpath space exceeded");
1197
Thiemo Seufere30ec452008-01-28 20:05:38 +00001198 uasm_resolve_relocs(relocs, labels);
1199 pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
1200 (unsigned int)(p - handle_tlbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001202 dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Ralf Baechle234fcd12008-03-08 09:56:28 +00001205static void __cpuinit build_r4000_tlb_store_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 u32 *p = handle_tlbs;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001208 struct uasm_label *l = labels;
1209 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 memset(handle_tlbs, 0, sizeof(handle_tlbs));
1212 memset(labels, 0, sizeof(labels));
1213 memset(relocs, 0, sizeof(relocs));
1214
1215 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
1216 build_pte_writable(&p, &l, &r, K0, K1, label_nopage_tlbs);
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01001217 if (m4kc_tlbp_war())
1218 build_tlb_probe_entry(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 build_make_write(&p, &r, K0, K1);
1220 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1221
Thiemo Seufere30ec452008-01-28 20:05:38 +00001222 uasm_l_nopage_tlbs(&l, p);
1223 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1224 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 if ((p - handle_tlbs) > FASTPATH_SIZE)
1227 panic("TLB store handler fastpath space exceeded");
1228
Thiemo Seufere30ec452008-01-28 20:05:38 +00001229 uasm_resolve_relocs(relocs, labels);
1230 pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
1231 (unsigned int)(p - handle_tlbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001233 dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Ralf Baechle234fcd12008-03-08 09:56:28 +00001236static void __cpuinit build_r4000_tlb_modify_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238 u32 *p = handle_tlbm;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001239 struct uasm_label *l = labels;
1240 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 memset(handle_tlbm, 0, sizeof(handle_tlbm));
1243 memset(labels, 0, sizeof(labels));
1244 memset(relocs, 0, sizeof(relocs));
1245
1246 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
1247 build_pte_modifiable(&p, &l, &r, K0, K1, label_nopage_tlbm);
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01001248 if (m4kc_tlbp_war())
1249 build_tlb_probe_entry(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /* Present and writable bits set, set accessed and dirty bits. */
1251 build_make_write(&p, &r, K0, K1);
1252 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1253
Thiemo Seufere30ec452008-01-28 20:05:38 +00001254 uasm_l_nopage_tlbm(&l, p);
1255 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1256 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 if ((p - handle_tlbm) > FASTPATH_SIZE)
1259 panic("TLB modify handler fastpath space exceeded");
1260
Thiemo Seufere30ec452008-01-28 20:05:38 +00001261 uasm_resolve_relocs(relocs, labels);
1262 pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
1263 (unsigned int)(p - handle_tlbm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001265 dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
Ralf Baechle234fcd12008-03-08 09:56:28 +00001268void __cpuinit build_tlb_refill_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 /*
1271 * The refill handler is generated per-CPU, multi-node systems
1272 * may have local storage for it. The other handlers are only
1273 * needed once.
1274 */
1275 static int run_once = 0;
1276
Ralf Baechle10cc3522007-10-11 23:46:15 +01001277 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 case CPU_R2000:
1279 case CPU_R3000:
1280 case CPU_R3000A:
1281 case CPU_R3081E:
1282 case CPU_TX3912:
1283 case CPU_TX3922:
1284 case CPU_TX3927:
1285 build_r3000_tlb_refill_handler();
1286 if (!run_once) {
1287 build_r3000_tlb_load_handler();
1288 build_r3000_tlb_store_handler();
1289 build_r3000_tlb_modify_handler();
1290 run_once++;
1291 }
1292 break;
1293
1294 case CPU_R6000:
1295 case CPU_R6000A:
1296 panic("No R6000 TLB refill handler yet");
1297 break;
1298
1299 case CPU_R8000:
1300 panic("No R8000 TLB refill handler yet");
1301 break;
1302
1303 default:
1304 build_r4000_tlb_refill_handler();
1305 if (!run_once) {
1306 build_r4000_tlb_load_handler();
1307 build_r4000_tlb_store_handler();
1308 build_r4000_tlb_modify_handler();
1309 run_once++;
1310 }
1311 }
1312}
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001313
Ralf Baechle234fcd12008-03-08 09:56:28 +00001314void __cpuinit flush_tlb_handlers(void)
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001315{
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001316 local_flush_icache_range((unsigned long)handle_tlbl,
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001317 (unsigned long)handle_tlbl + sizeof(handle_tlbl));
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001318 local_flush_icache_range((unsigned long)handle_tlbs,
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001319 (unsigned long)handle_tlbs + sizeof(handle_tlbs));
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001320 local_flush_icache_range((unsigned long)handle_tlbm,
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001321 (unsigned long)handle_tlbm + sizeof(handle_tlbm));
1322}