c9d00d9cb3c8b2a51b33ec5fd7586263342529d0
[linux-2.6-microblaze.git] / arch / mips / mm / tlbex.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Synthesize TLB refill handlers at runtime.
7  *
8  * Copyright (C) 2004, 2005, 2006, 2008  Thiemo Seufer
9  * Copyright (C) 2005, 2007, 2008, 2009  Maciej W. Rozycki
10  * Copyright (C) 2006  Ralf Baechle (ralf@linux-mips.org)
11  * Copyright (C) 2008, 2009 Cavium Networks, Inc.
12  * Copyright (C) 2011  MIPS Technologies, Inc.
13  *
14  * ... and the days got worse and worse and now you see
15  * I've gone completely 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)
22  */
23
24 #include <linux/bug.h>
25 #include <linux/export.h>
26 #include <linux/kernel.h>
27 #include <linux/types.h>
28 #include <linux/smp.h>
29 #include <linux/string.h>
30 #include <linux/cache.h>
31 #include <linux/pgtable.h>
32
33 #include <asm/cacheflush.h>
34 #include <asm/cpu-type.h>
35 #include <asm/mipsregs.h>
36 #include <asm/mmu_context.h>
37 #include <asm/uasm.h>
38 #include <asm/setup.h>
39 #include <asm/tlbex.h>
40
41 static int mips_xpa_disabled;
42
43 static int __init xpa_disable(char *s)
44 {
45         mips_xpa_disabled = 1;
46
47         return 1;
48 }
49
50 __setup("noxpa", xpa_disable);
51
52 /*
53  * TLB load/store/modify handlers.
54  *
55  * Only the fastpath gets synthesized at runtime, the slowpath for
56  * do_page_fault remains normal asm.
57  */
58 extern void tlb_do_page_fault_0(void);
59 extern void tlb_do_page_fault_1(void);
60
61 struct work_registers {
62         int r1;
63         int r2;
64         int r3;
65 };
66
67 struct tlb_reg_save {
68         unsigned long a;
69         unsigned long b;
70 } ____cacheline_aligned_in_smp;
71
72 static struct tlb_reg_save handler_reg_save[NR_CPUS];
73
74 static inline int r45k_bvahwbug(void)
75 {
76         /* XXX: We should probe for the presence of this bug, but we don't. */
77         return 0;
78 }
79
80 static inline int r4k_250MHZhwbug(void)
81 {
82         /* XXX: We should probe for the presence of this bug, but we don't. */
83         return 0;
84 }
85
86 extern int sb1250_m3_workaround_needed(void);
87
88 static inline int __maybe_unused bcm1250_m3_war(void)
89 {
90         if (IS_ENABLED(CONFIG_SB1_PASS_2_WORKAROUNDS))
91                 return sb1250_m3_workaround_needed();
92         return 0;
93 }
94
95 static inline int __maybe_unused r10000_llsc_war(void)
96 {
97         return IS_ENABLED(CONFIG_WAR_R10000_LLSC);
98 }
99
100 static int use_bbit_insns(void)
101 {
102         switch (current_cpu_type()) {
103         case CPU_CAVIUM_OCTEON:
104         case CPU_CAVIUM_OCTEON_PLUS:
105         case CPU_CAVIUM_OCTEON2:
106         case CPU_CAVIUM_OCTEON3:
107                 return 1;
108         default:
109                 return 0;
110         }
111 }
112
113 static int use_lwx_insns(void)
114 {
115         switch (current_cpu_type()) {
116         case CPU_CAVIUM_OCTEON2:
117         case CPU_CAVIUM_OCTEON3:
118                 return 1;
119         default:
120                 return 0;
121         }
122 }
123 #if defined(CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE) && \
124     CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE > 0
125 static bool scratchpad_available(void)
126 {
127         return true;
128 }
129 static int scratchpad_offset(int i)
130 {
131         /*
132          * CVMSEG starts at address -32768 and extends for
133          * CAVIUM_OCTEON_CVMSEG_SIZE 128 byte cache lines.
134          */
135         i += 1; /* Kernel use starts at the top and works down. */
136         return CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE * 128 - (8 * i) - 32768;
137 }
138 #else
139 static bool scratchpad_available(void)
140 {
141         return false;
142 }
143 static int scratchpad_offset(int i)
144 {
145         BUG();
146         /* Really unreachable, but evidently some GCC want this. */
147         return 0;
148 }
149 #endif
150 /*
151  * Found by experiment: At least some revisions of the 4kc throw under
152  * some circumstances a machine check exception, triggered by invalid
153  * values in the index register.  Delaying the tlbp instruction until
154  * after the next branch,  plus adding an additional nop in front of
155  * tlbwi/tlbwr avoids the invalid index register values. Nobody knows
156  * why; it's not an issue caused by the core RTL.
157  *
158  */
159 static int m4kc_tlbp_war(void)
160 {
161         return current_cpu_type() == CPU_4KC;
162 }
163
164 /* Handle labels (which must be positive integers). */
165 enum label_id {
166         label_second_part = 1,
167         label_leave,
168         label_vmalloc,
169         label_vmalloc_done,
170         label_tlbw_hazard_0,
171         label_split = label_tlbw_hazard_0 + 8,
172         label_tlbl_goaround1,
173         label_tlbl_goaround2,
174         label_nopage_tlbl,
175         label_nopage_tlbs,
176         label_nopage_tlbm,
177         label_smp_pgtable_change,
178         label_r3000_write_probe_fail,
179         label_large_segbits_fault,
180 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
181         label_tlb_huge_update,
182 #endif
183 };
184
185 UASM_L_LA(_second_part)
186 UASM_L_LA(_leave)
187 UASM_L_LA(_vmalloc)
188 UASM_L_LA(_vmalloc_done)
189 /* _tlbw_hazard_x is handled differently.  */
190 UASM_L_LA(_split)
191 UASM_L_LA(_tlbl_goaround1)
192 UASM_L_LA(_tlbl_goaround2)
193 UASM_L_LA(_nopage_tlbl)
194 UASM_L_LA(_nopage_tlbs)
195 UASM_L_LA(_nopage_tlbm)
196 UASM_L_LA(_smp_pgtable_change)
197 UASM_L_LA(_r3000_write_probe_fail)
198 UASM_L_LA(_large_segbits_fault)
199 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
200 UASM_L_LA(_tlb_huge_update)
201 #endif
202
203 static int hazard_instance;
204
205 static void uasm_bgezl_hazard(u32 **p, struct uasm_reloc **r, int instance)
206 {
207         switch (instance) {
208         case 0 ... 7:
209                 uasm_il_bgezl(p, r, 0, label_tlbw_hazard_0 + instance);
210                 return;
211         default:
212                 BUG();
213         }
214 }
215
216 static void uasm_bgezl_label(struct uasm_label **l, u32 **p, int instance)
217 {
218         switch (instance) {
219         case 0 ... 7:
220                 uasm_build_label(l, *p, label_tlbw_hazard_0 + instance);
221                 break;
222         default:
223                 BUG();
224         }
225 }
226
227 /*
228  * pgtable bits are assigned dynamically depending on processor feature
229  * and statically based on kernel configuration.  This spits out the actual
230  * values the kernel is using.  Required to make sense from disassembled
231  * TLB exception handlers.
232  */
233 static void output_pgtable_bits_defines(void)
234 {
235 #define pr_define(fmt, ...)                                     \
236         pr_debug("#define " fmt, ##__VA_ARGS__)
237
238         pr_debug("#include <asm/asm.h>\n");
239         pr_debug("#include <asm/regdef.h>\n");
240         pr_debug("\n");
241
242         pr_define("_PAGE_PRESENT_SHIFT %d\n", _PAGE_PRESENT_SHIFT);
243         pr_define("_PAGE_NO_READ_SHIFT %d\n", _PAGE_NO_READ_SHIFT);
244         pr_define("_PAGE_WRITE_SHIFT %d\n", _PAGE_WRITE_SHIFT);
245         pr_define("_PAGE_ACCESSED_SHIFT %d\n", _PAGE_ACCESSED_SHIFT);
246         pr_define("_PAGE_MODIFIED_SHIFT %d\n", _PAGE_MODIFIED_SHIFT);
247 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
248         pr_define("_PAGE_HUGE_SHIFT %d\n", _PAGE_HUGE_SHIFT);
249 #endif
250 #ifdef _PAGE_NO_EXEC_SHIFT
251         if (cpu_has_rixi)
252                 pr_define("_PAGE_NO_EXEC_SHIFT %d\n", _PAGE_NO_EXEC_SHIFT);
253 #endif
254         pr_define("_PAGE_GLOBAL_SHIFT %d\n", _PAGE_GLOBAL_SHIFT);
255         pr_define("_PAGE_VALID_SHIFT %d\n", _PAGE_VALID_SHIFT);
256         pr_define("_PAGE_DIRTY_SHIFT %d\n", _PAGE_DIRTY_SHIFT);
257         pr_define("PFN_PTE_SHIFT %d\n", PFN_PTE_SHIFT);
258         pr_debug("\n");
259 }
260
261 static inline void dump_handler(const char *symbol, const void *start, const void *end)
262 {
263         unsigned int count = (end - start) / sizeof(u32);
264         const u32 *handler = start;
265         int i;
266
267         pr_debug("LEAF(%s)\n", symbol);
268
269         pr_debug("\t.set push\n");
270         pr_debug("\t.set noreorder\n");
271
272         for (i = 0; i < count; i++)
273                 pr_debug("\t.word\t0x%08x\t\t# %p\n", handler[i], &handler[i]);
274
275         pr_debug("\t.set\tpop\n");
276
277         pr_debug("\tEND(%s)\n", symbol);
278 }
279
280 /* The only general purpose registers allowed in TLB handlers. */
281 #define K0              26
282 #define K1              27
283
284 #ifdef CONFIG_64BIT
285 # define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_XCONTEXT)
286 #else
287 # define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_CONTEXT)
288 #endif
289
290 /* The worst case length of the handler is around 18 instructions for
291  * R3000-style TLBs and up to 63 instructions for R4000-style TLBs.
292  * Maximum space available is 32 instructions for R3000 and 64
293  * instructions for R4000.
294  *
295  * We deliberately chose a buffer size of 128, so we won't scribble
296  * over anything important on overflow before we panic.
297  */
298 static u32 tlb_handler[128];
299
300 /* simply assume worst case size for labels and relocs */
301 static struct uasm_label labels[128];
302 static struct uasm_reloc relocs[128];
303
304 static int check_for_high_segbits;
305 static bool fill_includes_sw_bits;
306
307 static unsigned int kscratch_used_mask;
308
309 static inline int __maybe_unused c0_kscratch(void)
310 {
311         return 31;
312 }
313
314 static int allocate_kscratch(void)
315 {
316         int r;
317         unsigned int a = cpu_data[0].kscratch_mask & ~kscratch_used_mask;
318
319         r = ffs(a);
320
321         if (r == 0)
322                 return -1;
323
324         r--; /* make it zero based */
325
326         kscratch_used_mask |= (1 << r);
327
328         return r;
329 }
330
331 static int scratch_reg;
332 int pgd_reg;
333 EXPORT_SYMBOL_GPL(pgd_reg);
334 enum vmalloc64_mode {not_refill, refill_scratch, refill_noscratch};
335
336 static struct work_registers build_get_work_registers(u32 **p)
337 {
338         struct work_registers r;
339
340         if (scratch_reg >= 0) {
341                 /* Save in CPU local C0_KScratch? */
342                 UASM_i_MTC0(p, 1, c0_kscratch(), scratch_reg);
343                 r.r1 = K0;
344                 r.r2 = K1;
345                 r.r3 = 1;
346                 return r;
347         }
348
349         if (num_possible_cpus() > 1) {
350                 /* Get smp_processor_id */
351                 UASM_i_CPUID_MFC0(p, K0, SMP_CPUID_REG);
352                 UASM_i_SRL_SAFE(p, K0, K0, SMP_CPUID_REGSHIFT);
353
354                 /* handler_reg_save index in K0 */
355                 UASM_i_SLL(p, K0, K0, ilog2(sizeof(struct tlb_reg_save)));
356
357                 UASM_i_LA(p, K1, (long)&handler_reg_save);
358                 UASM_i_ADDU(p, K0, K0, K1);
359         } else {
360                 UASM_i_LA(p, K0, (long)&handler_reg_save);
361         }
362         /* K0 now points to save area, save $1 and $2  */
363         UASM_i_SW(p, 1, offsetof(struct tlb_reg_save, a), K0);
364         UASM_i_SW(p, 2, offsetof(struct tlb_reg_save, b), K0);
365
366         r.r1 = K1;
367         r.r2 = 1;
368         r.r3 = 2;
369         return r;
370 }
371
372 static void build_restore_work_registers(u32 **p)
373 {
374         if (scratch_reg >= 0) {
375                 uasm_i_ehb(p);
376                 UASM_i_MFC0(p, 1, c0_kscratch(), scratch_reg);
377                 return;
378         }
379         /* K0 already points to save area, restore $1 and $2  */
380         UASM_i_LW(p, 1, offsetof(struct tlb_reg_save, a), K0);
381         UASM_i_LW(p, 2, offsetof(struct tlb_reg_save, b), K0);
382 }
383
384 #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
385
386 /*
387  * CONFIG_MIPS_PGD_C0_CONTEXT implies 64 bit and lack of pgd_current,
388  * we cannot do r3000 under these circumstances.
389  *
390  * The R3000 TLB handler is simple.
391  */
392 static void build_r3000_tlb_refill_handler(void)
393 {
394         long pgdc = (long)pgd_current;
395         u32 *p;
396
397         memset(tlb_handler, 0, sizeof(tlb_handler));
398         p = tlb_handler;
399
400         uasm_i_mfc0(&p, K0, C0_BADVADDR);
401         uasm_i_lui(&p, K1, uasm_rel_hi(pgdc)); /* cp0 delay */
402         uasm_i_lw(&p, K1, uasm_rel_lo(pgdc), K1);
403         uasm_i_srl(&p, K0, K0, 22); /* load delay */
404         uasm_i_sll(&p, K0, K0, 2);
405         uasm_i_addu(&p, K1, K1, K0);
406         uasm_i_mfc0(&p, K0, C0_CONTEXT);
407         uasm_i_lw(&p, K1, 0, K1); /* cp0 delay */
408         uasm_i_andi(&p, K0, K0, 0xffc); /* load delay */
409         uasm_i_addu(&p, K1, K1, K0);
410         uasm_i_lw(&p, K0, 0, K1);
411         uasm_i_nop(&p); /* load delay */
412         uasm_i_mtc0(&p, K0, C0_ENTRYLO0);
413         uasm_i_mfc0(&p, K1, C0_EPC); /* cp0 delay */
414         uasm_i_tlbwr(&p); /* cp0 delay */
415         uasm_i_jr(&p, K1);
416         uasm_i_rfe(&p); /* branch delay */
417
418         if (p > tlb_handler + 32)
419                 panic("TLB refill handler space exceeded");
420
421         pr_debug("Wrote TLB refill handler (%u instructions).\n",
422                  (unsigned int)(p - tlb_handler));
423
424         memcpy((void *)ebase, tlb_handler, 0x80);
425         local_flush_icache_range(ebase, ebase + 0x80);
426         dump_handler("r3000_tlb_refill", (u32 *)ebase, (u32 *)(ebase + 0x80));
427 }
428 #endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
429
430 /*
431  * The R4000 TLB handler is much more complicated. We have two
432  * consecutive handler areas with 32 instructions space each.
433  * Since they aren't used at the same time, we can overflow in the
434  * other one.To keep things simple, we first assume linear space,
435  * then we relocate it to the final handler layout as needed.
436  */
437 static u32 final_handler[64];
438
439 /*
440  * Hazards
441  *
442  * From the IDT errata for the QED RM5230 (Nevada), processor revision 1.0:
443  * 2. A timing hazard exists for the TLBP instruction.
444  *
445  *      stalling_instruction
446  *      TLBP
447  *
448  * The JTLB is being read for the TLBP throughout the stall generated by the
449  * previous instruction. This is not really correct as the stalling instruction
450  * can modify the address used to access the JTLB.  The failure symptom is that
451  * the TLBP instruction will use an address created for the stalling instruction
452  * and not the address held in C0_ENHI and thus report the wrong results.
453  *
454  * The software work-around is to not allow the instruction preceding the TLBP
455  * to stall - make it an NOP or some other instruction guaranteed not to stall.
456  *
457  * Errata 2 will not be fixed.  This errata is also on the R5000.
458  *
459  * As if we MIPS hackers wouldn't know how to nop pipelines happy ...
460  */
461 static void __maybe_unused build_tlb_probe_entry(u32 **p)
462 {
463         switch (current_cpu_type()) {
464         /* Found by experiment: R4600 v2.0/R4700 needs this, too.  */
465         case CPU_R4600:
466         case CPU_R4700:
467         case CPU_R5000:
468         case CPU_NEVADA:
469                 uasm_i_nop(p);
470                 uasm_i_tlbp(p);
471                 break;
472
473         default:
474                 uasm_i_tlbp(p);
475                 break;
476         }
477 }
478
479 void build_tlb_write_entry(u32 **p, struct uasm_label **l,
480                            struct uasm_reloc **r,
481                            enum tlb_write_entry wmode)
482 {
483         void(*tlbw)(u32 **) = NULL;
484
485         switch (wmode) {
486         case tlb_random: tlbw = uasm_i_tlbwr; break;
487         case tlb_indexed: tlbw = uasm_i_tlbwi; break;
488         }
489
490         if (cpu_has_mips_r2_r6) {
491                 if (cpu_has_mips_r2_exec_hazard)
492                         uasm_i_ehb(p);
493                 tlbw(p);
494                 return;
495         }
496
497         switch (current_cpu_type()) {
498         case CPU_R4000PC:
499         case CPU_R4000SC:
500         case CPU_R4000MC:
501         case CPU_R4400PC:
502         case CPU_R4400SC:
503         case CPU_R4400MC:
504                 /*
505                  * This branch uses up a mtc0 hazard nop slot and saves
506                  * two nops after the tlbw instruction.
507                  */
508                 uasm_bgezl_hazard(p, r, hazard_instance);
509                 tlbw(p);
510                 uasm_bgezl_label(l, p, hazard_instance);
511                 hazard_instance++;
512                 uasm_i_nop(p);
513                 break;
514
515         case CPU_R4600:
516         case CPU_R4700:
517                 uasm_i_nop(p);
518                 tlbw(p);
519                 uasm_i_nop(p);
520                 break;
521
522         case CPU_R5000:
523         case CPU_NEVADA:
524                 uasm_i_nop(p); /* QED specifies 2 nops hazard */
525                 uasm_i_nop(p); /* QED specifies 2 nops hazard */
526                 tlbw(p);
527                 break;
528
529         case CPU_R4300:
530         case CPU_5KC:
531         case CPU_TX49XX:
532         case CPU_PR4450:
533                 uasm_i_nop(p);
534                 tlbw(p);
535                 break;
536
537         case CPU_R10000:
538         case CPU_R12000:
539         case CPU_R14000:
540         case CPU_R16000:
541         case CPU_4KC:
542         case CPU_4KEC:
543         case CPU_M14KC:
544         case CPU_M14KEC:
545         case CPU_SB1:
546         case CPU_SB1A:
547         case CPU_4KSC:
548         case CPU_20KC:
549         case CPU_25KF:
550         case CPU_BMIPS32:
551         case CPU_BMIPS3300:
552         case CPU_BMIPS4350:
553         case CPU_BMIPS4380:
554         case CPU_BMIPS5000:
555         case CPU_LOONGSON2EF:
556         case CPU_LOONGSON64:
557         case CPU_R5500:
558                 if (m4kc_tlbp_war())
559                         uasm_i_nop(p);
560                 fallthrough;
561         case CPU_ALCHEMY:
562                 tlbw(p);
563                 break;
564
565         case CPU_RM7000:
566                 uasm_i_nop(p);
567                 uasm_i_nop(p);
568                 uasm_i_nop(p);
569                 uasm_i_nop(p);
570                 tlbw(p);
571                 break;
572
573         case CPU_XBURST:
574                 tlbw(p);
575                 uasm_i_nop(p);
576                 break;
577
578         default:
579                 panic("No TLB refill handler yet (CPU type: %d)",
580                       current_cpu_type());
581                 break;
582         }
583 }
584 EXPORT_SYMBOL_GPL(build_tlb_write_entry);
585
586 static __maybe_unused void build_convert_pte_to_entrylo(u32 **p,
587                                                         unsigned int reg)
588 {
589         if (_PAGE_GLOBAL_SHIFT == 0) {
590                 /* pte_t is already in EntryLo format */
591                 return;
592         }
593
594         if (cpu_has_rixi && _PAGE_NO_EXEC != 0) {
595                 if (fill_includes_sw_bits) {
596                         UASM_i_ROTR(p, reg, reg, ilog2(_PAGE_GLOBAL));
597                 } else {
598                         UASM_i_SRL(p, reg, reg, ilog2(_PAGE_NO_EXEC));
599                         UASM_i_ROTR(p, reg, reg,
600                                     ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
601                 }
602         } else {
603 #ifdef CONFIG_PHYS_ADDR_T_64BIT
604                 uasm_i_dsrl_safe(p, reg, reg, ilog2(_PAGE_GLOBAL));
605 #else
606                 UASM_i_SRL(p, reg, reg, ilog2(_PAGE_GLOBAL));
607 #endif
608         }
609 }
610
611 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
612
613 static void build_restore_pagemask(u32 **p, struct uasm_reloc **r,
614                                    unsigned int tmp, enum label_id lid,
615                                    int restore_scratch)
616 {
617         if (restore_scratch) {
618                 /*
619                  * Ensure the MFC0 below observes the value written to the
620                  * KScratch register by the prior MTC0.
621                  */
622                 if (scratch_reg >= 0)
623                         uasm_i_ehb(p);
624
625                 /* Reset default page size */
626                 if (PM_DEFAULT_MASK >> 16) {
627                         uasm_i_lui(p, tmp, PM_DEFAULT_MASK >> 16);
628                         uasm_i_ori(p, tmp, tmp, PM_DEFAULT_MASK & 0xffff);
629                         uasm_i_mtc0(p, tmp, C0_PAGEMASK);
630                         uasm_il_b(p, r, lid);
631                 } else if (PM_DEFAULT_MASK) {
632                         uasm_i_ori(p, tmp, 0, PM_DEFAULT_MASK);
633                         uasm_i_mtc0(p, tmp, C0_PAGEMASK);
634                         uasm_il_b(p, r, lid);
635                 } else {
636                         uasm_i_mtc0(p, 0, C0_PAGEMASK);
637                         uasm_il_b(p, r, lid);
638                 }
639                 if (scratch_reg >= 0)
640                         UASM_i_MFC0(p, 1, c0_kscratch(), scratch_reg);
641                 else
642                         UASM_i_LW(p, 1, scratchpad_offset(0), 0);
643         } else {
644                 /* Reset default page size */
645                 if (PM_DEFAULT_MASK >> 16) {
646                         uasm_i_lui(p, tmp, PM_DEFAULT_MASK >> 16);
647                         uasm_i_ori(p, tmp, tmp, PM_DEFAULT_MASK & 0xffff);
648                         uasm_il_b(p, r, lid);
649                         uasm_i_mtc0(p, tmp, C0_PAGEMASK);
650                 } else if (PM_DEFAULT_MASK) {
651                         uasm_i_ori(p, tmp, 0, PM_DEFAULT_MASK);
652                         uasm_il_b(p, r, lid);
653                         uasm_i_mtc0(p, tmp, C0_PAGEMASK);
654                 } else {
655                         uasm_il_b(p, r, lid);
656                         uasm_i_mtc0(p, 0, C0_PAGEMASK);
657                 }
658         }
659 }
660
661 static void build_huge_tlb_write_entry(u32 **p, struct uasm_label **l,
662                                        struct uasm_reloc **r,
663                                        unsigned int tmp,
664                                        enum tlb_write_entry wmode,
665                                        int restore_scratch)
666 {
667         /* Set huge page tlb entry size */
668         uasm_i_lui(p, tmp, PM_HUGE_MASK >> 16);
669         uasm_i_ori(p, tmp, tmp, PM_HUGE_MASK & 0xffff);
670         uasm_i_mtc0(p, tmp, C0_PAGEMASK);
671
672         build_tlb_write_entry(p, l, r, wmode);
673
674         build_restore_pagemask(p, r, tmp, label_leave, restore_scratch);
675 }
676
677 /*
678  * Check if Huge PTE is present, if so then jump to LABEL.
679  */
680 static void
681 build_is_huge_pte(u32 **p, struct uasm_reloc **r, unsigned int tmp,
682                   unsigned int pmd, int lid)
683 {
684         UASM_i_LW(p, tmp, 0, pmd);
685         if (use_bbit_insns()) {
686                 uasm_il_bbit1(p, r, tmp, ilog2(_PAGE_HUGE), lid);
687         } else {
688                 uasm_i_andi(p, tmp, tmp, _PAGE_HUGE);
689                 uasm_il_bnez(p, r, tmp, lid);
690         }
691 }
692
693 static void build_huge_update_entries(u32 **p, unsigned int pte,
694                                       unsigned int tmp)
695 {
696         int small_sequence;
697
698         /*
699          * A huge PTE describes an area the size of the
700          * configured huge page size. This is twice the
701          * of the large TLB entry size we intend to use.
702          * A TLB entry half the size of the configured
703          * huge page size is configured into entrylo0
704          * and entrylo1 to cover the contiguous huge PTE
705          * address space.
706          */
707         small_sequence = (HPAGE_SIZE >> 7) < 0x10000;
708
709         /* We can clobber tmp.  It isn't used after this.*/
710         if (!small_sequence)
711                 uasm_i_lui(p, tmp, HPAGE_SIZE >> (7 + 16));
712
713         build_convert_pte_to_entrylo(p, pte);
714         UASM_i_MTC0(p, pte, C0_ENTRYLO0); /* load it */
715         /* convert to entrylo1 */
716         if (small_sequence)
717                 UASM_i_ADDIU(p, pte, pte, HPAGE_SIZE >> 7);
718         else
719                 UASM_i_ADDU(p, pte, pte, tmp);
720
721         UASM_i_MTC0(p, pte, C0_ENTRYLO1); /* load it */
722 }
723
724 static void build_huge_handler_tail(u32 **p, struct uasm_reloc **r,
725                                     struct uasm_label **l,
726                                     unsigned int pte,
727                                     unsigned int ptr,
728                                     unsigned int flush)
729 {
730 #ifdef CONFIG_SMP
731         UASM_i_SC(p, pte, 0, ptr);
732         uasm_il_beqz(p, r, pte, label_tlb_huge_update);
733         UASM_i_LW(p, pte, 0, ptr); /* Needed because SC killed our PTE */
734 #else
735         UASM_i_SW(p, pte, 0, ptr);
736 #endif
737         if (cpu_has_ftlb && flush) {
738                 BUG_ON(!cpu_has_tlbinv);
739
740                 UASM_i_MFC0(p, ptr, C0_ENTRYHI);
741                 uasm_i_ori(p, ptr, ptr, MIPS_ENTRYHI_EHINV);
742                 UASM_i_MTC0(p, ptr, C0_ENTRYHI);
743                 build_tlb_write_entry(p, l, r, tlb_indexed);
744
745                 uasm_i_xori(p, ptr, ptr, MIPS_ENTRYHI_EHINV);
746                 UASM_i_MTC0(p, ptr, C0_ENTRYHI);
747                 build_huge_update_entries(p, pte, ptr);
748                 build_huge_tlb_write_entry(p, l, r, pte, tlb_random, 0);
749
750                 return;
751         }
752
753         build_huge_update_entries(p, pte, ptr);
754         build_huge_tlb_write_entry(p, l, r, pte, tlb_indexed, 0);
755 }
756 #endif /* CONFIG_MIPS_HUGE_TLB_SUPPORT */
757
758 #ifdef CONFIG_64BIT
759 /*
760  * TMP and PTR are scratch.
761  * TMP will be clobbered, PTR will hold the pmd entry.
762  */
763 void build_get_pmde64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
764                       unsigned int tmp, unsigned int ptr)
765 {
766 #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
767         long pgdc = (long)pgd_current;
768 #endif
769         /*
770          * The vmalloc handling is not in the hotpath.
771          */
772         uasm_i_dmfc0(p, tmp, C0_BADVADDR);
773
774         if (check_for_high_segbits) {
775                 /*
776                  * The kernel currently implicitly assumes that the
777                  * MIPS SEGBITS parameter for the processor is
778                  * (PGDIR_SHIFT+PGDIR_BITS) or less, and will never
779                  * allocate virtual addresses outside the maximum
780                  * range for SEGBITS = (PGDIR_SHIFT+PGDIR_BITS). But
781                  * that doesn't prevent user code from accessing the
782                  * higher xuseg addresses.  Here, we make sure that
783                  * everything but the lower xuseg addresses goes down
784                  * the module_alloc/vmalloc path.
785                  */
786                 uasm_i_dsrl_safe(p, ptr, tmp, PGDIR_SHIFT + PGD_TABLE_ORDER + PAGE_SHIFT - 3);
787                 uasm_il_bnez(p, r, ptr, label_vmalloc);
788         } else {
789                 uasm_il_bltz(p, r, tmp, label_vmalloc);
790         }
791         /* No uasm_i_nop needed here, since the next insn doesn't touch TMP. */
792
793         if (pgd_reg != -1) {
794                 /* pgd is in pgd_reg */
795                 if (cpu_has_ldpte)
796                         UASM_i_MFC0(p, ptr, C0_PWBASE);
797                 else
798                         UASM_i_MFC0(p, ptr, c0_kscratch(), pgd_reg);
799         } else {
800 #if defined(CONFIG_MIPS_PGD_C0_CONTEXT)
801                 /*
802                  * &pgd << 11 stored in CONTEXT [23..63].
803                  */
804                 UASM_i_MFC0(p, ptr, C0_CONTEXT);
805
806                 /* Clear lower 23 bits of context. */
807                 uasm_i_dins(p, ptr, 0, 0, 23);
808
809                 /* insert bit[63:59] of CAC_BASE into bit[11:6] of ptr */
810                 uasm_i_ori(p, ptr, ptr, ((u64)(CAC_BASE) >> 53));
811                 uasm_i_drotr(p, ptr, ptr, 11);
812 #elif defined(CONFIG_SMP)
813                 UASM_i_CPUID_MFC0(p, ptr, SMP_CPUID_REG);
814                 uasm_i_dsrl_safe(p, ptr, ptr, SMP_CPUID_PTRSHIFT);
815                 UASM_i_LA_mostly(p, tmp, pgdc);
816                 uasm_i_daddu(p, ptr, ptr, tmp);
817                 uasm_i_dmfc0(p, tmp, C0_BADVADDR);
818                 uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
819 #else
820                 UASM_i_LA_mostly(p, ptr, pgdc);
821                 uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
822 #endif
823         }
824
825         uasm_l_vmalloc_done(l, *p);
826
827         /* get pgd offset in bytes */
828         uasm_i_dsrl_safe(p, tmp, tmp, PGDIR_SHIFT - 3);
829
830         uasm_i_andi(p, tmp, tmp, (PTRS_PER_PGD - 1)<<3);
831         uasm_i_daddu(p, ptr, ptr, tmp); /* add in pgd offset */
832 #ifndef __PAGETABLE_PUD_FOLDED
833         uasm_i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
834         uasm_i_ld(p, ptr, 0, ptr); /* get pud pointer */
835         uasm_i_dsrl_safe(p, tmp, tmp, PUD_SHIFT - 3); /* get pud offset in bytes */
836         uasm_i_andi(p, tmp, tmp, (PTRS_PER_PUD - 1) << 3);
837         uasm_i_daddu(p, ptr, ptr, tmp); /* add in pud offset */
838 #endif
839 #ifndef __PAGETABLE_PMD_FOLDED
840         uasm_i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
841         uasm_i_ld(p, ptr, 0, ptr); /* get pmd pointer */
842         uasm_i_dsrl_safe(p, tmp, tmp, PMD_SHIFT-3); /* get pmd offset in bytes */
843         uasm_i_andi(p, tmp, tmp, (PTRS_PER_PMD - 1)<<3);
844         uasm_i_daddu(p, ptr, ptr, tmp); /* add in pmd offset */
845 #endif
846 }
847 EXPORT_SYMBOL_GPL(build_get_pmde64);
848
849 /*
850  * BVADDR is the faulting address, PTR is scratch.
851  * PTR will hold the pgd for vmalloc.
852  */
853 static void
854 build_get_pgd_vmalloc64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
855                         unsigned int bvaddr, unsigned int ptr,
856                         enum vmalloc64_mode mode)
857 {
858         long swpd = (long)swapper_pg_dir;
859         int single_insn_swpd;
860         int did_vmalloc_branch = 0;
861
862         single_insn_swpd = uasm_in_compat_space_p(swpd) && !uasm_rel_lo(swpd);
863
864         uasm_l_vmalloc(l, *p);
865
866         if (mode != not_refill && check_for_high_segbits) {
867                 if (single_insn_swpd) {
868                         uasm_il_bltz(p, r, bvaddr, label_vmalloc_done);
869                         uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
870                         did_vmalloc_branch = 1;
871                         /* fall through */
872                 } else {
873                         uasm_il_bgez(p, r, bvaddr, label_large_segbits_fault);
874                 }
875         }
876         if (!did_vmalloc_branch) {
877                 if (single_insn_swpd) {
878                         uasm_il_b(p, r, label_vmalloc_done);
879                         uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
880                 } else {
881                         UASM_i_LA_mostly(p, ptr, swpd);
882                         uasm_il_b(p, r, label_vmalloc_done);
883                         if (uasm_in_compat_space_p(swpd))
884                                 uasm_i_addiu(p, ptr, ptr, uasm_rel_lo(swpd));
885                         else
886                                 uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(swpd));
887                 }
888         }
889         if (mode != not_refill && check_for_high_segbits) {
890                 uasm_l_large_segbits_fault(l, *p);
891
892                 if (mode == refill_scratch && scratch_reg >= 0)
893                         uasm_i_ehb(p);
894
895                 /*
896                  * We get here if we are an xsseg address, or if we are
897                  * an xuseg address above (PGDIR_SHIFT+PGDIR_BITS) boundary.
898                  *
899                  * Ignoring xsseg (assume disabled so would generate
900                  * (address errors?), the only remaining possibility
901                  * is the upper xuseg addresses.  On processors with
902                  * TLB_SEGBITS <= PGDIR_SHIFT+PGDIR_BITS, these
903                  * addresses would have taken an address error. We try
904                  * to mimic that here by taking a load/istream page
905                  * fault.
906                  */
907                 if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
908                         uasm_i_sync(p, 0);
909                 UASM_i_LA(p, ptr, (unsigned long)tlb_do_page_fault_0);
910                 uasm_i_jr(p, ptr);
911
912                 if (mode == refill_scratch) {
913                         if (scratch_reg >= 0)
914                                 UASM_i_MFC0(p, 1, c0_kscratch(), scratch_reg);
915                         else
916                                 UASM_i_LW(p, 1, scratchpad_offset(0), 0);
917                 } else {
918                         uasm_i_nop(p);
919                 }
920         }
921 }
922
923 #else /* !CONFIG_64BIT */
924
925 /*
926  * TMP and PTR are scratch.
927  * TMP will be clobbered, PTR will hold the pgd entry.
928  */
929 void build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
930 {
931         if (pgd_reg != -1) {
932                 /* pgd is in pgd_reg */
933                 uasm_i_mfc0(p, ptr, c0_kscratch(), pgd_reg);
934                 uasm_i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
935         } else {
936                 long pgdc = (long)pgd_current;
937
938                 /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
939 #ifdef CONFIG_SMP
940                 uasm_i_mfc0(p, ptr, SMP_CPUID_REG);
941                 UASM_i_LA_mostly(p, tmp, pgdc);
942                 uasm_i_srl(p, ptr, ptr, SMP_CPUID_PTRSHIFT);
943                 uasm_i_addu(p, ptr, tmp, ptr);
944 #else
945                 UASM_i_LA_mostly(p, ptr, pgdc);
946 #endif
947                 uasm_i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
948                 uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
949         }
950         uasm_i_srl(p, tmp, tmp, PGDIR_SHIFT); /* get pgd only bits */
951         uasm_i_sll(p, tmp, tmp, PGD_T_LOG2);
952         uasm_i_addu(p, ptr, ptr, tmp); /* add in pgd offset */
953 }
954 EXPORT_SYMBOL_GPL(build_get_pgde32);
955
956 #endif /* !CONFIG_64BIT */
957
958 static void build_adjust_context(u32 **p, unsigned int ctx)
959 {
960         unsigned int shift = 4 - (PTE_T_LOG2 + 1) + PAGE_SHIFT - 12;
961         unsigned int mask = (PTRS_PER_PTE / 2 - 1) << (PTE_T_LOG2 + 1);
962
963         if (shift)
964                 UASM_i_SRL(p, ctx, ctx, shift);
965         uasm_i_andi(p, ctx, ctx, mask);
966 }
967
968 void build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
969 {
970         /*
971          * Bug workaround for the Nevada. It seems as if under certain
972          * circumstances the move from cp0_context might produce a
973          * bogus result when the mfc0 instruction and its consumer are
974          * in a different cacheline or a load instruction, probably any
975          * memory reference, is between them.
976          */
977         switch (current_cpu_type()) {
978         case CPU_NEVADA:
979                 UASM_i_LW(p, ptr, 0, ptr);
980                 GET_CONTEXT(p, tmp); /* get context reg */
981                 break;
982
983         default:
984                 GET_CONTEXT(p, tmp); /* get context reg */
985                 UASM_i_LW(p, ptr, 0, ptr);
986                 break;
987         }
988
989         build_adjust_context(p, tmp);
990         UASM_i_ADDU(p, ptr, ptr, tmp); /* add in offset */
991 }
992 EXPORT_SYMBOL_GPL(build_get_ptep);
993
994 void build_update_entries(u32 **p, unsigned int tmp, unsigned int ptep)
995 {
996         int pte_off_even = 0;
997         int pte_off_odd = sizeof(pte_t);
998
999 #if defined(CONFIG_CPU_MIPS32) && defined(CONFIG_PHYS_ADDR_T_64BIT)
1000         /* The low 32 bits of EntryLo is stored in pte_high */
1001         pte_off_even += offsetof(pte_t, pte_high);
1002         pte_off_odd += offsetof(pte_t, pte_high);
1003 #endif
1004
1005         if (IS_ENABLED(CONFIG_XPA)) {
1006                 uasm_i_lw(p, tmp, pte_off_even, ptep); /* even pte */
1007                 UASM_i_ROTR(p, tmp, tmp, ilog2(_PAGE_GLOBAL));
1008                 UASM_i_MTC0(p, tmp, C0_ENTRYLO0);
1009
1010                 if (cpu_has_xpa && !mips_xpa_disabled) {
1011                         uasm_i_lw(p, tmp, 0, ptep);
1012                         uasm_i_ext(p, tmp, tmp, 0, 24);
1013                         uasm_i_mthc0(p, tmp, C0_ENTRYLO0);
1014                 }
1015
1016                 uasm_i_lw(p, tmp, pte_off_odd, ptep); /* odd pte */
1017                 UASM_i_ROTR(p, tmp, tmp, ilog2(_PAGE_GLOBAL));
1018                 UASM_i_MTC0(p, tmp, C0_ENTRYLO1);
1019
1020                 if (cpu_has_xpa && !mips_xpa_disabled) {
1021                         uasm_i_lw(p, tmp, sizeof(pte_t), ptep);
1022                         uasm_i_ext(p, tmp, tmp, 0, 24);
1023                         uasm_i_mthc0(p, tmp, C0_ENTRYLO1);
1024                 }
1025                 return;
1026         }
1027
1028         UASM_i_LW(p, tmp, pte_off_even, ptep); /* get even pte */
1029         UASM_i_LW(p, ptep, pte_off_odd, ptep); /* get odd pte */
1030         if (r45k_bvahwbug())
1031                 build_tlb_probe_entry(p);
1032         build_convert_pte_to_entrylo(p, tmp);
1033         if (r4k_250MHZhwbug())
1034                 UASM_i_MTC0(p, 0, C0_ENTRYLO0);
1035         UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
1036         build_convert_pte_to_entrylo(p, ptep);
1037         if (r45k_bvahwbug())
1038                 uasm_i_mfc0(p, tmp, C0_INDEX);
1039         if (r4k_250MHZhwbug())
1040                 UASM_i_MTC0(p, 0, C0_ENTRYLO1);
1041         UASM_i_MTC0(p, ptep, C0_ENTRYLO1); /* load it */
1042 }
1043 EXPORT_SYMBOL_GPL(build_update_entries);
1044
1045 struct mips_huge_tlb_info {
1046         int huge_pte;
1047         int restore_scratch;
1048         bool need_reload_pte;
1049 };
1050
1051 static struct mips_huge_tlb_info
1052 build_fast_tlb_refill_handler (u32 **p, struct uasm_label **l,
1053                                struct uasm_reloc **r, unsigned int tmp,
1054                                unsigned int ptr, int c0_scratch_reg)
1055 {
1056         struct mips_huge_tlb_info rv;
1057         unsigned int even, odd;
1058         int vmalloc_branch_delay_filled = 0;
1059         const int scratch = 1; /* Our extra working register */
1060
1061         rv.huge_pte = scratch;
1062         rv.restore_scratch = 0;
1063         rv.need_reload_pte = false;
1064
1065         if (check_for_high_segbits) {
1066                 UASM_i_MFC0(p, tmp, C0_BADVADDR);
1067
1068                 if (pgd_reg != -1)
1069                         UASM_i_MFC0(p, ptr, c0_kscratch(), pgd_reg);
1070                 else
1071                         UASM_i_MFC0(p, ptr, C0_CONTEXT);
1072
1073                 if (c0_scratch_reg >= 0)
1074                         UASM_i_MTC0(p, scratch, c0_kscratch(), c0_scratch_reg);
1075                 else
1076                         UASM_i_SW(p, scratch, scratchpad_offset(0), 0);
1077
1078                 uasm_i_dsrl_safe(p, scratch, tmp,
1079                                  PGDIR_SHIFT + PGD_TABLE_ORDER + PAGE_SHIFT - 3);
1080                 uasm_il_bnez(p, r, scratch, label_vmalloc);
1081
1082                 if (pgd_reg == -1) {
1083                         vmalloc_branch_delay_filled = 1;
1084                         /* Clear lower 23 bits of context. */
1085                         uasm_i_dins(p, ptr, 0, 0, 23);
1086                 }
1087         } else {
1088                 if (pgd_reg != -1)
1089                         UASM_i_MFC0(p, ptr, c0_kscratch(), pgd_reg);
1090                 else
1091                         UASM_i_MFC0(p, ptr, C0_CONTEXT);
1092
1093                 UASM_i_MFC0(p, tmp, C0_BADVADDR);
1094
1095                 if (c0_scratch_reg >= 0)
1096                         UASM_i_MTC0(p, scratch, c0_kscratch(), c0_scratch_reg);
1097                 else
1098                         UASM_i_SW(p, scratch, scratchpad_offset(0), 0);
1099
1100                 if (pgd_reg == -1)
1101                         /* Clear lower 23 bits of context. */
1102                         uasm_i_dins(p, ptr, 0, 0, 23);
1103
1104                 uasm_il_bltz(p, r, tmp, label_vmalloc);
1105         }
1106
1107         if (pgd_reg == -1) {
1108                 vmalloc_branch_delay_filled = 1;
1109                 /* insert bit[63:59] of CAC_BASE into bit[11:6] of ptr */
1110                 uasm_i_ori(p, ptr, ptr, ((u64)(CAC_BASE) >> 53));
1111
1112                 uasm_i_drotr(p, ptr, ptr, 11);
1113         }
1114
1115 #ifdef __PAGETABLE_PMD_FOLDED
1116 #define LOC_PTEP scratch
1117 #else
1118 #define LOC_PTEP ptr
1119 #endif
1120
1121         if (!vmalloc_branch_delay_filled)
1122                 /* get pgd offset in bytes */
1123                 uasm_i_dsrl_safe(p, scratch, tmp, PGDIR_SHIFT - 3);
1124
1125         uasm_l_vmalloc_done(l, *p);
1126
1127         /*
1128          *                         tmp          ptr
1129          * fall-through case =   badvaddr  *pgd_current
1130          * vmalloc case      =   badvaddr  swapper_pg_dir
1131          */
1132
1133         if (vmalloc_branch_delay_filled)
1134                 /* get pgd offset in bytes */
1135                 uasm_i_dsrl_safe(p, scratch, tmp, PGDIR_SHIFT - 3);
1136
1137 #ifdef __PAGETABLE_PMD_FOLDED
1138         GET_CONTEXT(p, tmp); /* get context reg */
1139 #endif
1140         uasm_i_andi(p, scratch, scratch, (PTRS_PER_PGD - 1) << 3);
1141
1142         if (use_lwx_insns()) {
1143                 UASM_i_LWX(p, LOC_PTEP, scratch, ptr);
1144         } else {
1145                 uasm_i_daddu(p, ptr, ptr, scratch); /* add in pgd offset */
1146                 uasm_i_ld(p, LOC_PTEP, 0, ptr); /* get pmd pointer */
1147         }
1148
1149 #ifndef __PAGETABLE_PUD_FOLDED
1150         /* get pud offset in bytes */
1151         uasm_i_dsrl_safe(p, scratch, tmp, PUD_SHIFT - 3);
1152         uasm_i_andi(p, scratch, scratch, (PTRS_PER_PUD - 1) << 3);
1153
1154         if (use_lwx_insns()) {
1155                 UASM_i_LWX(p, ptr, scratch, ptr);
1156         } else {
1157                 uasm_i_daddu(p, ptr, ptr, scratch); /* add in pmd offset */
1158                 UASM_i_LW(p, ptr, 0, ptr);
1159         }
1160         /* ptr contains a pointer to PMD entry */
1161         /* tmp contains the address */
1162 #endif
1163
1164 #ifndef __PAGETABLE_PMD_FOLDED
1165         /* get pmd offset in bytes */
1166         uasm_i_dsrl_safe(p, scratch, tmp, PMD_SHIFT - 3);
1167         uasm_i_andi(p, scratch, scratch, (PTRS_PER_PMD - 1) << 3);
1168         GET_CONTEXT(p, tmp); /* get context reg */
1169
1170         if (use_lwx_insns()) {
1171                 UASM_i_LWX(p, scratch, scratch, ptr);
1172         } else {
1173                 uasm_i_daddu(p, ptr, ptr, scratch); /* add in pmd offset */
1174                 UASM_i_LW(p, scratch, 0, ptr);
1175         }
1176 #endif
1177         /* Adjust the context during the load latency. */
1178         build_adjust_context(p, tmp);
1179
1180 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
1181         uasm_il_bbit1(p, r, scratch, ilog2(_PAGE_HUGE), label_tlb_huge_update);
1182         /*
1183          * The in the LWX case we don't want to do the load in the
1184          * delay slot.  It cannot issue in the same cycle and may be
1185          * speculative and unneeded.
1186          */
1187         if (use_lwx_insns())
1188                 uasm_i_nop(p);
1189 #endif /* CONFIG_MIPS_HUGE_TLB_SUPPORT */
1190
1191
1192         /* build_update_entries */
1193         if (use_lwx_insns()) {
1194                 even = ptr;
1195                 odd = tmp;
1196                 UASM_i_LWX(p, even, scratch, tmp);
1197                 UASM_i_ADDIU(p, tmp, tmp, sizeof(pte_t));
1198                 UASM_i_LWX(p, odd, scratch, tmp);
1199         } else {
1200                 UASM_i_ADDU(p, ptr, scratch, tmp); /* add in offset */
1201                 even = tmp;
1202                 odd = ptr;
1203                 UASM_i_LW(p, even, 0, ptr); /* get even pte */
1204                 UASM_i_LW(p, odd, sizeof(pte_t), ptr); /* get odd pte */
1205         }
1206         if (cpu_has_rixi) {
1207                 uasm_i_drotr(p, even, even, ilog2(_PAGE_GLOBAL));
1208                 UASM_i_MTC0(p, even, C0_ENTRYLO0); /* load it */
1209                 uasm_i_drotr(p, odd, odd, ilog2(_PAGE_GLOBAL));
1210         } else {
1211                 uasm_i_dsrl_safe(p, even, even, ilog2(_PAGE_GLOBAL));
1212                 UASM_i_MTC0(p, even, C0_ENTRYLO0); /* load it */
1213                 uasm_i_dsrl_safe(p, odd, odd, ilog2(_PAGE_GLOBAL));
1214         }
1215         UASM_i_MTC0(p, odd, C0_ENTRYLO1); /* load it */
1216
1217         if (c0_scratch_reg >= 0) {
1218                 uasm_i_ehb(p);
1219                 UASM_i_MFC0(p, scratch, c0_kscratch(), c0_scratch_reg);
1220                 build_tlb_write_entry(p, l, r, tlb_random);
1221                 uasm_l_leave(l, *p);
1222                 rv.restore_scratch = 1;
1223         } else if (PAGE_SHIFT == 14 || PAGE_SHIFT == 13)  {
1224                 build_tlb_write_entry(p, l, r, tlb_random);
1225                 uasm_l_leave(l, *p);
1226                 UASM_i_LW(p, scratch, scratchpad_offset(0), 0);
1227         } else {
1228                 UASM_i_LW(p, scratch, scratchpad_offset(0), 0);
1229                 build_tlb_write_entry(p, l, r, tlb_random);
1230                 uasm_l_leave(l, *p);
1231                 rv.restore_scratch = 1;
1232         }
1233
1234         uasm_i_eret(p); /* return from trap */
1235
1236         return rv;
1237 }
1238
1239 /*
1240  * For a 64-bit kernel, we are using the 64-bit XTLB refill exception
1241  * because EXL == 0.  If we wrap, we can also use the 32 instruction
1242  * slots before the XTLB refill exception handler which belong to the
1243  * unused TLB refill exception.
1244  */
1245 #define MIPS64_REFILL_INSNS 32
1246
1247 static void build_r4000_tlb_refill_handler(void)
1248 {
1249         u32 *p = tlb_handler;
1250         struct uasm_label *l = labels;
1251         struct uasm_reloc *r = relocs;
1252         u32 *f;
1253         unsigned int final_len;
1254         struct mips_huge_tlb_info htlb_info __maybe_unused;
1255         enum vmalloc64_mode vmalloc_mode __maybe_unused;
1256
1257         memset(tlb_handler, 0, sizeof(tlb_handler));
1258         memset(labels, 0, sizeof(labels));
1259         memset(relocs, 0, sizeof(relocs));
1260         memset(final_handler, 0, sizeof(final_handler));
1261
1262         if (IS_ENABLED(CONFIG_64BIT) && (scratch_reg >= 0 || scratchpad_available()) && use_bbit_insns()) {
1263                 htlb_info = build_fast_tlb_refill_handler(&p, &l, &r, K0, K1,
1264                                                           scratch_reg);
1265                 vmalloc_mode = refill_scratch;
1266         } else {
1267                 htlb_info.huge_pte = K0;
1268                 htlb_info.restore_scratch = 0;
1269                 htlb_info.need_reload_pte = true;
1270                 vmalloc_mode = refill_noscratch;
1271                 /*
1272                  * create the plain linear handler
1273                  */
1274                 if (bcm1250_m3_war()) {
1275                         unsigned int segbits = 44;
1276
1277                         uasm_i_dmfc0(&p, K0, C0_BADVADDR);
1278                         uasm_i_dmfc0(&p, K1, C0_ENTRYHI);
1279                         uasm_i_xor(&p, K0, K0, K1);
1280                         uasm_i_dsrl_safe(&p, K1, K0, 62);
1281                         uasm_i_dsrl_safe(&p, K0, K0, 12 + 1);
1282                         uasm_i_dsll_safe(&p, K0, K0, 64 + 12 + 1 - segbits);
1283                         uasm_i_or(&p, K0, K0, K1);
1284                         uasm_il_bnez(&p, &r, K0, label_leave);
1285                         /* No need for uasm_i_nop */
1286                 }
1287
1288 #ifdef CONFIG_64BIT
1289                 build_get_pmde64(&p, &l, &r, K0, K1); /* get pmd in K1 */
1290 #else
1291                 build_get_pgde32(&p, K0, K1); /* get pgd in K1 */
1292 #endif
1293
1294 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
1295                 build_is_huge_pte(&p, &r, K0, K1, label_tlb_huge_update);
1296 #endif
1297
1298                 build_get_ptep(&p, K0, K1);
1299                 build_update_entries(&p, K0, K1);
1300                 build_tlb_write_entry(&p, &l, &r, tlb_random);
1301                 uasm_l_leave(&l, p);
1302                 uasm_i_eret(&p); /* return from trap */
1303         }
1304 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
1305         uasm_l_tlb_huge_update(&l, p);
1306         if (htlb_info.need_reload_pte)
1307                 UASM_i_LW(&p, htlb_info.huge_pte, 0, K1);
1308         build_huge_update_entries(&p, htlb_info.huge_pte, K1);
1309         build_huge_tlb_write_entry(&p, &l, &r, K0, tlb_random,
1310                                    htlb_info.restore_scratch);
1311 #endif
1312
1313 #ifdef CONFIG_64BIT
1314         build_get_pgd_vmalloc64(&p, &l, &r, K0, K1, vmalloc_mode);
1315 #endif
1316
1317         /*
1318          * Overflow check: For the 64bit handler, we need at least one
1319          * free instruction slot for the wrap-around branch. In worst
1320          * case, if the intended insertion point is a delay slot, we
1321          * need three, with the second nop'ed and the third being
1322          * unused.
1323          */
1324         switch (boot_cpu_type()) {
1325         default:
1326                 if (sizeof(long) == 4) {
1327                 fallthrough;
1328         case CPU_LOONGSON2EF:
1329                 /* Loongson2 ebase is different than r4k, we have more space */
1330                         if ((p - tlb_handler) > 64)
1331                                 panic("TLB refill handler space exceeded");
1332                         /*
1333                          * Now fold the handler in the TLB refill handler space.
1334                          */
1335                         f = final_handler;
1336                         /* Simplest case, just copy the handler. */
1337                         uasm_copy_handler(relocs, labels, tlb_handler, p, f);
1338                         final_len = p - tlb_handler;
1339                         break;
1340                 } else {
1341                         if (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 1)
1342                             || (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 3)
1343                                 && uasm_insn_has_bdelay(relocs,
1344                                                         tlb_handler + MIPS64_REFILL_INSNS - 3)))
1345                                 panic("TLB refill handler space exceeded");
1346                         /*
1347                          * Now fold the handler in the TLB refill handler space.
1348                          */
1349                         f = final_handler + MIPS64_REFILL_INSNS;
1350                         if ((p - tlb_handler) <= MIPS64_REFILL_INSNS) {
1351                                 /* Just copy the handler. */
1352                                 uasm_copy_handler(relocs, labels, tlb_handler, p, f);
1353                                 final_len = p - tlb_handler;
1354                         } else {
1355 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
1356                                 const enum label_id ls = label_tlb_huge_update;
1357 #else
1358                                 const enum label_id ls = label_vmalloc;
1359 #endif
1360                                 u32 *split;
1361                                 int ov = 0;
1362                                 int i;
1363
1364                                 for (i = 0; i < ARRAY_SIZE(labels) && labels[i].lab != ls; i++)
1365                                         ;
1366                                 BUG_ON(i == ARRAY_SIZE(labels));
1367                                 split = labels[i].addr;
1368
1369                                 /*
1370                                  * See if we have overflown one way or the other.
1371                                  */
1372                                 if (split > tlb_handler + MIPS64_REFILL_INSNS ||
1373                                     split < p - MIPS64_REFILL_INSNS)
1374                                         ov = 1;
1375
1376                                 if (ov) {
1377                                         /*
1378                                          * Split two instructions before the end.  One
1379                                          * for the branch and one for the instruction
1380                                          * in the delay slot.
1381                                          */
1382                                         split = tlb_handler + MIPS64_REFILL_INSNS - 2;
1383
1384                                         /*
1385                                          * If the branch would fall in a delay slot,
1386                                          * we must back up an additional instruction
1387                                          * so that it is no longer in a delay slot.
1388                                          */
1389                                         if (uasm_insn_has_bdelay(relocs, split - 1))
1390                                                 split--;
1391                                 }
1392                                 /* Copy first part of the handler. */
1393                                 uasm_copy_handler(relocs, labels, tlb_handler, split, f);
1394                                 f += split - tlb_handler;
1395
1396                                 if (ov) {
1397                                         /* Insert branch. */
1398                                         uasm_l_split(&l, final_handler);
1399                                         uasm_il_b(&f, &r, label_split);
1400                                         if (uasm_insn_has_bdelay(relocs, split))
1401                                                 uasm_i_nop(&f);
1402                                         else {
1403                                                 uasm_copy_handler(relocs, labels,
1404                                                                   split, split + 1, f);
1405                                                 uasm_move_labels(labels, f, f + 1, -1);
1406                                                 f++;
1407                                                 split++;
1408                                         }
1409                                 }
1410
1411                                 /* Copy the rest of the handler. */
1412                                 uasm_copy_handler(relocs, labels, split, p, final_handler);
1413                                 final_len = (f - (final_handler + MIPS64_REFILL_INSNS)) +
1414                                             (p - split);
1415                         }
1416                 }
1417                 break;
1418         }
1419
1420         uasm_resolve_relocs(relocs, labels);
1421         pr_debug("Wrote TLB refill handler (%u instructions).\n",
1422                  final_len);
1423
1424         memcpy((void *)ebase, final_handler, 0x100);
1425         local_flush_icache_range(ebase, ebase + 0x100);
1426         dump_handler("r4000_tlb_refill", (u32 *)ebase, (u32 *)(ebase + 0x100));
1427 }
1428
1429 static void setup_pw(void)
1430 {
1431         unsigned int pwctl;
1432         unsigned long pgd_i, pgd_w;
1433 #ifndef __PAGETABLE_PMD_FOLDED
1434         unsigned long pmd_i, pmd_w;
1435 #endif
1436         unsigned long pt_i, pt_w;
1437         unsigned long pte_i, pte_w;
1438 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
1439         unsigned long psn;
1440
1441         psn = ilog2(_PAGE_HUGE);     /* bit used to indicate huge page */
1442 #endif
1443         pgd_i = PGDIR_SHIFT;  /* 1st level PGD */
1444 #ifndef __PAGETABLE_PMD_FOLDED
1445         pgd_w = PGDIR_SHIFT - PMD_SHIFT + PGD_TABLE_ORDER;
1446
1447         pmd_i = PMD_SHIFT;    /* 2nd level PMD */
1448         pmd_w = PMD_SHIFT - PAGE_SHIFT;
1449 #else
1450         pgd_w = PGDIR_SHIFT - PAGE_SHIFT + PGD_TABLE_ORDER;
1451 #endif
1452
1453         pt_i  = PAGE_SHIFT;    /* 3rd level PTE */
1454         pt_w  = PAGE_SHIFT - 3;
1455
1456         pte_i = ilog2(_PAGE_GLOBAL);
1457         pte_w = 0;
1458         pwctl = 1 << 30; /* Set PWDirExt */
1459
1460 #ifndef __PAGETABLE_PMD_FOLDED
1461         write_c0_pwfield(pgd_i << 24 | pmd_i << 12 | pt_i << 6 | pte_i);
1462         write_c0_pwsize(1 << 30 | pgd_w << 24 | pmd_w << 12 | pt_w << 6 | pte_w);
1463 #else
1464         write_c0_pwfield(pgd_i << 24 | pt_i << 6 | pte_i);
1465         write_c0_pwsize(1 << 30 | pgd_w << 24 | pt_w << 6 | pte_w);
1466 #endif
1467
1468 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
1469         pwctl |= (1 << 6 | psn);
1470 #endif
1471         write_c0_pwctl(pwctl);
1472         write_c0_kpgd((long)swapper_pg_dir);
1473         kscratch_used_mask |= (1 << 7); /* KScratch6 is used for KPGD */
1474 }
1475
1476 static void build_loongson3_tlb_refill_handler(void)
1477 {
1478         u32 *p = tlb_handler;
1479         struct uasm_label *l = labels;
1480         struct uasm_reloc *r = relocs;
1481
1482         memset(labels, 0, sizeof(labels));
1483         memset(relocs, 0, sizeof(relocs));
1484         memset(tlb_handler, 0, sizeof(tlb_handler));
1485
1486         if (check_for_high_segbits) {
1487                 uasm_i_dmfc0(&p, K0, C0_BADVADDR);
1488                 uasm_i_dsrl_safe(&p, K1, K0, PGDIR_SHIFT + PGD_TABLE_ORDER + PAGE_SHIFT - 3);
1489                 uasm_il_beqz(&p, &r, K1, label_vmalloc);
1490                 uasm_i_nop(&p);
1491
1492                 uasm_il_bgez(&p, &r, K0, label_large_segbits_fault);
1493                 uasm_i_nop(&p);
1494                 uasm_l_vmalloc(&l, p);
1495         }
1496
1497         uasm_i_dmfc0(&p, K1, C0_PGD);
1498
1499         uasm_i_lddir(&p, K0, K1, 3);  /* global page dir */
1500 #ifndef __PAGETABLE_PMD_FOLDED
1501         uasm_i_lddir(&p, K1, K0, 1);  /* middle page dir */
1502 #endif
1503         uasm_i_ldpte(&p, K1, 0);      /* even */
1504         uasm_i_ldpte(&p, K1, 1);      /* odd */
1505         uasm_i_tlbwr(&p);
1506
1507         /* restore page mask */
1508         if (PM_DEFAULT_MASK >> 16) {
1509                 uasm_i_lui(&p, K0, PM_DEFAULT_MASK >> 16);
1510                 uasm_i_ori(&p, K0, K0, PM_DEFAULT_MASK & 0xffff);
1511                 uasm_i_mtc0(&p, K0, C0_PAGEMASK);
1512         } else if (PM_DEFAULT_MASK) {
1513                 uasm_i_ori(&p, K0, 0, PM_DEFAULT_MASK);
1514                 uasm_i_mtc0(&p, K0, C0_PAGEMASK);
1515         } else {
1516                 uasm_i_mtc0(&p, 0, C0_PAGEMASK);
1517         }
1518
1519         uasm_i_eret(&p);
1520
1521         if (check_for_high_segbits) {
1522                 uasm_l_large_segbits_fault(&l, p);
1523                 UASM_i_LA(&p, K1, (unsigned long)tlb_do_page_fault_0);
1524                 uasm_i_jr(&p, K1);
1525                 uasm_i_nop(&p);
1526         }
1527
1528         uasm_resolve_relocs(relocs, labels);
1529         memcpy((void *)(ebase + 0x80), tlb_handler, 0x80);
1530         local_flush_icache_range(ebase + 0x80, ebase + 0x100);
1531         dump_handler("loongson3_tlb_refill",
1532                      (u32 *)(ebase + 0x80), (u32 *)(ebase + 0x100));
1533 }
1534
1535 static void build_setup_pgd(void)
1536 {
1537         const int a0 = 4;
1538         const int __maybe_unused a1 = 5;
1539         const int __maybe_unused a2 = 6;
1540         u32 *p = (u32 *)msk_isa16_mode((ulong)tlbmiss_handler_setup_pgd);
1541 #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
1542         long pgdc = (long)pgd_current;
1543 #endif
1544
1545         memset(p, 0, tlbmiss_handler_setup_pgd_end - (char *)p);
1546         memset(labels, 0, sizeof(labels));
1547         memset(relocs, 0, sizeof(relocs));
1548         pgd_reg = allocate_kscratch();
1549 #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
1550         if (pgd_reg == -1) {
1551                 struct uasm_label *l = labels;
1552                 struct uasm_reloc *r = relocs;
1553
1554                 /* PGD << 11 in c0_Context */
1555                 /*
1556                  * If it is a ckseg0 address, convert to a physical
1557                  * address.  Shifting right by 29 and adding 4 will
1558                  * result in zero for these addresses.
1559                  *
1560                  */
1561                 UASM_i_SRA(&p, a1, a0, 29);
1562                 UASM_i_ADDIU(&p, a1, a1, 4);
1563                 uasm_il_bnez(&p, &r, a1, label_tlbl_goaround1);
1564                 uasm_i_nop(&p);
1565                 uasm_i_dinsm(&p, a0, 0, 29, 64 - 29);
1566                 uasm_l_tlbl_goaround1(&l, p);
1567                 UASM_i_SLL(&p, a0, a0, 11);
1568                 UASM_i_MTC0(&p, a0, C0_CONTEXT);
1569                 uasm_i_jr(&p, 31);
1570                 uasm_i_ehb(&p);
1571         } else {
1572                 /* PGD in c0_KScratch */
1573                 if (cpu_has_ldpte)
1574                         UASM_i_MTC0(&p, a0, C0_PWBASE);
1575                 else
1576                         UASM_i_MTC0(&p, a0, c0_kscratch(), pgd_reg);
1577                 uasm_i_jr(&p, 31);
1578                 uasm_i_ehb(&p);
1579         }
1580 #else
1581 #ifdef CONFIG_SMP
1582         /* Save PGD to pgd_current[smp_processor_id()] */
1583         UASM_i_CPUID_MFC0(&p, a1, SMP_CPUID_REG);
1584         UASM_i_SRL_SAFE(&p, a1, a1, SMP_CPUID_PTRSHIFT);
1585         UASM_i_LA_mostly(&p, a2, pgdc);
1586         UASM_i_ADDU(&p, a2, a2, a1);
1587         UASM_i_SW(&p, a0, uasm_rel_lo(pgdc), a2);
1588 #else
1589         UASM_i_LA_mostly(&p, a2, pgdc);
1590         UASM_i_SW(&p, a0, uasm_rel_lo(pgdc), a2);
1591 #endif /* SMP */
1592
1593         /* if pgd_reg is allocated, save PGD also to scratch register */
1594         if (pgd_reg != -1) {
1595                 UASM_i_MTC0(&p, a0, c0_kscratch(), pgd_reg);
1596                 uasm_i_jr(&p, 31);
1597                 uasm_i_ehb(&p);
1598         } else {
1599                 uasm_i_jr(&p, 31);
1600                 uasm_i_nop(&p);
1601         }
1602 #endif
1603         if (p >= (u32 *)tlbmiss_handler_setup_pgd_end)
1604                 panic("tlbmiss_handler_setup_pgd space exceeded");
1605
1606         uasm_resolve_relocs(relocs, labels);
1607         pr_debug("Wrote tlbmiss_handler_setup_pgd (%u instructions).\n",
1608                  (unsigned int)(p - (u32 *)tlbmiss_handler_setup_pgd));
1609
1610         dump_handler("tlbmiss_handler", tlbmiss_handler_setup_pgd,
1611                                         tlbmiss_handler_setup_pgd_end);
1612 }
1613
1614 static void
1615 iPTE_LW(u32 **p, unsigned int pte, unsigned int ptr)
1616 {
1617 #ifdef CONFIG_SMP
1618         if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
1619                 uasm_i_sync(p, 0);
1620 # ifdef CONFIG_PHYS_ADDR_T_64BIT
1621         if (cpu_has_64bits)
1622                 uasm_i_lld(p, pte, 0, ptr);
1623         else
1624 # endif
1625                 UASM_i_LL(p, pte, 0, ptr);
1626 #else
1627 # ifdef CONFIG_PHYS_ADDR_T_64BIT
1628         if (cpu_has_64bits)
1629                 uasm_i_ld(p, pte, 0, ptr);
1630         else
1631 # endif
1632                 UASM_i_LW(p, pte, 0, ptr);
1633 #endif
1634 }
1635
1636 static void
1637 iPTE_SW(u32 **p, struct uasm_reloc **r, unsigned int pte, unsigned int ptr,
1638         unsigned int mode, unsigned int scratch)
1639 {
1640         unsigned int hwmode = mode & (_PAGE_VALID | _PAGE_DIRTY);
1641         unsigned int swmode = mode & ~hwmode;
1642
1643         if (IS_ENABLED(CONFIG_XPA) && !cpu_has_64bits) {
1644                 uasm_i_lui(p, scratch, swmode >> 16);
1645                 uasm_i_or(p, pte, pte, scratch);
1646                 BUG_ON(swmode & 0xffff);
1647         } else {
1648                 uasm_i_ori(p, pte, pte, mode);
1649         }
1650
1651 #ifdef CONFIG_SMP
1652 # ifdef CONFIG_PHYS_ADDR_T_64BIT
1653         if (cpu_has_64bits)
1654                 uasm_i_scd(p, pte, 0, ptr);
1655         else
1656 # endif
1657                 UASM_i_SC(p, pte, 0, ptr);
1658
1659         if (r10000_llsc_war())
1660                 uasm_il_beqzl(p, r, pte, label_smp_pgtable_change);
1661         else
1662                 uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
1663
1664 # ifdef CONFIG_PHYS_ADDR_T_64BIT
1665         if (!cpu_has_64bits) {
1666                 /* no uasm_i_nop needed */
1667                 uasm_i_ll(p, pte, sizeof(pte_t) / 2, ptr);
1668                 uasm_i_ori(p, pte, pte, hwmode);
1669                 BUG_ON(hwmode & ~0xffff);
1670                 uasm_i_sc(p, pte, sizeof(pte_t) / 2, ptr);
1671                 uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
1672                 /* no uasm_i_nop needed */
1673                 uasm_i_lw(p, pte, 0, ptr);
1674         } else
1675                 uasm_i_nop(p);
1676 # else
1677         uasm_i_nop(p);
1678 # endif
1679 #else
1680 # ifdef CONFIG_PHYS_ADDR_T_64BIT
1681         if (cpu_has_64bits)
1682                 uasm_i_sd(p, pte, 0, ptr);
1683         else
1684 # endif
1685                 UASM_i_SW(p, pte, 0, ptr);
1686
1687 # ifdef CONFIG_PHYS_ADDR_T_64BIT
1688         if (!cpu_has_64bits) {
1689                 uasm_i_lw(p, pte, sizeof(pte_t) / 2, ptr);
1690                 uasm_i_ori(p, pte, pte, hwmode);
1691                 BUG_ON(hwmode & ~0xffff);
1692                 uasm_i_sw(p, pte, sizeof(pte_t) / 2, ptr);
1693                 uasm_i_lw(p, pte, 0, ptr);
1694         }
1695 # endif
1696 #endif
1697 }
1698
1699 /*
1700  * Check if PTE is present, if not then jump to LABEL. PTR points to
1701  * the page table where this PTE is located, PTE will be re-loaded
1702  * with its original value.
1703  */
1704 static void
1705 build_pte_present(u32 **p, struct uasm_reloc **r,
1706                   int pte, int ptr, int scratch, enum label_id lid)
1707 {
1708         int t = scratch >= 0 ? scratch : pte;
1709         int cur = pte;
1710
1711         if (cpu_has_rixi) {
1712                 if (use_bbit_insns()) {
1713                         uasm_il_bbit0(p, r, pte, ilog2(_PAGE_PRESENT), lid);
1714                         uasm_i_nop(p);
1715                 } else {
1716                         if (_PAGE_PRESENT_SHIFT) {
1717                                 uasm_i_srl(p, t, cur, _PAGE_PRESENT_SHIFT);
1718                                 cur = t;
1719                         }
1720                         uasm_i_andi(p, t, cur, 1);
1721                         uasm_il_beqz(p, r, t, lid);
1722                         if (pte == t)
1723                                 /* You lose the SMP race :-(*/
1724                                 iPTE_LW(p, pte, ptr);
1725                 }
1726         } else {
1727                 if (_PAGE_PRESENT_SHIFT) {
1728                         uasm_i_srl(p, t, cur, _PAGE_PRESENT_SHIFT);
1729                         cur = t;
1730                 }
1731                 uasm_i_andi(p, t, cur,
1732                         (_PAGE_PRESENT | _PAGE_NO_READ) >> _PAGE_PRESENT_SHIFT);
1733                 uasm_i_xori(p, t, t, _PAGE_PRESENT >> _PAGE_PRESENT_SHIFT);
1734                 uasm_il_bnez(p, r, t, lid);
1735                 if (pte == t)
1736                         /* You lose the SMP race :-(*/
1737                         iPTE_LW(p, pte, ptr);
1738         }
1739 }
1740
1741 /* Make PTE valid, store result in PTR. */
1742 static void
1743 build_make_valid(u32 **p, struct uasm_reloc **r, unsigned int pte,
1744                  unsigned int ptr, unsigned int scratch)
1745 {
1746         unsigned int mode = _PAGE_VALID | _PAGE_ACCESSED;
1747
1748         iPTE_SW(p, r, pte, ptr, mode, scratch);
1749 }
1750
1751 /*
1752  * Check if PTE can be written to, if not branch to LABEL. Regardless
1753  * restore PTE with value from PTR when done.
1754  */
1755 static void
1756 build_pte_writable(u32 **p, struct uasm_reloc **r,
1757                    unsigned int pte, unsigned int ptr, int scratch,
1758                    enum label_id lid)
1759 {
1760         int t = scratch >= 0 ? scratch : pte;
1761         int cur = pte;
1762
1763         if (_PAGE_PRESENT_SHIFT) {
1764                 uasm_i_srl(p, t, cur, _PAGE_PRESENT_SHIFT);
1765                 cur = t;
1766         }
1767         uasm_i_andi(p, t, cur,
1768                     (_PAGE_PRESENT | _PAGE_WRITE) >> _PAGE_PRESENT_SHIFT);
1769         uasm_i_xori(p, t, t,
1770                     (_PAGE_PRESENT | _PAGE_WRITE) >> _PAGE_PRESENT_SHIFT);
1771         uasm_il_bnez(p, r, t, lid);
1772         if (pte == t)
1773                 /* You lose the SMP race :-(*/
1774                 iPTE_LW(p, pte, ptr);
1775         else
1776                 uasm_i_nop(p);
1777 }
1778
1779 /* Make PTE writable, update software status bits as well, then store
1780  * at PTR.
1781  */
1782 static void
1783 build_make_write(u32 **p, struct uasm_reloc **r, unsigned int pte,
1784                  unsigned int ptr, unsigned int scratch)
1785 {
1786         unsigned int mode = (_PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID
1787                              | _PAGE_DIRTY);
1788
1789         iPTE_SW(p, r, pte, ptr, mode, scratch);
1790 }
1791
1792 /*
1793  * Check if PTE can be modified, if not branch to LABEL. Regardless
1794  * restore PTE with value from PTR when done.
1795  */
1796 static void
1797 build_pte_modifiable(u32 **p, struct uasm_reloc **r,
1798                      unsigned int pte, unsigned int ptr, int scratch,
1799                      enum label_id lid)
1800 {
1801         if (use_bbit_insns()) {
1802                 uasm_il_bbit0(p, r, pte, ilog2(_PAGE_WRITE), lid);
1803                 uasm_i_nop(p);
1804         } else {
1805                 int t = scratch >= 0 ? scratch : pte;
1806                 uasm_i_srl(p, t, pte, _PAGE_WRITE_SHIFT);
1807                 uasm_i_andi(p, t, t, 1);
1808                 uasm_il_beqz(p, r, t, lid);
1809                 if (pte == t)
1810                         /* You lose the SMP race :-(*/
1811                         iPTE_LW(p, pte, ptr);
1812         }
1813 }
1814
1815 #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
1816
1817
1818 /*
1819  * R3000 style TLB load/store/modify handlers.
1820  */
1821
1822 /*
1823  * This places the pte into ENTRYLO0 and writes it with tlbwi.
1824  * Then it returns.
1825  */
1826 static void
1827 build_r3000_pte_reload_tlbwi(u32 **p, unsigned int pte, unsigned int tmp)
1828 {
1829         uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
1830         uasm_i_mfc0(p, tmp, C0_EPC); /* cp0 delay */
1831         uasm_i_tlbwi(p);
1832         uasm_i_jr(p, tmp);
1833         uasm_i_rfe(p); /* branch delay */
1834 }
1835
1836 /*
1837  * This places the pte into ENTRYLO0 and writes it with tlbwi
1838  * or tlbwr as appropriate.  This is because the index register
1839  * may have the probe fail bit set as a result of a trap on a
1840  * kseg2 access, i.e. without refill.  Then it returns.
1841  */
1842 static void
1843 build_r3000_tlb_reload_write(u32 **p, struct uasm_label **l,
1844                              struct uasm_reloc **r, unsigned int pte,
1845                              unsigned int tmp)
1846 {
1847         uasm_i_mfc0(p, tmp, C0_INDEX);
1848         uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
1849         uasm_il_bltz(p, r, tmp, label_r3000_write_probe_fail); /* cp0 delay */
1850         uasm_i_mfc0(p, tmp, C0_EPC); /* branch delay */
1851         uasm_i_tlbwi(p); /* cp0 delay */
1852         uasm_i_jr(p, tmp);
1853         uasm_i_rfe(p); /* branch delay */
1854         uasm_l_r3000_write_probe_fail(l, *p);
1855         uasm_i_tlbwr(p); /* cp0 delay */
1856         uasm_i_jr(p, tmp);
1857         uasm_i_rfe(p); /* branch delay */
1858 }
1859
1860 static void
1861 build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
1862                                    unsigned int ptr)
1863 {
1864         long pgdc = (long)pgd_current;
1865
1866         uasm_i_mfc0(p, pte, C0_BADVADDR);
1867         uasm_i_lui(p, ptr, uasm_rel_hi(pgdc)); /* cp0 delay */
1868         uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
1869         uasm_i_srl(p, pte, pte, 22); /* load delay */
1870         uasm_i_sll(p, pte, pte, 2);
1871         uasm_i_addu(p, ptr, ptr, pte);
1872         uasm_i_mfc0(p, pte, C0_CONTEXT);
1873         uasm_i_lw(p, ptr, 0, ptr); /* cp0 delay */
1874         uasm_i_andi(p, pte, pte, 0xffc); /* load delay */
1875         uasm_i_addu(p, ptr, ptr, pte);
1876         uasm_i_lw(p, pte, 0, ptr);
1877         uasm_i_tlbp(p); /* load delay */
1878 }
1879
1880 static void build_r3000_tlb_load_handler(void)
1881 {
1882         u32 *p = (u32 *)handle_tlbl;
1883         struct uasm_label *l = labels;
1884         struct uasm_reloc *r = relocs;
1885
1886         memset(p, 0, handle_tlbl_end - (char *)p);
1887         memset(labels, 0, sizeof(labels));
1888         memset(relocs, 0, sizeof(relocs));
1889
1890         build_r3000_tlbchange_handler_head(&p, K0, K1);
1891         build_pte_present(&p, &r, K0, K1, -1, label_nopage_tlbl);
1892         uasm_i_nop(&p); /* load delay */
1893         build_make_valid(&p, &r, K0, K1, -1);
1894         build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
1895
1896         uasm_l_nopage_tlbl(&l, p);
1897         uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
1898         uasm_i_nop(&p);
1899
1900         if (p >= (u32 *)handle_tlbl_end)
1901                 panic("TLB load handler fastpath space exceeded");
1902
1903         uasm_resolve_relocs(relocs, labels);
1904         pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
1905                  (unsigned int)(p - (u32 *)handle_tlbl));
1906
1907         dump_handler("r3000_tlb_load", handle_tlbl, handle_tlbl_end);
1908 }
1909
1910 static void build_r3000_tlb_store_handler(void)
1911 {
1912         u32 *p = (u32 *)handle_tlbs;
1913         struct uasm_label *l = labels;
1914         struct uasm_reloc *r = relocs;
1915
1916         memset(p, 0, handle_tlbs_end - (char *)p);
1917         memset(labels, 0, sizeof(labels));
1918         memset(relocs, 0, sizeof(relocs));
1919
1920         build_r3000_tlbchange_handler_head(&p, K0, K1);
1921         build_pte_writable(&p, &r, K0, K1, -1, label_nopage_tlbs);
1922         uasm_i_nop(&p); /* load delay */
1923         build_make_write(&p, &r, K0, K1, -1);
1924         build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
1925
1926         uasm_l_nopage_tlbs(&l, p);
1927         uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1928         uasm_i_nop(&p);
1929
1930         if (p >= (u32 *)handle_tlbs_end)
1931                 panic("TLB store handler fastpath space exceeded");
1932
1933         uasm_resolve_relocs(relocs, labels);
1934         pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
1935                  (unsigned int)(p - (u32 *)handle_tlbs));
1936
1937         dump_handler("r3000_tlb_store", handle_tlbs, handle_tlbs_end);
1938 }
1939
1940 static void build_r3000_tlb_modify_handler(void)
1941 {
1942         u32 *p = (u32 *)handle_tlbm;
1943         struct uasm_label *l = labels;
1944         struct uasm_reloc *r = relocs;
1945
1946         memset(p, 0, handle_tlbm_end - (char *)p);
1947         memset(labels, 0, sizeof(labels));
1948         memset(relocs, 0, sizeof(relocs));
1949
1950         build_r3000_tlbchange_handler_head(&p, K0, K1);
1951         build_pte_modifiable(&p, &r, K0, K1,  -1, label_nopage_tlbm);
1952         uasm_i_nop(&p); /* load delay */
1953         build_make_write(&p, &r, K0, K1, -1);
1954         build_r3000_pte_reload_tlbwi(&p, K0, K1);
1955
1956         uasm_l_nopage_tlbm(&l, p);
1957         uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1958         uasm_i_nop(&p);
1959
1960         if (p >= (u32 *)handle_tlbm_end)
1961                 panic("TLB modify handler fastpath space exceeded");
1962
1963         uasm_resolve_relocs(relocs, labels);
1964         pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
1965                  (unsigned int)(p - (u32 *)handle_tlbm));
1966
1967         dump_handler("r3000_tlb_modify", handle_tlbm, handle_tlbm_end);
1968 }
1969 #endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
1970
1971 static bool cpu_has_tlbex_tlbp_race(void)
1972 {
1973         /*
1974          * When a Hardware Table Walker is running it can replace TLB entries
1975          * at any time, leading to a race between it & the CPU.
1976          */
1977         if (cpu_has_htw)
1978                 return true;
1979
1980         /*
1981          * If the CPU shares FTLB RAM with its siblings then our entry may be
1982          * replaced at any time by a sibling performing a write to the FTLB.
1983          */
1984         if (cpu_has_shared_ftlb_ram)
1985                 return true;
1986
1987         /* In all other cases there ought to be no race condition to handle */
1988         return false;
1989 }
1990
1991 /*
1992  * R4000 style TLB load/store/modify handlers.
1993  */
1994 static struct work_registers
1995 build_r4000_tlbchange_handler_head(u32 **p, struct uasm_label **l,
1996                                    struct uasm_reloc **r)
1997 {
1998         struct work_registers wr = build_get_work_registers(p);
1999
2000 #ifdef CONFIG_64BIT
2001         build_get_pmde64(p, l, r, wr.r1, wr.r2); /* get pmd in ptr */
2002 #else
2003         build_get_pgde32(p, wr.r1, wr.r2); /* get pgd in ptr */
2004 #endif
2005
2006 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
2007         /*
2008          * For huge tlb entries, pmd doesn't contain an address but
2009          * instead contains the tlb pte. Check the PAGE_HUGE bit and
2010          * see if we need to jump to huge tlb processing.
2011          */
2012         build_is_huge_pte(p, r, wr.r1, wr.r2, label_tlb_huge_update);
2013 #endif
2014
2015         UASM_i_MFC0(p, wr.r1, C0_BADVADDR);
2016         UASM_i_LW(p, wr.r2, 0, wr.r2);
2017         UASM_i_SRL(p, wr.r1, wr.r1, PAGE_SHIFT - PTE_T_LOG2);
2018         uasm_i_andi(p, wr.r1, wr.r1, (PTRS_PER_PTE - 1) << PTE_T_LOG2);
2019         UASM_i_ADDU(p, wr.r2, wr.r2, wr.r1);
2020
2021 #ifdef CONFIG_SMP
2022         uasm_l_smp_pgtable_change(l, *p);
2023 #endif
2024         iPTE_LW(p, wr.r1, wr.r2); /* get even pte */
2025         if (!m4kc_tlbp_war()) {
2026                 build_tlb_probe_entry(p);
2027                 if (cpu_has_tlbex_tlbp_race()) {
2028                         /* race condition happens, leaving */
2029                         uasm_i_ehb(p);
2030                         uasm_i_mfc0(p, wr.r3, C0_INDEX);
2031                         uasm_il_bltz(p, r, wr.r3, label_leave);
2032                         uasm_i_nop(p);
2033                 }
2034         }
2035         return wr;
2036 }
2037
2038 static void
2039 build_r4000_tlbchange_handler_tail(u32 **p, struct uasm_label **l,
2040                                    struct uasm_reloc **r, unsigned int tmp,
2041                                    unsigned int ptr)
2042 {
2043         uasm_i_ori(p, ptr, ptr, sizeof(pte_t));
2044         uasm_i_xori(p, ptr, ptr, sizeof(pte_t));
2045         build_update_entries(p, tmp, ptr);
2046         build_tlb_write_entry(p, l, r, tlb_indexed);
2047         uasm_l_leave(l, *p);
2048         build_restore_work_registers(p);
2049         uasm_i_eret(p); /* return from trap */
2050
2051 #ifdef CONFIG_64BIT
2052         build_get_pgd_vmalloc64(p, l, r, tmp, ptr, not_refill);
2053 #endif
2054 }
2055
2056 static void build_r4000_tlb_load_handler(void)
2057 {
2058         u32 *p = (u32 *)msk_isa16_mode((ulong)handle_tlbl);
2059         struct uasm_label *l = labels;
2060         struct uasm_reloc *r = relocs;
2061         struct work_registers wr;
2062
2063         memset(p, 0, handle_tlbl_end - (char *)p);
2064         memset(labels, 0, sizeof(labels));
2065         memset(relocs, 0, sizeof(relocs));
2066
2067         if (bcm1250_m3_war()) {
2068                 unsigned int segbits = 44;
2069
2070                 uasm_i_dmfc0(&p, K0, C0_BADVADDR);
2071                 uasm_i_dmfc0(&p, K1, C0_ENTRYHI);
2072                 uasm_i_xor(&p, K0, K0, K1);
2073                 uasm_i_dsrl_safe(&p, K1, K0, 62);
2074                 uasm_i_dsrl_safe(&p, K0, K0, 12 + 1);
2075                 uasm_i_dsll_safe(&p, K0, K0, 64 + 12 + 1 - segbits);
2076                 uasm_i_or(&p, K0, K0, K1);
2077                 uasm_il_bnez(&p, &r, K0, label_leave);
2078                 /* No need for uasm_i_nop */
2079         }
2080
2081         wr = build_r4000_tlbchange_handler_head(&p, &l, &r);
2082         build_pte_present(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbl);
2083         if (m4kc_tlbp_war())
2084                 build_tlb_probe_entry(&p);
2085
2086         if (cpu_has_rixi && !cpu_has_rixiex) {
2087                 /*
2088                  * If the page is not _PAGE_VALID, RI or XI could not
2089                  * have triggered it.  Skip the expensive test..
2090                  */
2091                 if (use_bbit_insns()) {
2092                         uasm_il_bbit0(&p, &r, wr.r1, ilog2(_PAGE_VALID),
2093                                       label_tlbl_goaround1);
2094                 } else {
2095                         uasm_i_andi(&p, wr.r3, wr.r1, _PAGE_VALID);
2096                         uasm_il_beqz(&p, &r, wr.r3, label_tlbl_goaround1);
2097                 }
2098                 uasm_i_nop(&p);
2099
2100                 /*
2101                  * Warn if something may race with us & replace the TLB entry
2102                  * before we read it here. Everything with such races should
2103                  * also have dedicated RiXi exception handlers, so this
2104                  * shouldn't be hit.
2105                  */
2106                 WARN(cpu_has_tlbex_tlbp_race(), "Unhandled race in RiXi path");
2107
2108                 uasm_i_tlbr(&p);
2109
2110                 if (cpu_has_mips_r2_exec_hazard)
2111                         uasm_i_ehb(&p);
2112
2113                 /* Examine  entrylo 0 or 1 based on ptr. */
2114                 if (use_bbit_insns()) {
2115                         uasm_i_bbit0(&p, wr.r2, ilog2(sizeof(pte_t)), 8);
2116                 } else {
2117                         uasm_i_andi(&p, wr.r3, wr.r2, sizeof(pte_t));
2118                         uasm_i_beqz(&p, wr.r3, 8);
2119                 }
2120                 /* load it in the delay slot*/
2121                 UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO0);
2122                 /* load it if ptr is odd */
2123                 UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO1);
2124                 /*
2125                  * If the entryLo (now in wr.r3) is valid (bit 1), RI or
2126                  * XI must have triggered it.
2127                  */
2128                 if (use_bbit_insns()) {
2129                         uasm_il_bbit1(&p, &r, wr.r3, 1, label_nopage_tlbl);
2130                         uasm_i_nop(&p);
2131                         uasm_l_tlbl_goaround1(&l, p);
2132                 } else {
2133                         uasm_i_andi(&p, wr.r3, wr.r3, 2);
2134                         uasm_il_bnez(&p, &r, wr.r3, label_nopage_tlbl);
2135                         uasm_i_nop(&p);
2136                 }
2137                 uasm_l_tlbl_goaround1(&l, p);
2138         }
2139         build_make_valid(&p, &r, wr.r1, wr.r2, wr.r3);
2140         build_r4000_tlbchange_handler_tail(&p, &l, &r, wr.r1, wr.r2);
2141
2142 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
2143         /*
2144          * This is the entry point when build_r4000_tlbchange_handler_head
2145          * spots a huge page.
2146          */
2147         uasm_l_tlb_huge_update(&l, p);
2148         iPTE_LW(&p, wr.r1, wr.r2);
2149         build_pte_present(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbl);
2150         build_tlb_probe_entry(&p);
2151
2152         if (cpu_has_rixi && !cpu_has_rixiex) {
2153                 /*
2154                  * If the page is not _PAGE_VALID, RI or XI could not
2155                  * have triggered it.  Skip the expensive test..
2156                  */
2157                 if (use_bbit_insns()) {
2158                         uasm_il_bbit0(&p, &r, wr.r1, ilog2(_PAGE_VALID),
2159                                       label_tlbl_goaround2);
2160                 } else {
2161                         uasm_i_andi(&p, wr.r3, wr.r1, _PAGE_VALID);
2162                         uasm_il_beqz(&p, &r, wr.r3, label_tlbl_goaround2);
2163                 }
2164                 uasm_i_nop(&p);
2165
2166                 /*
2167                  * Warn if something may race with us & replace the TLB entry
2168                  * before we read it here. Everything with such races should
2169                  * also have dedicated RiXi exception handlers, so this
2170                  * shouldn't be hit.
2171                  */
2172                 WARN(cpu_has_tlbex_tlbp_race(), "Unhandled race in RiXi path");
2173
2174                 uasm_i_tlbr(&p);
2175
2176                 if (cpu_has_mips_r2_exec_hazard)
2177                         uasm_i_ehb(&p);
2178
2179                 /* Examine  entrylo 0 or 1 based on ptr. */
2180                 if (use_bbit_insns()) {
2181                         uasm_i_bbit0(&p, wr.r2, ilog2(sizeof(pte_t)), 8);
2182                 } else {
2183                         uasm_i_andi(&p, wr.r3, wr.r2, sizeof(pte_t));
2184                         uasm_i_beqz(&p, wr.r3, 8);
2185                 }
2186                 /* load it in the delay slot*/
2187                 UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO0);
2188                 /* load it if ptr is odd */
2189                 UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO1);
2190                 /*
2191                  * If the entryLo (now in wr.r3) is valid (bit 1), RI or
2192                  * XI must have triggered it.
2193                  */
2194                 if (use_bbit_insns()) {
2195                         uasm_il_bbit0(&p, &r, wr.r3, 1, label_tlbl_goaround2);
2196                 } else {
2197                         uasm_i_andi(&p, wr.r3, wr.r3, 2);
2198                         uasm_il_beqz(&p, &r, wr.r3, label_tlbl_goaround2);
2199                 }
2200                 if (PM_DEFAULT_MASK == 0)
2201                         uasm_i_nop(&p);
2202                 /*
2203                  * We clobbered C0_PAGEMASK, restore it.  On the other branch
2204                  * it is restored in build_huge_tlb_write_entry.
2205                  */
2206                 build_restore_pagemask(&p, &r, wr.r3, label_nopage_tlbl, 0);
2207
2208                 uasm_l_tlbl_goaround2(&l, p);
2209         }
2210         uasm_i_ori(&p, wr.r1, wr.r1, (_PAGE_ACCESSED | _PAGE_VALID));
2211         build_huge_handler_tail(&p, &r, &l, wr.r1, wr.r2, 1);
2212 #endif
2213
2214         uasm_l_nopage_tlbl(&l, p);
2215         if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
2216                 uasm_i_sync(&p, 0);
2217         build_restore_work_registers(&p);
2218 #ifdef CONFIG_CPU_MICROMIPS
2219         if ((unsigned long)tlb_do_page_fault_0 & 1) {
2220                 uasm_i_lui(&p, K0, uasm_rel_hi((long)tlb_do_page_fault_0));
2221                 uasm_i_addiu(&p, K0, K0, uasm_rel_lo((long)tlb_do_page_fault_0));
2222                 uasm_i_jr(&p, K0);
2223         } else
2224 #endif
2225         uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
2226         uasm_i_nop(&p);
2227
2228         if (p >= (u32 *)handle_tlbl_end)
2229                 panic("TLB load handler fastpath space exceeded");
2230
2231         uasm_resolve_relocs(relocs, labels);
2232         pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
2233                  (unsigned int)(p - (u32 *)handle_tlbl));
2234
2235         dump_handler("r4000_tlb_load", handle_tlbl, handle_tlbl_end);
2236 }
2237
2238 static void build_r4000_tlb_store_handler(void)
2239 {
2240         u32 *p = (u32 *)msk_isa16_mode((ulong)handle_tlbs);
2241         struct uasm_label *l = labels;
2242         struct uasm_reloc *r = relocs;
2243         struct work_registers wr;
2244
2245         memset(p, 0, handle_tlbs_end - (char *)p);
2246         memset(labels, 0, sizeof(labels));
2247         memset(relocs, 0, sizeof(relocs));
2248
2249         wr = build_r4000_tlbchange_handler_head(&p, &l, &r);
2250         build_pte_writable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbs);
2251         if (m4kc_tlbp_war())
2252                 build_tlb_probe_entry(&p);
2253         build_make_write(&p, &r, wr.r1, wr.r2, wr.r3);
2254         build_r4000_tlbchange_handler_tail(&p, &l, &r, wr.r1, wr.r2);
2255
2256 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
2257         /*
2258          * This is the entry point when
2259          * build_r4000_tlbchange_handler_head spots a huge page.
2260          */
2261         uasm_l_tlb_huge_update(&l, p);
2262         iPTE_LW(&p, wr.r1, wr.r2);
2263         build_pte_writable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbs);
2264         build_tlb_probe_entry(&p);
2265         uasm_i_ori(&p, wr.r1, wr.r1,
2266                    _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
2267         build_huge_handler_tail(&p, &r, &l, wr.r1, wr.r2, 1);
2268 #endif
2269
2270         uasm_l_nopage_tlbs(&l, p);
2271         if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
2272                 uasm_i_sync(&p, 0);
2273         build_restore_work_registers(&p);
2274 #ifdef CONFIG_CPU_MICROMIPS
2275         if ((unsigned long)tlb_do_page_fault_1 & 1) {
2276                 uasm_i_lui(&p, K0, uasm_rel_hi((long)tlb_do_page_fault_1));
2277                 uasm_i_addiu(&p, K0, K0, uasm_rel_lo((long)tlb_do_page_fault_1));
2278                 uasm_i_jr(&p, K0);
2279         } else
2280 #endif
2281         uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
2282         uasm_i_nop(&p);
2283
2284         if (p >= (u32 *)handle_tlbs_end)
2285                 panic("TLB store handler fastpath space exceeded");
2286
2287         uasm_resolve_relocs(relocs, labels);
2288         pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
2289                  (unsigned int)(p - (u32 *)handle_tlbs));
2290
2291         dump_handler("r4000_tlb_store", handle_tlbs, handle_tlbs_end);
2292 }
2293
2294 static void build_r4000_tlb_modify_handler(void)
2295 {
2296         u32 *p = (u32 *)msk_isa16_mode((ulong)handle_tlbm);
2297         struct uasm_label *l = labels;
2298         struct uasm_reloc *r = relocs;
2299         struct work_registers wr;
2300
2301         memset(p, 0, handle_tlbm_end - (char *)p);
2302         memset(labels, 0, sizeof(labels));
2303         memset(relocs, 0, sizeof(relocs));
2304
2305         wr = build_r4000_tlbchange_handler_head(&p, &l, &r);
2306         build_pte_modifiable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbm);
2307         if (m4kc_tlbp_war())
2308                 build_tlb_probe_entry(&p);
2309         /* Present and writable bits set, set accessed and dirty bits. */
2310         build_make_write(&p, &r, wr.r1, wr.r2, wr.r3);
2311         build_r4000_tlbchange_handler_tail(&p, &l, &r, wr.r1, wr.r2);
2312
2313 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
2314         /*
2315          * This is the entry point when
2316          * build_r4000_tlbchange_handler_head spots a huge page.
2317          */
2318         uasm_l_tlb_huge_update(&l, p);
2319         iPTE_LW(&p, wr.r1, wr.r2);
2320         build_pte_modifiable(&p, &r, wr.r1, wr.r2,  wr.r3, label_nopage_tlbm);
2321         build_tlb_probe_entry(&p);
2322         uasm_i_ori(&p, wr.r1, wr.r1,
2323                    _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
2324         build_huge_handler_tail(&p, &r, &l, wr.r1, wr.r2, 0);
2325 #endif
2326
2327         uasm_l_nopage_tlbm(&l, p);
2328         if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
2329                 uasm_i_sync(&p, 0);
2330         build_restore_work_registers(&p);
2331 #ifdef CONFIG_CPU_MICROMIPS
2332         if ((unsigned long)tlb_do_page_fault_1 & 1) {
2333                 uasm_i_lui(&p, K0, uasm_rel_hi((long)tlb_do_page_fault_1));
2334                 uasm_i_addiu(&p, K0, K0, uasm_rel_lo((long)tlb_do_page_fault_1));
2335                 uasm_i_jr(&p, K0);
2336         } else
2337 #endif
2338         uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
2339         uasm_i_nop(&p);
2340
2341         if (p >= (u32 *)handle_tlbm_end)
2342                 panic("TLB modify handler fastpath space exceeded");
2343
2344         uasm_resolve_relocs(relocs, labels);
2345         pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
2346                  (unsigned int)(p - (u32 *)handle_tlbm));
2347
2348         dump_handler("r4000_tlb_modify", handle_tlbm, handle_tlbm_end);
2349 }
2350
2351 static void flush_tlb_handlers(void)
2352 {
2353         local_flush_icache_range((unsigned long)handle_tlbl,
2354                            (unsigned long)handle_tlbl_end);
2355         local_flush_icache_range((unsigned long)handle_tlbs,
2356                            (unsigned long)handle_tlbs_end);
2357         local_flush_icache_range((unsigned long)handle_tlbm,
2358                            (unsigned long)handle_tlbm_end);
2359         local_flush_icache_range((unsigned long)tlbmiss_handler_setup_pgd,
2360                            (unsigned long)tlbmiss_handler_setup_pgd_end);
2361 }
2362
2363 static void print_htw_config(void)
2364 {
2365         unsigned long config;
2366         unsigned int pwctl;
2367         const int field = 2 * sizeof(unsigned long);
2368
2369         config = read_c0_pwfield();
2370         pr_debug("PWField (0x%0*lx): GDI: 0x%02lx  UDI: 0x%02lx  MDI: 0x%02lx  PTI: 0x%02lx  PTEI: 0x%02lx\n",
2371                 field, config,
2372                 (config & MIPS_PWFIELD_GDI_MASK) >> MIPS_PWFIELD_GDI_SHIFT,
2373                 (config & MIPS_PWFIELD_UDI_MASK) >> MIPS_PWFIELD_UDI_SHIFT,
2374                 (config & MIPS_PWFIELD_MDI_MASK) >> MIPS_PWFIELD_MDI_SHIFT,
2375                 (config & MIPS_PWFIELD_PTI_MASK) >> MIPS_PWFIELD_PTI_SHIFT,
2376                 (config & MIPS_PWFIELD_PTEI_MASK) >> MIPS_PWFIELD_PTEI_SHIFT);
2377
2378         config = read_c0_pwsize();
2379         pr_debug("PWSize  (0x%0*lx): PS: 0x%lx  GDW: 0x%02lx  UDW: 0x%02lx  MDW: 0x%02lx  PTW: 0x%02lx  PTEW: 0x%02lx\n",
2380                 field, config,
2381                 (config & MIPS_PWSIZE_PS_MASK) >> MIPS_PWSIZE_PS_SHIFT,
2382                 (config & MIPS_PWSIZE_GDW_MASK) >> MIPS_PWSIZE_GDW_SHIFT,
2383                 (config & MIPS_PWSIZE_UDW_MASK) >> MIPS_PWSIZE_UDW_SHIFT,
2384                 (config & MIPS_PWSIZE_MDW_MASK) >> MIPS_PWSIZE_MDW_SHIFT,
2385                 (config & MIPS_PWSIZE_PTW_MASK) >> MIPS_PWSIZE_PTW_SHIFT,
2386                 (config & MIPS_PWSIZE_PTEW_MASK) >> MIPS_PWSIZE_PTEW_SHIFT);
2387
2388         pwctl = read_c0_pwctl();
2389         pr_debug("PWCtl   (0x%x): PWEn: 0x%x  XK: 0x%x  XS: 0x%x  XU: 0x%x  DPH: 0x%x  HugePg: 0x%x  Psn: 0x%x\n",
2390                 pwctl,
2391                 (pwctl & MIPS_PWCTL_PWEN_MASK) >> MIPS_PWCTL_PWEN_SHIFT,
2392                 (pwctl & MIPS_PWCTL_XK_MASK) >> MIPS_PWCTL_XK_SHIFT,
2393                 (pwctl & MIPS_PWCTL_XS_MASK) >> MIPS_PWCTL_XS_SHIFT,
2394                 (pwctl & MIPS_PWCTL_XU_MASK) >> MIPS_PWCTL_XU_SHIFT,
2395                 (pwctl & MIPS_PWCTL_DPH_MASK) >> MIPS_PWCTL_DPH_SHIFT,
2396                 (pwctl & MIPS_PWCTL_HUGEPG_MASK) >> MIPS_PWCTL_HUGEPG_SHIFT,
2397                 (pwctl & MIPS_PWCTL_PSN_MASK) >> MIPS_PWCTL_PSN_SHIFT);
2398 }
2399
2400 static void config_htw_params(void)
2401 {
2402         unsigned long pwfield, pwsize, ptei;
2403         unsigned int config;
2404
2405         /*
2406          * We are using 2-level page tables, so we only need to
2407          * setup GDW and PTW appropriately. UDW and MDW will remain 0.
2408          * The default value of GDI/UDI/MDI/PTI is 0xc. It is illegal to
2409          * write values less than 0xc in these fields because the entire
2410          * write will be dropped. As a result of which, we must preserve
2411          * the original reset values and overwrite only what we really want.
2412          */
2413
2414         pwfield = read_c0_pwfield();
2415         /* re-initialize the GDI field */
2416         pwfield &= ~MIPS_PWFIELD_GDI_MASK;
2417         pwfield |= PGDIR_SHIFT << MIPS_PWFIELD_GDI_SHIFT;
2418         /* re-initialize the PTI field including the even/odd bit */
2419         pwfield &= ~MIPS_PWFIELD_PTI_MASK;
2420         pwfield |= PAGE_SHIFT << MIPS_PWFIELD_PTI_SHIFT;
2421         if (CONFIG_PGTABLE_LEVELS >= 3) {
2422                 pwfield &= ~MIPS_PWFIELD_MDI_MASK;
2423                 pwfield |= PMD_SHIFT << MIPS_PWFIELD_MDI_SHIFT;
2424         }
2425         /* Set the PTEI right shift */
2426         ptei = _PAGE_GLOBAL_SHIFT << MIPS_PWFIELD_PTEI_SHIFT;
2427         pwfield |= ptei;
2428         write_c0_pwfield(pwfield);
2429         /* Check whether the PTEI value is supported */
2430         back_to_back_c0_hazard();
2431         pwfield = read_c0_pwfield();
2432         if (((pwfield & MIPS_PWFIELD_PTEI_MASK) << MIPS_PWFIELD_PTEI_SHIFT)
2433                 != ptei) {
2434                 pr_warn("Unsupported PTEI field value: 0x%lx. HTW will not be enabled",
2435                         ptei);
2436                 /*
2437                  * Drop option to avoid HTW being enabled via another path
2438                  * (eg htw_reset())
2439                  */
2440                 current_cpu_data.options &= ~MIPS_CPU_HTW;
2441                 return;
2442         }
2443
2444         pwsize = ilog2(PTRS_PER_PGD) << MIPS_PWSIZE_GDW_SHIFT;
2445         pwsize |= ilog2(PTRS_PER_PTE) << MIPS_PWSIZE_PTW_SHIFT;
2446         if (CONFIG_PGTABLE_LEVELS >= 3)
2447                 pwsize |= ilog2(PTRS_PER_PMD) << MIPS_PWSIZE_MDW_SHIFT;
2448
2449         /* Set pointer size to size of directory pointers */
2450         if (IS_ENABLED(CONFIG_64BIT))
2451                 pwsize |= MIPS_PWSIZE_PS_MASK;
2452         /* PTEs may be multiple pointers long (e.g. with XPA) */
2453         pwsize |= ((PTE_T_LOG2 - PGD_T_LOG2) << MIPS_PWSIZE_PTEW_SHIFT)
2454                         & MIPS_PWSIZE_PTEW_MASK;
2455
2456         write_c0_pwsize(pwsize);
2457
2458         /* Make sure everything is set before we enable the HTW */
2459         back_to_back_c0_hazard();
2460
2461         /*
2462          * Enable HTW (and only for XUSeg on 64-bit), and disable the rest of
2463          * the pwctl fields.
2464          */
2465         config = 1 << MIPS_PWCTL_PWEN_SHIFT;
2466         if (IS_ENABLED(CONFIG_64BIT))
2467                 config |= MIPS_PWCTL_XU_MASK;
2468         write_c0_pwctl(config);
2469         pr_info("Hardware Page Table Walker enabled\n");
2470
2471         print_htw_config();
2472 }
2473
2474 static void config_xpa_params(void)
2475 {
2476 #ifdef CONFIG_XPA
2477         unsigned int pagegrain;
2478
2479         if (mips_xpa_disabled) {
2480                 pr_info("Extended Physical Addressing (XPA) disabled\n");
2481                 return;
2482         }
2483
2484         pagegrain = read_c0_pagegrain();
2485         write_c0_pagegrain(pagegrain | PG_ELPA);
2486         back_to_back_c0_hazard();
2487         pagegrain = read_c0_pagegrain();
2488
2489         if (pagegrain & PG_ELPA)
2490                 pr_info("Extended Physical Addressing (XPA) enabled\n");
2491         else
2492                 panic("Extended Physical Addressing (XPA) disabled");
2493 #endif
2494 }
2495
2496 static void check_pabits(void)
2497 {
2498         unsigned long entry;
2499         unsigned pabits, fillbits;
2500
2501         if (!cpu_has_rixi || _PAGE_NO_EXEC == 0) {
2502                 /*
2503                  * We'll only be making use of the fact that we can rotate bits
2504                  * into the fill if the CPU supports RIXI, so don't bother
2505                  * probing this for CPUs which don't.
2506                  */
2507                 return;
2508         }
2509
2510         write_c0_entrylo0(~0ul);
2511         back_to_back_c0_hazard();
2512         entry = read_c0_entrylo0();
2513
2514         /* clear all non-PFN bits */
2515         entry &= ~((1 << MIPS_ENTRYLO_PFN_SHIFT) - 1);
2516         entry &= ~(MIPS_ENTRYLO_RI | MIPS_ENTRYLO_XI);
2517
2518         /* find a lower bound on PABITS, and upper bound on fill bits */
2519         pabits = fls_long(entry) + 6;
2520         fillbits = max_t(int, (int)BITS_PER_LONG - pabits, 0);
2521
2522         /* minus the RI & XI bits */
2523         fillbits -= min_t(unsigned, fillbits, 2);
2524
2525         if (fillbits >= ilog2(_PAGE_NO_EXEC))
2526                 fill_includes_sw_bits = true;
2527
2528         pr_debug("Entry* registers contain %u fill bits\n", fillbits);
2529 }
2530
2531 void build_tlb_refill_handler(void)
2532 {
2533         /*
2534          * The refill handler is generated per-CPU, multi-node systems
2535          * may have local storage for it. The other handlers are only
2536          * needed once.
2537          */
2538         static int run_once = 0;
2539
2540         if (IS_ENABLED(CONFIG_XPA) && !cpu_has_rixi)
2541                 panic("Kernels supporting XPA currently require CPUs with RIXI");
2542
2543         output_pgtable_bits_defines();
2544         check_pabits();
2545
2546 #ifdef CONFIG_64BIT
2547         check_for_high_segbits = current_cpu_data.vmbits > (PGDIR_SHIFT + PGD_TABLE_ORDER + PAGE_SHIFT - 3);
2548 #endif
2549
2550         if (cpu_has_3kex) {
2551 #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
2552                 if (!run_once) {
2553                         build_setup_pgd();
2554                         build_r3000_tlb_refill_handler();
2555                         build_r3000_tlb_load_handler();
2556                         build_r3000_tlb_store_handler();
2557                         build_r3000_tlb_modify_handler();
2558                         flush_tlb_handlers();
2559                         run_once++;
2560                 }
2561 #else
2562                 panic("No R3000 TLB refill handler");
2563 #endif
2564                 return;
2565         }
2566
2567         if (cpu_has_ldpte)
2568                 setup_pw();
2569
2570         if (!run_once) {
2571                 scratch_reg = allocate_kscratch();
2572                 build_setup_pgd();
2573                 build_r4000_tlb_load_handler();
2574                 build_r4000_tlb_store_handler();
2575                 build_r4000_tlb_modify_handler();
2576                 if (cpu_has_ldpte)
2577                         build_loongson3_tlb_refill_handler();
2578                 else
2579                         build_r4000_tlb_refill_handler();
2580                 flush_tlb_handlers();
2581                 run_once++;
2582         }
2583         if (cpu_has_xpa)
2584                 config_xpa_params();
2585         if (cpu_has_htw)
2586                 config_htw_params();
2587 }