blob: 678e63398461eaa1c5cb289b6defc441c17cc7e8 [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
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +01009 * Copyright (C) 2005, 2007 Maciej W. Rozycki
Ralf Baechle41c594a2006-04-05 09:45:45 +010010 * Copyright (C) 2006 Ralf Baechle (ralf@linux-mips.org)
11 *
12 * ... and the days got worse and worse and now you see
13 * I've gone completly out of my mind.
14 *
15 * They're coming to take me a away haha
16 * they're coming to take me a away hoho hihi haha
17 * to the funny farm where code is beautiful all the time ...
18 *
19 * (Condolences to Napoleon XIV)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/init.h>
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/war.h>
29
Thiemo Seufere30ec452008-01-28 20:05:38 +000030#include "uasm.h"
31
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010032static inline int r45k_bvahwbug(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 /* XXX: We should probe for the presence of this bug, but we don't. */
35 return 0;
36}
37
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010038static inline int r4k_250MHZhwbug(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 /* XXX: We should probe for the presence of this bug, but we don't. */
41 return 0;
42}
43
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010044static inline int __maybe_unused bcm1250_m3_war(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 return BCM1250_M3_WAR;
47}
48
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010049static inline int __maybe_unused r10000_llsc_war(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 return R10000_LLSC_WAR;
52}
53
54/*
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +010055 * 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 */
Ralf Baechle234fcd12008-03-08 09:56:28 +000063static int __cpuinit m4kc_tlbp_war(void)
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +010064{
65 return (current_cpu_data.processor_id & 0xffff00) ==
66 (PRID_COMP_MIPS | PRID_IMP_4KC);
67}
68
Thiemo Seufere30ec452008-01-28 20:05:38 +000069/* Handle labels (which must be positive integers). */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070enum label_id {
Thiemo Seufere30ec452008-01-28 20:05:38 +000071 label_second_part = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 label_leave,
Atsushi Nemoto656be922006-10-26 00:08:31 +090073#ifdef MODULE_START
74 label_module_alloc,
75#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
Thiemo Seufere30ec452008-01-28 20:05:38 +000087UASM_L_LA(_second_part)
88UASM_L_LA(_leave)
Atsushi Nemoto656be922006-10-26 00:08:31 +090089#ifdef MODULE_START
Thiemo Seufere30ec452008-01-28 20:05:38 +000090UASM_L_LA(_module_alloc)
Atsushi Nemoto656be922006-10-26 00:08:31 +090091#endif
Thiemo Seufere30ec452008-01-28 20:05:38 +000092UASM_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)
Atsushi Nemoto656be922006-10-26 00:08:31 +0900101
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +0200102/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/* The only general purpose registers allowed in TLB handlers. */
119#define K0 26
120#define K1 27
121
122/* Some CP0 registers */
Ralf Baechle41c594a2006-04-05 09:45:45 +0100123#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Ralf Baechle875d43e2005-09-03 15:56:16 -0700133#ifdef CONFIG_64BIT
Thiemo Seufere30ec452008-01-28 20:05:38 +0000134# define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_XCONTEXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000136# define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_CONTEXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#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 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000147static u32 tlb_handler[128] __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149/* simply assume worst case size for labels and relocs */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000150static struct uasm_label labels[128] __cpuinitdata;
151static struct uasm_reloc relocs[128] __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153/*
154 * The R3000 TLB handler is simple.
155 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000156static void __cpuinit build_r3000_tlb_refill_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 long pgdc = (long)pgd_current;
159 u32 *p;
160
161 memset(tlb_handler, 0, sizeof(tlb_handler));
162 p = tlb_handler;
163
Thiemo Seufere30ec452008-01-28 20:05:38 +0000164 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 if (p > tlb_handler + 32)
183 panic("TLB refill handler space exceeded");
184
Thiemo Seufere30ec452008-01-28 20:05:38 +0000185 pr_debug("Wrote TLB refill handler (%u instructions).\n",
186 (unsigned int)(p - tlb_handler));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Ralf Baechle91b05e62006-03-29 18:53:00 +0100188 memcpy((void *)ebase, tlb_handler, 0x80);
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +0200189
190 dump_handler((u32 *)ebase, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
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 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000200static u32 final_handler[64] __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
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 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000224static void __cpuinit __maybe_unused build_tlb_probe_entry(u32 **p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Ralf Baechle10cc3522007-10-11 23:46:15 +0100226 switch (current_cpu_type()) {
Thomas Bogendoerfer326e2e12008-05-12 13:55:42 +0200227 /* Found by experiment: R4600 v2.0/R4700 needs this, too. */
Thiemo Seuferf5b4d952005-09-09 17:11:50 +0000228 case CPU_R4600:
Thomas Bogendoerfer326e2e12008-05-12 13:55:42 +0200229 case CPU_R4700:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 case CPU_R5000:
231 case CPU_R5000A:
232 case CPU_NEVADA:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000233 uasm_i_nop(p);
234 uasm_i_tlbp(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 break;
236
237 default:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000238 uasm_i_tlbp(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 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
Ralf Baechle234fcd12008-03-08 09:56:28 +0000249static void __cpuinit build_tlb_write_entry(u32 **p, struct uasm_label **l,
Thiemo Seufere30ec452008-01-28 20:05:38 +0000250 struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 enum tlb_write_entry wmode)
252{
253 void(*tlbw)(u32 **) = NULL;
254
255 switch (wmode) {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000256 case tlb_random: tlbw = uasm_i_tlbwr; break;
257 case tlb_indexed: tlbw = uasm_i_tlbwi; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259
Ralf Baechle161548b2008-01-29 10:14:54 +0000260 if (cpu_has_mips_r2) {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000261 uasm_i_ehb(p);
Ralf Baechle161548b2008-01-29 10:14:54 +0000262 tlbw(p);
263 return;
264 }
265
Ralf Baechle10cc3522007-10-11 23:46:15 +0100266 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 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 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000277 uasm_il_bgezl(p, r, 0, label_tlbw_hazard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 tlbw(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000279 uasm_l_tlbw_hazard(l, *p);
280 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 break;
282
283 case CPU_R4600:
284 case CPU_R4700:
285 case CPU_R5000:
286 case CPU_R5000A:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000287 uasm_i_nop(p);
Maciej W. Rozycki2c93e122005-06-30 10:51:01 +0000288 tlbw(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000289 uasm_i_nop(p);
Maciej W. Rozycki2c93e122005-06-30 10:51:01 +0000290 break;
291
292 case CPU_R4300:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 case CPU_5KC:
294 case CPU_TX49XX:
Pete Popovbdf21b12005-07-14 17:47:57 +0000295 case CPU_PR4450:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000296 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 tlbw(p);
298 break;
299
300 case CPU_R10000:
301 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -0400302 case CPU_R14000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 case CPU_4KC:
Thomas Bogendoerferb1ec4c82008-03-26 16:42:54 +0100304 case CPU_4KEC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 case CPU_SB1:
Andrew Isaacson93ce2f522005-10-19 23:56:20 -0700306 case CPU_SB1A:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 case CPU_4KSC:
308 case CPU_20KC:
309 case CPU_25KF:
Aurelien Jarno1c0c13e2007-09-25 15:40:12 +0200310 case CPU_BCM3302:
311 case CPU_BCM4710:
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800312 case CPU_LOONGSON2:
David Daneyec454d82008-12-11 15:33:35 -0800313 case CPU_CAVIUM_OCTEON:
Shinya Kuribayashia644b272009-03-03 18:05:51 +0900314 case CPU_R5500:
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +0100315 if (m4kc_tlbp_war())
Thiemo Seufere30ec452008-01-28 20:05:38 +0000316 uasm_i_nop(p);
Manuel Lauss2f794d02009-03-25 17:49:30 +0100317 case CPU_ALCHEMY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 tlbw(p);
319 break;
320
321 case CPU_NEVADA:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000322 uasm_i_nop(p); /* QED specifies 2 nops hazard */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /*
324 * This branch uses up a mtc0 hazard nop slot and saves
325 * a nop after the tlbw instruction.
326 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000327 uasm_il_bgezl(p, r, 0, label_tlbw_hazard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 tlbw(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000329 uasm_l_tlbw_hazard(l, *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 break;
331
332 case CPU_RM7000:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000333 uasm_i_nop(p);
334 uasm_i_nop(p);
335 uasm_i_nop(p);
336 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 tlbw(p);
338 break;
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 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 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000347 uasm_i_ssnop(p);
348 uasm_i_ssnop(p);
349 uasm_i_ssnop(p);
350 uasm_i_ssnop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 tlbw(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000352 uasm_i_ssnop(p);
353 uasm_i_ssnop(p);
354 uasm_i_ssnop(p);
355 uasm_i_ssnop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 break;
357
358 case CPU_VR4111:
359 case CPU_VR4121:
360 case CPU_VR4122:
361 case CPU_VR4181:
362 case CPU_VR4181A:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000363 uasm_i_nop(p);
364 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 tlbw(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000366 uasm_i_nop(p);
367 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 break;
369
370 case CPU_VR4131:
371 case CPU_VR4133:
Ralf Baechle7623deb2005-08-29 16:49:55 +0000372 case CPU_R5432:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000373 uasm_i_nop(p);
374 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 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
Ralf Baechle875d43e2005-09-03 15:56:16 -0700385#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/*
387 * TMP and PTR are scratch.
388 * TMP will be clobbered, PTR will hold the pmd entry.
389 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000390static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000391build_get_pmde64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 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 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000399 uasm_i_dmfc0(p, tmp, C0_BADVADDR);
Atsushi Nemoto656be922006-10-26 00:08:31 +0900400#ifdef MODULE_START
Thiemo Seufere30ec452008-01-28 20:05:38 +0000401 uasm_il_bltz(p, r, tmp, label_module_alloc);
Atsushi Nemoto656be922006-10-26 00:08:31 +0900402#else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000403 uasm_il_bltz(p, r, tmp, label_vmalloc);
Atsushi Nemoto656be922006-10-26 00:08:31 +0900404#endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000405 /* No uasm_i_nop needed here, since the next insn doesn't touch TMP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407#ifdef CONFIG_SMP
Ralf Baechle41c594a2006-04-05 09:45:45 +0100408# ifdef CONFIG_MIPS_MT_SMTC
409 /*
410 * SMTC uses TCBind value as "CPU" index
411 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000412 uasm_i_mfc0(p, ptr, C0_TCBIND);
413 uasm_i_dsrl(p, ptr, ptr, 19);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100414# else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /*
Thiemo Seufer1b3a6e92005-04-01 14:07:13 +0000416 * 64 bit SMP running in XKPHYS has smp_processor_id() << 3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * stored in CONTEXT.
418 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000419 uasm_i_dmfc0(p, ptr, C0_CONTEXT);
420 uasm_i_dsrl(p, ptr, ptr, 23);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100421#endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000422 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426#else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000427 UASM_i_LA_mostly(p, ptr, pgdc);
428 uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429#endif
430
Thiemo Seufere30ec452008-01-28 20:05:38 +0000431 uasm_l_vmalloc_done(l, *p);
Ralf Baechle242954b2006-10-24 02:29:01 +0100432
433 if (PGDIR_SHIFT - 3 < 32) /* get pgd offset in bytes */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000434 uasm_i_dsrl(p, tmp, tmp, PGDIR_SHIFT-3);
Ralf Baechle242954b2006-10-24 02:29:01 +0100435 else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000436 uasm_i_dsrl32(p, tmp, tmp, PGDIR_SHIFT - 3 - 32);
Ralf Baechle242954b2006-10-24 02:29:01 +0100437
Thiemo Seufere30ec452008-01-28 20:05:38 +0000438 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447/*
448 * BVADDR is the faulting address, PTR is scratch.
449 * PTR will hold the pgd for vmalloc.
450 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000451static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000452build_get_pgd_vmalloc64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 unsigned int bvaddr, unsigned int ptr)
454{
455 long swpd = (long)swapper_pg_dir;
456
Atsushi Nemoto656be922006-10-26 00:08:31 +0900457#ifdef MODULE_START
458 long modd = (long)module_pg_dir;
459
Thiemo Seufere30ec452008-01-28 20:05:38 +0000460 uasm_l_module_alloc(l, *p);
Atsushi Nemoto656be922006-10-26 00:08:31 +0900461 /*
462 * Assumption:
463 * VMALLOC_START >= 0xc000000000000000UL
464 * MODULE_START >= 0xe000000000000000UL
465 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000466 UASM_i_SLL(p, ptr, bvaddr, 2);
467 uasm_il_bgez(p, r, ptr, label_vmalloc);
Atsushi Nemoto656be922006-10-26 00:08:31 +0900468
Thiemo Seufere30ec452008-01-28 20:05:38 +0000469 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 */
Atsushi Nemoto656be922006-10-26 00:08:31 +0900472 } else {
473 /* unlikely configuration */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000474 uasm_i_nop(p); /* delay slot */
475 UASM_i_LA(p, ptr, MODULE_START);
Atsushi Nemoto656be922006-10-26 00:08:31 +0900476 }
Thiemo Seufere30ec452008-01-28 20:05:38 +0000477 uasm_i_dsubu(p, bvaddr, bvaddr, ptr);
Atsushi Nemoto656be922006-10-26 00:08:31 +0900478
Thiemo Seufere30ec452008-01-28 20:05:38 +0000479 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));
Atsushi Nemoto656be922006-10-26 00:08:31 +0900482 } else {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000483 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));
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100487 else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000488 uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(modd));
Atsushi Nemoto656be922006-10-26 00:08:31 +0900489 }
490
Thiemo Seufere30ec452008-01-28 20:05:38 +0000491 uasm_l_vmalloc(l, *p);
492 if (uasm_in_compat_space_p(MODULE_START) &&
493 !uasm_rel_lo(MODULE_START) &&
Atsushi Nemoto656be922006-10-26 00:08:31 +0900494 MODULE_START << 32 == VMALLOC_START)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000495 uasm_i_dsll32(p, ptr, ptr, 0); /* typical case */
Atsushi Nemoto656be922006-10-26 00:08:31 +0900496 else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000497 UASM_i_LA(p, ptr, VMALLOC_START);
Atsushi Nemoto656be922006-10-26 00:08:31 +0900498#else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000499 uasm_l_vmalloc(l, *p);
500 UASM_i_LA(p, ptr, VMALLOC_START);
Atsushi Nemoto656be922006-10-26 00:08:31 +0900501#endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000502 uasm_i_dsubu(p, bvaddr, bvaddr, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Thiemo Seufere30ec452008-01-28 20:05:38 +0000504 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 } else {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000508 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));
Maciej W. Rozycki619b6e12007-10-23 12:43:25 +0100512 else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000513 uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(swpd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515}
516
Ralf Baechle875d43e2005-09-03 15:56:16 -0700517#else /* !CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519/*
520 * TMP and PTR are scratch.
521 * TMP will be clobbered, PTR will hold the pgd entry.
522 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000523static void __cpuinit __maybe_unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524build_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
Ralf Baechle41c594a2006-04-05 09:45:45 +0100530#ifdef CONFIG_MIPS_MT_SMTC
531 /*
532 * SMTC uses TCBind value as "CPU" index
533 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000534 uasm_i_mfc0(p, ptr, C0_TCBIND);
535 UASM_i_LA_mostly(p, tmp, pgdc);
536 uasm_i_srl(p, ptr, ptr, 19);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100537#else
538 /*
539 * smp_processor_id() << 3 is stored in CONTEXT.
540 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000541 uasm_i_mfc0(p, ptr, C0_CONTEXT);
542 UASM_i_LA_mostly(p, tmp, pgdc);
543 uasm_i_srl(p, ptr, ptr, 23);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100544#endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000545 uasm_i_addu(p, ptr, tmp, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546#else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000547 UASM_i_LA_mostly(p, ptr, pgdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548#endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000549 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Ralf Baechle875d43e2005-09-03 15:56:16 -0700556#endif /* !CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Ralf Baechle234fcd12008-03-08 09:56:28 +0000558static void __cpuinit build_adjust_context(u32 **p, unsigned int ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Ralf Baechle242954b2006-10-24 02:29:01 +0100560 unsigned int shift = 4 - (PTE_T_LOG2 + 1) + PAGE_SHIFT - 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 unsigned int mask = (PTRS_PER_PTE / 2 - 1) << (PTE_T_LOG2 + 1);
562
Ralf Baechle10cc3522007-10-11 23:46:15 +0100563 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 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)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000580 UASM_i_SRL(p, ctx, ctx, shift);
581 uasm_i_andi(p, ctx, ctx, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Ralf Baechle234fcd12008-03-08 09:56:28 +0000584static void __cpuinit build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
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 */
Ralf Baechle10cc3522007-10-11 23:46:15 +0100593 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 case CPU_NEVADA:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000595 UASM_i_LW(p, ptr, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 GET_CONTEXT(p, tmp); /* get context reg */
597 break;
598
599 default:
600 GET_CONTEXT(p, tmp); /* get context reg */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000601 UASM_i_LW(p, ptr, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 break;
603 }
604
605 build_adjust_context(p, tmp);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000606 UASM_i_ADDU(p, ptr, ptr, tmp); /* add in offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Ralf Baechle234fcd12008-03-08 09:56:28 +0000609static void __cpuinit build_update_entries(u32 **p, unsigned int tmp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 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) {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000618 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 } 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 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000629 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634#else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000635 UASM_i_LW(p, tmp, 0, ptep); /* get even pte */
636 UASM_i_LW(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (r45k_bvahwbug())
638 build_tlb_probe_entry(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000639 UASM_i_SRL(p, tmp, tmp, 6); /* convert to entrylo0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (r4k_250MHZhwbug())
Thiemo Seufere30ec452008-01-28 20:05:38 +0000641 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (r45k_bvahwbug())
Thiemo Seufere30ec452008-01-28 20:05:38 +0000645 uasm_i_mfc0(p, tmp, C0_INDEX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (r4k_250MHZhwbug())
Thiemo Seufere30ec452008-01-28 20:05:38 +0000647 uasm_i_mtc0(p, 0, C0_ENTRYLO1);
648 uasm_i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649#endif
650}
651
David Daneye6f72d32009-05-20 11:40:58 -0700652/*
653 * For a 64-bit kernel, we are using the 64-bit XTLB refill exception
654 * because EXL == 0. If we wrap, we can also use the 32 instruction
655 * slots before the XTLB refill exception handler which belong to the
656 * unused TLB refill exception.
657 */
658#define MIPS64_REFILL_INSNS 32
659
Ralf Baechle234fcd12008-03-08 09:56:28 +0000660static void __cpuinit build_r4000_tlb_refill_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 u32 *p = tlb_handler;
Thiemo Seufere30ec452008-01-28 20:05:38 +0000663 struct uasm_label *l = labels;
664 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 u32 *f;
666 unsigned int final_len;
667
668 memset(tlb_handler, 0, sizeof(tlb_handler));
669 memset(labels, 0, sizeof(labels));
670 memset(relocs, 0, sizeof(relocs));
671 memset(final_handler, 0, sizeof(final_handler));
672
673 /*
674 * create the plain linear handler
675 */
676 if (bcm1250_m3_war()) {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000677 UASM_i_MFC0(&p, K0, C0_BADVADDR);
678 UASM_i_MFC0(&p, K1, C0_ENTRYHI);
679 uasm_i_xor(&p, K0, K0, K1);
680 UASM_i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
681 uasm_il_bnez(&p, &r, K0, label_leave);
682 /* No need for uasm_i_nop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
Ralf Baechle875d43e2005-09-03 15:56:16 -0700685#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 build_get_pmde64(&p, &l, &r, K0, K1); /* get pmd in K1 */
687#else
688 build_get_pgde32(&p, K0, K1); /* get pgd in K1 */
689#endif
690
691 build_get_ptep(&p, K0, K1);
692 build_update_entries(&p, K0, K1);
693 build_tlb_write_entry(&p, &l, &r, tlb_random);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000694 uasm_l_leave(&l, p);
695 uasm_i_eret(&p); /* return from trap */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Ralf Baechle875d43e2005-09-03 15:56:16 -0700697#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 build_get_pgd_vmalloc64(&p, &l, &r, K0, K1);
699#endif
700
701 /*
702 * Overflow check: For the 64bit handler, we need at least one
703 * free instruction slot for the wrap-around branch. In worst
704 * case, if the intended insertion point is a delay slot, we
Matt LaPlante4b3f6862006-10-03 22:21:02 +0200705 * need three, with the second nop'ed and the third being
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 * unused.
707 */
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800708 /* Loongson2 ebase is different than r4k, we have more space */
709#if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if ((p - tlb_handler) > 64)
711 panic("TLB refill handler space exceeded");
712#else
David Daneye6f72d32009-05-20 11:40:58 -0700713 if (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 1)
714 || (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 3)
715 && uasm_insn_has_bdelay(relocs,
716 tlb_handler + MIPS64_REFILL_INSNS - 3)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 panic("TLB refill handler space exceeded");
718#endif
719
720 /*
721 * Now fold the handler in the TLB refill handler space.
722 */
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800723#if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 f = final_handler;
725 /* Simplest case, just copy the handler. */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000726 uasm_copy_handler(relocs, labels, tlb_handler, p, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 final_len = p - tlb_handler;
Ralf Baechle875d43e2005-09-03 15:56:16 -0700728#else /* CONFIG_64BIT */
David Daneye6f72d32009-05-20 11:40:58 -0700729 f = final_handler + MIPS64_REFILL_INSNS;
730 if ((p - tlb_handler) <= MIPS64_REFILL_INSNS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 /* Just copy the handler. */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000732 uasm_copy_handler(relocs, labels, tlb_handler, p, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 final_len = p - tlb_handler;
734 } else {
David Daneye6f72d32009-05-20 11:40:58 -0700735 /*
736 * Split two instructions before the end. One for the
737 * branch and one for the instruction in the delay
738 * slot.
739 */
740 u32 *split = tlb_handler + MIPS64_REFILL_INSNS - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 /*
David Daneye6f72d32009-05-20 11:40:58 -0700743 * Find the split point. If the branch would fall in
744 * a delay slot, we must back up an additional
745 * instruction so that it is no longer in a delay
746 * slot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000748 if (uasm_insn_has_bdelay(relocs, split - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 split--;
750
751 /* Copy first part of the handler. */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000752 uasm_copy_handler(relocs, labels, tlb_handler, split, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 f += split - tlb_handler;
754
755 /* Insert branch. */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000756 uasm_l_split(&l, final_handler);
757 uasm_il_b(&f, &r, label_split);
758 if (uasm_insn_has_bdelay(relocs, split))
759 uasm_i_nop(&f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 else {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000761 uasm_copy_handler(relocs, labels, split, split + 1, f);
762 uasm_move_labels(labels, f, f + 1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 f++;
764 split++;
765 }
766
767 /* Copy the rest of the handler. */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000768 uasm_copy_handler(relocs, labels, split, p, final_handler);
David Daneye6f72d32009-05-20 11:40:58 -0700769 final_len = (f - (final_handler + MIPS64_REFILL_INSNS)) +
770 (p - split);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Ralf Baechle875d43e2005-09-03 15:56:16 -0700772#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Thiemo Seufere30ec452008-01-28 20:05:38 +0000774 uasm_resolve_relocs(relocs, labels);
775 pr_debug("Wrote TLB refill handler (%u instructions).\n",
776 final_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Ralf Baechle91b05e62006-03-29 18:53:00 +0100778 memcpy((void *)ebase, final_handler, 0x100);
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +0200779
780 dump_handler((u32 *)ebase, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
783/*
784 * TLB load/store/modify handlers.
785 *
786 * Only the fastpath gets synthesized at runtime, the slowpath for
787 * do_page_fault remains normal asm.
788 */
789extern void tlb_do_page_fault_0(void);
790extern void tlb_do_page_fault_1(void);
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792/*
793 * 128 instructions for the fastpath handler is generous and should
794 * never be exceeded.
795 */
796#define FASTPATH_SIZE 128
797
Franck Bui-Huucbdbe072007-10-18 09:11:16 +0200798u32 handle_tlbl[FASTPATH_SIZE] __cacheline_aligned;
799u32 handle_tlbs[FASTPATH_SIZE] __cacheline_aligned;
800u32 handle_tlbm[FASTPATH_SIZE] __cacheline_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Ralf Baechle234fcd12008-03-08 09:56:28 +0000802static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000803iPTE_LW(u32 **p, struct uasm_label **l, unsigned int pte, unsigned int ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805#ifdef CONFIG_SMP
806# ifdef CONFIG_64BIT_PHYS_ADDR
807 if (cpu_has_64bits)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000808 uasm_i_lld(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 else
810# endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000811 UASM_i_LL(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812#else
813# ifdef CONFIG_64BIT_PHYS_ADDR
814 if (cpu_has_64bits)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000815 uasm_i_ld(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 else
817# endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000818 UASM_i_LW(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819#endif
820}
821
Ralf Baechle234fcd12008-03-08 09:56:28 +0000822static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000823iPTE_SW(u32 **p, struct uasm_reloc **r, unsigned int pte, unsigned int ptr,
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +0000824 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +0000826#ifdef CONFIG_64BIT_PHYS_ADDR
827 unsigned int hwmode = mode & (_PAGE_VALID | _PAGE_DIRTY);
828#endif
829
Thiemo Seufere30ec452008-01-28 20:05:38 +0000830 uasm_i_ori(p, pte, pte, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831#ifdef CONFIG_SMP
832# ifdef CONFIG_64BIT_PHYS_ADDR
833 if (cpu_has_64bits)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000834 uasm_i_scd(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_SC(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 if (r10000_llsc_war())
Thiemo Seufere30ec452008-01-28 20:05:38 +0000840 uasm_il_beqzl(p, r, pte, label_smp_pgtable_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000842 uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844# ifdef CONFIG_64BIT_PHYS_ADDR
845 if (!cpu_has_64bits) {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000846 /* no uasm_i_nop needed */
847 uasm_i_ll(p, pte, sizeof(pte_t) / 2, ptr);
848 uasm_i_ori(p, pte, pte, hwmode);
849 uasm_i_sc(p, pte, sizeof(pte_t) / 2, ptr);
850 uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
851 /* no uasm_i_nop needed */
852 uasm_i_lw(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 } else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000854 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855# else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000856 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857# endif
858#else
859# ifdef CONFIG_64BIT_PHYS_ADDR
860 if (cpu_has_64bits)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000861 uasm_i_sd(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 else
863# endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000864 UASM_i_SW(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866# ifdef CONFIG_64BIT_PHYS_ADDR
867 if (!cpu_has_64bits) {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000868 uasm_i_lw(p, pte, sizeof(pte_t) / 2, ptr);
869 uasm_i_ori(p, pte, pte, hwmode);
870 uasm_i_sw(p, pte, sizeof(pte_t) / 2, ptr);
871 uasm_i_lw(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873# endif
874#endif
875}
876
877/*
878 * Check if PTE is present, if not then jump to LABEL. PTR points to
879 * the page table where this PTE is located, PTE will be re-loaded
880 * with it's original value.
881 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000882static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000883build_pte_present(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 unsigned int pte, unsigned int ptr, enum label_id lid)
885{
Thiemo Seufere30ec452008-01-28 20:05:38 +0000886 uasm_i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
887 uasm_i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
888 uasm_il_bnez(p, r, pte, lid);
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +0000889 iPTE_LW(p, l, pte, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892/* Make PTE valid, store result in PTR. */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000893static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000894build_make_valid(u32 **p, struct uasm_reloc **r, unsigned int pte,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 unsigned int ptr)
896{
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +0000897 unsigned int mode = _PAGE_VALID | _PAGE_ACCESSED;
898
899 iPTE_SW(p, r, pte, ptr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902/*
903 * Check if PTE can be written to, if not branch to LABEL. Regardless
904 * restore PTE with value from PTR when done.
905 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000906static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000907build_pte_writable(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 unsigned int pte, unsigned int ptr, enum label_id lid)
909{
Thiemo Seufere30ec452008-01-28 20:05:38 +0000910 uasm_i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
911 uasm_i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
912 uasm_il_bnez(p, r, pte, lid);
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +0000913 iPTE_LW(p, l, pte, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
916/* Make PTE writable, update software status bits as well, then store
917 * at PTR.
918 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000919static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000920build_make_write(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_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID
924 | _PAGE_DIRTY);
925
926 iPTE_SW(p, r, pte, ptr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
929/*
930 * Check if PTE can be modified, if not branch to LABEL. Regardless
931 * restore PTE with value from PTR when done.
932 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000933static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000934build_pte_modifiable(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 unsigned int pte, unsigned int ptr, enum label_id lid)
936{
Thiemo Seufere30ec452008-01-28 20:05:38 +0000937 uasm_i_andi(p, pte, pte, _PAGE_WRITE);
938 uasm_il_beqz(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/*
943 * R3000 style TLB load/store/modify handlers.
944 */
945
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +0000946/*
947 * This places the pte into ENTRYLO0 and writes it with tlbwi.
948 * Then it returns.
949 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000950static void __cpuinit
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +0000951build_r3000_pte_reload_tlbwi(u32 **p, unsigned int pte, unsigned int tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Thiemo Seufere30ec452008-01-28 20:05:38 +0000953 uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
954 uasm_i_mfc0(p, tmp, C0_EPC); /* cp0 delay */
955 uasm_i_tlbwi(p);
956 uasm_i_jr(p, tmp);
957 uasm_i_rfe(p); /* branch delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960/*
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +0000961 * This places the pte into ENTRYLO0 and writes it with tlbwi
962 * or tlbwr as appropriate. This is because the index register
963 * may have the probe fail bit set as a result of a trap on a
964 * kseg2 access, i.e. without refill. Then it returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000966static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000967build_r3000_tlb_reload_write(u32 **p, struct uasm_label **l,
968 struct uasm_reloc **r, unsigned int pte,
969 unsigned int tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Thiemo Seufere30ec452008-01-28 20:05:38 +0000971 uasm_i_mfc0(p, tmp, C0_INDEX);
972 uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
973 uasm_il_bltz(p, r, tmp, label_r3000_write_probe_fail); /* cp0 delay */
974 uasm_i_mfc0(p, tmp, C0_EPC); /* branch delay */
975 uasm_i_tlbwi(p); /* cp0 delay */
976 uasm_i_jr(p, tmp);
977 uasm_i_rfe(p); /* branch delay */
978 uasm_l_r3000_write_probe_fail(l, *p);
979 uasm_i_tlbwr(p); /* cp0 delay */
980 uasm_i_jr(p, tmp);
981 uasm_i_rfe(p); /* branch delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Ralf Baechle234fcd12008-03-08 09:56:28 +0000984static void __cpuinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
986 unsigned int ptr)
987{
988 long pgdc = (long)pgd_current;
989
Thiemo Seufere30ec452008-01-28 20:05:38 +0000990 uasm_i_mfc0(p, pte, C0_BADVADDR);
991 uasm_i_lui(p, ptr, uasm_rel_hi(pgdc)); /* cp0 delay */
992 uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
993 uasm_i_srl(p, pte, pte, 22); /* load delay */
994 uasm_i_sll(p, pte, pte, 2);
995 uasm_i_addu(p, ptr, ptr, pte);
996 uasm_i_mfc0(p, pte, C0_CONTEXT);
997 uasm_i_lw(p, ptr, 0, ptr); /* cp0 delay */
998 uasm_i_andi(p, pte, pte, 0xffc); /* load delay */
999 uasm_i_addu(p, ptr, ptr, pte);
1000 uasm_i_lw(p, pte, 0, ptr);
1001 uasm_i_tlbp(p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Ralf Baechle234fcd12008-03-08 09:56:28 +00001004static void __cpuinit build_r3000_tlb_load_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 u32 *p = handle_tlbl;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001007 struct uasm_label *l = labels;
1008 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 memset(handle_tlbl, 0, sizeof(handle_tlbl));
1011 memset(labels, 0, sizeof(labels));
1012 memset(relocs, 0, sizeof(relocs));
1013
1014 build_r3000_tlbchange_handler_head(&p, K0, K1);
1015 build_pte_present(&p, &l, &r, K0, K1, label_nopage_tlbl);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001016 uasm_i_nop(&p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 build_make_valid(&p, &r, K0, K1);
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001018 build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Thiemo Seufere30ec452008-01-28 20:05:38 +00001020 uasm_l_nopage_tlbl(&l, p);
1021 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
1022 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 if ((p - handle_tlbl) > FASTPATH_SIZE)
1025 panic("TLB load handler fastpath space exceeded");
1026
Thiemo Seufere30ec452008-01-28 20:05:38 +00001027 uasm_resolve_relocs(relocs, labels);
1028 pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
1029 (unsigned int)(p - handle_tlbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001031 dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
Ralf Baechle234fcd12008-03-08 09:56:28 +00001034static void __cpuinit build_r3000_tlb_store_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 u32 *p = handle_tlbs;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001037 struct uasm_label *l = labels;
1038 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 memset(handle_tlbs, 0, sizeof(handle_tlbs));
1041 memset(labels, 0, sizeof(labels));
1042 memset(relocs, 0, sizeof(relocs));
1043
1044 build_r3000_tlbchange_handler_head(&p, K0, K1);
1045 build_pte_writable(&p, &l, &r, K0, K1, label_nopage_tlbs);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001046 uasm_i_nop(&p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 build_make_write(&p, &r, K0, K1);
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001048 build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Thiemo Seufere30ec452008-01-28 20:05:38 +00001050 uasm_l_nopage_tlbs(&l, p);
1051 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1052 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 if ((p - handle_tlbs) > FASTPATH_SIZE)
1055 panic("TLB store handler fastpath space exceeded");
1056
Thiemo Seufere30ec452008-01-28 20:05:38 +00001057 uasm_resolve_relocs(relocs, labels);
1058 pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
1059 (unsigned int)(p - handle_tlbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001061 dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
Ralf Baechle234fcd12008-03-08 09:56:28 +00001064static void __cpuinit build_r3000_tlb_modify_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 u32 *p = handle_tlbm;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001067 struct uasm_label *l = labels;
1068 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 memset(handle_tlbm, 0, sizeof(handle_tlbm));
1071 memset(labels, 0, sizeof(labels));
1072 memset(relocs, 0, sizeof(relocs));
1073
1074 build_r3000_tlbchange_handler_head(&p, K0, K1);
1075 build_pte_modifiable(&p, &l, &r, K0, K1, label_nopage_tlbm);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001076 uasm_i_nop(&p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 build_make_write(&p, &r, K0, K1);
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001078 build_r3000_pte_reload_tlbwi(&p, K0, K1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Thiemo Seufere30ec452008-01-28 20:05:38 +00001080 uasm_l_nopage_tlbm(&l, p);
1081 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1082 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 if ((p - handle_tlbm) > FASTPATH_SIZE)
1085 panic("TLB modify handler fastpath space exceeded");
1086
Thiemo Seufere30ec452008-01-28 20:05:38 +00001087 uasm_resolve_relocs(relocs, labels);
1088 pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
1089 (unsigned int)(p - handle_tlbm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001091 dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
1094/*
1095 * R4000 style TLB load/store/modify handlers.
1096 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001097static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +00001098build_r4000_tlbchange_handler_head(u32 **p, struct uasm_label **l,
1099 struct uasm_reloc **r, unsigned int pte,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 unsigned int ptr)
1101{
Ralf Baechle875d43e2005-09-03 15:56:16 -07001102#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 build_get_pmde64(p, l, r, pte, ptr); /* get pmd in ptr */
1104#else
1105 build_get_pgde32(p, pte, ptr); /* get pgd in ptr */
1106#endif
1107
Thiemo Seufere30ec452008-01-28 20:05:38 +00001108 UASM_i_MFC0(p, pte, C0_BADVADDR);
1109 UASM_i_LW(p, ptr, 0, ptr);
1110 UASM_i_SRL(p, pte, pte, PAGE_SHIFT + PTE_ORDER - PTE_T_LOG2);
1111 uasm_i_andi(p, pte, pte, (PTRS_PER_PTE - 1) << PTE_T_LOG2);
1112 UASM_i_ADDU(p, ptr, ptr, pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114#ifdef CONFIG_SMP
Thiemo Seufere30ec452008-01-28 20:05:38 +00001115 uasm_l_smp_pgtable_change(l, *p);
1116#endif
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001117 iPTE_LW(p, l, pte, ptr); /* get even pte */
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01001118 if (!m4kc_tlbp_war())
1119 build_tlb_probe_entry(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
Ralf Baechle234fcd12008-03-08 09:56:28 +00001122static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +00001123build_r4000_tlbchange_handler_tail(u32 **p, struct uasm_label **l,
1124 struct uasm_reloc **r, unsigned int tmp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 unsigned int ptr)
1126{
Thiemo Seufere30ec452008-01-28 20:05:38 +00001127 uasm_i_ori(p, ptr, ptr, sizeof(pte_t));
1128 uasm_i_xori(p, ptr, ptr, sizeof(pte_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 build_update_entries(p, tmp, ptr);
1130 build_tlb_write_entry(p, l, r, tlb_indexed);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001131 uasm_l_leave(l, *p);
1132 uasm_i_eret(p); /* return from trap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Ralf Baechle875d43e2005-09-03 15:56:16 -07001134#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 build_get_pgd_vmalloc64(p, l, r, tmp, ptr);
1136#endif
1137}
1138
Ralf Baechle234fcd12008-03-08 09:56:28 +00001139static void __cpuinit build_r4000_tlb_load_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 u32 *p = handle_tlbl;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001142 struct uasm_label *l = labels;
1143 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 memset(handle_tlbl, 0, sizeof(handle_tlbl));
1146 memset(labels, 0, sizeof(labels));
1147 memset(relocs, 0, sizeof(relocs));
1148
1149 if (bcm1250_m3_war()) {
Thiemo Seufere30ec452008-01-28 20:05:38 +00001150 UASM_i_MFC0(&p, K0, C0_BADVADDR);
1151 UASM_i_MFC0(&p, K1, C0_ENTRYHI);
1152 uasm_i_xor(&p, K0, K0, K1);
1153 UASM_i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
1154 uasm_il_bnez(&p, &r, K0, label_leave);
1155 /* No need for uasm_i_nop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157
1158 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
1159 build_pte_present(&p, &l, &r, K0, K1, label_nopage_tlbl);
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01001160 if (m4kc_tlbp_war())
1161 build_tlb_probe_entry(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 build_make_valid(&p, &r, K0, K1);
1163 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1164
Thiemo Seufere30ec452008-01-28 20:05:38 +00001165 uasm_l_nopage_tlbl(&l, p);
1166 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
1167 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 if ((p - handle_tlbl) > FASTPATH_SIZE)
1170 panic("TLB load handler fastpath space exceeded");
1171
Thiemo Seufere30ec452008-01-28 20:05:38 +00001172 uasm_resolve_relocs(relocs, labels);
1173 pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
1174 (unsigned int)(p - handle_tlbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001176 dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Ralf Baechle234fcd12008-03-08 09:56:28 +00001179static void __cpuinit build_r4000_tlb_store_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
1181 u32 *p = handle_tlbs;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001182 struct uasm_label *l = labels;
1183 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 memset(handle_tlbs, 0, sizeof(handle_tlbs));
1186 memset(labels, 0, sizeof(labels));
1187 memset(relocs, 0, sizeof(relocs));
1188
1189 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
1190 build_pte_writable(&p, &l, &r, K0, K1, label_nopage_tlbs);
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01001191 if (m4kc_tlbp_war())
1192 build_tlb_probe_entry(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 build_make_write(&p, &r, K0, K1);
1194 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1195
Thiemo Seufere30ec452008-01-28 20:05:38 +00001196 uasm_l_nopage_tlbs(&l, p);
1197 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1198 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 if ((p - handle_tlbs) > FASTPATH_SIZE)
1201 panic("TLB store handler fastpath space exceeded");
1202
Thiemo Seufere30ec452008-01-28 20:05:38 +00001203 uasm_resolve_relocs(relocs, labels);
1204 pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
1205 (unsigned int)(p - handle_tlbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001207 dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Ralf Baechle234fcd12008-03-08 09:56:28 +00001210static void __cpuinit build_r4000_tlb_modify_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 u32 *p = handle_tlbm;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001213 struct uasm_label *l = labels;
1214 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 memset(handle_tlbm, 0, sizeof(handle_tlbm));
1217 memset(labels, 0, sizeof(labels));
1218 memset(relocs, 0, sizeof(relocs));
1219
1220 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
1221 build_pte_modifiable(&p, &l, &r, K0, K1, label_nopage_tlbm);
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01001222 if (m4kc_tlbp_war())
1223 build_tlb_probe_entry(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 /* Present and writable bits set, set accessed and dirty bits. */
1225 build_make_write(&p, &r, K0, K1);
1226 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1227
Thiemo Seufere30ec452008-01-28 20:05:38 +00001228 uasm_l_nopage_tlbm(&l, p);
1229 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1230 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 if ((p - handle_tlbm) > FASTPATH_SIZE)
1233 panic("TLB modify handler fastpath space exceeded");
1234
Thiemo Seufere30ec452008-01-28 20:05:38 +00001235 uasm_resolve_relocs(relocs, labels);
1236 pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
1237 (unsigned int)(p - handle_tlbm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001239 dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
Ralf Baechle234fcd12008-03-08 09:56:28 +00001242void __cpuinit build_tlb_refill_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 /*
1245 * The refill handler is generated per-CPU, multi-node systems
1246 * may have local storage for it. The other handlers are only
1247 * needed once.
1248 */
1249 static int run_once = 0;
1250
Ralf Baechle10cc3522007-10-11 23:46:15 +01001251 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 case CPU_R2000:
1253 case CPU_R3000:
1254 case CPU_R3000A:
1255 case CPU_R3081E:
1256 case CPU_TX3912:
1257 case CPU_TX3922:
1258 case CPU_TX3927:
1259 build_r3000_tlb_refill_handler();
1260 if (!run_once) {
1261 build_r3000_tlb_load_handler();
1262 build_r3000_tlb_store_handler();
1263 build_r3000_tlb_modify_handler();
1264 run_once++;
1265 }
1266 break;
1267
1268 case CPU_R6000:
1269 case CPU_R6000A:
1270 panic("No R6000 TLB refill handler yet");
1271 break;
1272
1273 case CPU_R8000:
1274 panic("No R8000 TLB refill handler yet");
1275 break;
1276
1277 default:
1278 build_r4000_tlb_refill_handler();
1279 if (!run_once) {
1280 build_r4000_tlb_load_handler();
1281 build_r4000_tlb_store_handler();
1282 build_r4000_tlb_modify_handler();
1283 run_once++;
1284 }
1285 }
1286}
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001287
Ralf Baechle234fcd12008-03-08 09:56:28 +00001288void __cpuinit flush_tlb_handlers(void)
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001289{
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001290 local_flush_icache_range((unsigned long)handle_tlbl,
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001291 (unsigned long)handle_tlbl + sizeof(handle_tlbl));
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001292 local_flush_icache_range((unsigned long)handle_tlbs,
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001293 (unsigned long)handle_tlbs + sizeof(handle_tlbs));
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001294 local_flush_icache_range((unsigned long)handle_tlbm,
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001295 (unsigned long)handle_tlbm + sizeof(handle_tlbm));
1296}