blob: 883cf76fb2bdb636544c2c3762b60db8ce0f6037 [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)
David Daneyfd062c82009-05-27 17:47:44 -070011 * Copyright (C) 2008, 2009 Cavium Networks, Inc.
Ralf Baechle41c594a2006-04-05 09:45:45 +010012 *
13 * ... and the days got worse and worse and now you see
14 * I've gone completly out of my mind.
15 *
16 * They're coming to take me a away haha
17 * they're coming to take me a away hoho hihi haha
18 * to the funny farm where code is beautiful all the time ...
19 *
20 * (Condolences to Napoleon XIV)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
David Daney95affdd2009-05-20 11:40:59 -070023#include <linux/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/types.h>
Ralf Baechle631330f2009-06-19 14:05:26 +010026#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/string.h>
28#include <linux/init.h>
David Daney3d8bfdd2010-12-21 14:19:11 -080029#include <linux/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
David Daney3d8bfdd2010-12-21 14:19:11 -080031#include <asm/cacheflush.h>
32#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/war.h>
Florian Fainelli3482d712010-01-28 15:21:24 +010034#include <asm/uasm.h>
Thiemo Seufere30ec452008-01-28 20:05:38 +000035
David Daney1ec56322010-04-28 12:16:18 -070036/*
37 * TLB load/store/modify handlers.
38 *
39 * Only the fastpath gets synthesized at runtime, the slowpath for
40 * do_page_fault remains normal asm.
41 */
42extern void tlb_do_page_fault_0(void);
43extern void tlb_do_page_fault_1(void);
44
45
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010046static inline int r45k_bvahwbug(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 /* XXX: We should probe for the presence of this bug, but we don't. */
49 return 0;
50}
51
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010052static inline int r4k_250MHZhwbug(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 /* XXX: We should probe for the presence of this bug, but we don't. */
55 return 0;
56}
57
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010058static inline int __maybe_unused bcm1250_m3_war(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 return BCM1250_M3_WAR;
61}
62
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010063static inline int __maybe_unused r10000_llsc_war(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 return R10000_LLSC_WAR;
66}
67
David Daneycc33ae42010-12-20 15:54:50 -080068static int use_bbit_insns(void)
69{
70 switch (current_cpu_type()) {
71 case CPU_CAVIUM_OCTEON:
72 case CPU_CAVIUM_OCTEON_PLUS:
73 case CPU_CAVIUM_OCTEON2:
74 return 1;
75 default:
76 return 0;
77 }
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +010081 * Found by experiment: At least some revisions of the 4kc throw under
82 * some circumstances a machine check exception, triggered by invalid
83 * values in the index register. Delaying the tlbp instruction until
84 * after the next branch, plus adding an additional nop in front of
85 * tlbwi/tlbwr avoids the invalid index register values. Nobody knows
86 * why; it's not an issue caused by the core RTL.
87 *
88 */
Ralf Baechle234fcd12008-03-08 09:56:28 +000089static int __cpuinit m4kc_tlbp_war(void)
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +010090{
91 return (current_cpu_data.processor_id & 0xffff00) ==
92 (PRID_COMP_MIPS | PRID_IMP_4KC);
93}
94
Thiemo Seufere30ec452008-01-28 20:05:38 +000095/* Handle labels (which must be positive integers). */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096enum label_id {
Thiemo Seufere30ec452008-01-28 20:05:38 +000097 label_second_part = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 label_leave,
99 label_vmalloc,
100 label_vmalloc_done,
101 label_tlbw_hazard,
102 label_split,
David Daney6dd93442010-02-10 15:12:47 -0800103 label_tlbl_goaround1,
104 label_tlbl_goaround2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 label_nopage_tlbl,
106 label_nopage_tlbs,
107 label_nopage_tlbm,
108 label_smp_pgtable_change,
109 label_r3000_write_probe_fail,
David Daney1ec56322010-04-28 12:16:18 -0700110 label_large_segbits_fault,
David Daneyfd062c82009-05-27 17:47:44 -0700111#ifdef CONFIG_HUGETLB_PAGE
112 label_tlb_huge_update,
113#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
Thiemo Seufere30ec452008-01-28 20:05:38 +0000116UASM_L_LA(_second_part)
117UASM_L_LA(_leave)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000118UASM_L_LA(_vmalloc)
119UASM_L_LA(_vmalloc_done)
120UASM_L_LA(_tlbw_hazard)
121UASM_L_LA(_split)
David Daney6dd93442010-02-10 15:12:47 -0800122UASM_L_LA(_tlbl_goaround1)
123UASM_L_LA(_tlbl_goaround2)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000124UASM_L_LA(_nopage_tlbl)
125UASM_L_LA(_nopage_tlbs)
126UASM_L_LA(_nopage_tlbm)
127UASM_L_LA(_smp_pgtable_change)
128UASM_L_LA(_r3000_write_probe_fail)
David Daney1ec56322010-04-28 12:16:18 -0700129UASM_L_LA(_large_segbits_fault)
David Daneyfd062c82009-05-27 17:47:44 -0700130#ifdef CONFIG_HUGETLB_PAGE
131UASM_L_LA(_tlb_huge_update)
132#endif
Atsushi Nemoto656be922006-10-26 00:08:31 +0900133
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +0200134/*
135 * For debug purposes.
136 */
137static inline void dump_handler(const u32 *handler, int count)
138{
139 int i;
140
141 pr_debug("\t.set push\n");
142 pr_debug("\t.set noreorder\n");
143
144 for (i = 0; i < count; i++)
145 pr_debug("\t%p\t.word 0x%08x\n", &handler[i], handler[i]);
146
147 pr_debug("\t.set pop\n");
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* The only general purpose registers allowed in TLB handlers. */
151#define K0 26
152#define K1 27
153
154/* Some CP0 registers */
Ralf Baechle41c594a2006-04-05 09:45:45 +0100155#define C0_INDEX 0, 0
156#define C0_ENTRYLO0 2, 0
157#define C0_TCBIND 2, 2
158#define C0_ENTRYLO1 3, 0
159#define C0_CONTEXT 4, 0
David Daneyfd062c82009-05-27 17:47:44 -0700160#define C0_PAGEMASK 5, 0
Ralf Baechle41c594a2006-04-05 09:45:45 +0100161#define C0_BADVADDR 8, 0
162#define C0_ENTRYHI 10, 0
163#define C0_EPC 14, 0
164#define C0_XCONTEXT 20, 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Ralf Baechle875d43e2005-09-03 15:56:16 -0700166#ifdef CONFIG_64BIT
Thiemo Seufere30ec452008-01-28 20:05:38 +0000167# define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_XCONTEXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000169# define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_CONTEXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#endif
171
172/* The worst case length of the handler is around 18 instructions for
173 * R3000-style TLBs and up to 63 instructions for R4000-style TLBs.
174 * Maximum space available is 32 instructions for R3000 and 64
175 * instructions for R4000.
176 *
177 * We deliberately chose a buffer size of 128, so we won't scribble
178 * over anything important on overflow before we panic.
179 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000180static u32 tlb_handler[128] __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/* simply assume worst case size for labels and relocs */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000183static struct uasm_label labels[128] __cpuinitdata;
184static struct uasm_reloc relocs[128] __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
David Daney1ec56322010-04-28 12:16:18 -0700186#ifdef CONFIG_64BIT
187static int check_for_high_segbits __cpuinitdata;
188#endif
189
David Daney3d8bfdd2010-12-21 14:19:11 -0800190#ifdef CONFIG_MIPS_PGD_C0_CONTEXT
191
192static unsigned int kscratch_used_mask __cpuinitdata;
193
194static int __cpuinit allocate_kscratch(void)
195{
196 int r;
197 unsigned int a = cpu_data[0].kscratch_mask & ~kscratch_used_mask;
198
199 r = ffs(a);
200
201 if (r == 0)
202 return -1;
203
204 r--; /* make it zero based */
205
206 kscratch_used_mask |= (1 << r);
207
208 return r;
209}
210
211static int pgd_reg __cpuinitdata;
212
213#else /* !CONFIG_MIPS_PGD_C0_CONTEXT*/
David Daney82622282009-10-14 12:16:56 -0700214/*
215 * CONFIG_MIPS_PGD_C0_CONTEXT implies 64 bit and lack of pgd_current,
216 * we cannot do r3000 under these circumstances.
David Daney3d8bfdd2010-12-21 14:19:11 -0800217 *
218 * Declare pgd_current here instead of including mmu_context.h to avoid type
219 * conflicts for tlbmiss_handler_setup_pgd
David Daney82622282009-10-14 12:16:56 -0700220 */
David Daney3d8bfdd2010-12-21 14:19:11 -0800221extern unsigned long pgd_current[];
David Daney82622282009-10-14 12:16:56 -0700222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/*
224 * The R3000 TLB handler is simple.
225 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000226static void __cpuinit build_r3000_tlb_refill_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 long pgdc = (long)pgd_current;
229 u32 *p;
230
231 memset(tlb_handler, 0, sizeof(tlb_handler));
232 p = tlb_handler;
233
Thiemo Seufere30ec452008-01-28 20:05:38 +0000234 uasm_i_mfc0(&p, K0, C0_BADVADDR);
235 uasm_i_lui(&p, K1, uasm_rel_hi(pgdc)); /* cp0 delay */
236 uasm_i_lw(&p, K1, uasm_rel_lo(pgdc), K1);
237 uasm_i_srl(&p, K0, K0, 22); /* load delay */
238 uasm_i_sll(&p, K0, K0, 2);
239 uasm_i_addu(&p, K1, K1, K0);
240 uasm_i_mfc0(&p, K0, C0_CONTEXT);
241 uasm_i_lw(&p, K1, 0, K1); /* cp0 delay */
242 uasm_i_andi(&p, K0, K0, 0xffc); /* load delay */
243 uasm_i_addu(&p, K1, K1, K0);
244 uasm_i_lw(&p, K0, 0, K1);
245 uasm_i_nop(&p); /* load delay */
246 uasm_i_mtc0(&p, K0, C0_ENTRYLO0);
247 uasm_i_mfc0(&p, K1, C0_EPC); /* cp0 delay */
248 uasm_i_tlbwr(&p); /* cp0 delay */
249 uasm_i_jr(&p, K1);
250 uasm_i_rfe(&p); /* branch delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 if (p > tlb_handler + 32)
253 panic("TLB refill handler space exceeded");
254
Thiemo Seufere30ec452008-01-28 20:05:38 +0000255 pr_debug("Wrote TLB refill handler (%u instructions).\n",
256 (unsigned int)(p - tlb_handler));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Ralf Baechle91b05e62006-03-29 18:53:00 +0100258 memcpy((void *)ebase, tlb_handler, 0x80);
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +0200259
260 dump_handler((u32 *)ebase, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
David Daney82622282009-10-14 12:16:56 -0700262#endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264/*
265 * The R4000 TLB handler is much more complicated. We have two
266 * consecutive handler areas with 32 instructions space each.
267 * Since they aren't used at the same time, we can overflow in the
268 * other one.To keep things simple, we first assume linear space,
269 * then we relocate it to the final handler layout as needed.
270 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000271static u32 final_handler[64] __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273/*
274 * Hazards
275 *
276 * From the IDT errata for the QED RM5230 (Nevada), processor revision 1.0:
277 * 2. A timing hazard exists for the TLBP instruction.
278 *
279 * stalling_instruction
280 * TLBP
281 *
282 * The JTLB is being read for the TLBP throughout the stall generated by the
283 * previous instruction. This is not really correct as the stalling instruction
284 * can modify the address used to access the JTLB. The failure symptom is that
285 * the TLBP instruction will use an address created for the stalling instruction
286 * and not the address held in C0_ENHI and thus report the wrong results.
287 *
288 * The software work-around is to not allow the instruction preceding the TLBP
289 * to stall - make it an NOP or some other instruction guaranteed not to stall.
290 *
291 * Errata 2 will not be fixed. This errata is also on the R5000.
292 *
293 * As if we MIPS hackers wouldn't know how to nop pipelines happy ...
294 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000295static void __cpuinit __maybe_unused build_tlb_probe_entry(u32 **p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Ralf Baechle10cc3522007-10-11 23:46:15 +0100297 switch (current_cpu_type()) {
Thomas Bogendoerfer326e2e12008-05-12 13:55:42 +0200298 /* Found by experiment: R4600 v2.0/R4700 needs this, too. */
Thiemo Seuferf5b4d952005-09-09 17:11:50 +0000299 case CPU_R4600:
Thomas Bogendoerfer326e2e12008-05-12 13:55:42 +0200300 case CPU_R4700:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 case CPU_R5000:
302 case CPU_R5000A:
303 case CPU_NEVADA:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000304 uasm_i_nop(p);
305 uasm_i_tlbp(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 break;
307
308 default:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000309 uasm_i_tlbp(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 break;
311 }
312}
313
314/*
315 * Write random or indexed TLB entry, and care about the hazards from
316 * the preceeding mtc0 and for the following eret.
317 */
318enum tlb_write_entry { tlb_random, tlb_indexed };
319
Ralf Baechle234fcd12008-03-08 09:56:28 +0000320static void __cpuinit build_tlb_write_entry(u32 **p, struct uasm_label **l,
Thiemo Seufere30ec452008-01-28 20:05:38 +0000321 struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 enum tlb_write_entry wmode)
323{
324 void(*tlbw)(u32 **) = NULL;
325
326 switch (wmode) {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000327 case tlb_random: tlbw = uasm_i_tlbwr; break;
328 case tlb_indexed: tlbw = uasm_i_tlbwi; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330
Ralf Baechle161548b2008-01-29 10:14:54 +0000331 if (cpu_has_mips_r2) {
David Daney41f0e4d2009-05-12 12:41:53 -0700332 if (cpu_has_mips_r2_exec_hazard)
333 uasm_i_ehb(p);
Ralf Baechle161548b2008-01-29 10:14:54 +0000334 tlbw(p);
335 return;
336 }
337
Ralf Baechle10cc3522007-10-11 23:46:15 +0100338 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 case CPU_R4000PC:
340 case CPU_R4000SC:
341 case CPU_R4000MC:
342 case CPU_R4400PC:
343 case CPU_R4400SC:
344 case CPU_R4400MC:
345 /*
346 * This branch uses up a mtc0 hazard nop slot and saves
347 * two nops after the tlbw instruction.
348 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000349 uasm_il_bgezl(p, r, 0, label_tlbw_hazard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 tlbw(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000351 uasm_l_tlbw_hazard(l, *p);
352 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
354
355 case CPU_R4600:
356 case CPU_R4700:
357 case CPU_R5000:
358 case CPU_R5000A:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000359 uasm_i_nop(p);
Maciej W. Rozycki2c93e122005-06-30 10:51:01 +0000360 tlbw(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000361 uasm_i_nop(p);
Maciej W. Rozycki2c93e122005-06-30 10:51:01 +0000362 break;
363
364 case CPU_R4300:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 case CPU_5KC:
366 case CPU_TX49XX:
Pete Popovbdf21b12005-07-14 17:47:57 +0000367 case CPU_PR4450:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000368 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 tlbw(p);
370 break;
371
372 case CPU_R10000:
373 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -0400374 case CPU_R14000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 case CPU_4KC:
Thomas Bogendoerferb1ec4c82008-03-26 16:42:54 +0100376 case CPU_4KEC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 case CPU_SB1:
Andrew Isaacson93ce2f522005-10-19 23:56:20 -0700378 case CPU_SB1A:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 case CPU_4KSC:
380 case CPU_20KC:
381 case CPU_25KF:
Kevin Cernekee602977b2010-10-16 14:22:30 -0700382 case CPU_BMIPS32:
383 case CPU_BMIPS3300:
384 case CPU_BMIPS4350:
385 case CPU_BMIPS4380:
386 case CPU_BMIPS5000:
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800387 case CPU_LOONGSON2:
Shinya Kuribayashia644b272009-03-03 18:05:51 +0900388 case CPU_R5500:
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +0100389 if (m4kc_tlbp_war())
Thiemo Seufere30ec452008-01-28 20:05:38 +0000390 uasm_i_nop(p);
Manuel Lauss2f794d02009-03-25 17:49:30 +0100391 case CPU_ALCHEMY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 tlbw(p);
393 break;
394
395 case CPU_NEVADA:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000396 uasm_i_nop(p); /* QED specifies 2 nops hazard */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /*
398 * This branch uses up a mtc0 hazard nop slot and saves
399 * a nop after the tlbw instruction.
400 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000401 uasm_il_bgezl(p, r, 0, label_tlbw_hazard);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 tlbw(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000403 uasm_l_tlbw_hazard(l, *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 break;
405
406 case CPU_RM7000:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000407 uasm_i_nop(p);
408 uasm_i_nop(p);
409 uasm_i_nop(p);
410 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 tlbw(p);
412 break;
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 case CPU_RM9000:
415 /*
416 * When the JTLB is updated by tlbwi or tlbwr, a subsequent
417 * use of the JTLB for instructions should not occur for 4
418 * cpu cycles and use for data translations should not occur
419 * for 3 cpu cycles.
420 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000421 uasm_i_ssnop(p);
422 uasm_i_ssnop(p);
423 uasm_i_ssnop(p);
424 uasm_i_ssnop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 tlbw(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000426 uasm_i_ssnop(p);
427 uasm_i_ssnop(p);
428 uasm_i_ssnop(p);
429 uasm_i_ssnop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 break;
431
432 case CPU_VR4111:
433 case CPU_VR4121:
434 case CPU_VR4122:
435 case CPU_VR4181:
436 case CPU_VR4181A:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000437 uasm_i_nop(p);
438 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 tlbw(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000440 uasm_i_nop(p);
441 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 break;
443
444 case CPU_VR4131:
445 case CPU_VR4133:
Ralf Baechle7623deb2005-08-29 16:49:55 +0000446 case CPU_R5432:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000447 uasm_i_nop(p);
448 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 tlbw(p);
450 break;
451
Lars-Peter Clausen83ccf692010-07-17 11:07:51 +0000452 case CPU_JZRISC:
453 tlbw(p);
454 uasm_i_nop(p);
455 break;
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 default:
458 panic("No TLB refill handler yet (CPU type: %d)",
459 current_cpu_data.cputype);
460 break;
461 }
462}
463
David Daney6dd93442010-02-10 15:12:47 -0800464static __cpuinit __maybe_unused void build_convert_pte_to_entrylo(u32 **p,
465 unsigned int reg)
466{
467 if (kernel_uses_smartmips_rixi) {
468 UASM_i_SRL(p, reg, reg, ilog2(_PAGE_NO_EXEC));
469 UASM_i_ROTR(p, reg, reg, ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
470 } else {
471#ifdef CONFIG_64BIT_PHYS_ADDR
David Daney3be60222010-04-28 12:16:17 -0700472 uasm_i_dsrl_safe(p, reg, reg, ilog2(_PAGE_GLOBAL));
David Daney6dd93442010-02-10 15:12:47 -0800473#else
474 UASM_i_SRL(p, reg, reg, ilog2(_PAGE_GLOBAL));
475#endif
476 }
477}
478
David Daneyfd062c82009-05-27 17:47:44 -0700479#ifdef CONFIG_HUGETLB_PAGE
David Daney6dd93442010-02-10 15:12:47 -0800480
481static __cpuinit void build_restore_pagemask(u32 **p,
482 struct uasm_reloc **r,
483 unsigned int tmp,
484 enum label_id lid)
485{
486 /* Reset default page size */
487 if (PM_DEFAULT_MASK >> 16) {
488 uasm_i_lui(p, tmp, PM_DEFAULT_MASK >> 16);
489 uasm_i_ori(p, tmp, tmp, PM_DEFAULT_MASK & 0xffff);
490 uasm_il_b(p, r, lid);
491 uasm_i_mtc0(p, tmp, C0_PAGEMASK);
492 } else if (PM_DEFAULT_MASK) {
493 uasm_i_ori(p, tmp, 0, PM_DEFAULT_MASK);
494 uasm_il_b(p, r, lid);
495 uasm_i_mtc0(p, tmp, C0_PAGEMASK);
496 } else {
497 uasm_il_b(p, r, lid);
498 uasm_i_mtc0(p, 0, C0_PAGEMASK);
499 }
500}
501
David Daneyfd062c82009-05-27 17:47:44 -0700502static __cpuinit void build_huge_tlb_write_entry(u32 **p,
503 struct uasm_label **l,
504 struct uasm_reloc **r,
505 unsigned int tmp,
506 enum tlb_write_entry wmode)
507{
508 /* Set huge page tlb entry size */
509 uasm_i_lui(p, tmp, PM_HUGE_MASK >> 16);
510 uasm_i_ori(p, tmp, tmp, PM_HUGE_MASK & 0xffff);
511 uasm_i_mtc0(p, tmp, C0_PAGEMASK);
512
513 build_tlb_write_entry(p, l, r, wmode);
514
David Daney6dd93442010-02-10 15:12:47 -0800515 build_restore_pagemask(p, r, tmp, label_leave);
David Daneyfd062c82009-05-27 17:47:44 -0700516}
517
518/*
519 * Check if Huge PTE is present, if so then jump to LABEL.
520 */
521static void __cpuinit
522build_is_huge_pte(u32 **p, struct uasm_reloc **r, unsigned int tmp,
523 unsigned int pmd, int lid)
524{
525 UASM_i_LW(p, tmp, 0, pmd);
David Daneycc33ae42010-12-20 15:54:50 -0800526 if (use_bbit_insns()) {
527 uasm_il_bbit1(p, r, tmp, ilog2(_PAGE_HUGE), lid);
528 } else {
529 uasm_i_andi(p, tmp, tmp, _PAGE_HUGE);
530 uasm_il_bnez(p, r, tmp, lid);
531 }
David Daneyfd062c82009-05-27 17:47:44 -0700532}
533
534static __cpuinit void build_huge_update_entries(u32 **p,
535 unsigned int pte,
536 unsigned int tmp)
537{
538 int small_sequence;
539
540 /*
541 * A huge PTE describes an area the size of the
542 * configured huge page size. This is twice the
543 * of the large TLB entry size we intend to use.
544 * A TLB entry half the size of the configured
545 * huge page size is configured into entrylo0
546 * and entrylo1 to cover the contiguous huge PTE
547 * address space.
548 */
549 small_sequence = (HPAGE_SIZE >> 7) < 0x10000;
550
551 /* We can clobber tmp. It isn't used after this.*/
552 if (!small_sequence)
553 uasm_i_lui(p, tmp, HPAGE_SIZE >> (7 + 16));
554
David Daney6dd93442010-02-10 15:12:47 -0800555 build_convert_pte_to_entrylo(p, pte);
David Daney9b8c3892010-02-10 15:12:44 -0800556 UASM_i_MTC0(p, pte, C0_ENTRYLO0); /* load it */
David Daneyfd062c82009-05-27 17:47:44 -0700557 /* convert to entrylo1 */
558 if (small_sequence)
559 UASM_i_ADDIU(p, pte, pte, HPAGE_SIZE >> 7);
560 else
561 UASM_i_ADDU(p, pte, pte, tmp);
562
David Daney9b8c3892010-02-10 15:12:44 -0800563 UASM_i_MTC0(p, pte, C0_ENTRYLO1); /* load it */
David Daneyfd062c82009-05-27 17:47:44 -0700564}
565
566static __cpuinit void build_huge_handler_tail(u32 **p,
567 struct uasm_reloc **r,
568 struct uasm_label **l,
569 unsigned int pte,
570 unsigned int ptr)
571{
572#ifdef CONFIG_SMP
573 UASM_i_SC(p, pte, 0, ptr);
574 uasm_il_beqz(p, r, pte, label_tlb_huge_update);
575 UASM_i_LW(p, pte, 0, ptr); /* Needed because SC killed our PTE */
576#else
577 UASM_i_SW(p, pte, 0, ptr);
578#endif
579 build_huge_update_entries(p, pte, ptr);
580 build_huge_tlb_write_entry(p, l, r, pte, tlb_indexed);
581}
582#endif /* CONFIG_HUGETLB_PAGE */
583
Ralf Baechle875d43e2005-09-03 15:56:16 -0700584#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/*
586 * TMP and PTR are scratch.
587 * TMP will be clobbered, PTR will hold the pmd entry.
588 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000589static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000590build_get_pmde64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 unsigned int tmp, unsigned int ptr)
592{
David Daney82622282009-10-14 12:16:56 -0700593#ifndef CONFIG_MIPS_PGD_C0_CONTEXT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 long pgdc = (long)pgd_current;
David Daney82622282009-10-14 12:16:56 -0700595#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /*
597 * The vmalloc handling is not in the hotpath.
598 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000599 uasm_i_dmfc0(p, tmp, C0_BADVADDR);
David Daney1ec56322010-04-28 12:16:18 -0700600
601 if (check_for_high_segbits) {
602 /*
603 * The kernel currently implicitely assumes that the
604 * MIPS SEGBITS parameter for the processor is
605 * (PGDIR_SHIFT+PGDIR_BITS) or less, and will never
606 * allocate virtual addresses outside the maximum
607 * range for SEGBITS = (PGDIR_SHIFT+PGDIR_BITS). But
608 * that doesn't prevent user code from accessing the
609 * higher xuseg addresses. Here, we make sure that
610 * everything but the lower xuseg addresses goes down
611 * the module_alloc/vmalloc path.
612 */
613 uasm_i_dsrl_safe(p, ptr, tmp, PGDIR_SHIFT + PGD_ORDER + PAGE_SHIFT - 3);
614 uasm_il_bnez(p, r, ptr, label_vmalloc);
615 } else {
616 uasm_il_bltz(p, r, tmp, label_vmalloc);
617 }
Thiemo Seufere30ec452008-01-28 20:05:38 +0000618 /* No uasm_i_nop needed here, since the next insn doesn't touch TMP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
David Daney82622282009-10-14 12:16:56 -0700620#ifdef CONFIG_MIPS_PGD_C0_CONTEXT
David Daney3d8bfdd2010-12-21 14:19:11 -0800621 if (pgd_reg != -1) {
622 /* pgd is in pgd_reg */
623 UASM_i_MFC0(p, ptr, 31, pgd_reg);
624 } else {
625 /*
626 * &pgd << 11 stored in CONTEXT [23..63].
627 */
628 UASM_i_MFC0(p, ptr, C0_CONTEXT);
629
630 /* Clear lower 23 bits of context. */
631 uasm_i_dins(p, ptr, 0, 0, 23);
632
633 /* 1 0 1 0 1 << 6 xkphys cached */
634 uasm_i_ori(p, ptr, ptr, 0x540);
635 uasm_i_drotr(p, ptr, ptr, 11);
636 }
David Daney82622282009-10-14 12:16:56 -0700637#elif defined(CONFIG_SMP)
Ralf Baechle41c594a2006-04-05 09:45:45 +0100638# ifdef CONFIG_MIPS_MT_SMTC
639 /*
640 * SMTC uses TCBind value as "CPU" index
641 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000642 uasm_i_mfc0(p, ptr, C0_TCBIND);
David Daney3be60222010-04-28 12:16:17 -0700643 uasm_i_dsrl_safe(p, ptr, ptr, 19);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100644# else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /*
Thiemo Seufer1b3a6e92005-04-01 14:07:13 +0000646 * 64 bit SMP running in XKPHYS has smp_processor_id() << 3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 * stored in CONTEXT.
648 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000649 uasm_i_dmfc0(p, ptr, C0_CONTEXT);
David Daney3be60222010-04-28 12:16:17 -0700650 uasm_i_dsrl_safe(p, ptr, ptr, 23);
David Daney82622282009-10-14 12:16:56 -0700651# endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000652 UASM_i_LA_mostly(p, tmp, pgdc);
653 uasm_i_daddu(p, ptr, ptr, tmp);
654 uasm_i_dmfc0(p, tmp, C0_BADVADDR);
655 uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656#else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000657 UASM_i_LA_mostly(p, ptr, pgdc);
658 uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659#endif
660
Thiemo Seufere30ec452008-01-28 20:05:38 +0000661 uasm_l_vmalloc_done(l, *p);
Ralf Baechle242954b2006-10-24 02:29:01 +0100662
David Daney3be60222010-04-28 12:16:17 -0700663 /* get pgd offset in bytes */
664 uasm_i_dsrl_safe(p, tmp, tmp, PGDIR_SHIFT - 3);
Ralf Baechle242954b2006-10-24 02:29:01 +0100665
Thiemo Seufere30ec452008-01-28 20:05:38 +0000666 uasm_i_andi(p, tmp, tmp, (PTRS_PER_PGD - 1)<<3);
667 uasm_i_daddu(p, ptr, ptr, tmp); /* add in pgd offset */
David Daney325f8a02009-12-04 13:52:36 -0800668#ifndef __PAGETABLE_PMD_FOLDED
Thiemo Seufere30ec452008-01-28 20:05:38 +0000669 uasm_i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
670 uasm_i_ld(p, ptr, 0, ptr); /* get pmd pointer */
David Daney3be60222010-04-28 12:16:17 -0700671 uasm_i_dsrl_safe(p, tmp, tmp, PMD_SHIFT-3); /* get pmd offset in bytes */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000672 uasm_i_andi(p, tmp, tmp, (PTRS_PER_PMD - 1)<<3);
673 uasm_i_daddu(p, ptr, ptr, tmp); /* add in pmd offset */
David Daney325f8a02009-12-04 13:52:36 -0800674#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
David Daney1ec56322010-04-28 12:16:18 -0700677enum vmalloc64_mode {not_refill, refill};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678/*
679 * BVADDR is the faulting address, PTR is scratch.
680 * PTR will hold the pgd for vmalloc.
681 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000682static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000683build_get_pgd_vmalloc64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
David Daney1ec56322010-04-28 12:16:18 -0700684 unsigned int bvaddr, unsigned int ptr,
685 enum vmalloc64_mode mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 long swpd = (long)swapper_pg_dir;
David Daney1ec56322010-04-28 12:16:18 -0700688 int single_insn_swpd;
689 int did_vmalloc_branch = 0;
690
691 single_insn_swpd = uasm_in_compat_space_p(swpd) && !uasm_rel_lo(swpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Thiemo Seufere30ec452008-01-28 20:05:38 +0000693 uasm_l_vmalloc(l, *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
David Daney1ec56322010-04-28 12:16:18 -0700695 if (mode == refill && check_for_high_segbits) {
696 if (single_insn_swpd) {
697 uasm_il_bltz(p, r, bvaddr, label_vmalloc_done);
698 uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
699 did_vmalloc_branch = 1;
700 /* fall through */
701 } else {
702 uasm_il_bgez(p, r, bvaddr, label_large_segbits_fault);
703 }
704 }
705 if (!did_vmalloc_branch) {
706 if (uasm_in_compat_space_p(swpd) && !uasm_rel_lo(swpd)) {
707 uasm_il_b(p, r, label_vmalloc_done);
708 uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
709 } else {
710 UASM_i_LA_mostly(p, ptr, swpd);
711 uasm_il_b(p, r, label_vmalloc_done);
712 if (uasm_in_compat_space_p(swpd))
713 uasm_i_addiu(p, ptr, ptr, uasm_rel_lo(swpd));
714 else
715 uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(swpd));
716 }
717 }
718 if (mode == refill && check_for_high_segbits) {
719 uasm_l_large_segbits_fault(l, *p);
720 /*
721 * We get here if we are an xsseg address, or if we are
722 * an xuseg address above (PGDIR_SHIFT+PGDIR_BITS) boundary.
723 *
724 * Ignoring xsseg (assume disabled so would generate
725 * (address errors?), the only remaining possibility
726 * is the upper xuseg addresses. On processors with
727 * TLB_SEGBITS <= PGDIR_SHIFT+PGDIR_BITS, these
728 * addresses would have taken an address error. We try
729 * to mimic that here by taking a load/istream page
730 * fault.
731 */
732 UASM_i_LA(p, ptr, (unsigned long)tlb_do_page_fault_0);
733 uasm_i_jr(p, ptr);
734 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736}
737
Ralf Baechle875d43e2005-09-03 15:56:16 -0700738#else /* !CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740/*
741 * TMP and PTR are scratch.
742 * TMP will be clobbered, PTR will hold the pgd entry.
743 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000744static void __cpuinit __maybe_unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
746{
747 long pgdc = (long)pgd_current;
748
749 /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
750#ifdef CONFIG_SMP
Ralf Baechle41c594a2006-04-05 09:45:45 +0100751#ifdef CONFIG_MIPS_MT_SMTC
752 /*
753 * SMTC uses TCBind value as "CPU" index
754 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000755 uasm_i_mfc0(p, ptr, C0_TCBIND);
756 UASM_i_LA_mostly(p, tmp, pgdc);
757 uasm_i_srl(p, ptr, ptr, 19);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100758#else
759 /*
760 * smp_processor_id() << 3 is stored in CONTEXT.
761 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000762 uasm_i_mfc0(p, ptr, C0_CONTEXT);
763 UASM_i_LA_mostly(p, tmp, pgdc);
764 uasm_i_srl(p, ptr, ptr, 23);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100765#endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000766 uasm_i_addu(p, ptr, tmp, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767#else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000768 UASM_i_LA_mostly(p, ptr, pgdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769#endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000770 uasm_i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
771 uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
772 uasm_i_srl(p, tmp, tmp, PGDIR_SHIFT); /* get pgd only bits */
773 uasm_i_sll(p, tmp, tmp, PGD_T_LOG2);
774 uasm_i_addu(p, ptr, ptr, tmp); /* add in pgd offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Ralf Baechle875d43e2005-09-03 15:56:16 -0700777#endif /* !CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Ralf Baechle234fcd12008-03-08 09:56:28 +0000779static void __cpuinit build_adjust_context(u32 **p, unsigned int ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Ralf Baechle242954b2006-10-24 02:29:01 +0100781 unsigned int shift = 4 - (PTE_T_LOG2 + 1) + PAGE_SHIFT - 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 unsigned int mask = (PTRS_PER_PTE / 2 - 1) << (PTE_T_LOG2 + 1);
783
Ralf Baechle10cc3522007-10-11 23:46:15 +0100784 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 case CPU_VR41XX:
786 case CPU_VR4111:
787 case CPU_VR4121:
788 case CPU_VR4122:
789 case CPU_VR4131:
790 case CPU_VR4181:
791 case CPU_VR4181A:
792 case CPU_VR4133:
793 shift += 2;
794 break;
795
796 default:
797 break;
798 }
799
800 if (shift)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000801 UASM_i_SRL(p, ctx, ctx, shift);
802 uasm_i_andi(p, ctx, ctx, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Ralf Baechle234fcd12008-03-08 09:56:28 +0000805static void __cpuinit build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 /*
808 * Bug workaround for the Nevada. It seems as if under certain
809 * circumstances the move from cp0_context might produce a
810 * bogus result when the mfc0 instruction and its consumer are
811 * in a different cacheline or a load instruction, probably any
812 * memory reference, is between them.
813 */
Ralf Baechle10cc3522007-10-11 23:46:15 +0100814 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 case CPU_NEVADA:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000816 UASM_i_LW(p, ptr, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 GET_CONTEXT(p, tmp); /* get context reg */
818 break;
819
820 default:
821 GET_CONTEXT(p, tmp); /* get context reg */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000822 UASM_i_LW(p, ptr, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 break;
824 }
825
826 build_adjust_context(p, tmp);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000827 UASM_i_ADDU(p, ptr, ptr, tmp); /* add in offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Ralf Baechle234fcd12008-03-08 09:56:28 +0000830static void __cpuinit build_update_entries(u32 **p, unsigned int tmp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 unsigned int ptep)
832{
833 /*
834 * 64bit address support (36bit on a 32bit CPU) in a 32bit
835 * Kernel is a special case. Only a few CPUs use it.
836 */
837#ifdef CONFIG_64BIT_PHYS_ADDR
838 if (cpu_has_64bits) {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000839 uasm_i_ld(p, tmp, 0, ptep); /* get even pte */
840 uasm_i_ld(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
David Daney6dd93442010-02-10 15:12:47 -0800841 if (kernel_uses_smartmips_rixi) {
842 UASM_i_SRL(p, tmp, tmp, ilog2(_PAGE_NO_EXEC));
843 UASM_i_SRL(p, ptep, ptep, ilog2(_PAGE_NO_EXEC));
844 UASM_i_ROTR(p, tmp, tmp, ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
845 UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
846 UASM_i_ROTR(p, ptep, ptep, ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
847 } else {
David Daney3be60222010-04-28 12:16:17 -0700848 uasm_i_dsrl_safe(p, tmp, tmp, ilog2(_PAGE_GLOBAL)); /* convert to entrylo0 */
David Daney6dd93442010-02-10 15:12:47 -0800849 UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
David Daney3be60222010-04-28 12:16:17 -0700850 uasm_i_dsrl_safe(p, ptep, ptep, ilog2(_PAGE_GLOBAL)); /* convert to entrylo1 */
David Daney6dd93442010-02-10 15:12:47 -0800851 }
David Daney9b8c3892010-02-10 15:12:44 -0800852 UASM_i_MTC0(p, ptep, C0_ENTRYLO1); /* load it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 } else {
854 int pte_off_even = sizeof(pte_t) / 2;
855 int pte_off_odd = pte_off_even + sizeof(pte_t);
856
857 /* The pte entries are pre-shifted */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000858 uasm_i_lw(p, tmp, pte_off_even, ptep); /* get even pte */
David Daney9b8c3892010-02-10 15:12:44 -0800859 UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000860 uasm_i_lw(p, ptep, pte_off_odd, ptep); /* get odd pte */
David Daney9b8c3892010-02-10 15:12:44 -0800861 UASM_i_MTC0(p, ptep, C0_ENTRYLO1); /* load it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863#else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000864 UASM_i_LW(p, tmp, 0, ptep); /* get even pte */
865 UASM_i_LW(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (r45k_bvahwbug())
867 build_tlb_probe_entry(p);
David Daney6dd93442010-02-10 15:12:47 -0800868 if (kernel_uses_smartmips_rixi) {
869 UASM_i_SRL(p, tmp, tmp, ilog2(_PAGE_NO_EXEC));
870 UASM_i_SRL(p, ptep, ptep, ilog2(_PAGE_NO_EXEC));
871 UASM_i_ROTR(p, tmp, tmp, ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
872 if (r4k_250MHZhwbug())
873 UASM_i_MTC0(p, 0, C0_ENTRYLO0);
874 UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
875 UASM_i_ROTR(p, ptep, ptep, ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
876 } else {
877 UASM_i_SRL(p, tmp, tmp, ilog2(_PAGE_GLOBAL)); /* convert to entrylo0 */
878 if (r4k_250MHZhwbug())
879 UASM_i_MTC0(p, 0, C0_ENTRYLO0);
880 UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
881 UASM_i_SRL(p, ptep, ptep, ilog2(_PAGE_GLOBAL)); /* convert to entrylo1 */
882 if (r45k_bvahwbug())
883 uasm_i_mfc0(p, tmp, C0_INDEX);
884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (r4k_250MHZhwbug())
David Daney9b8c3892010-02-10 15:12:44 -0800886 UASM_i_MTC0(p, 0, C0_ENTRYLO1);
887 UASM_i_MTC0(p, ptep, C0_ENTRYLO1); /* load it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888#endif
889}
890
David Daneye6f72d32009-05-20 11:40:58 -0700891/*
892 * For a 64-bit kernel, we are using the 64-bit XTLB refill exception
893 * because EXL == 0. If we wrap, we can also use the 32 instruction
894 * slots before the XTLB refill exception handler which belong to the
895 * unused TLB refill exception.
896 */
897#define MIPS64_REFILL_INSNS 32
898
Ralf Baechle234fcd12008-03-08 09:56:28 +0000899static void __cpuinit build_r4000_tlb_refill_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 u32 *p = tlb_handler;
Thiemo Seufere30ec452008-01-28 20:05:38 +0000902 struct uasm_label *l = labels;
903 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 u32 *f;
905 unsigned int final_len;
906
907 memset(tlb_handler, 0, sizeof(tlb_handler));
908 memset(labels, 0, sizeof(labels));
909 memset(relocs, 0, sizeof(relocs));
910 memset(final_handler, 0, sizeof(final_handler));
911
912 /*
913 * create the plain linear handler
914 */
915 if (bcm1250_m3_war()) {
Ralf Baechle3d452852010-03-23 17:56:38 +0100916 unsigned int segbits = 44;
917
918 uasm_i_dmfc0(&p, K0, C0_BADVADDR);
919 uasm_i_dmfc0(&p, K1, C0_ENTRYHI);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000920 uasm_i_xor(&p, K0, K0, K1);
David Daney3be60222010-04-28 12:16:17 -0700921 uasm_i_dsrl_safe(&p, K1, K0, 62);
922 uasm_i_dsrl_safe(&p, K0, K0, 12 + 1);
923 uasm_i_dsll_safe(&p, K0, K0, 64 + 12 + 1 - segbits);
Ralf Baechle3d452852010-03-23 17:56:38 +0100924 uasm_i_or(&p, K0, K0, K1);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000925 uasm_il_bnez(&p, &r, K0, label_leave);
926 /* No need for uasm_i_nop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928
Ralf Baechle875d43e2005-09-03 15:56:16 -0700929#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 build_get_pmde64(&p, &l, &r, K0, K1); /* get pmd in K1 */
931#else
932 build_get_pgde32(&p, K0, K1); /* get pgd in K1 */
933#endif
934
David Daneyfd062c82009-05-27 17:47:44 -0700935#ifdef CONFIG_HUGETLB_PAGE
936 build_is_huge_pte(&p, &r, K0, K1, label_tlb_huge_update);
937#endif
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 build_get_ptep(&p, K0, K1);
940 build_update_entries(&p, K0, K1);
941 build_tlb_write_entry(&p, &l, &r, tlb_random);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000942 uasm_l_leave(&l, p);
943 uasm_i_eret(&p); /* return from trap */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
David Daneyfd062c82009-05-27 17:47:44 -0700945#ifdef CONFIG_HUGETLB_PAGE
946 uasm_l_tlb_huge_update(&l, p);
947 UASM_i_LW(&p, K0, 0, K1);
948 build_huge_update_entries(&p, K0, K1);
949 build_huge_tlb_write_entry(&p, &l, &r, K0, tlb_random);
950#endif
951
Ralf Baechle875d43e2005-09-03 15:56:16 -0700952#ifdef CONFIG_64BIT
David Daney1ec56322010-04-28 12:16:18 -0700953 build_get_pgd_vmalloc64(&p, &l, &r, K0, K1, refill);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954#endif
955
956 /*
957 * Overflow check: For the 64bit handler, we need at least one
958 * free instruction slot for the wrap-around branch. In worst
959 * case, if the intended insertion point is a delay slot, we
Matt LaPlante4b3f6862006-10-03 22:21:02 +0200960 * need three, with the second nop'ed and the third being
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * unused.
962 */
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800963 /* Loongson2 ebase is different than r4k, we have more space */
964#if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if ((p - tlb_handler) > 64)
966 panic("TLB refill handler space exceeded");
967#else
David Daneye6f72d32009-05-20 11:40:58 -0700968 if (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 1)
969 || (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 3)
970 && uasm_insn_has_bdelay(relocs,
971 tlb_handler + MIPS64_REFILL_INSNS - 3)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 panic("TLB refill handler space exceeded");
973#endif
974
975 /*
976 * Now fold the handler in the TLB refill handler space.
977 */
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800978#if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 f = final_handler;
980 /* Simplest case, just copy the handler. */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000981 uasm_copy_handler(relocs, labels, tlb_handler, p, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 final_len = p - tlb_handler;
Ralf Baechle875d43e2005-09-03 15:56:16 -0700983#else /* CONFIG_64BIT */
David Daneye6f72d32009-05-20 11:40:58 -0700984 f = final_handler + MIPS64_REFILL_INSNS;
985 if ((p - tlb_handler) <= MIPS64_REFILL_INSNS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 /* Just copy the handler. */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000987 uasm_copy_handler(relocs, labels, tlb_handler, p, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 final_len = p - tlb_handler;
989 } else {
David Daneyfd062c82009-05-27 17:47:44 -0700990#if defined(CONFIG_HUGETLB_PAGE)
991 const enum label_id ls = label_tlb_huge_update;
David Daney95affdd2009-05-20 11:40:59 -0700992#else
993 const enum label_id ls = label_vmalloc;
994#endif
995 u32 *split;
996 int ov = 0;
997 int i;
998
999 for (i = 0; i < ARRAY_SIZE(labels) && labels[i].lab != ls; i++)
1000 ;
1001 BUG_ON(i == ARRAY_SIZE(labels));
1002 split = labels[i].addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 /*
David Daney95affdd2009-05-20 11:40:59 -07001005 * See if we have overflown one way or the other.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 */
David Daney95affdd2009-05-20 11:40:59 -07001007 if (split > tlb_handler + MIPS64_REFILL_INSNS ||
1008 split < p - MIPS64_REFILL_INSNS)
1009 ov = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
David Daney95affdd2009-05-20 11:40:59 -07001011 if (ov) {
1012 /*
1013 * Split two instructions before the end. One
1014 * for the branch and one for the instruction
1015 * in the delay slot.
1016 */
1017 split = tlb_handler + MIPS64_REFILL_INSNS - 2;
1018
1019 /*
1020 * If the branch would fall in a delay slot,
1021 * we must back up an additional instruction
1022 * so that it is no longer in a delay slot.
1023 */
1024 if (uasm_insn_has_bdelay(relocs, split - 1))
1025 split--;
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /* Copy first part of the handler. */
Thiemo Seufere30ec452008-01-28 20:05:38 +00001028 uasm_copy_handler(relocs, labels, tlb_handler, split, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 f += split - tlb_handler;
1030
David Daney95affdd2009-05-20 11:40:59 -07001031 if (ov) {
1032 /* Insert branch. */
1033 uasm_l_split(&l, final_handler);
1034 uasm_il_b(&f, &r, label_split);
1035 if (uasm_insn_has_bdelay(relocs, split))
1036 uasm_i_nop(&f);
1037 else {
1038 uasm_copy_handler(relocs, labels,
1039 split, split + 1, f);
1040 uasm_move_labels(labels, f, f + 1, -1);
1041 f++;
1042 split++;
1043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045
1046 /* Copy the rest of the handler. */
Thiemo Seufere30ec452008-01-28 20:05:38 +00001047 uasm_copy_handler(relocs, labels, split, p, final_handler);
David Daneye6f72d32009-05-20 11:40:58 -07001048 final_len = (f - (final_handler + MIPS64_REFILL_INSNS)) +
1049 (p - split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
Ralf Baechle875d43e2005-09-03 15:56:16 -07001051#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Thiemo Seufere30ec452008-01-28 20:05:38 +00001053 uasm_resolve_relocs(relocs, labels);
1054 pr_debug("Wrote TLB refill handler (%u instructions).\n",
1055 final_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Ralf Baechle91b05e62006-03-29 18:53:00 +01001057 memcpy((void *)ebase, final_handler, 0x100);
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001058
1059 dump_handler((u32 *)ebase, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 * 128 instructions for the fastpath handler is generous and should
1064 * never be exceeded.
1065 */
1066#define FASTPATH_SIZE 128
1067
Franck Bui-Huucbdbe072007-10-18 09:11:16 +02001068u32 handle_tlbl[FASTPATH_SIZE] __cacheline_aligned;
1069u32 handle_tlbs[FASTPATH_SIZE] __cacheline_aligned;
1070u32 handle_tlbm[FASTPATH_SIZE] __cacheline_aligned;
David Daney3d8bfdd2010-12-21 14:19:11 -08001071#ifdef CONFIG_MIPS_PGD_C0_CONTEXT
1072u32 tlbmiss_handler_setup_pgd[16] __cacheline_aligned;
1073
1074static void __cpuinit build_r4000_setup_pgd(void)
1075{
1076 const int a0 = 4;
1077 const int a1 = 5;
1078 u32 *p = tlbmiss_handler_setup_pgd;
1079 struct uasm_label *l = labels;
1080 struct uasm_reloc *r = relocs;
1081
1082 memset(tlbmiss_handler_setup_pgd, 0, sizeof(tlbmiss_handler_setup_pgd));
1083 memset(labels, 0, sizeof(labels));
1084 memset(relocs, 0, sizeof(relocs));
1085
1086 pgd_reg = allocate_kscratch();
1087
1088 if (pgd_reg == -1) {
1089 /* PGD << 11 in c0_Context */
1090 /*
1091 * If it is a ckseg0 address, convert to a physical
1092 * address. Shifting right by 29 and adding 4 will
1093 * result in zero for these addresses.
1094 *
1095 */
1096 UASM_i_SRA(&p, a1, a0, 29);
1097 UASM_i_ADDIU(&p, a1, a1, 4);
1098 uasm_il_bnez(&p, &r, a1, label_tlbl_goaround1);
1099 uasm_i_nop(&p);
1100 uasm_i_dinsm(&p, a0, 0, 29, 64 - 29);
1101 uasm_l_tlbl_goaround1(&l, p);
1102 UASM_i_SLL(&p, a0, a0, 11);
1103 uasm_i_jr(&p, 31);
1104 UASM_i_MTC0(&p, a0, C0_CONTEXT);
1105 } else {
1106 /* PGD in c0_KScratch */
1107 uasm_i_jr(&p, 31);
1108 UASM_i_MTC0(&p, a0, 31, pgd_reg);
1109 }
1110 if (p - tlbmiss_handler_setup_pgd > ARRAY_SIZE(tlbmiss_handler_setup_pgd))
1111 panic("tlbmiss_handler_setup_pgd space exceeded");
1112 uasm_resolve_relocs(relocs, labels);
1113 pr_debug("Wrote tlbmiss_handler_setup_pgd (%u instructions).\n",
1114 (unsigned int)(p - tlbmiss_handler_setup_pgd));
1115
1116 dump_handler(tlbmiss_handler_setup_pgd,
1117 ARRAY_SIZE(tlbmiss_handler_setup_pgd));
1118}
1119#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Ralf Baechle234fcd12008-03-08 09:56:28 +00001121static void __cpuinit
David Daneybd1437e2009-05-08 15:10:50 -07001122iPTE_LW(u32 **p, unsigned int pte, unsigned int ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124#ifdef CONFIG_SMP
1125# ifdef CONFIG_64BIT_PHYS_ADDR
1126 if (cpu_has_64bits)
Thiemo Seufere30ec452008-01-28 20:05:38 +00001127 uasm_i_lld(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 else
1129# endif
Thiemo Seufere30ec452008-01-28 20:05:38 +00001130 UASM_i_LL(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131#else
1132# ifdef CONFIG_64BIT_PHYS_ADDR
1133 if (cpu_has_64bits)
Thiemo Seufere30ec452008-01-28 20:05:38 +00001134 uasm_i_ld(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 else
1136# endif
Thiemo Seufere30ec452008-01-28 20:05:38 +00001137 UASM_i_LW(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138#endif
1139}
1140
Ralf Baechle234fcd12008-03-08 09:56:28 +00001141static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +00001142iPTE_SW(u32 **p, struct uasm_reloc **r, unsigned int pte, unsigned int ptr,
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001143 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001145#ifdef CONFIG_64BIT_PHYS_ADDR
1146 unsigned int hwmode = mode & (_PAGE_VALID | _PAGE_DIRTY);
1147#endif
1148
Thiemo Seufere30ec452008-01-28 20:05:38 +00001149 uasm_i_ori(p, pte, pte, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150#ifdef CONFIG_SMP
1151# ifdef CONFIG_64BIT_PHYS_ADDR
1152 if (cpu_has_64bits)
Thiemo Seufere30ec452008-01-28 20:05:38 +00001153 uasm_i_scd(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 else
1155# endif
Thiemo Seufere30ec452008-01-28 20:05:38 +00001156 UASM_i_SC(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 if (r10000_llsc_war())
Thiemo Seufere30ec452008-01-28 20:05:38 +00001159 uasm_il_beqzl(p, r, pte, label_smp_pgtable_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 else
Thiemo Seufere30ec452008-01-28 20:05:38 +00001161 uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163# ifdef CONFIG_64BIT_PHYS_ADDR
1164 if (!cpu_has_64bits) {
Thiemo Seufere30ec452008-01-28 20:05:38 +00001165 /* no uasm_i_nop needed */
1166 uasm_i_ll(p, pte, sizeof(pte_t) / 2, ptr);
1167 uasm_i_ori(p, pte, pte, hwmode);
1168 uasm_i_sc(p, pte, sizeof(pte_t) / 2, ptr);
1169 uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
1170 /* no uasm_i_nop needed */
1171 uasm_i_lw(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 } else
Thiemo Seufere30ec452008-01-28 20:05:38 +00001173 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174# else
Thiemo Seufere30ec452008-01-28 20:05:38 +00001175 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176# endif
1177#else
1178# ifdef CONFIG_64BIT_PHYS_ADDR
1179 if (cpu_has_64bits)
Thiemo Seufere30ec452008-01-28 20:05:38 +00001180 uasm_i_sd(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 else
1182# endif
Thiemo Seufere30ec452008-01-28 20:05:38 +00001183 UASM_i_SW(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185# ifdef CONFIG_64BIT_PHYS_ADDR
1186 if (!cpu_has_64bits) {
Thiemo Seufere30ec452008-01-28 20:05:38 +00001187 uasm_i_lw(p, pte, sizeof(pte_t) / 2, ptr);
1188 uasm_i_ori(p, pte, pte, hwmode);
1189 uasm_i_sw(p, pte, sizeof(pte_t) / 2, ptr);
1190 uasm_i_lw(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192# endif
1193#endif
1194}
1195
1196/*
1197 * Check if PTE is present, if not then jump to LABEL. PTR points to
1198 * the page table where this PTE is located, PTE will be re-loaded
1199 * with it's original value.
1200 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001201static void __cpuinit
David Daneybd1437e2009-05-08 15:10:50 -07001202build_pte_present(u32 **p, struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 unsigned int pte, unsigned int ptr, enum label_id lid)
1204{
David Daney6dd93442010-02-10 15:12:47 -08001205 if (kernel_uses_smartmips_rixi) {
David Daneycc33ae42010-12-20 15:54:50 -08001206 if (use_bbit_insns()) {
1207 uasm_il_bbit0(p, r, pte, ilog2(_PAGE_PRESENT), lid);
1208 uasm_i_nop(p);
1209 } else {
1210 uasm_i_andi(p, pte, pte, _PAGE_PRESENT);
1211 uasm_il_beqz(p, r, pte, lid);
1212 iPTE_LW(p, pte, ptr);
1213 }
David Daney6dd93442010-02-10 15:12:47 -08001214 } else {
1215 uasm_i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
1216 uasm_i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
1217 uasm_il_bnez(p, r, pte, lid);
David Daneycc33ae42010-12-20 15:54:50 -08001218 iPTE_LW(p, pte, ptr);
David Daney6dd93442010-02-10 15:12:47 -08001219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
1222/* Make PTE valid, store result in PTR. */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001223static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +00001224build_make_valid(u32 **p, struct uasm_reloc **r, unsigned int pte,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 unsigned int ptr)
1226{
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001227 unsigned int mode = _PAGE_VALID | _PAGE_ACCESSED;
1228
1229 iPTE_SW(p, r, pte, ptr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
1232/*
1233 * Check if PTE can be written to, if not branch to LABEL. Regardless
1234 * restore PTE with value from PTR when done.
1235 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001236static void __cpuinit
David Daneybd1437e2009-05-08 15:10:50 -07001237build_pte_writable(u32 **p, struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 unsigned int pte, unsigned int ptr, enum label_id lid)
1239{
David Daneycc33ae42010-12-20 15:54:50 -08001240 if (use_bbit_insns()) {
1241 uasm_il_bbit0(p, r, pte, ilog2(_PAGE_PRESENT), lid);
1242 uasm_i_nop(p);
1243 uasm_il_bbit0(p, r, pte, ilog2(_PAGE_WRITE), lid);
1244 uasm_i_nop(p);
1245 } else {
1246 uasm_i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
1247 uasm_i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
1248 uasm_il_bnez(p, r, pte, lid);
1249 iPTE_LW(p, pte, ptr);
1250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
1253/* Make PTE writable, update software status bits as well, then store
1254 * at PTR.
1255 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001256static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +00001257build_make_write(u32 **p, struct uasm_reloc **r, unsigned int pte,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 unsigned int ptr)
1259{
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001260 unsigned int mode = (_PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID
1261 | _PAGE_DIRTY);
1262
1263 iPTE_SW(p, r, pte, ptr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
1266/*
1267 * Check if PTE can be modified, if not branch to LABEL. Regardless
1268 * restore PTE with value from PTR when done.
1269 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001270static void __cpuinit
David Daneybd1437e2009-05-08 15:10:50 -07001271build_pte_modifiable(u32 **p, struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 unsigned int pte, unsigned int ptr, enum label_id lid)
1273{
David Daneycc33ae42010-12-20 15:54:50 -08001274 if (use_bbit_insns()) {
1275 uasm_il_bbit0(p, r, pte, ilog2(_PAGE_WRITE), lid);
1276 uasm_i_nop(p);
1277 } else {
1278 uasm_i_andi(p, pte, pte, _PAGE_WRITE);
1279 uasm_il_beqz(p, r, pte, lid);
1280 iPTE_LW(p, pte, ptr);
1281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
David Daney82622282009-10-14 12:16:56 -07001284#ifndef CONFIG_MIPS_PGD_C0_CONTEXT
David Daney3d8bfdd2010-12-21 14:19:11 -08001285
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287/*
1288 * R3000 style TLB load/store/modify handlers.
1289 */
1290
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001291/*
1292 * This places the pte into ENTRYLO0 and writes it with tlbwi.
1293 * Then it returns.
1294 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001295static void __cpuinit
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001296build_r3000_pte_reload_tlbwi(u32 **p, unsigned int pte, unsigned int tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Thiemo Seufere30ec452008-01-28 20:05:38 +00001298 uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
1299 uasm_i_mfc0(p, tmp, C0_EPC); /* cp0 delay */
1300 uasm_i_tlbwi(p);
1301 uasm_i_jr(p, tmp);
1302 uasm_i_rfe(p); /* branch delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
1305/*
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001306 * This places the pte into ENTRYLO0 and writes it with tlbwi
1307 * or tlbwr as appropriate. This is because the index register
1308 * may have the probe fail bit set as a result of a trap on a
1309 * kseg2 access, i.e. without refill. Then it returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001311static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +00001312build_r3000_tlb_reload_write(u32 **p, struct uasm_label **l,
1313 struct uasm_reloc **r, unsigned int pte,
1314 unsigned int tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Thiemo Seufere30ec452008-01-28 20:05:38 +00001316 uasm_i_mfc0(p, tmp, C0_INDEX);
1317 uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
1318 uasm_il_bltz(p, r, tmp, label_r3000_write_probe_fail); /* cp0 delay */
1319 uasm_i_mfc0(p, tmp, C0_EPC); /* branch delay */
1320 uasm_i_tlbwi(p); /* cp0 delay */
1321 uasm_i_jr(p, tmp);
1322 uasm_i_rfe(p); /* branch delay */
1323 uasm_l_r3000_write_probe_fail(l, *p);
1324 uasm_i_tlbwr(p); /* cp0 delay */
1325 uasm_i_jr(p, tmp);
1326 uasm_i_rfe(p); /* branch delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Ralf Baechle234fcd12008-03-08 09:56:28 +00001329static void __cpuinit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
1331 unsigned int ptr)
1332{
1333 long pgdc = (long)pgd_current;
1334
Thiemo Seufere30ec452008-01-28 20:05:38 +00001335 uasm_i_mfc0(p, pte, C0_BADVADDR);
1336 uasm_i_lui(p, ptr, uasm_rel_hi(pgdc)); /* cp0 delay */
1337 uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
1338 uasm_i_srl(p, pte, pte, 22); /* load delay */
1339 uasm_i_sll(p, pte, pte, 2);
1340 uasm_i_addu(p, ptr, ptr, pte);
1341 uasm_i_mfc0(p, pte, C0_CONTEXT);
1342 uasm_i_lw(p, ptr, 0, ptr); /* cp0 delay */
1343 uasm_i_andi(p, pte, pte, 0xffc); /* load delay */
1344 uasm_i_addu(p, ptr, ptr, pte);
1345 uasm_i_lw(p, pte, 0, ptr);
1346 uasm_i_tlbp(p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Ralf Baechle234fcd12008-03-08 09:56:28 +00001349static void __cpuinit build_r3000_tlb_load_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 u32 *p = handle_tlbl;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001352 struct uasm_label *l = labels;
1353 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 memset(handle_tlbl, 0, sizeof(handle_tlbl));
1356 memset(labels, 0, sizeof(labels));
1357 memset(relocs, 0, sizeof(relocs));
1358
1359 build_r3000_tlbchange_handler_head(&p, K0, K1);
David Daneybd1437e2009-05-08 15:10:50 -07001360 build_pte_present(&p, &r, K0, K1, label_nopage_tlbl);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001361 uasm_i_nop(&p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 build_make_valid(&p, &r, K0, K1);
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001363 build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Thiemo Seufere30ec452008-01-28 20:05:38 +00001365 uasm_l_nopage_tlbl(&l, p);
1366 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
1367 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 if ((p - handle_tlbl) > FASTPATH_SIZE)
1370 panic("TLB load handler fastpath space exceeded");
1371
Thiemo Seufere30ec452008-01-28 20:05:38 +00001372 uasm_resolve_relocs(relocs, labels);
1373 pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
1374 (unsigned int)(p - handle_tlbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001376 dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
Ralf Baechle234fcd12008-03-08 09:56:28 +00001379static void __cpuinit build_r3000_tlb_store_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 u32 *p = handle_tlbs;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001382 struct uasm_label *l = labels;
1383 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 memset(handle_tlbs, 0, sizeof(handle_tlbs));
1386 memset(labels, 0, sizeof(labels));
1387 memset(relocs, 0, sizeof(relocs));
1388
1389 build_r3000_tlbchange_handler_head(&p, K0, K1);
David Daneybd1437e2009-05-08 15:10:50 -07001390 build_pte_writable(&p, &r, K0, K1, label_nopage_tlbs);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001391 uasm_i_nop(&p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 build_make_write(&p, &r, K0, K1);
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001393 build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Thiemo Seufere30ec452008-01-28 20:05:38 +00001395 uasm_l_nopage_tlbs(&l, p);
1396 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1397 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 if ((p - handle_tlbs) > FASTPATH_SIZE)
1400 panic("TLB store handler fastpath space exceeded");
1401
Thiemo Seufere30ec452008-01-28 20:05:38 +00001402 uasm_resolve_relocs(relocs, labels);
1403 pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
1404 (unsigned int)(p - handle_tlbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001406 dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407}
1408
Ralf Baechle234fcd12008-03-08 09:56:28 +00001409static void __cpuinit build_r3000_tlb_modify_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
1411 u32 *p = handle_tlbm;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001412 struct uasm_label *l = labels;
1413 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 memset(handle_tlbm, 0, sizeof(handle_tlbm));
1416 memset(labels, 0, sizeof(labels));
1417 memset(relocs, 0, sizeof(relocs));
1418
1419 build_r3000_tlbchange_handler_head(&p, K0, K1);
David Daneybd1437e2009-05-08 15:10:50 -07001420 build_pte_modifiable(&p, &r, K0, K1, label_nopage_tlbm);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001421 uasm_i_nop(&p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 build_make_write(&p, &r, K0, K1);
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001423 build_r3000_pte_reload_tlbwi(&p, K0, K1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Thiemo Seufere30ec452008-01-28 20:05:38 +00001425 uasm_l_nopage_tlbm(&l, p);
1426 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1427 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 if ((p - handle_tlbm) > FASTPATH_SIZE)
1430 panic("TLB modify handler fastpath space exceeded");
1431
Thiemo Seufere30ec452008-01-28 20:05:38 +00001432 uasm_resolve_relocs(relocs, labels);
1433 pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
1434 (unsigned int)(p - handle_tlbm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001436 dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
David Daney82622282009-10-14 12:16:56 -07001438#endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440/*
1441 * R4000 style TLB load/store/modify handlers.
1442 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001443static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +00001444build_r4000_tlbchange_handler_head(u32 **p, struct uasm_label **l,
1445 struct uasm_reloc **r, unsigned int pte,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 unsigned int ptr)
1447{
Ralf Baechle875d43e2005-09-03 15:56:16 -07001448#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 build_get_pmde64(p, l, r, pte, ptr); /* get pmd in ptr */
1450#else
1451 build_get_pgde32(p, pte, ptr); /* get pgd in ptr */
1452#endif
1453
David Daneyfd062c82009-05-27 17:47:44 -07001454#ifdef CONFIG_HUGETLB_PAGE
1455 /*
1456 * For huge tlb entries, pmd doesn't contain an address but
1457 * instead contains the tlb pte. Check the PAGE_HUGE bit and
1458 * see if we need to jump to huge tlb processing.
1459 */
1460 build_is_huge_pte(p, r, pte, ptr, label_tlb_huge_update);
1461#endif
1462
Thiemo Seufere30ec452008-01-28 20:05:38 +00001463 UASM_i_MFC0(p, pte, C0_BADVADDR);
1464 UASM_i_LW(p, ptr, 0, ptr);
1465 UASM_i_SRL(p, pte, pte, PAGE_SHIFT + PTE_ORDER - PTE_T_LOG2);
1466 uasm_i_andi(p, pte, pte, (PTRS_PER_PTE - 1) << PTE_T_LOG2);
1467 UASM_i_ADDU(p, ptr, ptr, pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469#ifdef CONFIG_SMP
Thiemo Seufere30ec452008-01-28 20:05:38 +00001470 uasm_l_smp_pgtable_change(l, *p);
1471#endif
David Daneybd1437e2009-05-08 15:10:50 -07001472 iPTE_LW(p, pte, ptr); /* get even pte */
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01001473 if (!m4kc_tlbp_war())
1474 build_tlb_probe_entry(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
Ralf Baechle234fcd12008-03-08 09:56:28 +00001477static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +00001478build_r4000_tlbchange_handler_tail(u32 **p, struct uasm_label **l,
1479 struct uasm_reloc **r, unsigned int tmp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 unsigned int ptr)
1481{
Thiemo Seufere30ec452008-01-28 20:05:38 +00001482 uasm_i_ori(p, ptr, ptr, sizeof(pte_t));
1483 uasm_i_xori(p, ptr, ptr, sizeof(pte_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 build_update_entries(p, tmp, ptr);
1485 build_tlb_write_entry(p, l, r, tlb_indexed);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001486 uasm_l_leave(l, *p);
1487 uasm_i_eret(p); /* return from trap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Ralf Baechle875d43e2005-09-03 15:56:16 -07001489#ifdef CONFIG_64BIT
David Daney1ec56322010-04-28 12:16:18 -07001490 build_get_pgd_vmalloc64(p, l, r, tmp, ptr, not_refill);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491#endif
1492}
1493
Ralf Baechle234fcd12008-03-08 09:56:28 +00001494static void __cpuinit build_r4000_tlb_load_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 u32 *p = handle_tlbl;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001497 struct uasm_label *l = labels;
1498 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 memset(handle_tlbl, 0, sizeof(handle_tlbl));
1501 memset(labels, 0, sizeof(labels));
1502 memset(relocs, 0, sizeof(relocs));
1503
1504 if (bcm1250_m3_war()) {
Ralf Baechle3d452852010-03-23 17:56:38 +01001505 unsigned int segbits = 44;
1506
1507 uasm_i_dmfc0(&p, K0, C0_BADVADDR);
1508 uasm_i_dmfc0(&p, K1, C0_ENTRYHI);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001509 uasm_i_xor(&p, K0, K0, K1);
David Daney3be60222010-04-28 12:16:17 -07001510 uasm_i_dsrl_safe(&p, K1, K0, 62);
1511 uasm_i_dsrl_safe(&p, K0, K0, 12 + 1);
1512 uasm_i_dsll_safe(&p, K0, K0, 64 + 12 + 1 - segbits);
Ralf Baechle3d452852010-03-23 17:56:38 +01001513 uasm_i_or(&p, K0, K0, K1);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001514 uasm_il_bnez(&p, &r, K0, label_leave);
1515 /* No need for uasm_i_nop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
1517
1518 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
David Daneybd1437e2009-05-08 15:10:50 -07001519 build_pte_present(&p, &r, K0, K1, label_nopage_tlbl);
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01001520 if (m4kc_tlbp_war())
1521 build_tlb_probe_entry(&p);
David Daney6dd93442010-02-10 15:12:47 -08001522
1523 if (kernel_uses_smartmips_rixi) {
1524 /*
1525 * If the page is not _PAGE_VALID, RI or XI could not
1526 * have triggered it. Skip the expensive test..
1527 */
David Daneycc33ae42010-12-20 15:54:50 -08001528 if (use_bbit_insns()) {
1529 uasm_il_bbit0(&p, &r, K0, ilog2(_PAGE_VALID),
1530 label_tlbl_goaround1);
1531 } else {
1532 uasm_i_andi(&p, K0, K0, _PAGE_VALID);
1533 uasm_il_beqz(&p, &r, K0, label_tlbl_goaround1);
1534 }
David Daney6dd93442010-02-10 15:12:47 -08001535 uasm_i_nop(&p);
1536
1537 uasm_i_tlbr(&p);
1538 /* Examine entrylo 0 or 1 based on ptr. */
David Daneycc33ae42010-12-20 15:54:50 -08001539 if (use_bbit_insns()) {
1540 uasm_i_bbit0(&p, K1, ilog2(sizeof(pte_t)), 8);
1541 } else {
1542 uasm_i_andi(&p, K0, K1, sizeof(pte_t));
1543 uasm_i_beqz(&p, K0, 8);
1544 }
David Daney6dd93442010-02-10 15:12:47 -08001545
1546 UASM_i_MFC0(&p, K0, C0_ENTRYLO0); /* load it in the delay slot*/
1547 UASM_i_MFC0(&p, K0, C0_ENTRYLO1); /* load it if ptr is odd */
1548 /*
1549 * If the entryLo (now in K0) is valid (bit 1), RI or
1550 * XI must have triggered it.
1551 */
David Daneycc33ae42010-12-20 15:54:50 -08001552 if (use_bbit_insns()) {
1553 uasm_il_bbit1(&p, &r, K0, 1, label_nopage_tlbl);
1554 /* Reload the PTE value */
1555 iPTE_LW(&p, K0, K1);
1556 uasm_l_tlbl_goaround1(&l, p);
1557 } else {
1558 uasm_i_andi(&p, K0, K0, 2);
1559 uasm_il_bnez(&p, &r, K0, label_nopage_tlbl);
1560 uasm_l_tlbl_goaround1(&l, p);
1561 /* Reload the PTE value */
1562 iPTE_LW(&p, K0, K1);
1563 }
David Daney6dd93442010-02-10 15:12:47 -08001564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 build_make_valid(&p, &r, K0, K1);
1566 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1567
David Daneyfd062c82009-05-27 17:47:44 -07001568#ifdef CONFIG_HUGETLB_PAGE
1569 /*
1570 * This is the entry point when build_r4000_tlbchange_handler_head
1571 * spots a huge page.
1572 */
1573 uasm_l_tlb_huge_update(&l, p);
1574 iPTE_LW(&p, K0, K1);
1575 build_pte_present(&p, &r, K0, K1, label_nopage_tlbl);
1576 build_tlb_probe_entry(&p);
David Daney6dd93442010-02-10 15:12:47 -08001577
1578 if (kernel_uses_smartmips_rixi) {
1579 /*
1580 * If the page is not _PAGE_VALID, RI or XI could not
1581 * have triggered it. Skip the expensive test..
1582 */
David Daneycc33ae42010-12-20 15:54:50 -08001583 if (use_bbit_insns()) {
1584 uasm_il_bbit0(&p, &r, K0, ilog2(_PAGE_VALID),
1585 label_tlbl_goaround2);
1586 } else {
1587 uasm_i_andi(&p, K0, K0, _PAGE_VALID);
1588 uasm_il_beqz(&p, &r, K0, label_tlbl_goaround2);
1589 }
David Daney6dd93442010-02-10 15:12:47 -08001590 uasm_i_nop(&p);
1591
1592 uasm_i_tlbr(&p);
1593 /* Examine entrylo 0 or 1 based on ptr. */
David Daneycc33ae42010-12-20 15:54:50 -08001594 if (use_bbit_insns()) {
1595 uasm_i_bbit0(&p, K1, ilog2(sizeof(pte_t)), 8);
1596 } else {
1597 uasm_i_andi(&p, K0, K1, sizeof(pte_t));
1598 uasm_i_beqz(&p, K0, 8);
1599 }
David Daney6dd93442010-02-10 15:12:47 -08001600 UASM_i_MFC0(&p, K0, C0_ENTRYLO0); /* load it in the delay slot*/
1601 UASM_i_MFC0(&p, K0, C0_ENTRYLO1); /* load it if ptr is odd */
1602 /*
1603 * If the entryLo (now in K0) is valid (bit 1), RI or
1604 * XI must have triggered it.
1605 */
David Daneycc33ae42010-12-20 15:54:50 -08001606 if (use_bbit_insns()) {
1607 uasm_il_bbit0(&p, &r, K0, 1, label_tlbl_goaround2);
1608 } else {
1609 uasm_i_andi(&p, K0, K0, 2);
1610 uasm_il_beqz(&p, &r, K0, label_tlbl_goaround2);
1611 }
David Daney6dd93442010-02-10 15:12:47 -08001612 /* Reload the PTE value */
1613 iPTE_LW(&p, K0, K1);
1614
1615 /*
1616 * We clobbered C0_PAGEMASK, restore it. On the other branch
1617 * it is restored in build_huge_tlb_write_entry.
1618 */
1619 build_restore_pagemask(&p, &r, K0, label_nopage_tlbl);
1620
1621 uasm_l_tlbl_goaround2(&l, p);
1622 }
David Daneyfd062c82009-05-27 17:47:44 -07001623 uasm_i_ori(&p, K0, K0, (_PAGE_ACCESSED | _PAGE_VALID));
1624 build_huge_handler_tail(&p, &r, &l, K0, K1);
1625#endif
1626
Thiemo Seufere30ec452008-01-28 20:05:38 +00001627 uasm_l_nopage_tlbl(&l, p);
1628 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
1629 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 if ((p - handle_tlbl) > FASTPATH_SIZE)
1632 panic("TLB load handler fastpath space exceeded");
1633
Thiemo Seufere30ec452008-01-28 20:05:38 +00001634 uasm_resolve_relocs(relocs, labels);
1635 pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
1636 (unsigned int)(p - handle_tlbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001638 dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
Ralf Baechle234fcd12008-03-08 09:56:28 +00001641static void __cpuinit build_r4000_tlb_store_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
1643 u32 *p = handle_tlbs;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001644 struct uasm_label *l = labels;
1645 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 memset(handle_tlbs, 0, sizeof(handle_tlbs));
1648 memset(labels, 0, sizeof(labels));
1649 memset(relocs, 0, sizeof(relocs));
1650
1651 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
David Daneybd1437e2009-05-08 15:10:50 -07001652 build_pte_writable(&p, &r, K0, K1, label_nopage_tlbs);
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01001653 if (m4kc_tlbp_war())
1654 build_tlb_probe_entry(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 build_make_write(&p, &r, K0, K1);
1656 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1657
David Daneyfd062c82009-05-27 17:47:44 -07001658#ifdef CONFIG_HUGETLB_PAGE
1659 /*
1660 * This is the entry point when
1661 * build_r4000_tlbchange_handler_head spots a huge page.
1662 */
1663 uasm_l_tlb_huge_update(&l, p);
1664 iPTE_LW(&p, K0, K1);
1665 build_pte_writable(&p, &r, K0, K1, label_nopage_tlbs);
1666 build_tlb_probe_entry(&p);
1667 uasm_i_ori(&p, K0, K0,
1668 _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
1669 build_huge_handler_tail(&p, &r, &l, K0, K1);
1670#endif
1671
Thiemo Seufere30ec452008-01-28 20:05:38 +00001672 uasm_l_nopage_tlbs(&l, p);
1673 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1674 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 if ((p - handle_tlbs) > FASTPATH_SIZE)
1677 panic("TLB store handler fastpath space exceeded");
1678
Thiemo Seufere30ec452008-01-28 20:05:38 +00001679 uasm_resolve_relocs(relocs, labels);
1680 pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
1681 (unsigned int)(p - handle_tlbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001683 dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
Ralf Baechle234fcd12008-03-08 09:56:28 +00001686static void __cpuinit build_r4000_tlb_modify_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
1688 u32 *p = handle_tlbm;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001689 struct uasm_label *l = labels;
1690 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 memset(handle_tlbm, 0, sizeof(handle_tlbm));
1693 memset(labels, 0, sizeof(labels));
1694 memset(relocs, 0, sizeof(relocs));
1695
1696 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
David Daneybd1437e2009-05-08 15:10:50 -07001697 build_pte_modifiable(&p, &r, K0, K1, label_nopage_tlbm);
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01001698 if (m4kc_tlbp_war())
1699 build_tlb_probe_entry(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 /* Present and writable bits set, set accessed and dirty bits. */
1701 build_make_write(&p, &r, K0, K1);
1702 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1703
David Daneyfd062c82009-05-27 17:47:44 -07001704#ifdef CONFIG_HUGETLB_PAGE
1705 /*
1706 * This is the entry point when
1707 * build_r4000_tlbchange_handler_head spots a huge page.
1708 */
1709 uasm_l_tlb_huge_update(&l, p);
1710 iPTE_LW(&p, K0, K1);
1711 build_pte_modifiable(&p, &r, K0, K1, label_nopage_tlbm);
1712 build_tlb_probe_entry(&p);
1713 uasm_i_ori(&p, K0, K0,
1714 _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
1715 build_huge_handler_tail(&p, &r, &l, K0, K1);
1716#endif
1717
Thiemo Seufere30ec452008-01-28 20:05:38 +00001718 uasm_l_nopage_tlbm(&l, p);
1719 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1720 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 if ((p - handle_tlbm) > FASTPATH_SIZE)
1723 panic("TLB modify handler fastpath space exceeded");
1724
Thiemo Seufere30ec452008-01-28 20:05:38 +00001725 uasm_resolve_relocs(relocs, labels);
1726 pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
1727 (unsigned int)(p - handle_tlbm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001729 dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
Ralf Baechle234fcd12008-03-08 09:56:28 +00001732void __cpuinit build_tlb_refill_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
1734 /*
1735 * The refill handler is generated per-CPU, multi-node systems
1736 * may have local storage for it. The other handlers are only
1737 * needed once.
1738 */
1739 static int run_once = 0;
1740
David Daney1ec56322010-04-28 12:16:18 -07001741#ifdef CONFIG_64BIT
1742 check_for_high_segbits = current_cpu_data.vmbits > (PGDIR_SHIFT + PGD_ORDER + PAGE_SHIFT - 3);
1743#endif
1744
Ralf Baechle10cc3522007-10-11 23:46:15 +01001745 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 case CPU_R2000:
1747 case CPU_R3000:
1748 case CPU_R3000A:
1749 case CPU_R3081E:
1750 case CPU_TX3912:
1751 case CPU_TX3922:
1752 case CPU_TX3927:
David Daney82622282009-10-14 12:16:56 -07001753#ifndef CONFIG_MIPS_PGD_C0_CONTEXT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 build_r3000_tlb_refill_handler();
1755 if (!run_once) {
1756 build_r3000_tlb_load_handler();
1757 build_r3000_tlb_store_handler();
1758 build_r3000_tlb_modify_handler();
1759 run_once++;
1760 }
David Daney82622282009-10-14 12:16:56 -07001761#else
1762 panic("No R3000 TLB refill handler");
1763#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 break;
1765
1766 case CPU_R6000:
1767 case CPU_R6000A:
1768 panic("No R6000 TLB refill handler yet");
1769 break;
1770
1771 case CPU_R8000:
1772 panic("No R8000 TLB refill handler yet");
1773 break;
1774
1775 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (!run_once) {
David Daney3d8bfdd2010-12-21 14:19:11 -08001777#ifdef CONFIG_MIPS_PGD_C0_CONTEXT
1778 build_r4000_setup_pgd();
1779#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 build_r4000_tlb_load_handler();
1781 build_r4000_tlb_store_handler();
1782 build_r4000_tlb_modify_handler();
1783 run_once++;
1784 }
David Daney3d8bfdd2010-12-21 14:19:11 -08001785 build_r4000_tlb_refill_handler();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
1787}
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001788
Ralf Baechle234fcd12008-03-08 09:56:28 +00001789void __cpuinit flush_tlb_handlers(void)
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001790{
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001791 local_flush_icache_range((unsigned long)handle_tlbl,
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001792 (unsigned long)handle_tlbl + sizeof(handle_tlbl));
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001793 local_flush_icache_range((unsigned long)handle_tlbs,
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001794 (unsigned long)handle_tlbs + sizeof(handle_tlbs));
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001795 local_flush_icache_range((unsigned long)handle_tlbm,
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001796 (unsigned long)handle_tlbm + sizeof(handle_tlbm));
David Daney3d8bfdd2010-12-21 14:19:11 -08001797#ifdef CONFIG_MIPS_PGD_C0_CONTEXT
1798 local_flush_icache_range((unsigned long)tlbmiss_handler_setup_pgd,
1799 (unsigned long)tlbmiss_handler_setup_pgd + sizeof(handle_tlbm));
1800#endif
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001801}