blob: e2a9e3687c45815db326cad47d33d38cbcac6b6e [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 *
Ralf Baechle70342282013-01-22 12:59:30 +01008 * Copyright (C) 2004, 2005, 2006, 2008 Thiemo Seufer
9 * 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.
Steven J. Hill113c62d2012-07-06 23:56:00 +020012 * Copyright (C) 2011 MIPS Technologies, Inc.
Ralf Baechle41c594a2006-04-05 09:45:45 +010013 *
14 * ... and the days got worse and worse and now you see
15 * I've gone completly out of my mind.
16 *
17 * They're coming to take me a away haha
18 * they're coming to take me a away hoho hihi haha
19 * to the funny farm where code is beautiful all the time ...
20 *
21 * (Condolences to Napoleon XIV)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
David Daney95affdd2009-05-20 11:40:59 -070024#include <linux/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/kernel.h>
26#include <linux/types.h>
Ralf Baechle631330f2009-06-19 14:05:26 +010027#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/string.h>
29#include <linux/init.h>
David Daney3d8bfdd2010-12-21 14:19:11 -080030#include <linux/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Steven J. Hilld532f3d2013-03-25 11:58:57 -050032#include <asm/mmu_context.h>
David Daney3d8bfdd2010-12-21 14:19:11 -080033#include <asm/cacheflush.h>
34#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/war.h>
Florian Fainelli3482d712010-01-28 15:21:24 +010036#include <asm/uasm.h>
David Howellsb81947c2012-03-28 18:30:02 +010037#include <asm/setup.h>
Thiemo Seufere30ec452008-01-28 20:05:38 +000038
David Daney1ec56322010-04-28 12:16:18 -070039/*
40 * TLB load/store/modify handlers.
41 *
42 * Only the fastpath gets synthesized at runtime, the slowpath for
43 * do_page_fault remains normal asm.
44 */
45extern void tlb_do_page_fault_0(void);
46extern void tlb_do_page_fault_1(void);
47
David Daneybf286072011-07-05 16:34:46 -070048struct work_registers {
49 int r1;
50 int r2;
51 int r3;
52};
53
54struct tlb_reg_save {
55 unsigned long a;
56 unsigned long b;
57} ____cacheline_aligned_in_smp;
58
59static struct tlb_reg_save handler_reg_save[NR_CPUS];
David Daney1ec56322010-04-28 12:16:18 -070060
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010061static inline int r45k_bvahwbug(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 /* XXX: We should probe for the presence of this bug, but we don't. */
64 return 0;
65}
66
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010067static inline int r4k_250MHZhwbug(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 /* XXX: We should probe for the presence of this bug, but we don't. */
70 return 0;
71}
72
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010073static inline int __maybe_unused bcm1250_m3_war(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 return BCM1250_M3_WAR;
76}
77
Ralf Baechleaeffdbb2007-10-11 23:46:14 +010078static inline int __maybe_unused r10000_llsc_war(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 return R10000_LLSC_WAR;
81}
82
David Daneycc33ae42010-12-20 15:54:50 -080083static int use_bbit_insns(void)
84{
85 switch (current_cpu_type()) {
86 case CPU_CAVIUM_OCTEON:
87 case CPU_CAVIUM_OCTEON_PLUS:
88 case CPU_CAVIUM_OCTEON2:
89 return 1;
90 default:
91 return 0;
92 }
93}
94
David Daney2c8c53e2010-12-27 18:07:57 -080095static int use_lwx_insns(void)
96{
97 switch (current_cpu_type()) {
98 case CPU_CAVIUM_OCTEON2:
99 return 1;
100 default:
101 return 0;
102 }
103}
104#if defined(CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE) && \
105 CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE > 0
106static bool scratchpad_available(void)
107{
108 return true;
109}
110static int scratchpad_offset(int i)
111{
112 /*
113 * CVMSEG starts at address -32768 and extends for
114 * CAVIUM_OCTEON_CVMSEG_SIZE 128 byte cache lines.
115 */
116 i += 1; /* Kernel use starts at the top and works down. */
117 return CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE * 128 - (8 * i) - 32768;
118}
119#else
120static bool scratchpad_available(void)
121{
122 return false;
123}
124static int scratchpad_offset(int i)
125{
126 BUG();
David Daneye1c87d22011-01-19 15:24:42 -0800127 /* Really unreachable, but evidently some GCC want this. */
128 return 0;
David Daney2c8c53e2010-12-27 18:07:57 -0800129}
130#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/*
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +0100132 * Found by experiment: At least some revisions of the 4kc throw under
133 * some circumstances a machine check exception, triggered by invalid
134 * values in the index register. Delaying the tlbp instruction until
135 * after the next branch, plus adding an additional nop in front of
136 * tlbwi/tlbwr avoids the invalid index register values. Nobody knows
137 * why; it's not an issue caused by the core RTL.
138 *
139 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000140static int __cpuinit m4kc_tlbp_war(void)
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +0100141{
142 return (current_cpu_data.processor_id & 0xffff00) ==
143 (PRID_COMP_MIPS | PRID_IMP_4KC);
144}
145
Thiemo Seufere30ec452008-01-28 20:05:38 +0000146/* Handle labels (which must be positive integers). */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147enum label_id {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000148 label_second_part = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 label_leave,
150 label_vmalloc,
151 label_vmalloc_done,
Ralf Baechle02a54172012-10-13 22:46:26 +0200152 label_tlbw_hazard_0,
153 label_split = label_tlbw_hazard_0 + 8,
David Daney6dd93442010-02-10 15:12:47 -0800154 label_tlbl_goaround1,
155 label_tlbl_goaround2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 label_nopage_tlbl,
157 label_nopage_tlbs,
158 label_nopage_tlbm,
159 label_smp_pgtable_change,
160 label_r3000_write_probe_fail,
David Daney1ec56322010-04-28 12:16:18 -0700161 label_large_segbits_fault,
David Daneyaa1762f2012-10-17 00:48:10 +0200162#ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
David Daneyfd062c82009-05-27 17:47:44 -0700163 label_tlb_huge_update,
164#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
Thiemo Seufere30ec452008-01-28 20:05:38 +0000167UASM_L_LA(_second_part)
168UASM_L_LA(_leave)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000169UASM_L_LA(_vmalloc)
170UASM_L_LA(_vmalloc_done)
Ralf Baechle02a54172012-10-13 22:46:26 +0200171/* _tlbw_hazard_x is handled differently. */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000172UASM_L_LA(_split)
David Daney6dd93442010-02-10 15:12:47 -0800173UASM_L_LA(_tlbl_goaround1)
174UASM_L_LA(_tlbl_goaround2)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000175UASM_L_LA(_nopage_tlbl)
176UASM_L_LA(_nopage_tlbs)
177UASM_L_LA(_nopage_tlbm)
178UASM_L_LA(_smp_pgtable_change)
179UASM_L_LA(_r3000_write_probe_fail)
David Daney1ec56322010-04-28 12:16:18 -0700180UASM_L_LA(_large_segbits_fault)
David Daneyaa1762f2012-10-17 00:48:10 +0200181#ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
David Daneyfd062c82009-05-27 17:47:44 -0700182UASM_L_LA(_tlb_huge_update)
183#endif
Atsushi Nemoto656be922006-10-26 00:08:31 +0900184
Ralf Baechle02a54172012-10-13 22:46:26 +0200185static int __cpuinitdata hazard_instance;
186
Kevin Cernekeef151f3b2012-11-07 18:39:48 +0000187static void __cpuinit uasm_bgezl_hazard(u32 **p,
188 struct uasm_reloc **r,
189 int instance)
Ralf Baechle02a54172012-10-13 22:46:26 +0200190{
191 switch (instance) {
192 case 0 ... 7:
193 uasm_il_bgezl(p, r, 0, label_tlbw_hazard_0 + instance);
194 return;
195 default:
196 BUG();
197 }
198}
199
Kevin Cernekeef151f3b2012-11-07 18:39:48 +0000200static void __cpuinit uasm_bgezl_label(struct uasm_label **l,
201 u32 **p,
202 int instance)
Ralf Baechle02a54172012-10-13 22:46:26 +0200203{
204 switch (instance) {
205 case 0 ... 7:
206 uasm_build_label(l, *p, label_tlbw_hazard_0 + instance);
207 break;
208 default:
209 BUG();
210 }
211}
212
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +0200213/*
Ralf Baechlea2c763e2012-10-16 22:20:26 +0200214 * pgtable bits are assigned dynamically depending on processor feature
215 * and statically based on kernel configuration. This spits out the actual
Ralf Baechle70342282013-01-22 12:59:30 +0100216 * values the kernel is using. Required to make sense from disassembled
Ralf Baechlea2c763e2012-10-16 22:20:26 +0200217 * TLB exception handlers.
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +0200218 */
Ralf Baechlea2c763e2012-10-16 22:20:26 +0200219static void output_pgtable_bits_defines(void)
220{
221#define pr_define(fmt, ...) \
222 pr_debug("#define " fmt, ##__VA_ARGS__)
223
224 pr_debug("#include <asm/asm.h>\n");
225 pr_debug("#include <asm/regdef.h>\n");
226 pr_debug("\n");
227
228 pr_define("_PAGE_PRESENT_SHIFT %d\n", _PAGE_PRESENT_SHIFT);
229 pr_define("_PAGE_READ_SHIFT %d\n", _PAGE_READ_SHIFT);
230 pr_define("_PAGE_WRITE_SHIFT %d\n", _PAGE_WRITE_SHIFT);
231 pr_define("_PAGE_ACCESSED_SHIFT %d\n", _PAGE_ACCESSED_SHIFT);
232 pr_define("_PAGE_MODIFIED_SHIFT %d\n", _PAGE_MODIFIED_SHIFT);
Ralf Baechle970d0322012-10-18 13:54:15 +0200233#ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
Ralf Baechlea2c763e2012-10-16 22:20:26 +0200234 pr_define("_PAGE_HUGE_SHIFT %d\n", _PAGE_HUGE_SHIFT);
Ralf Baechle970d0322012-10-18 13:54:15 +0200235 pr_define("_PAGE_SPLITTING_SHIFT %d\n", _PAGE_SPLITTING_SHIFT);
Ralf Baechlea2c763e2012-10-16 22:20:26 +0200236#endif
237 if (cpu_has_rixi) {
238#ifdef _PAGE_NO_EXEC_SHIFT
239 pr_define("_PAGE_NO_EXEC_SHIFT %d\n", _PAGE_NO_EXEC_SHIFT);
240#endif
241#ifdef _PAGE_NO_READ_SHIFT
242 pr_define("_PAGE_NO_READ_SHIFT %d\n", _PAGE_NO_READ_SHIFT);
243#endif
244 }
245 pr_define("_PAGE_GLOBAL_SHIFT %d\n", _PAGE_GLOBAL_SHIFT);
246 pr_define("_PAGE_VALID_SHIFT %d\n", _PAGE_VALID_SHIFT);
247 pr_define("_PAGE_DIRTY_SHIFT %d\n", _PAGE_DIRTY_SHIFT);
248 pr_define("_PFN_SHIFT %d\n", _PFN_SHIFT);
249 pr_debug("\n");
250}
251
252static inline void dump_handler(const char *symbol, const u32 *handler, int count)
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +0200253{
254 int i;
255
Ralf Baechlea2c763e2012-10-16 22:20:26 +0200256 pr_debug("LEAF(%s)\n", symbol);
257
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +0200258 pr_debug("\t.set push\n");
259 pr_debug("\t.set noreorder\n");
260
261 for (i = 0; i < count; i++)
Ralf Baechlea2c763e2012-10-16 22:20:26 +0200262 pr_debug("\t.word\t0x%08x\t\t# %p\n", handler[i], &handler[i]);
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +0200263
Ralf Baechlea2c763e2012-10-16 22:20:26 +0200264 pr_debug("\t.set\tpop\n");
265
266 pr_debug("\tEND(%s)\n", symbol);
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +0200267}
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/* The only general purpose registers allowed in TLB handlers. */
270#define K0 26
271#define K1 27
272
273/* Some CP0 registers */
Ralf Baechle41c594a2006-04-05 09:45:45 +0100274#define C0_INDEX 0, 0
275#define C0_ENTRYLO0 2, 0
276#define C0_TCBIND 2, 2
277#define C0_ENTRYLO1 3, 0
278#define C0_CONTEXT 4, 0
David Daneyfd062c82009-05-27 17:47:44 -0700279#define C0_PAGEMASK 5, 0
Ralf Baechle41c594a2006-04-05 09:45:45 +0100280#define C0_BADVADDR 8, 0
281#define C0_ENTRYHI 10, 0
282#define C0_EPC 14, 0
283#define C0_XCONTEXT 20, 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Ralf Baechle875d43e2005-09-03 15:56:16 -0700285#ifdef CONFIG_64BIT
Thiemo Seufere30ec452008-01-28 20:05:38 +0000286# define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_XCONTEXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287#else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000288# define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_CONTEXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289#endif
290
291/* The worst case length of the handler is around 18 instructions for
292 * R3000-style TLBs and up to 63 instructions for R4000-style TLBs.
293 * Maximum space available is 32 instructions for R3000 and 64
294 * instructions for R4000.
295 *
296 * We deliberately chose a buffer size of 128, so we won't scribble
297 * over anything important on overflow before we panic.
298 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000299static u32 tlb_handler[128] __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301/* simply assume worst case size for labels and relocs */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000302static struct uasm_label labels[128] __cpuinitdata;
303static struct uasm_reloc relocs[128] __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
David Daney1ec56322010-04-28 12:16:18 -0700305#ifdef CONFIG_64BIT
306static int check_for_high_segbits __cpuinitdata;
307#endif
308
Steven J. Hilld532f3d2013-03-25 11:58:57 -0500309static void __cpuinit insn_fixup(unsigned int **start, unsigned int **stop,
310 unsigned int i_const)
311{
312 unsigned int **p, *ip;
313
314 for (p = start; p < stop; p++) {
315 ip = *p;
316 *ip = (*ip & 0xffff0000) | i_const;
317 }
318 local_flush_icache_range((unsigned long)*p, (unsigned long)((*p) + 1));
319}
320
321#define asid_insn_fixup(section, const) \
322do { \
323 extern unsigned int *__start_ ## section; \
324 extern unsigned int *__stop_ ## section; \
325 insn_fixup(&__start_ ## section, &__stop_ ## section, const); \
326} while(0)
327
328/*
329 * Caller is assumed to flush the caches before the first context switch.
330 */
331static void __cpuinit setup_asid(unsigned int inc, unsigned int mask,
332 unsigned int version_mask,
333 unsigned int first_version)
334{
335 extern asmlinkage void handle_ri_rdhwr_vivt(void);
336 unsigned long *vivt_exc;
337
338 asid_insn_fixup(__asid_inc, inc);
339 asid_insn_fixup(__asid_mask, mask);
340 asid_insn_fixup(__asid_version_mask, version_mask);
341 asid_insn_fixup(__asid_first_version, first_version);
342
343 /* Patch up the 'handle_ri_rdhwr_vivt' handler. */
344 vivt_exc = (unsigned long *) &handle_ri_rdhwr_vivt;
345 vivt_exc++;
346 *vivt_exc = (*vivt_exc & ~mask) | mask;
347
348 current_cpu_data.asid_cache = first_version;
349}
350
David Daney2c8c53e2010-12-27 18:07:57 -0800351static int check_for_high_segbits __cpuinitdata;
David Daney3d8bfdd2010-12-21 14:19:11 -0800352
353static unsigned int kscratch_used_mask __cpuinitdata;
354
355static int __cpuinit allocate_kscratch(void)
356{
357 int r;
358 unsigned int a = cpu_data[0].kscratch_mask & ~kscratch_used_mask;
359
360 r = ffs(a);
361
362 if (r == 0)
363 return -1;
364
365 r--; /* make it zero based */
366
367 kscratch_used_mask |= (1 << r);
368
369 return r;
370}
371
David Daney2c8c53e2010-12-27 18:07:57 -0800372static int scratch_reg __cpuinitdata;
David Daney3d8bfdd2010-12-21 14:19:11 -0800373static int pgd_reg __cpuinitdata;
David Daney2c8c53e2010-12-27 18:07:57 -0800374enum vmalloc64_mode {not_refill, refill_scratch, refill_noscratch};
David Daney3d8bfdd2010-12-21 14:19:11 -0800375
David Daneybf286072011-07-05 16:34:46 -0700376static struct work_registers __cpuinit build_get_work_registers(u32 **p)
377{
378 struct work_registers r;
379
380 int smp_processor_id_reg;
381 int smp_processor_id_sel;
382 int smp_processor_id_shift;
383
384 if (scratch_reg > 0) {
385 /* Save in CPU local C0_KScratch? */
386 UASM_i_MTC0(p, 1, 31, scratch_reg);
387 r.r1 = K0;
388 r.r2 = K1;
389 r.r3 = 1;
390 return r;
391 }
392
393 if (num_possible_cpus() > 1) {
394#ifdef CONFIG_MIPS_PGD_C0_CONTEXT
395 smp_processor_id_shift = 51;
396 smp_processor_id_reg = 20; /* XContext */
397 smp_processor_id_sel = 0;
398#else
399# ifdef CONFIG_32BIT
400 smp_processor_id_shift = 25;
401 smp_processor_id_reg = 4; /* Context */
402 smp_processor_id_sel = 0;
403# endif
404# ifdef CONFIG_64BIT
405 smp_processor_id_shift = 26;
406 smp_processor_id_reg = 4; /* Context */
407 smp_processor_id_sel = 0;
408# endif
409#endif
410 /* Get smp_processor_id */
411 UASM_i_MFC0(p, K0, smp_processor_id_reg, smp_processor_id_sel);
412 UASM_i_SRL_SAFE(p, K0, K0, smp_processor_id_shift);
413
414 /* handler_reg_save index in K0 */
415 UASM_i_SLL(p, K0, K0, ilog2(sizeof(struct tlb_reg_save)));
416
417 UASM_i_LA(p, K1, (long)&handler_reg_save);
418 UASM_i_ADDU(p, K0, K0, K1);
419 } else {
420 UASM_i_LA(p, K0, (long)&handler_reg_save);
421 }
422 /* K0 now points to save area, save $1 and $2 */
423 UASM_i_SW(p, 1, offsetof(struct tlb_reg_save, a), K0);
424 UASM_i_SW(p, 2, offsetof(struct tlb_reg_save, b), K0);
425
426 r.r1 = K1;
427 r.r2 = 1;
428 r.r3 = 2;
429 return r;
430}
431
432static void __cpuinit build_restore_work_registers(u32 **p)
433{
434 if (scratch_reg > 0) {
435 UASM_i_MFC0(p, 1, 31, scratch_reg);
436 return;
437 }
438 /* K0 already points to save area, restore $1 and $2 */
439 UASM_i_LW(p, 1, offsetof(struct tlb_reg_save, a), K0);
440 UASM_i_LW(p, 2, offsetof(struct tlb_reg_save, b), K0);
441}
442
David Daney2c8c53e2010-12-27 18:07:57 -0800443#ifndef CONFIG_MIPS_PGD_C0_CONTEXT
444
David Daney82622282009-10-14 12:16:56 -0700445/*
446 * CONFIG_MIPS_PGD_C0_CONTEXT implies 64 bit and lack of pgd_current,
447 * we cannot do r3000 under these circumstances.
David Daney3d8bfdd2010-12-21 14:19:11 -0800448 *
449 * Declare pgd_current here instead of including mmu_context.h to avoid type
450 * conflicts for tlbmiss_handler_setup_pgd
David Daney82622282009-10-14 12:16:56 -0700451 */
David Daney3d8bfdd2010-12-21 14:19:11 -0800452extern unsigned long pgd_current[];
David Daney82622282009-10-14 12:16:56 -0700453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/*
455 * The R3000 TLB handler is simple.
456 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000457static void __cpuinit build_r3000_tlb_refill_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 long pgdc = (long)pgd_current;
460 u32 *p;
461
462 memset(tlb_handler, 0, sizeof(tlb_handler));
463 p = tlb_handler;
464
Thiemo Seufere30ec452008-01-28 20:05:38 +0000465 uasm_i_mfc0(&p, K0, C0_BADVADDR);
466 uasm_i_lui(&p, K1, uasm_rel_hi(pgdc)); /* cp0 delay */
467 uasm_i_lw(&p, K1, uasm_rel_lo(pgdc), K1);
468 uasm_i_srl(&p, K0, K0, 22); /* load delay */
469 uasm_i_sll(&p, K0, K0, 2);
470 uasm_i_addu(&p, K1, K1, K0);
471 uasm_i_mfc0(&p, K0, C0_CONTEXT);
472 uasm_i_lw(&p, K1, 0, K1); /* cp0 delay */
473 uasm_i_andi(&p, K0, K0, 0xffc); /* load delay */
474 uasm_i_addu(&p, K1, K1, K0);
475 uasm_i_lw(&p, K0, 0, K1);
476 uasm_i_nop(&p); /* load delay */
477 uasm_i_mtc0(&p, K0, C0_ENTRYLO0);
478 uasm_i_mfc0(&p, K1, C0_EPC); /* cp0 delay */
479 uasm_i_tlbwr(&p); /* cp0 delay */
480 uasm_i_jr(&p, K1);
481 uasm_i_rfe(&p); /* branch delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 if (p > tlb_handler + 32)
484 panic("TLB refill handler space exceeded");
485
Thiemo Seufere30ec452008-01-28 20:05:38 +0000486 pr_debug("Wrote TLB refill handler (%u instructions).\n",
487 (unsigned int)(p - tlb_handler));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Ralf Baechle91b05e62006-03-29 18:53:00 +0100489 memcpy((void *)ebase, tlb_handler, 0x80);
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +0200490
Ralf Baechlea2c763e2012-10-16 22:20:26 +0200491 dump_handler("r3000_tlb_refill", (u32 *)ebase, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
David Daney82622282009-10-14 12:16:56 -0700493#endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495/*
496 * The R4000 TLB handler is much more complicated. We have two
497 * consecutive handler areas with 32 instructions space each.
498 * Since they aren't used at the same time, we can overflow in the
499 * other one.To keep things simple, we first assume linear space,
500 * then we relocate it to the final handler layout as needed.
501 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000502static u32 final_handler[64] __cpuinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504/*
505 * Hazards
506 *
507 * From the IDT errata for the QED RM5230 (Nevada), processor revision 1.0:
508 * 2. A timing hazard exists for the TLBP instruction.
509 *
Ralf Baechle70342282013-01-22 12:59:30 +0100510 * stalling_instruction
511 * TLBP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 *
513 * The JTLB is being read for the TLBP throughout the stall generated by the
514 * previous instruction. This is not really correct as the stalling instruction
515 * can modify the address used to access the JTLB. The failure symptom is that
516 * the TLBP instruction will use an address created for the stalling instruction
517 * and not the address held in C0_ENHI and thus report the wrong results.
518 *
519 * The software work-around is to not allow the instruction preceding the TLBP
520 * to stall - make it an NOP or some other instruction guaranteed not to stall.
521 *
Ralf Baechle70342282013-01-22 12:59:30 +0100522 * Errata 2 will not be fixed. This errata is also on the R5000.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 *
524 * As if we MIPS hackers wouldn't know how to nop pipelines happy ...
525 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000526static void __cpuinit __maybe_unused build_tlb_probe_entry(u32 **p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Ralf Baechle10cc3522007-10-11 23:46:15 +0100528 switch (current_cpu_type()) {
Thomas Bogendoerfer326e2e12008-05-12 13:55:42 +0200529 /* Found by experiment: R4600 v2.0/R4700 needs this, too. */
Thiemo Seuferf5b4d952005-09-09 17:11:50 +0000530 case CPU_R4600:
Thomas Bogendoerfer326e2e12008-05-12 13:55:42 +0200531 case CPU_R4700:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 case CPU_R5000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 case CPU_NEVADA:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000534 uasm_i_nop(p);
535 uasm_i_tlbp(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 break;
537
538 default:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000539 uasm_i_tlbp(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 break;
541 }
542}
543
544/*
545 * Write random or indexed TLB entry, and care about the hazards from
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300546 * the preceding mtc0 and for the following eret.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 */
548enum tlb_write_entry { tlb_random, tlb_indexed };
549
Ralf Baechle234fcd12008-03-08 09:56:28 +0000550static void __cpuinit build_tlb_write_entry(u32 **p, struct uasm_label **l,
Thiemo Seufere30ec452008-01-28 20:05:38 +0000551 struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 enum tlb_write_entry wmode)
553{
554 void(*tlbw)(u32 **) = NULL;
555
556 switch (wmode) {
Thiemo Seufere30ec452008-01-28 20:05:38 +0000557 case tlb_random: tlbw = uasm_i_tlbwr; break;
558 case tlb_indexed: tlbw = uasm_i_tlbwi; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560
Ralf Baechle161548b2008-01-29 10:14:54 +0000561 if (cpu_has_mips_r2) {
Steven J. Hill625c0a22012-08-28 23:20:08 -0500562 /*
563 * The architecture spec says an ehb is required here,
564 * but a number of cores do not have the hazard and
565 * using an ehb causes an expensive pipeline stall.
566 */
567 switch (current_cpu_type()) {
568 case CPU_M14KC:
569 case CPU_74K:
570 break;
571
572 default:
David Daney41f0e4d2009-05-12 12:41:53 -0700573 uasm_i_ehb(p);
Steven J. Hill625c0a22012-08-28 23:20:08 -0500574 break;
575 }
Ralf Baechle161548b2008-01-29 10:14:54 +0000576 tlbw(p);
577 return;
578 }
579
Ralf Baechle10cc3522007-10-11 23:46:15 +0100580 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 case CPU_R4000PC:
582 case CPU_R4000SC:
583 case CPU_R4000MC:
584 case CPU_R4400PC:
585 case CPU_R4400SC:
586 case CPU_R4400MC:
587 /*
588 * This branch uses up a mtc0 hazard nop slot and saves
589 * two nops after the tlbw instruction.
590 */
Ralf Baechle02a54172012-10-13 22:46:26 +0200591 uasm_bgezl_hazard(p, r, hazard_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 tlbw(p);
Ralf Baechle02a54172012-10-13 22:46:26 +0200593 uasm_bgezl_label(l, p, hazard_instance);
594 hazard_instance++;
Thiemo Seufere30ec452008-01-28 20:05:38 +0000595 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 break;
597
598 case CPU_R4600:
599 case CPU_R4700:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000600 uasm_i_nop(p);
Maciej W. Rozycki2c93e122005-06-30 10:51:01 +0000601 tlbw(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000602 uasm_i_nop(p);
Maciej W. Rozycki2c93e122005-06-30 10:51:01 +0000603 break;
604
Ralf Baechle359187d2012-10-16 22:13:06 +0200605 case CPU_R5000:
Ralf Baechle359187d2012-10-16 22:13:06 +0200606 case CPU_NEVADA:
607 uasm_i_nop(p); /* QED specifies 2 nops hazard */
608 uasm_i_nop(p); /* QED specifies 2 nops hazard */
609 tlbw(p);
610 break;
611
Maciej W. Rozycki2c93e122005-06-30 10:51:01 +0000612 case CPU_R4300:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 case CPU_5KC:
614 case CPU_TX49XX:
Pete Popovbdf21b12005-07-14 17:47:57 +0000615 case CPU_PR4450:
Jayachandran Cefa0f812011-05-07 01:36:21 +0530616 case CPU_XLR:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000617 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 tlbw(p);
619 break;
620
621 case CPU_R10000:
622 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -0400623 case CPU_R14000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 case CPU_4KC:
Thomas Bogendoerferb1ec4c82008-03-26 16:42:54 +0100625 case CPU_4KEC:
Steven J. Hill113c62d2012-07-06 23:56:00 +0200626 case CPU_M14KC:
Steven J. Hillf8fa4812012-12-07 03:51:35 +0000627 case CPU_M14KEC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 case CPU_SB1:
Andrew Isaacson93ce2f522005-10-19 23:56:20 -0700629 case CPU_SB1A:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 case CPU_4KSC:
631 case CPU_20KC:
632 case CPU_25KF:
Kevin Cernekee602977b2010-10-16 14:22:30 -0700633 case CPU_BMIPS32:
634 case CPU_BMIPS3300:
635 case CPU_BMIPS4350:
636 case CPU_BMIPS4380:
637 case CPU_BMIPS5000:
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800638 case CPU_LOONGSON2:
Shinya Kuribayashia644b272009-03-03 18:05:51 +0900639 case CPU_R5500:
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +0100640 if (m4kc_tlbp_war())
Thiemo Seufere30ec452008-01-28 20:05:38 +0000641 uasm_i_nop(p);
Manuel Lauss2f794d02009-03-25 17:49:30 +0100642 case CPU_ALCHEMY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 tlbw(p);
644 break;
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 case CPU_RM7000:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000647 uasm_i_nop(p);
648 uasm_i_nop(p);
649 uasm_i_nop(p);
650 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 tlbw(p);
652 break;
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 case CPU_VR4111:
655 case CPU_VR4121:
656 case CPU_VR4122:
657 case CPU_VR4181:
658 case CPU_VR4181A:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000659 uasm_i_nop(p);
660 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 tlbw(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000662 uasm_i_nop(p);
663 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 break;
665
666 case CPU_VR4131:
667 case CPU_VR4133:
Ralf Baechle7623deb2005-08-29 16:49:55 +0000668 case CPU_R5432:
Thiemo Seufere30ec452008-01-28 20:05:38 +0000669 uasm_i_nop(p);
670 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 tlbw(p);
672 break;
673
Lars-Peter Clausen83ccf692010-07-17 11:07:51 +0000674 case CPU_JZRISC:
675 tlbw(p);
676 uasm_i_nop(p);
677 break;
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 default:
680 panic("No TLB refill handler yet (CPU type: %d)",
681 current_cpu_data.cputype);
682 break;
683 }
684}
685
David Daney6dd93442010-02-10 15:12:47 -0800686static __cpuinit __maybe_unused void build_convert_pte_to_entrylo(u32 **p,
687 unsigned int reg)
688{
Steven J. Hill05857c62012-09-13 16:51:46 -0500689 if (cpu_has_rixi) {
David Daney748e7872012-08-23 10:02:03 -0700690 UASM_i_ROTR(p, reg, reg, ilog2(_PAGE_GLOBAL));
David Daney6dd93442010-02-10 15:12:47 -0800691 } else {
692#ifdef CONFIG_64BIT_PHYS_ADDR
David Daney3be60222010-04-28 12:16:17 -0700693 uasm_i_dsrl_safe(p, reg, reg, ilog2(_PAGE_GLOBAL));
David Daney6dd93442010-02-10 15:12:47 -0800694#else
695 UASM_i_SRL(p, reg, reg, ilog2(_PAGE_GLOBAL));
696#endif
697 }
698}
699
David Daneyaa1762f2012-10-17 00:48:10 +0200700#ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
David Daney6dd93442010-02-10 15:12:47 -0800701
702static __cpuinit void build_restore_pagemask(u32 **p,
703 struct uasm_reloc **r,
704 unsigned int tmp,
David Daney2c8c53e2010-12-27 18:07:57 -0800705 enum label_id lid,
706 int restore_scratch)
David Daney6dd93442010-02-10 15:12:47 -0800707{
David Daney2c8c53e2010-12-27 18:07:57 -0800708 if (restore_scratch) {
709 /* Reset default page size */
710 if (PM_DEFAULT_MASK >> 16) {
711 uasm_i_lui(p, tmp, PM_DEFAULT_MASK >> 16);
712 uasm_i_ori(p, tmp, tmp, PM_DEFAULT_MASK & 0xffff);
713 uasm_i_mtc0(p, tmp, C0_PAGEMASK);
714 uasm_il_b(p, r, lid);
715 } else if (PM_DEFAULT_MASK) {
716 uasm_i_ori(p, tmp, 0, PM_DEFAULT_MASK);
717 uasm_i_mtc0(p, tmp, C0_PAGEMASK);
718 uasm_il_b(p, r, lid);
719 } else {
720 uasm_i_mtc0(p, 0, C0_PAGEMASK);
721 uasm_il_b(p, r, lid);
722 }
723 if (scratch_reg > 0)
724 UASM_i_MFC0(p, 1, 31, scratch_reg);
725 else
726 UASM_i_LW(p, 1, scratchpad_offset(0), 0);
David Daney6dd93442010-02-10 15:12:47 -0800727 } else {
David Daney2c8c53e2010-12-27 18:07:57 -0800728 /* Reset default page size */
729 if (PM_DEFAULT_MASK >> 16) {
730 uasm_i_lui(p, tmp, PM_DEFAULT_MASK >> 16);
731 uasm_i_ori(p, tmp, tmp, PM_DEFAULT_MASK & 0xffff);
732 uasm_il_b(p, r, lid);
733 uasm_i_mtc0(p, tmp, C0_PAGEMASK);
734 } else if (PM_DEFAULT_MASK) {
735 uasm_i_ori(p, tmp, 0, PM_DEFAULT_MASK);
736 uasm_il_b(p, r, lid);
737 uasm_i_mtc0(p, tmp, C0_PAGEMASK);
738 } else {
739 uasm_il_b(p, r, lid);
740 uasm_i_mtc0(p, 0, C0_PAGEMASK);
741 }
David Daney6dd93442010-02-10 15:12:47 -0800742 }
743}
744
David Daneyfd062c82009-05-27 17:47:44 -0700745static __cpuinit void build_huge_tlb_write_entry(u32 **p,
746 struct uasm_label **l,
747 struct uasm_reloc **r,
748 unsigned int tmp,
David Daney2c8c53e2010-12-27 18:07:57 -0800749 enum tlb_write_entry wmode,
750 int restore_scratch)
David Daneyfd062c82009-05-27 17:47:44 -0700751{
752 /* Set huge page tlb entry size */
753 uasm_i_lui(p, tmp, PM_HUGE_MASK >> 16);
754 uasm_i_ori(p, tmp, tmp, PM_HUGE_MASK & 0xffff);
755 uasm_i_mtc0(p, tmp, C0_PAGEMASK);
756
757 build_tlb_write_entry(p, l, r, wmode);
758
David Daney2c8c53e2010-12-27 18:07:57 -0800759 build_restore_pagemask(p, r, tmp, label_leave, restore_scratch);
David Daneyfd062c82009-05-27 17:47:44 -0700760}
761
762/*
763 * Check if Huge PTE is present, if so then jump to LABEL.
764 */
765static void __cpuinit
766build_is_huge_pte(u32 **p, struct uasm_reloc **r, unsigned int tmp,
767 unsigned int pmd, int lid)
768{
769 UASM_i_LW(p, tmp, 0, pmd);
David Daneycc33ae42010-12-20 15:54:50 -0800770 if (use_bbit_insns()) {
771 uasm_il_bbit1(p, r, tmp, ilog2(_PAGE_HUGE), lid);
772 } else {
773 uasm_i_andi(p, tmp, tmp, _PAGE_HUGE);
774 uasm_il_bnez(p, r, tmp, lid);
775 }
David Daneyfd062c82009-05-27 17:47:44 -0700776}
777
778static __cpuinit void build_huge_update_entries(u32 **p,
779 unsigned int pte,
780 unsigned int tmp)
781{
782 int small_sequence;
783
784 /*
785 * A huge PTE describes an area the size of the
786 * configured huge page size. This is twice the
787 * of the large TLB entry size we intend to use.
788 * A TLB entry half the size of the configured
789 * huge page size is configured into entrylo0
790 * and entrylo1 to cover the contiguous huge PTE
791 * address space.
792 */
793 small_sequence = (HPAGE_SIZE >> 7) < 0x10000;
794
Ralf Baechle70342282013-01-22 12:59:30 +0100795 /* We can clobber tmp. It isn't used after this.*/
David Daneyfd062c82009-05-27 17:47:44 -0700796 if (!small_sequence)
797 uasm_i_lui(p, tmp, HPAGE_SIZE >> (7 + 16));
798
David Daney6dd93442010-02-10 15:12:47 -0800799 build_convert_pte_to_entrylo(p, pte);
David Daney9b8c3892010-02-10 15:12:44 -0800800 UASM_i_MTC0(p, pte, C0_ENTRYLO0); /* load it */
David Daneyfd062c82009-05-27 17:47:44 -0700801 /* convert to entrylo1 */
802 if (small_sequence)
803 UASM_i_ADDIU(p, pte, pte, HPAGE_SIZE >> 7);
804 else
805 UASM_i_ADDU(p, pte, pte, tmp);
806
David Daney9b8c3892010-02-10 15:12:44 -0800807 UASM_i_MTC0(p, pte, C0_ENTRYLO1); /* load it */
David Daneyfd062c82009-05-27 17:47:44 -0700808}
809
810static __cpuinit void build_huge_handler_tail(u32 **p,
811 struct uasm_reloc **r,
812 struct uasm_label **l,
813 unsigned int pte,
814 unsigned int ptr)
815{
816#ifdef CONFIG_SMP
817 UASM_i_SC(p, pte, 0, ptr);
818 uasm_il_beqz(p, r, pte, label_tlb_huge_update);
819 UASM_i_LW(p, pte, 0, ptr); /* Needed because SC killed our PTE */
820#else
821 UASM_i_SW(p, pte, 0, ptr);
822#endif
823 build_huge_update_entries(p, pte, ptr);
David Daney2c8c53e2010-12-27 18:07:57 -0800824 build_huge_tlb_write_entry(p, l, r, pte, tlb_indexed, 0);
David Daneyfd062c82009-05-27 17:47:44 -0700825}
David Daneyaa1762f2012-10-17 00:48:10 +0200826#endif /* CONFIG_MIPS_HUGE_TLB_SUPPORT */
David Daneyfd062c82009-05-27 17:47:44 -0700827
Ralf Baechle875d43e2005-09-03 15:56:16 -0700828#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829/*
830 * TMP and PTR are scratch.
831 * TMP will be clobbered, PTR will hold the pmd entry.
832 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000833static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000834build_get_pmde64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 unsigned int tmp, unsigned int ptr)
836{
David Daney82622282009-10-14 12:16:56 -0700837#ifndef CONFIG_MIPS_PGD_C0_CONTEXT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 long pgdc = (long)pgd_current;
David Daney82622282009-10-14 12:16:56 -0700839#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 /*
841 * The vmalloc handling is not in the hotpath.
842 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000843 uasm_i_dmfc0(p, tmp, C0_BADVADDR);
David Daney1ec56322010-04-28 12:16:18 -0700844
845 if (check_for_high_segbits) {
846 /*
847 * The kernel currently implicitely assumes that the
848 * MIPS SEGBITS parameter for the processor is
849 * (PGDIR_SHIFT+PGDIR_BITS) or less, and will never
850 * allocate virtual addresses outside the maximum
851 * range for SEGBITS = (PGDIR_SHIFT+PGDIR_BITS). But
852 * that doesn't prevent user code from accessing the
853 * higher xuseg addresses. Here, we make sure that
854 * everything but the lower xuseg addresses goes down
855 * the module_alloc/vmalloc path.
856 */
857 uasm_i_dsrl_safe(p, ptr, tmp, PGDIR_SHIFT + PGD_ORDER + PAGE_SHIFT - 3);
858 uasm_il_bnez(p, r, ptr, label_vmalloc);
859 } else {
860 uasm_il_bltz(p, r, tmp, label_vmalloc);
861 }
Thiemo Seufere30ec452008-01-28 20:05:38 +0000862 /* No uasm_i_nop needed here, since the next insn doesn't touch TMP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
David Daney82622282009-10-14 12:16:56 -0700864#ifdef CONFIG_MIPS_PGD_C0_CONTEXT
David Daney3d8bfdd2010-12-21 14:19:11 -0800865 if (pgd_reg != -1) {
866 /* pgd is in pgd_reg */
867 UASM_i_MFC0(p, ptr, 31, pgd_reg);
868 } else {
869 /*
870 * &pgd << 11 stored in CONTEXT [23..63].
871 */
872 UASM_i_MFC0(p, ptr, C0_CONTEXT);
873
874 /* Clear lower 23 bits of context. */
875 uasm_i_dins(p, ptr, 0, 0, 23);
876
Ralf Baechle70342282013-01-22 12:59:30 +0100877 /* 1 0 1 0 1 << 6 xkphys cached */
David Daney3d8bfdd2010-12-21 14:19:11 -0800878 uasm_i_ori(p, ptr, ptr, 0x540);
879 uasm_i_drotr(p, ptr, ptr, 11);
880 }
David Daney82622282009-10-14 12:16:56 -0700881#elif defined(CONFIG_SMP)
Ralf Baechle70342282013-01-22 12:59:30 +0100882# ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle41c594a2006-04-05 09:45:45 +0100883 /*
884 * SMTC uses TCBind value as "CPU" index
885 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000886 uasm_i_mfc0(p, ptr, C0_TCBIND);
David Daney3be60222010-04-28 12:16:17 -0700887 uasm_i_dsrl_safe(p, ptr, ptr, 19);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100888# else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 /*
Thiemo Seufer1b3a6e92005-04-01 14:07:13 +0000890 * 64 bit SMP running in XKPHYS has smp_processor_id() << 3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 * stored in CONTEXT.
892 */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000893 uasm_i_dmfc0(p, ptr, C0_CONTEXT);
David Daney3be60222010-04-28 12:16:17 -0700894 uasm_i_dsrl_safe(p, ptr, ptr, 23);
David Daney82622282009-10-14 12:16:56 -0700895# endif
Thiemo Seufere30ec452008-01-28 20:05:38 +0000896 UASM_i_LA_mostly(p, tmp, pgdc);
897 uasm_i_daddu(p, ptr, ptr, tmp);
898 uasm_i_dmfc0(p, tmp, C0_BADVADDR);
899 uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900#else
Thiemo Seufere30ec452008-01-28 20:05:38 +0000901 UASM_i_LA_mostly(p, ptr, pgdc);
902 uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903#endif
904
Thiemo Seufere30ec452008-01-28 20:05:38 +0000905 uasm_l_vmalloc_done(l, *p);
Ralf Baechle242954b2006-10-24 02:29:01 +0100906
David Daney3be60222010-04-28 12:16:17 -0700907 /* get pgd offset in bytes */
908 uasm_i_dsrl_safe(p, tmp, tmp, PGDIR_SHIFT - 3);
Ralf Baechle242954b2006-10-24 02:29:01 +0100909
Thiemo Seufere30ec452008-01-28 20:05:38 +0000910 uasm_i_andi(p, tmp, tmp, (PTRS_PER_PGD - 1)<<3);
911 uasm_i_daddu(p, ptr, ptr, tmp); /* add in pgd offset */
David Daney325f8a02009-12-04 13:52:36 -0800912#ifndef __PAGETABLE_PMD_FOLDED
Thiemo Seufere30ec452008-01-28 20:05:38 +0000913 uasm_i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
914 uasm_i_ld(p, ptr, 0, ptr); /* get pmd pointer */
David Daney3be60222010-04-28 12:16:17 -0700915 uasm_i_dsrl_safe(p, tmp, tmp, PMD_SHIFT-3); /* get pmd offset in bytes */
Thiemo Seufere30ec452008-01-28 20:05:38 +0000916 uasm_i_andi(p, tmp, tmp, (PTRS_PER_PMD - 1)<<3);
917 uasm_i_daddu(p, ptr, ptr, tmp); /* add in pmd offset */
David Daney325f8a02009-12-04 13:52:36 -0800918#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921/*
922 * BVADDR is the faulting address, PTR is scratch.
923 * PTR will hold the pgd for vmalloc.
924 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000925static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +0000926build_get_pgd_vmalloc64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
David Daney1ec56322010-04-28 12:16:18 -0700927 unsigned int bvaddr, unsigned int ptr,
928 enum vmalloc64_mode mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 long swpd = (long)swapper_pg_dir;
David Daney1ec56322010-04-28 12:16:18 -0700931 int single_insn_swpd;
932 int did_vmalloc_branch = 0;
933
934 single_insn_swpd = uasm_in_compat_space_p(swpd) && !uasm_rel_lo(swpd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Thiemo Seufere30ec452008-01-28 20:05:38 +0000936 uasm_l_vmalloc(l, *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
David Daney2c8c53e2010-12-27 18:07:57 -0800938 if (mode != not_refill && check_for_high_segbits) {
David Daney1ec56322010-04-28 12:16:18 -0700939 if (single_insn_swpd) {
940 uasm_il_bltz(p, r, bvaddr, label_vmalloc_done);
941 uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
942 did_vmalloc_branch = 1;
943 /* fall through */
944 } else {
945 uasm_il_bgez(p, r, bvaddr, label_large_segbits_fault);
946 }
947 }
948 if (!did_vmalloc_branch) {
949 if (uasm_in_compat_space_p(swpd) && !uasm_rel_lo(swpd)) {
950 uasm_il_b(p, r, label_vmalloc_done);
951 uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
952 } else {
953 UASM_i_LA_mostly(p, ptr, swpd);
954 uasm_il_b(p, r, label_vmalloc_done);
955 if (uasm_in_compat_space_p(swpd))
956 uasm_i_addiu(p, ptr, ptr, uasm_rel_lo(swpd));
957 else
958 uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(swpd));
959 }
960 }
David Daney2c8c53e2010-12-27 18:07:57 -0800961 if (mode != not_refill && check_for_high_segbits) {
David Daney1ec56322010-04-28 12:16:18 -0700962 uasm_l_large_segbits_fault(l, *p);
963 /*
964 * We get here if we are an xsseg address, or if we are
965 * an xuseg address above (PGDIR_SHIFT+PGDIR_BITS) boundary.
966 *
967 * Ignoring xsseg (assume disabled so would generate
968 * (address errors?), the only remaining possibility
969 * is the upper xuseg addresses. On processors with
970 * TLB_SEGBITS <= PGDIR_SHIFT+PGDIR_BITS, these
971 * addresses would have taken an address error. We try
972 * to mimic that here by taking a load/istream page
973 * fault.
974 */
975 UASM_i_LA(p, ptr, (unsigned long)tlb_do_page_fault_0);
976 uasm_i_jr(p, ptr);
David Daney2c8c53e2010-12-27 18:07:57 -0800977
978 if (mode == refill_scratch) {
979 if (scratch_reg > 0)
980 UASM_i_MFC0(p, 1, 31, scratch_reg);
981 else
982 UASM_i_LW(p, 1, scratchpad_offset(0), 0);
983 } else {
984 uasm_i_nop(p);
985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987}
988
Ralf Baechle875d43e2005-09-03 15:56:16 -0700989#else /* !CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991/*
992 * TMP and PTR are scratch.
993 * TMP will be clobbered, PTR will hold the pgd entry.
994 */
Ralf Baechle234fcd12008-03-08 09:56:28 +0000995static void __cpuinit __maybe_unused
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
997{
998 long pgdc = (long)pgd_current;
999
1000 /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
1001#ifdef CONFIG_SMP
Ralf Baechle70342282013-01-22 12:59:30 +01001002#ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle41c594a2006-04-05 09:45:45 +01001003 /*
1004 * SMTC uses TCBind value as "CPU" index
1005 */
Thiemo Seufere30ec452008-01-28 20:05:38 +00001006 uasm_i_mfc0(p, ptr, C0_TCBIND);
1007 UASM_i_LA_mostly(p, tmp, pgdc);
1008 uasm_i_srl(p, ptr, ptr, 19);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001009#else
1010 /*
1011 * smp_processor_id() << 3 is stored in CONTEXT.
Ralf Baechle70342282013-01-22 12:59:30 +01001012 */
Thiemo Seufere30ec452008-01-28 20:05:38 +00001013 uasm_i_mfc0(p, ptr, C0_CONTEXT);
1014 UASM_i_LA_mostly(p, tmp, pgdc);
1015 uasm_i_srl(p, ptr, ptr, 23);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001016#endif
Thiemo Seufere30ec452008-01-28 20:05:38 +00001017 uasm_i_addu(p, ptr, tmp, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018#else
Thiemo Seufere30ec452008-01-28 20:05:38 +00001019 UASM_i_LA_mostly(p, ptr, pgdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020#endif
Thiemo Seufere30ec452008-01-28 20:05:38 +00001021 uasm_i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
1022 uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
1023 uasm_i_srl(p, tmp, tmp, PGDIR_SHIFT); /* get pgd only bits */
1024 uasm_i_sll(p, tmp, tmp, PGD_T_LOG2);
1025 uasm_i_addu(p, ptr, ptr, tmp); /* add in pgd offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
Ralf Baechle875d43e2005-09-03 15:56:16 -07001028#endif /* !CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Ralf Baechle234fcd12008-03-08 09:56:28 +00001030static void __cpuinit build_adjust_context(u32 **p, unsigned int ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Ralf Baechle242954b2006-10-24 02:29:01 +01001032 unsigned int shift = 4 - (PTE_T_LOG2 + 1) + PAGE_SHIFT - 12;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 unsigned int mask = (PTRS_PER_PTE / 2 - 1) << (PTE_T_LOG2 + 1);
1034
Ralf Baechle10cc3522007-10-11 23:46:15 +01001035 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 case CPU_VR41XX:
1037 case CPU_VR4111:
1038 case CPU_VR4121:
1039 case CPU_VR4122:
1040 case CPU_VR4131:
1041 case CPU_VR4181:
1042 case CPU_VR4181A:
1043 case CPU_VR4133:
1044 shift += 2;
1045 break;
1046
1047 default:
1048 break;
1049 }
1050
1051 if (shift)
Thiemo Seufere30ec452008-01-28 20:05:38 +00001052 UASM_i_SRL(p, ctx, ctx, shift);
1053 uasm_i_andi(p, ctx, ctx, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
Ralf Baechle234fcd12008-03-08 09:56:28 +00001056static void __cpuinit build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 /*
1059 * Bug workaround for the Nevada. It seems as if under certain
1060 * circumstances the move from cp0_context might produce a
1061 * bogus result when the mfc0 instruction and its consumer are
1062 * in a different cacheline or a load instruction, probably any
1063 * memory reference, is between them.
1064 */
Ralf Baechle10cc3522007-10-11 23:46:15 +01001065 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 case CPU_NEVADA:
Thiemo Seufere30ec452008-01-28 20:05:38 +00001067 UASM_i_LW(p, ptr, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 GET_CONTEXT(p, tmp); /* get context reg */
1069 break;
1070
1071 default:
1072 GET_CONTEXT(p, tmp); /* get context reg */
Thiemo Seufere30ec452008-01-28 20:05:38 +00001073 UASM_i_LW(p, ptr, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 break;
1075 }
1076
1077 build_adjust_context(p, tmp);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001078 UASM_i_ADDU(p, ptr, ptr, tmp); /* add in offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
Ralf Baechle234fcd12008-03-08 09:56:28 +00001081static void __cpuinit build_update_entries(u32 **p, unsigned int tmp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 unsigned int ptep)
1083{
1084 /*
1085 * 64bit address support (36bit on a 32bit CPU) in a 32bit
1086 * Kernel is a special case. Only a few CPUs use it.
1087 */
1088#ifdef CONFIG_64BIT_PHYS_ADDR
1089 if (cpu_has_64bits) {
Thiemo Seufere30ec452008-01-28 20:05:38 +00001090 uasm_i_ld(p, tmp, 0, ptep); /* get even pte */
1091 uasm_i_ld(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
Steven J. Hill05857c62012-09-13 16:51:46 -05001092 if (cpu_has_rixi) {
David Daney748e7872012-08-23 10:02:03 -07001093 UASM_i_ROTR(p, tmp, tmp, ilog2(_PAGE_GLOBAL));
David Daney6dd93442010-02-10 15:12:47 -08001094 UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
David Daney748e7872012-08-23 10:02:03 -07001095 UASM_i_ROTR(p, ptep, ptep, ilog2(_PAGE_GLOBAL));
David Daney6dd93442010-02-10 15:12:47 -08001096 } else {
David Daney3be60222010-04-28 12:16:17 -07001097 uasm_i_dsrl_safe(p, tmp, tmp, ilog2(_PAGE_GLOBAL)); /* convert to entrylo0 */
David Daney6dd93442010-02-10 15:12:47 -08001098 UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
David Daney3be60222010-04-28 12:16:17 -07001099 uasm_i_dsrl_safe(p, ptep, ptep, ilog2(_PAGE_GLOBAL)); /* convert to entrylo1 */
David Daney6dd93442010-02-10 15:12:47 -08001100 }
David Daney9b8c3892010-02-10 15:12:44 -08001101 UASM_i_MTC0(p, ptep, C0_ENTRYLO1); /* load it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 } else {
1103 int pte_off_even = sizeof(pte_t) / 2;
1104 int pte_off_odd = pte_off_even + sizeof(pte_t);
1105
1106 /* The pte entries are pre-shifted */
Thiemo Seufere30ec452008-01-28 20:05:38 +00001107 uasm_i_lw(p, tmp, pte_off_even, ptep); /* get even pte */
David Daney9b8c3892010-02-10 15:12:44 -08001108 UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
Thiemo Seufere30ec452008-01-28 20:05:38 +00001109 uasm_i_lw(p, ptep, pte_off_odd, ptep); /* get odd pte */
David Daney9b8c3892010-02-10 15:12:44 -08001110 UASM_i_MTC0(p, ptep, C0_ENTRYLO1); /* load it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112#else
Thiemo Seufere30ec452008-01-28 20:05:38 +00001113 UASM_i_LW(p, tmp, 0, ptep); /* get even pte */
1114 UASM_i_LW(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (r45k_bvahwbug())
1116 build_tlb_probe_entry(p);
Steven J. Hill05857c62012-09-13 16:51:46 -05001117 if (cpu_has_rixi) {
David Daney748e7872012-08-23 10:02:03 -07001118 UASM_i_ROTR(p, tmp, tmp, ilog2(_PAGE_GLOBAL));
David Daney6dd93442010-02-10 15:12:47 -08001119 if (r4k_250MHZhwbug())
1120 UASM_i_MTC0(p, 0, C0_ENTRYLO0);
1121 UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
David Daney748e7872012-08-23 10:02:03 -07001122 UASM_i_ROTR(p, ptep, ptep, ilog2(_PAGE_GLOBAL));
David Daney6dd93442010-02-10 15:12:47 -08001123 } else {
1124 UASM_i_SRL(p, tmp, tmp, ilog2(_PAGE_GLOBAL)); /* convert to entrylo0 */
1125 if (r4k_250MHZhwbug())
1126 UASM_i_MTC0(p, 0, C0_ENTRYLO0);
1127 UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
1128 UASM_i_SRL(p, ptep, ptep, ilog2(_PAGE_GLOBAL)); /* convert to entrylo1 */
1129 if (r45k_bvahwbug())
1130 uasm_i_mfc0(p, tmp, C0_INDEX);
1131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (r4k_250MHZhwbug())
David Daney9b8c3892010-02-10 15:12:44 -08001133 UASM_i_MTC0(p, 0, C0_ENTRYLO1);
1134 UASM_i_MTC0(p, ptep, C0_ENTRYLO1); /* load it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135#endif
1136}
1137
David Daney2c8c53e2010-12-27 18:07:57 -08001138struct mips_huge_tlb_info {
1139 int huge_pte;
1140 int restore_scratch;
1141};
1142
1143static struct mips_huge_tlb_info __cpuinit
1144build_fast_tlb_refill_handler (u32 **p, struct uasm_label **l,
1145 struct uasm_reloc **r, unsigned int tmp,
1146 unsigned int ptr, int c0_scratch)
1147{
1148 struct mips_huge_tlb_info rv;
1149 unsigned int even, odd;
1150 int vmalloc_branch_delay_filled = 0;
1151 const int scratch = 1; /* Our extra working register */
1152
1153 rv.huge_pte = scratch;
1154 rv.restore_scratch = 0;
1155
1156 if (check_for_high_segbits) {
1157 UASM_i_MFC0(p, tmp, C0_BADVADDR);
1158
1159 if (pgd_reg != -1)
1160 UASM_i_MFC0(p, ptr, 31, pgd_reg);
1161 else
1162 UASM_i_MFC0(p, ptr, C0_CONTEXT);
1163
1164 if (c0_scratch >= 0)
1165 UASM_i_MTC0(p, scratch, 31, c0_scratch);
1166 else
1167 UASM_i_SW(p, scratch, scratchpad_offset(0), 0);
1168
1169 uasm_i_dsrl_safe(p, scratch, tmp,
1170 PGDIR_SHIFT + PGD_ORDER + PAGE_SHIFT - 3);
1171 uasm_il_bnez(p, r, scratch, label_vmalloc);
1172
1173 if (pgd_reg == -1) {
1174 vmalloc_branch_delay_filled = 1;
1175 /* Clear lower 23 bits of context. */
1176 uasm_i_dins(p, ptr, 0, 0, 23);
1177 }
1178 } else {
1179 if (pgd_reg != -1)
1180 UASM_i_MFC0(p, ptr, 31, pgd_reg);
1181 else
1182 UASM_i_MFC0(p, ptr, C0_CONTEXT);
1183
1184 UASM_i_MFC0(p, tmp, C0_BADVADDR);
1185
1186 if (c0_scratch >= 0)
1187 UASM_i_MTC0(p, scratch, 31, c0_scratch);
1188 else
1189 UASM_i_SW(p, scratch, scratchpad_offset(0), 0);
1190
1191 if (pgd_reg == -1)
1192 /* Clear lower 23 bits of context. */
1193 uasm_i_dins(p, ptr, 0, 0, 23);
1194
1195 uasm_il_bltz(p, r, tmp, label_vmalloc);
1196 }
1197
1198 if (pgd_reg == -1) {
1199 vmalloc_branch_delay_filled = 1;
Ralf Baechle70342282013-01-22 12:59:30 +01001200 /* 1 0 1 0 1 << 6 xkphys cached */
David Daney2c8c53e2010-12-27 18:07:57 -08001201 uasm_i_ori(p, ptr, ptr, 0x540);
1202 uasm_i_drotr(p, ptr, ptr, 11);
1203 }
1204
1205#ifdef __PAGETABLE_PMD_FOLDED
1206#define LOC_PTEP scratch
1207#else
1208#define LOC_PTEP ptr
1209#endif
1210
1211 if (!vmalloc_branch_delay_filled)
1212 /* get pgd offset in bytes */
1213 uasm_i_dsrl_safe(p, scratch, tmp, PGDIR_SHIFT - 3);
1214
1215 uasm_l_vmalloc_done(l, *p);
1216
1217 /*
Ralf Baechle70342282013-01-22 12:59:30 +01001218 * tmp ptr
1219 * fall-through case = badvaddr *pgd_current
1220 * vmalloc case = badvaddr swapper_pg_dir
David Daney2c8c53e2010-12-27 18:07:57 -08001221 */
1222
1223 if (vmalloc_branch_delay_filled)
1224 /* get pgd offset in bytes */
1225 uasm_i_dsrl_safe(p, scratch, tmp, PGDIR_SHIFT - 3);
1226
1227#ifdef __PAGETABLE_PMD_FOLDED
1228 GET_CONTEXT(p, tmp); /* get context reg */
1229#endif
1230 uasm_i_andi(p, scratch, scratch, (PTRS_PER_PGD - 1) << 3);
1231
1232 if (use_lwx_insns()) {
1233 UASM_i_LWX(p, LOC_PTEP, scratch, ptr);
1234 } else {
1235 uasm_i_daddu(p, ptr, ptr, scratch); /* add in pgd offset */
1236 uasm_i_ld(p, LOC_PTEP, 0, ptr); /* get pmd pointer */
1237 }
1238
1239#ifndef __PAGETABLE_PMD_FOLDED
1240 /* get pmd offset in bytes */
1241 uasm_i_dsrl_safe(p, scratch, tmp, PMD_SHIFT - 3);
1242 uasm_i_andi(p, scratch, scratch, (PTRS_PER_PMD - 1) << 3);
1243 GET_CONTEXT(p, tmp); /* get context reg */
1244
1245 if (use_lwx_insns()) {
1246 UASM_i_LWX(p, scratch, scratch, ptr);
1247 } else {
1248 uasm_i_daddu(p, ptr, ptr, scratch); /* add in pmd offset */
1249 UASM_i_LW(p, scratch, 0, ptr);
1250 }
1251#endif
1252 /* Adjust the context during the load latency. */
1253 build_adjust_context(p, tmp);
1254
David Daneyaa1762f2012-10-17 00:48:10 +02001255#ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
David Daney2c8c53e2010-12-27 18:07:57 -08001256 uasm_il_bbit1(p, r, scratch, ilog2(_PAGE_HUGE), label_tlb_huge_update);
1257 /*
1258 * The in the LWX case we don't want to do the load in the
Ralf Baechle70342282013-01-22 12:59:30 +01001259 * delay slot. It cannot issue in the same cycle and may be
David Daney2c8c53e2010-12-27 18:07:57 -08001260 * speculative and unneeded.
1261 */
1262 if (use_lwx_insns())
1263 uasm_i_nop(p);
David Daneyaa1762f2012-10-17 00:48:10 +02001264#endif /* CONFIG_MIPS_HUGE_TLB_SUPPORT */
David Daney2c8c53e2010-12-27 18:07:57 -08001265
1266
1267 /* build_update_entries */
1268 if (use_lwx_insns()) {
1269 even = ptr;
1270 odd = tmp;
1271 UASM_i_LWX(p, even, scratch, tmp);
1272 UASM_i_ADDIU(p, tmp, tmp, sizeof(pte_t));
1273 UASM_i_LWX(p, odd, scratch, tmp);
1274 } else {
1275 UASM_i_ADDU(p, ptr, scratch, tmp); /* add in offset */
1276 even = tmp;
1277 odd = ptr;
1278 UASM_i_LW(p, even, 0, ptr); /* get even pte */
1279 UASM_i_LW(p, odd, sizeof(pte_t), ptr); /* get odd pte */
1280 }
Steven J. Hill05857c62012-09-13 16:51:46 -05001281 if (cpu_has_rixi) {
David Daney748e7872012-08-23 10:02:03 -07001282 uasm_i_drotr(p, even, even, ilog2(_PAGE_GLOBAL));
David Daney2c8c53e2010-12-27 18:07:57 -08001283 UASM_i_MTC0(p, even, C0_ENTRYLO0); /* load it */
David Daney748e7872012-08-23 10:02:03 -07001284 uasm_i_drotr(p, odd, odd, ilog2(_PAGE_GLOBAL));
David Daney2c8c53e2010-12-27 18:07:57 -08001285 } else {
1286 uasm_i_dsrl_safe(p, even, even, ilog2(_PAGE_GLOBAL));
1287 UASM_i_MTC0(p, even, C0_ENTRYLO0); /* load it */
1288 uasm_i_dsrl_safe(p, odd, odd, ilog2(_PAGE_GLOBAL));
1289 }
1290 UASM_i_MTC0(p, odd, C0_ENTRYLO1); /* load it */
1291
1292 if (c0_scratch >= 0) {
1293 UASM_i_MFC0(p, scratch, 31, c0_scratch);
1294 build_tlb_write_entry(p, l, r, tlb_random);
1295 uasm_l_leave(l, *p);
1296 rv.restore_scratch = 1;
1297 } else if (PAGE_SHIFT == 14 || PAGE_SHIFT == 13) {
1298 build_tlb_write_entry(p, l, r, tlb_random);
1299 uasm_l_leave(l, *p);
1300 UASM_i_LW(p, scratch, scratchpad_offset(0), 0);
1301 } else {
1302 UASM_i_LW(p, scratch, scratchpad_offset(0), 0);
1303 build_tlb_write_entry(p, l, r, tlb_random);
1304 uasm_l_leave(l, *p);
1305 rv.restore_scratch = 1;
1306 }
1307
1308 uasm_i_eret(p); /* return from trap */
1309
1310 return rv;
1311}
1312
David Daneye6f72d32009-05-20 11:40:58 -07001313/*
1314 * For a 64-bit kernel, we are using the 64-bit XTLB refill exception
1315 * because EXL == 0. If we wrap, we can also use the 32 instruction
1316 * slots before the XTLB refill exception handler which belong to the
1317 * unused TLB refill exception.
1318 */
1319#define MIPS64_REFILL_INSNS 32
1320
Ralf Baechle234fcd12008-03-08 09:56:28 +00001321static void __cpuinit build_r4000_tlb_refill_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 u32 *p = tlb_handler;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001324 struct uasm_label *l = labels;
1325 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 u32 *f;
1327 unsigned int final_len;
Ralf Baechle4a9040f2011-03-29 10:54:54 +02001328 struct mips_huge_tlb_info htlb_info __maybe_unused;
1329 enum vmalloc64_mode vmalloc_mode __maybe_unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 memset(tlb_handler, 0, sizeof(tlb_handler));
1332 memset(labels, 0, sizeof(labels));
1333 memset(relocs, 0, sizeof(relocs));
1334 memset(final_handler, 0, sizeof(final_handler));
1335
David Daney2c8c53e2010-12-27 18:07:57 -08001336 if ((scratch_reg > 0 || scratchpad_available()) && use_bbit_insns()) {
1337 htlb_info = build_fast_tlb_refill_handler(&p, &l, &r, K0, K1,
1338 scratch_reg);
1339 vmalloc_mode = refill_scratch;
1340 } else {
1341 htlb_info.huge_pte = K0;
1342 htlb_info.restore_scratch = 0;
1343 vmalloc_mode = refill_noscratch;
1344 /*
1345 * create the plain linear handler
1346 */
1347 if (bcm1250_m3_war()) {
1348 unsigned int segbits = 44;
1349
1350 uasm_i_dmfc0(&p, K0, C0_BADVADDR);
1351 uasm_i_dmfc0(&p, K1, C0_ENTRYHI);
1352 uasm_i_xor(&p, K0, K0, K1);
1353 uasm_i_dsrl_safe(&p, K1, K0, 62);
1354 uasm_i_dsrl_safe(&p, K0, K0, 12 + 1);
1355 uasm_i_dsll_safe(&p, K0, K0, 64 + 12 + 1 - segbits);
1356 uasm_i_or(&p, K0, K0, K1);
1357 uasm_il_bnez(&p, &r, K0, label_leave);
1358 /* No need for uasm_i_nop */
1359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Ralf Baechle875d43e2005-09-03 15:56:16 -07001361#ifdef CONFIG_64BIT
David Daney2c8c53e2010-12-27 18:07:57 -08001362 build_get_pmde64(&p, &l, &r, K0, K1); /* get pmd in K1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363#else
David Daney2c8c53e2010-12-27 18:07:57 -08001364 build_get_pgde32(&p, K0, K1); /* get pgd in K1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365#endif
1366
David Daneyaa1762f2012-10-17 00:48:10 +02001367#ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
David Daney2c8c53e2010-12-27 18:07:57 -08001368 build_is_huge_pte(&p, &r, K0, K1, label_tlb_huge_update);
David Daneyfd062c82009-05-27 17:47:44 -07001369#endif
1370
David Daney2c8c53e2010-12-27 18:07:57 -08001371 build_get_ptep(&p, K0, K1);
1372 build_update_entries(&p, K0, K1);
1373 build_tlb_write_entry(&p, &l, &r, tlb_random);
1374 uasm_l_leave(&l, p);
1375 uasm_i_eret(&p); /* return from trap */
1376 }
David Daneyaa1762f2012-10-17 00:48:10 +02001377#ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
David Daneyfd062c82009-05-27 17:47:44 -07001378 uasm_l_tlb_huge_update(&l, p);
David Daney2c8c53e2010-12-27 18:07:57 -08001379 build_huge_update_entries(&p, htlb_info.huge_pte, K1);
1380 build_huge_tlb_write_entry(&p, &l, &r, K0, tlb_random,
1381 htlb_info.restore_scratch);
David Daneyfd062c82009-05-27 17:47:44 -07001382#endif
1383
Ralf Baechle875d43e2005-09-03 15:56:16 -07001384#ifdef CONFIG_64BIT
David Daney2c8c53e2010-12-27 18:07:57 -08001385 build_get_pgd_vmalloc64(&p, &l, &r, K0, K1, vmalloc_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386#endif
1387
1388 /*
1389 * Overflow check: For the 64bit handler, we need at least one
1390 * free instruction slot for the wrap-around branch. In worst
1391 * case, if the intended insertion point is a delay slot, we
Matt LaPlante4b3f6862006-10-03 22:21:02 +02001392 * need three, with the second nop'ed and the third being
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 * unused.
1394 */
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001395 /* Loongson2 ebase is different than r4k, we have more space */
1396#if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if ((p - tlb_handler) > 64)
1398 panic("TLB refill handler space exceeded");
1399#else
David Daneye6f72d32009-05-20 11:40:58 -07001400 if (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 1)
1401 || (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 3)
1402 && uasm_insn_has_bdelay(relocs,
1403 tlb_handler + MIPS64_REFILL_INSNS - 3)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 panic("TLB refill handler space exceeded");
1405#endif
1406
1407 /*
1408 * Now fold the handler in the TLB refill handler space.
1409 */
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001410#if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 f = final_handler;
1412 /* Simplest case, just copy the handler. */
Thiemo Seufere30ec452008-01-28 20:05:38 +00001413 uasm_copy_handler(relocs, labels, tlb_handler, p, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 final_len = p - tlb_handler;
Ralf Baechle875d43e2005-09-03 15:56:16 -07001415#else /* CONFIG_64BIT */
David Daneye6f72d32009-05-20 11:40:58 -07001416 f = final_handler + MIPS64_REFILL_INSNS;
1417 if ((p - tlb_handler) <= MIPS64_REFILL_INSNS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 /* Just copy the handler. */
Thiemo Seufere30ec452008-01-28 20:05:38 +00001419 uasm_copy_handler(relocs, labels, tlb_handler, p, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 final_len = p - tlb_handler;
1421 } else {
David Daneyaa1762f2012-10-17 00:48:10 +02001422#ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
David Daneyfd062c82009-05-27 17:47:44 -07001423 const enum label_id ls = label_tlb_huge_update;
David Daney95affdd2009-05-20 11:40:59 -07001424#else
1425 const enum label_id ls = label_vmalloc;
1426#endif
1427 u32 *split;
1428 int ov = 0;
1429 int i;
1430
1431 for (i = 0; i < ARRAY_SIZE(labels) && labels[i].lab != ls; i++)
1432 ;
1433 BUG_ON(i == ARRAY_SIZE(labels));
1434 split = labels[i].addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 /*
David Daney95affdd2009-05-20 11:40:59 -07001437 * See if we have overflown one way or the other.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 */
David Daney95affdd2009-05-20 11:40:59 -07001439 if (split > tlb_handler + MIPS64_REFILL_INSNS ||
1440 split < p - MIPS64_REFILL_INSNS)
1441 ov = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
David Daney95affdd2009-05-20 11:40:59 -07001443 if (ov) {
1444 /*
1445 * Split two instructions before the end. One
1446 * for the branch and one for the instruction
1447 * in the delay slot.
1448 */
1449 split = tlb_handler + MIPS64_REFILL_INSNS - 2;
1450
1451 /*
1452 * If the branch would fall in a delay slot,
1453 * we must back up an additional instruction
1454 * so that it is no longer in a delay slot.
1455 */
1456 if (uasm_insn_has_bdelay(relocs, split - 1))
1457 split--;
1458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 /* Copy first part of the handler. */
Thiemo Seufere30ec452008-01-28 20:05:38 +00001460 uasm_copy_handler(relocs, labels, tlb_handler, split, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 f += split - tlb_handler;
1462
David Daney95affdd2009-05-20 11:40:59 -07001463 if (ov) {
1464 /* Insert branch. */
1465 uasm_l_split(&l, final_handler);
1466 uasm_il_b(&f, &r, label_split);
1467 if (uasm_insn_has_bdelay(relocs, split))
1468 uasm_i_nop(&f);
1469 else {
1470 uasm_copy_handler(relocs, labels,
1471 split, split + 1, f);
1472 uasm_move_labels(labels, f, f + 1, -1);
1473 f++;
1474 split++;
1475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
1477
1478 /* Copy the rest of the handler. */
Thiemo Seufere30ec452008-01-28 20:05:38 +00001479 uasm_copy_handler(relocs, labels, split, p, final_handler);
David Daneye6f72d32009-05-20 11:40:58 -07001480 final_len = (f - (final_handler + MIPS64_REFILL_INSNS)) +
1481 (p - split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
Ralf Baechle875d43e2005-09-03 15:56:16 -07001483#endif /* CONFIG_64BIT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Thiemo Seufere30ec452008-01-28 20:05:38 +00001485 uasm_resolve_relocs(relocs, labels);
1486 pr_debug("Wrote TLB refill handler (%u instructions).\n",
1487 final_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Ralf Baechle91b05e62006-03-29 18:53:00 +01001489 memcpy((void *)ebase, final_handler, 0x100);
Franck Bui-Huu92b1e6a2007-10-18 09:11:17 +02001490
Ralf Baechlea2c763e2012-10-16 22:20:26 +02001491 dump_handler("r4000_tlb_refill", (u32 *)ebase, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
1493
1494/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 * 128 instructions for the fastpath handler is generous and should
1496 * never be exceeded.
1497 */
1498#define FASTPATH_SIZE 128
1499
Franck Bui-Huucbdbe072007-10-18 09:11:16 +02001500u32 handle_tlbl[FASTPATH_SIZE] __cacheline_aligned;
1501u32 handle_tlbs[FASTPATH_SIZE] __cacheline_aligned;
1502u32 handle_tlbm[FASTPATH_SIZE] __cacheline_aligned;
David Daney3d8bfdd2010-12-21 14:19:11 -08001503#ifdef CONFIG_MIPS_PGD_C0_CONTEXT
1504u32 tlbmiss_handler_setup_pgd[16] __cacheline_aligned;
1505
1506static void __cpuinit build_r4000_setup_pgd(void)
1507{
1508 const int a0 = 4;
1509 const int a1 = 5;
1510 u32 *p = tlbmiss_handler_setup_pgd;
1511 struct uasm_label *l = labels;
1512 struct uasm_reloc *r = relocs;
1513
1514 memset(tlbmiss_handler_setup_pgd, 0, sizeof(tlbmiss_handler_setup_pgd));
1515 memset(labels, 0, sizeof(labels));
1516 memset(relocs, 0, sizeof(relocs));
1517
1518 pgd_reg = allocate_kscratch();
1519
1520 if (pgd_reg == -1) {
1521 /* PGD << 11 in c0_Context */
1522 /*
1523 * If it is a ckseg0 address, convert to a physical
1524 * address. Shifting right by 29 and adding 4 will
1525 * result in zero for these addresses.
1526 *
1527 */
1528 UASM_i_SRA(&p, a1, a0, 29);
1529 UASM_i_ADDIU(&p, a1, a1, 4);
1530 uasm_il_bnez(&p, &r, a1, label_tlbl_goaround1);
1531 uasm_i_nop(&p);
1532 uasm_i_dinsm(&p, a0, 0, 29, 64 - 29);
1533 uasm_l_tlbl_goaround1(&l, p);
1534 UASM_i_SLL(&p, a0, a0, 11);
1535 uasm_i_jr(&p, 31);
1536 UASM_i_MTC0(&p, a0, C0_CONTEXT);
1537 } else {
1538 /* PGD in c0_KScratch */
1539 uasm_i_jr(&p, 31);
1540 UASM_i_MTC0(&p, a0, 31, pgd_reg);
1541 }
1542 if (p - tlbmiss_handler_setup_pgd > ARRAY_SIZE(tlbmiss_handler_setup_pgd))
1543 panic("tlbmiss_handler_setup_pgd space exceeded");
1544 uasm_resolve_relocs(relocs, labels);
1545 pr_debug("Wrote tlbmiss_handler_setup_pgd (%u instructions).\n",
1546 (unsigned int)(p - tlbmiss_handler_setup_pgd));
1547
Ralf Baechlea2c763e2012-10-16 22:20:26 +02001548 dump_handler("tlbmiss_handler",
1549 tlbmiss_handler_setup_pgd,
David Daney3d8bfdd2010-12-21 14:19:11 -08001550 ARRAY_SIZE(tlbmiss_handler_setup_pgd));
1551}
1552#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Ralf Baechle234fcd12008-03-08 09:56:28 +00001554static void __cpuinit
David Daneybd1437e2009-05-08 15:10:50 -07001555iPTE_LW(u32 **p, unsigned int pte, unsigned int ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557#ifdef CONFIG_SMP
1558# ifdef CONFIG_64BIT_PHYS_ADDR
1559 if (cpu_has_64bits)
Thiemo Seufere30ec452008-01-28 20:05:38 +00001560 uasm_i_lld(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 else
1562# endif
Thiemo Seufere30ec452008-01-28 20:05:38 +00001563 UASM_i_LL(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564#else
1565# ifdef CONFIG_64BIT_PHYS_ADDR
1566 if (cpu_has_64bits)
Thiemo Seufere30ec452008-01-28 20:05:38 +00001567 uasm_i_ld(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 else
1569# endif
Thiemo Seufere30ec452008-01-28 20:05:38 +00001570 UASM_i_LW(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571#endif
1572}
1573
Ralf Baechle234fcd12008-03-08 09:56:28 +00001574static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +00001575iPTE_SW(u32 **p, struct uasm_reloc **r, unsigned int pte, unsigned int ptr,
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001576 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001578#ifdef CONFIG_64BIT_PHYS_ADDR
1579 unsigned int hwmode = mode & (_PAGE_VALID | _PAGE_DIRTY);
1580#endif
1581
Thiemo Seufere30ec452008-01-28 20:05:38 +00001582 uasm_i_ori(p, pte, pte, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583#ifdef CONFIG_SMP
1584# ifdef CONFIG_64BIT_PHYS_ADDR
1585 if (cpu_has_64bits)
Thiemo Seufere30ec452008-01-28 20:05:38 +00001586 uasm_i_scd(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 else
1588# endif
Thiemo Seufere30ec452008-01-28 20:05:38 +00001589 UASM_i_SC(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 if (r10000_llsc_war())
Thiemo Seufere30ec452008-01-28 20:05:38 +00001592 uasm_il_beqzl(p, r, pte, label_smp_pgtable_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 else
Thiemo Seufere30ec452008-01-28 20:05:38 +00001594 uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596# ifdef CONFIG_64BIT_PHYS_ADDR
1597 if (!cpu_has_64bits) {
Thiemo Seufere30ec452008-01-28 20:05:38 +00001598 /* no uasm_i_nop needed */
1599 uasm_i_ll(p, pte, sizeof(pte_t) / 2, ptr);
1600 uasm_i_ori(p, pte, pte, hwmode);
1601 uasm_i_sc(p, pte, sizeof(pte_t) / 2, ptr);
1602 uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
1603 /* no uasm_i_nop needed */
1604 uasm_i_lw(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 } else
Thiemo Seufere30ec452008-01-28 20:05:38 +00001606 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607# else
Thiemo Seufere30ec452008-01-28 20:05:38 +00001608 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609# endif
1610#else
1611# ifdef CONFIG_64BIT_PHYS_ADDR
1612 if (cpu_has_64bits)
Thiemo Seufere30ec452008-01-28 20:05:38 +00001613 uasm_i_sd(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 else
1615# endif
Thiemo Seufere30ec452008-01-28 20:05:38 +00001616 UASM_i_SW(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618# ifdef CONFIG_64BIT_PHYS_ADDR
1619 if (!cpu_has_64bits) {
Thiemo Seufere30ec452008-01-28 20:05:38 +00001620 uasm_i_lw(p, pte, sizeof(pte_t) / 2, ptr);
1621 uasm_i_ori(p, pte, pte, hwmode);
1622 uasm_i_sw(p, pte, sizeof(pte_t) / 2, ptr);
1623 uasm_i_lw(p, pte, 0, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625# endif
1626#endif
1627}
1628
1629/*
1630 * Check if PTE is present, if not then jump to LABEL. PTR points to
1631 * the page table where this PTE is located, PTE will be re-loaded
1632 * with it's original value.
1633 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001634static void __cpuinit
David Daneybd1437e2009-05-08 15:10:50 -07001635build_pte_present(u32 **p, struct uasm_reloc **r,
David Daneybf286072011-07-05 16:34:46 -07001636 int pte, int ptr, int scratch, enum label_id lid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
David Daneybf286072011-07-05 16:34:46 -07001638 int t = scratch >= 0 ? scratch : pte;
1639
Steven J. Hill05857c62012-09-13 16:51:46 -05001640 if (cpu_has_rixi) {
David Daneycc33ae42010-12-20 15:54:50 -08001641 if (use_bbit_insns()) {
1642 uasm_il_bbit0(p, r, pte, ilog2(_PAGE_PRESENT), lid);
1643 uasm_i_nop(p);
1644 } else {
David Daneybf286072011-07-05 16:34:46 -07001645 uasm_i_andi(p, t, pte, _PAGE_PRESENT);
1646 uasm_il_beqz(p, r, t, lid);
1647 if (pte == t)
1648 /* You lose the SMP race :-(*/
1649 iPTE_LW(p, pte, ptr);
David Daneycc33ae42010-12-20 15:54:50 -08001650 }
David Daney6dd93442010-02-10 15:12:47 -08001651 } else {
David Daneybf286072011-07-05 16:34:46 -07001652 uasm_i_andi(p, t, pte, _PAGE_PRESENT | _PAGE_READ);
1653 uasm_i_xori(p, t, t, _PAGE_PRESENT | _PAGE_READ);
1654 uasm_il_bnez(p, r, t, lid);
1655 if (pte == t)
1656 /* You lose the SMP race :-(*/
1657 iPTE_LW(p, pte, ptr);
David Daney6dd93442010-02-10 15:12:47 -08001658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
1661/* Make PTE valid, store result in PTR. */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001662static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +00001663build_make_valid(u32 **p, struct uasm_reloc **r, unsigned int pte,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 unsigned int ptr)
1665{
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001666 unsigned int mode = _PAGE_VALID | _PAGE_ACCESSED;
1667
1668 iPTE_SW(p, r, pte, ptr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
1671/*
1672 * Check if PTE can be written to, if not branch to LABEL. Regardless
1673 * restore PTE with value from PTR when done.
1674 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001675static void __cpuinit
David Daneybd1437e2009-05-08 15:10:50 -07001676build_pte_writable(u32 **p, struct uasm_reloc **r,
David Daneybf286072011-07-05 16:34:46 -07001677 unsigned int pte, unsigned int ptr, int scratch,
1678 enum label_id lid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
David Daneybf286072011-07-05 16:34:46 -07001680 int t = scratch >= 0 ? scratch : pte;
1681
1682 uasm_i_andi(p, t, pte, _PAGE_PRESENT | _PAGE_WRITE);
1683 uasm_i_xori(p, t, t, _PAGE_PRESENT | _PAGE_WRITE);
1684 uasm_il_bnez(p, r, t, lid);
1685 if (pte == t)
1686 /* You lose the SMP race :-(*/
David Daneycc33ae42010-12-20 15:54:50 -08001687 iPTE_LW(p, pte, ptr);
David Daneybf286072011-07-05 16:34:46 -07001688 else
1689 uasm_i_nop(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}
1691
1692/* Make PTE writable, update software status bits as well, then store
1693 * at PTR.
1694 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001695static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +00001696build_make_write(u32 **p, struct uasm_reloc **r, unsigned int pte,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 unsigned int ptr)
1698{
Thiemo Seufer63b2d2f2005-04-28 08:52:57 +00001699 unsigned int mode = (_PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID
1700 | _PAGE_DIRTY);
1701
1702 iPTE_SW(p, r, pte, ptr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
1705/*
1706 * Check if PTE can be modified, if not branch to LABEL. Regardless
1707 * restore PTE with value from PTR when done.
1708 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001709static void __cpuinit
David Daneybd1437e2009-05-08 15:10:50 -07001710build_pte_modifiable(u32 **p, struct uasm_reloc **r,
David Daneybf286072011-07-05 16:34:46 -07001711 unsigned int pte, unsigned int ptr, int scratch,
1712 enum label_id lid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
David Daneycc33ae42010-12-20 15:54:50 -08001714 if (use_bbit_insns()) {
1715 uasm_il_bbit0(p, r, pte, ilog2(_PAGE_WRITE), lid);
1716 uasm_i_nop(p);
1717 } else {
David Daneybf286072011-07-05 16:34:46 -07001718 int t = scratch >= 0 ? scratch : pte;
1719 uasm_i_andi(p, t, pte, _PAGE_WRITE);
1720 uasm_il_beqz(p, r, t, lid);
1721 if (pte == t)
1722 /* You lose the SMP race :-(*/
1723 iPTE_LW(p, pte, ptr);
David Daneycc33ae42010-12-20 15:54:50 -08001724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
David Daney82622282009-10-14 12:16:56 -07001727#ifndef CONFIG_MIPS_PGD_C0_CONTEXT
David Daney3d8bfdd2010-12-21 14:19:11 -08001728
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730/*
1731 * R3000 style TLB load/store/modify handlers.
1732 */
1733
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001734/*
1735 * This places the pte into ENTRYLO0 and writes it with tlbwi.
1736 * Then it returns.
1737 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001738static void __cpuinit
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001739build_r3000_pte_reload_tlbwi(u32 **p, unsigned int pte, unsigned int tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
Thiemo Seufere30ec452008-01-28 20:05:38 +00001741 uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
1742 uasm_i_mfc0(p, tmp, C0_EPC); /* cp0 delay */
1743 uasm_i_tlbwi(p);
1744 uasm_i_jr(p, tmp);
1745 uasm_i_rfe(p); /* branch delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
1747
1748/*
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001749 * This places the pte into ENTRYLO0 and writes it with tlbwi
1750 * or tlbwr as appropriate. This is because the index register
1751 * may have the probe fail bit set as a result of a trap on a
1752 * kseg2 access, i.e. without refill. Then it returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001754static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +00001755build_r3000_tlb_reload_write(u32 **p, struct uasm_label **l,
1756 struct uasm_reloc **r, unsigned int pte,
1757 unsigned int tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Thiemo Seufere30ec452008-01-28 20:05:38 +00001759 uasm_i_mfc0(p, tmp, C0_INDEX);
1760 uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
1761 uasm_il_bltz(p, r, tmp, label_r3000_write_probe_fail); /* cp0 delay */
1762 uasm_i_mfc0(p, tmp, C0_EPC); /* branch delay */
1763 uasm_i_tlbwi(p); /* cp0 delay */
1764 uasm_i_jr(p, tmp);
1765 uasm_i_rfe(p); /* branch delay */
1766 uasm_l_r3000_write_probe_fail(l, *p);
1767 uasm_i_tlbwr(p); /* cp0 delay */
1768 uasm_i_jr(p, tmp);
1769 uasm_i_rfe(p); /* branch delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
Ralf Baechle234fcd12008-03-08 09:56:28 +00001772static void __cpuinit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
1774 unsigned int ptr)
1775{
1776 long pgdc = (long)pgd_current;
1777
Thiemo Seufere30ec452008-01-28 20:05:38 +00001778 uasm_i_mfc0(p, pte, C0_BADVADDR);
1779 uasm_i_lui(p, ptr, uasm_rel_hi(pgdc)); /* cp0 delay */
1780 uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
1781 uasm_i_srl(p, pte, pte, 22); /* load delay */
1782 uasm_i_sll(p, pte, pte, 2);
1783 uasm_i_addu(p, ptr, ptr, pte);
1784 uasm_i_mfc0(p, pte, C0_CONTEXT);
1785 uasm_i_lw(p, ptr, 0, ptr); /* cp0 delay */
1786 uasm_i_andi(p, pte, pte, 0xffc); /* load delay */
1787 uasm_i_addu(p, ptr, ptr, pte);
1788 uasm_i_lw(p, pte, 0, ptr);
1789 uasm_i_tlbp(p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
Ralf Baechle234fcd12008-03-08 09:56:28 +00001792static void __cpuinit build_r3000_tlb_load_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
1794 u32 *p = handle_tlbl;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001795 struct uasm_label *l = labels;
1796 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 memset(handle_tlbl, 0, sizeof(handle_tlbl));
1799 memset(labels, 0, sizeof(labels));
1800 memset(relocs, 0, sizeof(relocs));
1801
1802 build_r3000_tlbchange_handler_head(&p, K0, K1);
David Daneybf286072011-07-05 16:34:46 -07001803 build_pte_present(&p, &r, K0, K1, -1, label_nopage_tlbl);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001804 uasm_i_nop(&p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 build_make_valid(&p, &r, K0, K1);
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001806 build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Thiemo Seufere30ec452008-01-28 20:05:38 +00001808 uasm_l_nopage_tlbl(&l, p);
1809 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
1810 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 if ((p - handle_tlbl) > FASTPATH_SIZE)
1813 panic("TLB load handler fastpath space exceeded");
1814
Thiemo Seufere30ec452008-01-28 20:05:38 +00001815 uasm_resolve_relocs(relocs, labels);
1816 pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
1817 (unsigned int)(p - handle_tlbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Ralf Baechlea2c763e2012-10-16 22:20:26 +02001819 dump_handler("r3000_tlb_load", handle_tlbl, ARRAY_SIZE(handle_tlbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
Ralf Baechle234fcd12008-03-08 09:56:28 +00001822static void __cpuinit build_r3000_tlb_store_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
1824 u32 *p = handle_tlbs;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001825 struct uasm_label *l = labels;
1826 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 memset(handle_tlbs, 0, sizeof(handle_tlbs));
1829 memset(labels, 0, sizeof(labels));
1830 memset(relocs, 0, sizeof(relocs));
1831
1832 build_r3000_tlbchange_handler_head(&p, K0, K1);
David Daneybf286072011-07-05 16:34:46 -07001833 build_pte_writable(&p, &r, K0, K1, -1, label_nopage_tlbs);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001834 uasm_i_nop(&p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 build_make_write(&p, &r, K0, K1);
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001836 build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Thiemo Seufere30ec452008-01-28 20:05:38 +00001838 uasm_l_nopage_tlbs(&l, p);
1839 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1840 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 if ((p - handle_tlbs) > FASTPATH_SIZE)
1843 panic("TLB store handler fastpath space exceeded");
1844
Thiemo Seufere30ec452008-01-28 20:05:38 +00001845 uasm_resolve_relocs(relocs, labels);
1846 pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
1847 (unsigned int)(p - handle_tlbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Ralf Baechlea2c763e2012-10-16 22:20:26 +02001849 dump_handler("r3000_tlb_store", handle_tlbs, ARRAY_SIZE(handle_tlbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
Ralf Baechle234fcd12008-03-08 09:56:28 +00001852static void __cpuinit build_r3000_tlb_modify_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
1854 u32 *p = handle_tlbm;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001855 struct uasm_label *l = labels;
1856 struct uasm_reloc *r = relocs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 memset(handle_tlbm, 0, sizeof(handle_tlbm));
1859 memset(labels, 0, sizeof(labels));
1860 memset(relocs, 0, sizeof(relocs));
1861
1862 build_r3000_tlbchange_handler_head(&p, K0, K1);
Ralf Baechled954ffe2011-08-02 22:52:48 +01001863 build_pte_modifiable(&p, &r, K0, K1, -1, label_nopage_tlbm);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001864 uasm_i_nop(&p); /* load delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 build_make_write(&p, &r, K0, K1);
Maciej W. Rozyckifded2e52005-06-13 20:24:00 +00001866 build_r3000_pte_reload_tlbwi(&p, K0, K1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Thiemo Seufere30ec452008-01-28 20:05:38 +00001868 uasm_l_nopage_tlbm(&l, p);
1869 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1870 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 if ((p - handle_tlbm) > FASTPATH_SIZE)
1873 panic("TLB modify handler fastpath space exceeded");
1874
Thiemo Seufere30ec452008-01-28 20:05:38 +00001875 uasm_resolve_relocs(relocs, labels);
1876 pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
1877 (unsigned int)(p - handle_tlbm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Ralf Baechlea2c763e2012-10-16 22:20:26 +02001879 dump_handler("r3000_tlb_modify", handle_tlbm, ARRAY_SIZE(handle_tlbm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
David Daney82622282009-10-14 12:16:56 -07001881#endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883/*
1884 * R4000 style TLB load/store/modify handlers.
1885 */
David Daneybf286072011-07-05 16:34:46 -07001886static struct work_registers __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +00001887build_r4000_tlbchange_handler_head(u32 **p, struct uasm_label **l,
David Daneybf286072011-07-05 16:34:46 -07001888 struct uasm_reloc **r)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
David Daneybf286072011-07-05 16:34:46 -07001890 struct work_registers wr = build_get_work_registers(p);
1891
Ralf Baechle875d43e2005-09-03 15:56:16 -07001892#ifdef CONFIG_64BIT
David Daneybf286072011-07-05 16:34:46 -07001893 build_get_pmde64(p, l, r, wr.r1, wr.r2); /* get pmd in ptr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894#else
David Daneybf286072011-07-05 16:34:46 -07001895 build_get_pgde32(p, wr.r1, wr.r2); /* get pgd in ptr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896#endif
1897
David Daneyaa1762f2012-10-17 00:48:10 +02001898#ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
David Daneyfd062c82009-05-27 17:47:44 -07001899 /*
1900 * For huge tlb entries, pmd doesn't contain an address but
1901 * instead contains the tlb pte. Check the PAGE_HUGE bit and
1902 * see if we need to jump to huge tlb processing.
1903 */
David Daneybf286072011-07-05 16:34:46 -07001904 build_is_huge_pte(p, r, wr.r1, wr.r2, label_tlb_huge_update);
David Daneyfd062c82009-05-27 17:47:44 -07001905#endif
1906
David Daneybf286072011-07-05 16:34:46 -07001907 UASM_i_MFC0(p, wr.r1, C0_BADVADDR);
1908 UASM_i_LW(p, wr.r2, 0, wr.r2);
1909 UASM_i_SRL(p, wr.r1, wr.r1, PAGE_SHIFT + PTE_ORDER - PTE_T_LOG2);
1910 uasm_i_andi(p, wr.r1, wr.r1, (PTRS_PER_PTE - 1) << PTE_T_LOG2);
1911 UASM_i_ADDU(p, wr.r2, wr.r2, wr.r1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913#ifdef CONFIG_SMP
Thiemo Seufere30ec452008-01-28 20:05:38 +00001914 uasm_l_smp_pgtable_change(l, *p);
1915#endif
David Daneybf286072011-07-05 16:34:46 -07001916 iPTE_LW(p, wr.r1, wr.r2); /* get even pte */
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01001917 if (!m4kc_tlbp_war())
1918 build_tlb_probe_entry(p);
David Daneybf286072011-07-05 16:34:46 -07001919 return wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
1921
Ralf Baechle234fcd12008-03-08 09:56:28 +00001922static void __cpuinit
Thiemo Seufere30ec452008-01-28 20:05:38 +00001923build_r4000_tlbchange_handler_tail(u32 **p, struct uasm_label **l,
1924 struct uasm_reloc **r, unsigned int tmp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 unsigned int ptr)
1926{
Thiemo Seufere30ec452008-01-28 20:05:38 +00001927 uasm_i_ori(p, ptr, ptr, sizeof(pte_t));
1928 uasm_i_xori(p, ptr, ptr, sizeof(pte_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 build_update_entries(p, tmp, ptr);
1930 build_tlb_write_entry(p, l, r, tlb_indexed);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001931 uasm_l_leave(l, *p);
David Daneybf286072011-07-05 16:34:46 -07001932 build_restore_work_registers(p);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001933 uasm_i_eret(p); /* return from trap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Ralf Baechle875d43e2005-09-03 15:56:16 -07001935#ifdef CONFIG_64BIT
David Daney1ec56322010-04-28 12:16:18 -07001936 build_get_pgd_vmalloc64(p, l, r, tmp, ptr, not_refill);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937#endif
1938}
1939
Ralf Baechle234fcd12008-03-08 09:56:28 +00001940static void __cpuinit build_r4000_tlb_load_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
1942 u32 *p = handle_tlbl;
Thiemo Seufere30ec452008-01-28 20:05:38 +00001943 struct uasm_label *l = labels;
1944 struct uasm_reloc *r = relocs;
David Daneybf286072011-07-05 16:34:46 -07001945 struct work_registers wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 memset(handle_tlbl, 0, sizeof(handle_tlbl));
1948 memset(labels, 0, sizeof(labels));
1949 memset(relocs, 0, sizeof(relocs));
1950
1951 if (bcm1250_m3_war()) {
Ralf Baechle3d452852010-03-23 17:56:38 +01001952 unsigned int segbits = 44;
1953
1954 uasm_i_dmfc0(&p, K0, C0_BADVADDR);
1955 uasm_i_dmfc0(&p, K1, C0_ENTRYHI);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001956 uasm_i_xor(&p, K0, K0, K1);
David Daney3be60222010-04-28 12:16:17 -07001957 uasm_i_dsrl_safe(&p, K1, K0, 62);
1958 uasm_i_dsrl_safe(&p, K0, K0, 12 + 1);
1959 uasm_i_dsll_safe(&p, K0, K0, 64 + 12 + 1 - segbits);
Ralf Baechle3d452852010-03-23 17:56:38 +01001960 uasm_i_or(&p, K0, K0, K1);
Thiemo Seufere30ec452008-01-28 20:05:38 +00001961 uasm_il_bnez(&p, &r, K0, label_leave);
1962 /* No need for uasm_i_nop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964
David Daneybf286072011-07-05 16:34:46 -07001965 wr = build_r4000_tlbchange_handler_head(&p, &l, &r);
1966 build_pte_present(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbl);
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01001967 if (m4kc_tlbp_war())
1968 build_tlb_probe_entry(&p);
David Daney6dd93442010-02-10 15:12:47 -08001969
Steven J. Hill05857c62012-09-13 16:51:46 -05001970 if (cpu_has_rixi) {
David Daney6dd93442010-02-10 15:12:47 -08001971 /*
1972 * If the page is not _PAGE_VALID, RI or XI could not
1973 * have triggered it. Skip the expensive test..
1974 */
David Daneycc33ae42010-12-20 15:54:50 -08001975 if (use_bbit_insns()) {
David Daneybf286072011-07-05 16:34:46 -07001976 uasm_il_bbit0(&p, &r, wr.r1, ilog2(_PAGE_VALID),
David Daneycc33ae42010-12-20 15:54:50 -08001977 label_tlbl_goaround1);
1978 } else {
David Daneybf286072011-07-05 16:34:46 -07001979 uasm_i_andi(&p, wr.r3, wr.r1, _PAGE_VALID);
1980 uasm_il_beqz(&p, &r, wr.r3, label_tlbl_goaround1);
David Daneycc33ae42010-12-20 15:54:50 -08001981 }
David Daney6dd93442010-02-10 15:12:47 -08001982 uasm_i_nop(&p);
1983
1984 uasm_i_tlbr(&p);
1985 /* Examine entrylo 0 or 1 based on ptr. */
David Daneycc33ae42010-12-20 15:54:50 -08001986 if (use_bbit_insns()) {
David Daneybf286072011-07-05 16:34:46 -07001987 uasm_i_bbit0(&p, wr.r2, ilog2(sizeof(pte_t)), 8);
David Daneycc33ae42010-12-20 15:54:50 -08001988 } else {
David Daneybf286072011-07-05 16:34:46 -07001989 uasm_i_andi(&p, wr.r3, wr.r2, sizeof(pte_t));
1990 uasm_i_beqz(&p, wr.r3, 8);
David Daneycc33ae42010-12-20 15:54:50 -08001991 }
David Daneybf286072011-07-05 16:34:46 -07001992 /* load it in the delay slot*/
1993 UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO0);
1994 /* load it if ptr is odd */
1995 UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO1);
David Daney6dd93442010-02-10 15:12:47 -08001996 /*
David Daneybf286072011-07-05 16:34:46 -07001997 * If the entryLo (now in wr.r3) is valid (bit 1), RI or
David Daney6dd93442010-02-10 15:12:47 -08001998 * XI must have triggered it.
1999 */
David Daneycc33ae42010-12-20 15:54:50 -08002000 if (use_bbit_insns()) {
David Daneybf286072011-07-05 16:34:46 -07002001 uasm_il_bbit1(&p, &r, wr.r3, 1, label_nopage_tlbl);
2002 uasm_i_nop(&p);
David Daneycc33ae42010-12-20 15:54:50 -08002003 uasm_l_tlbl_goaround1(&l, p);
2004 } else {
David Daneybf286072011-07-05 16:34:46 -07002005 uasm_i_andi(&p, wr.r3, wr.r3, 2);
2006 uasm_il_bnez(&p, &r, wr.r3, label_nopage_tlbl);
2007 uasm_i_nop(&p);
David Daneycc33ae42010-12-20 15:54:50 -08002008 }
David Daneybf286072011-07-05 16:34:46 -07002009 uasm_l_tlbl_goaround1(&l, p);
David Daney6dd93442010-02-10 15:12:47 -08002010 }
David Daneybf286072011-07-05 16:34:46 -07002011 build_make_valid(&p, &r, wr.r1, wr.r2);
2012 build_r4000_tlbchange_handler_tail(&p, &l, &r, wr.r1, wr.r2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
David Daneyaa1762f2012-10-17 00:48:10 +02002014#ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
David Daneyfd062c82009-05-27 17:47:44 -07002015 /*
2016 * This is the entry point when build_r4000_tlbchange_handler_head
2017 * spots a huge page.
2018 */
2019 uasm_l_tlb_huge_update(&l, p);
David Daneybf286072011-07-05 16:34:46 -07002020 iPTE_LW(&p, wr.r1, wr.r2);
2021 build_pte_present(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbl);
David Daneyfd062c82009-05-27 17:47:44 -07002022 build_tlb_probe_entry(&p);
David Daney6dd93442010-02-10 15:12:47 -08002023
Steven J. Hill05857c62012-09-13 16:51:46 -05002024 if (cpu_has_rixi) {
David Daney6dd93442010-02-10 15:12:47 -08002025 /*
2026 * If the page is not _PAGE_VALID, RI or XI could not
2027 * have triggered it. Skip the expensive test..
2028 */
David Daneycc33ae42010-12-20 15:54:50 -08002029 if (use_bbit_insns()) {
David Daneybf286072011-07-05 16:34:46 -07002030 uasm_il_bbit0(&p, &r, wr.r1, ilog2(_PAGE_VALID),
David Daneycc33ae42010-12-20 15:54:50 -08002031 label_tlbl_goaround2);
2032 } else {
David Daneybf286072011-07-05 16:34:46 -07002033 uasm_i_andi(&p, wr.r3, wr.r1, _PAGE_VALID);
2034 uasm_il_beqz(&p, &r, wr.r3, label_tlbl_goaround2);
David Daneycc33ae42010-12-20 15:54:50 -08002035 }
David Daney6dd93442010-02-10 15:12:47 -08002036 uasm_i_nop(&p);
2037
2038 uasm_i_tlbr(&p);
2039 /* Examine entrylo 0 or 1 based on ptr. */
David Daneycc33ae42010-12-20 15:54:50 -08002040 if (use_bbit_insns()) {
David Daneybf286072011-07-05 16:34:46 -07002041 uasm_i_bbit0(&p, wr.r2, ilog2(sizeof(pte_t)), 8);
David Daneycc33ae42010-12-20 15:54:50 -08002042 } else {
David Daneybf286072011-07-05 16:34:46 -07002043 uasm_i_andi(&p, wr.r3, wr.r2, sizeof(pte_t));
2044 uasm_i_beqz(&p, wr.r3, 8);
David Daneycc33ae42010-12-20 15:54:50 -08002045 }
David Daneybf286072011-07-05 16:34:46 -07002046 /* load it in the delay slot*/
2047 UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO0);
2048 /* load it if ptr is odd */
2049 UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO1);
David Daney6dd93442010-02-10 15:12:47 -08002050 /*
David Daneybf286072011-07-05 16:34:46 -07002051 * If the entryLo (now in wr.r3) is valid (bit 1), RI or
David Daney6dd93442010-02-10 15:12:47 -08002052 * XI must have triggered it.
2053 */
David Daneycc33ae42010-12-20 15:54:50 -08002054 if (use_bbit_insns()) {
David Daneybf286072011-07-05 16:34:46 -07002055 uasm_il_bbit0(&p, &r, wr.r3, 1, label_tlbl_goaround2);
David Daneycc33ae42010-12-20 15:54:50 -08002056 } else {
David Daneybf286072011-07-05 16:34:46 -07002057 uasm_i_andi(&p, wr.r3, wr.r3, 2);
2058 uasm_il_beqz(&p, &r, wr.r3, label_tlbl_goaround2);
David Daneycc33ae42010-12-20 15:54:50 -08002059 }
David Daney0f4ccbc2011-09-16 18:06:02 -07002060 if (PM_DEFAULT_MASK == 0)
2061 uasm_i_nop(&p);
David Daney6dd93442010-02-10 15:12:47 -08002062 /*
2063 * We clobbered C0_PAGEMASK, restore it. On the other branch
2064 * it is restored in build_huge_tlb_write_entry.
2065 */
David Daneybf286072011-07-05 16:34:46 -07002066 build_restore_pagemask(&p, &r, wr.r3, label_nopage_tlbl, 0);
David Daney6dd93442010-02-10 15:12:47 -08002067
2068 uasm_l_tlbl_goaround2(&l, p);
2069 }
David Daneybf286072011-07-05 16:34:46 -07002070 uasm_i_ori(&p, wr.r1, wr.r1, (_PAGE_ACCESSED | _PAGE_VALID));
2071 build_huge_handler_tail(&p, &r, &l, wr.r1, wr.r2);
David Daneyfd062c82009-05-27 17:47:44 -07002072#endif
2073
Thiemo Seufere30ec452008-01-28 20:05:38 +00002074 uasm_l_nopage_tlbl(&l, p);
David Daneybf286072011-07-05 16:34:46 -07002075 build_restore_work_registers(&p);
Thiemo Seufere30ec452008-01-28 20:05:38 +00002076 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
2077 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079 if ((p - handle_tlbl) > FASTPATH_SIZE)
2080 panic("TLB load handler fastpath space exceeded");
2081
Thiemo Seufere30ec452008-01-28 20:05:38 +00002082 uasm_resolve_relocs(relocs, labels);
2083 pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
2084 (unsigned int)(p - handle_tlbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Ralf Baechlea2c763e2012-10-16 22:20:26 +02002086 dump_handler("r4000_tlb_load", handle_tlbl, ARRAY_SIZE(handle_tlbl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088
Ralf Baechle234fcd12008-03-08 09:56:28 +00002089static void __cpuinit build_r4000_tlb_store_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
2091 u32 *p = handle_tlbs;
Thiemo Seufere30ec452008-01-28 20:05:38 +00002092 struct uasm_label *l = labels;
2093 struct uasm_reloc *r = relocs;
David Daneybf286072011-07-05 16:34:46 -07002094 struct work_registers wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096 memset(handle_tlbs, 0, sizeof(handle_tlbs));
2097 memset(labels, 0, sizeof(labels));
2098 memset(relocs, 0, sizeof(relocs));
2099
David Daneybf286072011-07-05 16:34:46 -07002100 wr = build_r4000_tlbchange_handler_head(&p, &l, &r);
2101 build_pte_writable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbs);
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01002102 if (m4kc_tlbp_war())
2103 build_tlb_probe_entry(&p);
David Daneybf286072011-07-05 16:34:46 -07002104 build_make_write(&p, &r, wr.r1, wr.r2);
2105 build_r4000_tlbchange_handler_tail(&p, &l, &r, wr.r1, wr.r2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
David Daneyaa1762f2012-10-17 00:48:10 +02002107#ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
David Daneyfd062c82009-05-27 17:47:44 -07002108 /*
2109 * This is the entry point when
2110 * build_r4000_tlbchange_handler_head spots a huge page.
2111 */
2112 uasm_l_tlb_huge_update(&l, p);
David Daneybf286072011-07-05 16:34:46 -07002113 iPTE_LW(&p, wr.r1, wr.r2);
2114 build_pte_writable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbs);
David Daneyfd062c82009-05-27 17:47:44 -07002115 build_tlb_probe_entry(&p);
David Daneybf286072011-07-05 16:34:46 -07002116 uasm_i_ori(&p, wr.r1, wr.r1,
David Daneyfd062c82009-05-27 17:47:44 -07002117 _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
David Daneybf286072011-07-05 16:34:46 -07002118 build_huge_handler_tail(&p, &r, &l, wr.r1, wr.r2);
David Daneyfd062c82009-05-27 17:47:44 -07002119#endif
2120
Thiemo Seufere30ec452008-01-28 20:05:38 +00002121 uasm_l_nopage_tlbs(&l, p);
David Daneybf286072011-07-05 16:34:46 -07002122 build_restore_work_registers(&p);
Thiemo Seufere30ec452008-01-28 20:05:38 +00002123 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
2124 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 if ((p - handle_tlbs) > FASTPATH_SIZE)
2127 panic("TLB store handler fastpath space exceeded");
2128
Thiemo Seufere30ec452008-01-28 20:05:38 +00002129 uasm_resolve_relocs(relocs, labels);
2130 pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
2131 (unsigned int)(p - handle_tlbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Ralf Baechlea2c763e2012-10-16 22:20:26 +02002133 dump_handler("r4000_tlb_store", handle_tlbs, ARRAY_SIZE(handle_tlbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Ralf Baechle234fcd12008-03-08 09:56:28 +00002136static void __cpuinit build_r4000_tlb_modify_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
2138 u32 *p = handle_tlbm;
Thiemo Seufere30ec452008-01-28 20:05:38 +00002139 struct uasm_label *l = labels;
2140 struct uasm_reloc *r = relocs;
David Daneybf286072011-07-05 16:34:46 -07002141 struct work_registers wr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 memset(handle_tlbm, 0, sizeof(handle_tlbm));
2144 memset(labels, 0, sizeof(labels));
2145 memset(relocs, 0, sizeof(relocs));
2146
David Daneybf286072011-07-05 16:34:46 -07002147 wr = build_r4000_tlbchange_handler_head(&p, &l, &r);
2148 build_pte_modifiable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbm);
Maciej W. Rozycki8df5bea2006-08-23 14:26:50 +01002149 if (m4kc_tlbp_war())
2150 build_tlb_probe_entry(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 /* Present and writable bits set, set accessed and dirty bits. */
David Daneybf286072011-07-05 16:34:46 -07002152 build_make_write(&p, &r, wr.r1, wr.r2);
2153 build_r4000_tlbchange_handler_tail(&p, &l, &r, wr.r1, wr.r2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
David Daneyaa1762f2012-10-17 00:48:10 +02002155#ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
David Daneyfd062c82009-05-27 17:47:44 -07002156 /*
2157 * This is the entry point when
2158 * build_r4000_tlbchange_handler_head spots a huge page.
2159 */
2160 uasm_l_tlb_huge_update(&l, p);
David Daneybf286072011-07-05 16:34:46 -07002161 iPTE_LW(&p, wr.r1, wr.r2);
2162 build_pte_modifiable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbm);
David Daneyfd062c82009-05-27 17:47:44 -07002163 build_tlb_probe_entry(&p);
David Daneybf286072011-07-05 16:34:46 -07002164 uasm_i_ori(&p, wr.r1, wr.r1,
David Daneyfd062c82009-05-27 17:47:44 -07002165 _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
David Daneybf286072011-07-05 16:34:46 -07002166 build_huge_handler_tail(&p, &r, &l, wr.r1, wr.r2);
David Daneyfd062c82009-05-27 17:47:44 -07002167#endif
2168
Thiemo Seufere30ec452008-01-28 20:05:38 +00002169 uasm_l_nopage_tlbm(&l, p);
David Daneybf286072011-07-05 16:34:46 -07002170 build_restore_work_registers(&p);
Thiemo Seufere30ec452008-01-28 20:05:38 +00002171 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
2172 uasm_i_nop(&p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174 if ((p - handle_tlbm) > FASTPATH_SIZE)
2175 panic("TLB modify handler fastpath space exceeded");
2176
Thiemo Seufere30ec452008-01-28 20:05:38 +00002177 uasm_resolve_relocs(relocs, labels);
2178 pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
2179 (unsigned int)(p - handle_tlbm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Ralf Baechlea2c763e2012-10-16 22:20:26 +02002181 dump_handler("r4000_tlb_modify", handle_tlbm, ARRAY_SIZE(handle_tlbm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
Ralf Baechle234fcd12008-03-08 09:56:28 +00002184void __cpuinit build_tlb_refill_handler(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 /*
2187 * The refill handler is generated per-CPU, multi-node systems
2188 * may have local storage for it. The other handlers are only
2189 * needed once.
2190 */
2191 static int run_once = 0;
2192
Ralf Baechlea2c763e2012-10-16 22:20:26 +02002193 output_pgtable_bits_defines();
2194
David Daney1ec56322010-04-28 12:16:18 -07002195#ifdef CONFIG_64BIT
2196 check_for_high_segbits = current_cpu_data.vmbits > (PGDIR_SHIFT + PGD_ORDER + PAGE_SHIFT - 3);
2197#endif
2198
Ralf Baechle10cc3522007-10-11 23:46:15 +01002199 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 case CPU_R2000:
2201 case CPU_R3000:
2202 case CPU_R3000A:
2203 case CPU_R3081E:
2204 case CPU_TX3912:
2205 case CPU_TX3922:
2206 case CPU_TX3927:
David Daney82622282009-10-14 12:16:56 -07002207#ifndef CONFIG_MIPS_PGD_C0_CONTEXT
Steven J. Hilld532f3d2013-03-25 11:58:57 -05002208 setup_asid(0x40, 0xfc0, 0xf000, ASID_FIRST_VERSION_R3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 build_r3000_tlb_refill_handler();
2210 if (!run_once) {
2211 build_r3000_tlb_load_handler();
2212 build_r3000_tlb_store_handler();
2213 build_r3000_tlb_modify_handler();
2214 run_once++;
2215 }
David Daney82622282009-10-14 12:16:56 -07002216#else
2217 panic("No R3000 TLB refill handler");
2218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 break;
2220
2221 case CPU_R6000:
2222 case CPU_R6000A:
2223 panic("No R6000 TLB refill handler yet");
2224 break;
2225
2226 case CPU_R8000:
2227 panic("No R8000 TLB refill handler yet");
2228 break;
2229
2230 default:
Steven J. Hilld532f3d2013-03-25 11:58:57 -05002231#ifndef CONFIG_MIPS_MT_SMTC
2232 setup_asid(0x1, 0xff, 0xff00, ASID_FIRST_VERSION_R4000);
2233#else
2234 setup_asid(0x1, smtc_asid_mask, 0xff00, ASID_FIRST_VERSION_R4000);
2235#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 if (!run_once) {
David Daneybf286072011-07-05 16:34:46 -07002237 scratch_reg = allocate_kscratch();
David Daney3d8bfdd2010-12-21 14:19:11 -08002238#ifdef CONFIG_MIPS_PGD_C0_CONTEXT
2239 build_r4000_setup_pgd();
2240#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 build_r4000_tlb_load_handler();
2242 build_r4000_tlb_store_handler();
2243 build_r4000_tlb_modify_handler();
2244 run_once++;
2245 }
David Daney3d8bfdd2010-12-21 14:19:11 -08002246 build_r4000_tlb_refill_handler();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248}
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00002249
Ralf Baechle234fcd12008-03-08 09:56:28 +00002250void __cpuinit flush_tlb_handlers(void)
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00002251{
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02002252 local_flush_icache_range((unsigned long)handle_tlbl,
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00002253 (unsigned long)handle_tlbl + sizeof(handle_tlbl));
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02002254 local_flush_icache_range((unsigned long)handle_tlbs,
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00002255 (unsigned long)handle_tlbs + sizeof(handle_tlbs));
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02002256 local_flush_icache_range((unsigned long)handle_tlbm,
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00002257 (unsigned long)handle_tlbm + sizeof(handle_tlbm));
David Daney3d8bfdd2010-12-21 14:19:11 -08002258#ifdef CONFIG_MIPS_PGD_C0_CONTEXT
2259 local_flush_icache_range((unsigned long)tlbmiss_handler_setup_pgd,
2260 (unsigned long)tlbmiss_handler_setup_pgd + sizeof(handle_tlbm));
2261#endif
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00002262}