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
6 * Synthesize TLB refill handlers at runtime.
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.
14 * ... and the days got worse and worse and now you see
15 * I've gone completely out of my mind.
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 ...
21 * (Condolences to Napoleon XIV)
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>
33 #include <asm/cacheflush.h>
34 #include <asm/cpu-type.h>
35 #include <asm/mipsregs.h>
36 #include <asm/mmu_context.h>
38 #include <asm/setup.h>
39 #include <asm/tlbex.h>
41 static int mips_xpa_disabled;
43 static int __init xpa_disable(char *s)
45 mips_xpa_disabled = 1;
50 __setup("noxpa", xpa_disable);
53 * TLB load/store/modify handlers.
55 * Only the fastpath gets synthesized at runtime, the slowpath for
56 * do_page_fault remains normal asm.
58 extern void tlb_do_page_fault_0(void);
59 extern void tlb_do_page_fault_1(void);
61 struct work_registers {
70 } ____cacheline_aligned_in_smp;
72 static struct tlb_reg_save handler_reg_save[NR_CPUS];
74 static inline int r45k_bvahwbug(void)
76 /* XXX: We should probe for the presence of this bug, but we don't. */
80 static inline int r4k_250MHZhwbug(void)
82 /* XXX: We should probe for the presence of this bug, but we don't. */
86 extern int sb1250_m3_workaround_needed(void);
88 static inline int __maybe_unused bcm1250_m3_war(void)
90 if (IS_ENABLED(CONFIG_SB1_PASS_2_WORKAROUNDS))
91 return sb1250_m3_workaround_needed();
95 static inline int __maybe_unused r10000_llsc_war(void)
97 return IS_ENABLED(CONFIG_WAR_R10000_LLSC);
100 static int use_bbit_insns(void)
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:
113 static int use_lwx_insns(void)
115 switch (current_cpu_type()) {
116 case CPU_CAVIUM_OCTEON2:
117 case CPU_CAVIUM_OCTEON3:
123 #if defined(CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE) && \
124 CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE > 0
125 static bool scratchpad_available(void)
129 static int scratchpad_offset(int i)
132 * CVMSEG starts at address -32768 and extends for
133 * CAVIUM_OCTEON_CVMSEG_SIZE 128 byte cache lines.
135 i += 1; /* Kernel use starts at the top and works down. */
136 return CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE * 128 - (8 * i) - 32768;
139 static bool scratchpad_available(void)
143 static int scratchpad_offset(int i)
146 /* Really unreachable, but evidently some GCC want this. */
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.
159 static int m4kc_tlbp_war(void)
161 return current_cpu_type() == CPU_4KC;
164 /* Handle labels (which must be positive integers). */
166 label_second_part = 1,
171 label_split = label_tlbw_hazard_0 + 8,
172 label_tlbl_goaround1,
173 label_tlbl_goaround2,
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,
185 UASM_L_LA(_second_part)
188 UASM_L_LA(_vmalloc_done)
189 /* _tlbw_hazard_x is handled differently. */
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)
203 static int hazard_instance;
205 static void uasm_bgezl_hazard(u32 **p, struct uasm_reloc **r, int instance)
209 uasm_il_bgezl(p, r, 0, label_tlbw_hazard_0 + instance);
216 static void uasm_bgezl_label(struct uasm_label **l, u32 **p, int instance)
220 uasm_build_label(l, *p, label_tlbw_hazard_0 + instance);
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.
233 static void output_pgtable_bits_defines(void)
235 #define pr_define(fmt, ...) \
236 pr_debug("#define " fmt, ##__VA_ARGS__)
238 pr_debug("#include <asm/asm.h>\n");
239 pr_debug("#include <asm/regdef.h>\n");
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);
250 #ifdef _PAGE_NO_EXEC_SHIFT
252 pr_define("_PAGE_NO_EXEC_SHIFT %d\n", _PAGE_NO_EXEC_SHIFT);
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);
261 static inline void dump_handler(const char *symbol, const void *start, const void *end)
263 unsigned int count = (end - start) / sizeof(u32);
264 const u32 *handler = start;
267 pr_debug("LEAF(%s)\n", symbol);
269 pr_debug("\t.set push\n");
270 pr_debug("\t.set noreorder\n");
272 for (i = 0; i < count; i++)
273 pr_debug("\t.word\t0x%08x\t\t# %p\n", handler[i], &handler[i]);
275 pr_debug("\t.set\tpop\n");
277 pr_debug("\tEND(%s)\n", symbol);
280 /* The only general purpose registers allowed in TLB handlers. */
285 # define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_XCONTEXT)
287 # define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_CONTEXT)
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.
295 * We deliberately chose a buffer size of 128, so we won't scribble
296 * over anything important on overflow before we panic.
298 static u32 tlb_handler[128];
300 /* simply assume worst case size for labels and relocs */
301 static struct uasm_label labels[128];
302 static struct uasm_reloc relocs[128];
304 static int check_for_high_segbits;
305 static bool fill_includes_sw_bits;
307 static unsigned int kscratch_used_mask;
309 static inline int __maybe_unused c0_kscratch(void)
314 static int allocate_kscratch(void)
317 unsigned int a = cpu_data[0].kscratch_mask & ~kscratch_used_mask;
324 r--; /* make it zero based */
326 kscratch_used_mask |= (1 << r);
331 static int scratch_reg;
333 EXPORT_SYMBOL_GPL(pgd_reg);
334 enum vmalloc64_mode {not_refill, refill_scratch, refill_noscratch};
336 static struct work_registers build_get_work_registers(u32 **p)
338 struct work_registers r;
340 if (scratch_reg >= 0) {
341 /* Save in CPU local C0_KScratch? */
342 UASM_i_MTC0(p, 1, c0_kscratch(), scratch_reg);
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);
354 /* handler_reg_save index in K0 */
355 UASM_i_SLL(p, K0, K0, ilog2(sizeof(struct tlb_reg_save)));
357 UASM_i_LA(p, K1, (long)&handler_reg_save);
358 UASM_i_ADDU(p, K0, K0, K1);
360 UASM_i_LA(p, K0, (long)&handler_reg_save);
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);
372 static void build_restore_work_registers(u32 **p)
374 if (scratch_reg >= 0) {
376 UASM_i_MFC0(p, 1, c0_kscratch(), scratch_reg);
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);
384 #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
387 * CONFIG_MIPS_PGD_C0_CONTEXT implies 64 bit and lack of pgd_current,
388 * we cannot do r3000 under these circumstances.
390 * The R3000 TLB handler is simple.
392 static void build_r3000_tlb_refill_handler(void)
394 long pgdc = (long)pgd_current;
397 memset(tlb_handler, 0, sizeof(tlb_handler));
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 */
416 uasm_i_rfe(&p); /* branch delay */
418 if (p > tlb_handler + 32)
419 panic("TLB refill handler space exceeded");
421 pr_debug("Wrote TLB refill handler (%u instructions).\n",
422 (unsigned int)(p - tlb_handler));
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));
428 #endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
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.
437 static u32 final_handler[64];
442 * From the IDT errata for the QED RM5230 (Nevada), processor revision 1.0:
443 * 2. A timing hazard exists for the TLBP instruction.
445 * stalling_instruction
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.
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.
457 * Errata 2 will not be fixed. This errata is also on the R5000.
459 * As if we MIPS hackers wouldn't know how to nop pipelines happy ...
461 static void __maybe_unused build_tlb_probe_entry(u32 **p)
463 switch (current_cpu_type()) {
464 /* Found by experiment: R4600 v2.0/R4700 needs this, too. */
479 void build_tlb_write_entry(u32 **p, struct uasm_label **l,
480 struct uasm_reloc **r,
481 enum tlb_write_entry wmode)
483 void(*tlbw)(u32 **) = NULL;
486 case tlb_random: tlbw = uasm_i_tlbwr; break;
487 case tlb_indexed: tlbw = uasm_i_tlbwi; break;
490 if (cpu_has_mips_r2_r6) {
491 if (cpu_has_mips_r2_exec_hazard)
497 switch (current_cpu_type()) {
505 * This branch uses up a mtc0 hazard nop slot and saves
506 * two nops after the tlbw instruction.
508 uasm_bgezl_hazard(p, r, hazard_instance);
510 uasm_bgezl_label(l, p, hazard_instance);
524 uasm_i_nop(p); /* QED specifies 2 nops hazard */
525 uasm_i_nop(p); /* QED specifies 2 nops hazard */
555 case CPU_LOONGSON2EF:
579 panic("No TLB refill handler yet (CPU type: %d)",
584 EXPORT_SYMBOL_GPL(build_tlb_write_entry);
586 static __maybe_unused void build_convert_pte_to_entrylo(u32 **p,
589 if (_PAGE_GLOBAL_SHIFT == 0) {
590 /* pte_t is already in EntryLo format */
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));
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));
603 #ifdef CONFIG_PHYS_ADDR_T_64BIT
604 uasm_i_dsrl_safe(p, reg, reg, ilog2(_PAGE_GLOBAL));
606 UASM_i_SRL(p, reg, reg, ilog2(_PAGE_GLOBAL));
611 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
613 static void build_restore_pagemask(u32 **p, struct uasm_reloc **r,
614 unsigned int tmp, enum label_id lid,
617 if (restore_scratch) {
619 * Ensure the MFC0 below observes the value written to the
620 * KScratch register by the prior MTC0.
622 if (scratch_reg >= 0)
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);
636 uasm_i_mtc0(p, 0, C0_PAGEMASK);
637 uasm_il_b(p, r, lid);
639 if (scratch_reg >= 0)
640 UASM_i_MFC0(p, 1, c0_kscratch(), scratch_reg);
642 UASM_i_LW(p, 1, scratchpad_offset(0), 0);
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);
655 uasm_il_b(p, r, lid);
656 uasm_i_mtc0(p, 0, C0_PAGEMASK);
661 static void build_huge_tlb_write_entry(u32 **p, struct uasm_label **l,
662 struct uasm_reloc **r,
664 enum tlb_write_entry wmode,
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);
672 build_tlb_write_entry(p, l, r, wmode);
674 build_restore_pagemask(p, r, tmp, label_leave, restore_scratch);
678 * Check if Huge PTE is present, if so then jump to LABEL.
681 build_is_huge_pte(u32 **p, struct uasm_reloc **r, unsigned int tmp,
682 unsigned int pmd, int lid)
684 UASM_i_LW(p, tmp, 0, pmd);
685 if (use_bbit_insns()) {
686 uasm_il_bbit1(p, r, tmp, ilog2(_PAGE_HUGE), lid);
688 uasm_i_andi(p, tmp, tmp, _PAGE_HUGE);
689 uasm_il_bnez(p, r, tmp, lid);
693 static void build_huge_update_entries(u32 **p, unsigned int pte,
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
707 small_sequence = (HPAGE_SIZE >> 7) < 0x10000;
709 /* We can clobber tmp. It isn't used after this.*/
711 uasm_i_lui(p, tmp, HPAGE_SIZE >> (7 + 16));
713 build_convert_pte_to_entrylo(p, pte);
714 UASM_i_MTC0(p, pte, C0_ENTRYLO0); /* load it */
715 /* convert to entrylo1 */
717 UASM_i_ADDIU(p, pte, pte, HPAGE_SIZE >> 7);
719 UASM_i_ADDU(p, pte, pte, tmp);
721 UASM_i_MTC0(p, pte, C0_ENTRYLO1); /* load it */
724 static void build_huge_handler_tail(u32 **p, struct uasm_reloc **r,
725 struct uasm_label **l,
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 */
735 UASM_i_SW(p, pte, 0, ptr);
737 if (cpu_has_ftlb && flush) {
738 BUG_ON(!cpu_has_tlbinv);
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);
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);
753 build_huge_update_entries(p, pte, ptr);
754 build_huge_tlb_write_entry(p, l, r, pte, tlb_indexed, 0);
756 #endif /* CONFIG_MIPS_HUGE_TLB_SUPPORT */
760 * TMP and PTR are scratch.
761 * TMP will be clobbered, PTR will hold the pmd entry.
763 void build_get_pmde64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
764 unsigned int tmp, unsigned int ptr)
766 #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
767 long pgdc = (long)pgd_current;
770 * The vmalloc handling is not in the hotpath.
772 uasm_i_dmfc0(p, tmp, C0_BADVADDR);
774 if (check_for_high_segbits) {
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.
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);
789 uasm_il_bltz(p, r, tmp, label_vmalloc);
791 /* No uasm_i_nop needed here, since the next insn doesn't touch TMP. */
794 /* pgd is in pgd_reg */
796 UASM_i_MFC0(p, ptr, C0_PWBASE);
798 UASM_i_MFC0(p, ptr, c0_kscratch(), pgd_reg);
800 #if defined(CONFIG_MIPS_PGD_C0_CONTEXT)
802 * &pgd << 11 stored in CONTEXT [23..63].
804 UASM_i_MFC0(p, ptr, C0_CONTEXT);
806 /* Clear lower 23 bits of context. */
807 uasm_i_dins(p, ptr, 0, 0, 23);
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);
820 UASM_i_LA_mostly(p, ptr, pgdc);
821 uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
825 uasm_l_vmalloc_done(l, *p);
827 /* get pgd offset in bytes */
828 uasm_i_dsrl_safe(p, tmp, tmp, PGDIR_SHIFT - 3);
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 */
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 */
847 EXPORT_SYMBOL_GPL(build_get_pmde64);
850 * BVADDR is the faulting address, PTR is scratch.
851 * PTR will hold the pgd for vmalloc.
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)
858 long swpd = (long)swapper_pg_dir;
859 int single_insn_swpd;
860 int did_vmalloc_branch = 0;
862 single_insn_swpd = uasm_in_compat_space_p(swpd) && !uasm_rel_lo(swpd);
864 uasm_l_vmalloc(l, *p);
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;
873 uasm_il_bgez(p, r, bvaddr, label_large_segbits_fault);
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));
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));
886 uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(swpd));
889 if (mode != not_refill && check_for_high_segbits) {
890 uasm_l_large_segbits_fault(l, *p);
892 if (mode == refill_scratch && scratch_reg >= 0)
896 * We get here if we are an xsseg address, or if we are
897 * an xuseg address above (PGDIR_SHIFT+PGDIR_BITS) boundary.
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
907 if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
909 UASM_i_LA(p, ptr, (unsigned long)tlb_do_page_fault_0);
912 if (mode == refill_scratch) {
913 if (scratch_reg >= 0)
914 UASM_i_MFC0(p, 1, c0_kscratch(), scratch_reg);
916 UASM_i_LW(p, 1, scratchpad_offset(0), 0);
923 #else /* !CONFIG_64BIT */
926 * TMP and PTR are scratch.
927 * TMP will be clobbered, PTR will hold the pgd entry.
929 void build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
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 */
936 long pgdc = (long)pgd_current;
938 /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
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);
945 UASM_i_LA_mostly(p, ptr, pgdc);
947 uasm_i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
948 uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
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 */
954 EXPORT_SYMBOL_GPL(build_get_pgde32);
956 #endif /* !CONFIG_64BIT */
958 static void build_adjust_context(u32 **p, unsigned int ctx)
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);
964 UASM_i_SRL(p, ctx, ctx, shift);
965 uasm_i_andi(p, ctx, ctx, mask);
968 void build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
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.
977 switch (current_cpu_type()) {
979 UASM_i_LW(p, ptr, 0, ptr);
980 GET_CONTEXT(p, tmp); /* get context reg */
984 GET_CONTEXT(p, tmp); /* get context reg */
985 UASM_i_LW(p, ptr, 0, ptr);
989 build_adjust_context(p, tmp);
990 UASM_i_ADDU(p, ptr, ptr, tmp); /* add in offset */
992 EXPORT_SYMBOL_GPL(build_get_ptep);
994 void build_update_entries(u32 **p, unsigned int tmp, unsigned int ptep)
996 int pte_off_even = 0;
997 int pte_off_odd = sizeof(pte_t);
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);
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);
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);
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);
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);
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 */
1043 EXPORT_SYMBOL_GPL(build_update_entries);
1045 struct mips_huge_tlb_info {
1047 int restore_scratch;
1048 bool need_reload_pte;
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)
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 */
1061 rv.huge_pte = scratch;
1062 rv.restore_scratch = 0;
1063 rv.need_reload_pte = false;
1065 if (check_for_high_segbits) {
1066 UASM_i_MFC0(p, tmp, C0_BADVADDR);
1069 UASM_i_MFC0(p, ptr, c0_kscratch(), pgd_reg);
1071 UASM_i_MFC0(p, ptr, C0_CONTEXT);
1073 if (c0_scratch_reg >= 0)
1074 UASM_i_MTC0(p, scratch, c0_kscratch(), c0_scratch_reg);
1076 UASM_i_SW(p, scratch, scratchpad_offset(0), 0);
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);
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);
1089 UASM_i_MFC0(p, ptr, c0_kscratch(), pgd_reg);
1091 UASM_i_MFC0(p, ptr, C0_CONTEXT);
1093 UASM_i_MFC0(p, tmp, C0_BADVADDR);
1095 if (c0_scratch_reg >= 0)
1096 UASM_i_MTC0(p, scratch, c0_kscratch(), c0_scratch_reg);
1098 UASM_i_SW(p, scratch, scratchpad_offset(0), 0);
1101 /* Clear lower 23 bits of context. */
1102 uasm_i_dins(p, ptr, 0, 0, 23);
1104 uasm_il_bltz(p, r, tmp, label_vmalloc);
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));
1112 uasm_i_drotr(p, ptr, ptr, 11);
1115 #ifdef __PAGETABLE_PMD_FOLDED
1116 #define LOC_PTEP scratch
1118 #define LOC_PTEP ptr
1121 if (!vmalloc_branch_delay_filled)
1122 /* get pgd offset in bytes */
1123 uasm_i_dsrl_safe(p, scratch, tmp, PGDIR_SHIFT - 3);
1125 uasm_l_vmalloc_done(l, *p);
1129 * fall-through case = badvaddr *pgd_current
1130 * vmalloc case = badvaddr swapper_pg_dir
1133 if (vmalloc_branch_delay_filled)
1134 /* get pgd offset in bytes */
1135 uasm_i_dsrl_safe(p, scratch, tmp, PGDIR_SHIFT - 3);
1137 #ifdef __PAGETABLE_PMD_FOLDED
1138 GET_CONTEXT(p, tmp); /* get context reg */
1140 uasm_i_andi(p, scratch, scratch, (PTRS_PER_PGD - 1) << 3);
1142 if (use_lwx_insns()) {
1143 UASM_i_LWX(p, LOC_PTEP, scratch, ptr);
1145 uasm_i_daddu(p, ptr, ptr, scratch); /* add in pgd offset */
1146 uasm_i_ld(p, LOC_PTEP, 0, ptr); /* get pmd pointer */
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);
1154 if (use_lwx_insns()) {
1155 UASM_i_LWX(p, ptr, scratch, ptr);
1157 uasm_i_daddu(p, ptr, ptr, scratch); /* add in pmd offset */
1158 UASM_i_LW(p, ptr, 0, ptr);
1160 /* ptr contains a pointer to PMD entry */
1161 /* tmp contains the address */
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 */
1170 if (use_lwx_insns()) {
1171 UASM_i_LWX(p, scratch, scratch, ptr);
1173 uasm_i_daddu(p, ptr, ptr, scratch); /* add in pmd offset */
1174 UASM_i_LW(p, scratch, 0, ptr);
1177 /* Adjust the context during the load latency. */
1178 build_adjust_context(p, tmp);
1180 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
1181 uasm_il_bbit1(p, r, scratch, ilog2(_PAGE_HUGE), label_tlb_huge_update);
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.
1187 if (use_lwx_insns())
1189 #endif /* CONFIG_MIPS_HUGE_TLB_SUPPORT */
1192 /* build_update_entries */
1193 if (use_lwx_insns()) {
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);
1200 UASM_i_ADDU(p, ptr, scratch, tmp); /* add in offset */
1203 UASM_i_LW(p, even, 0, ptr); /* get even pte */
1204 UASM_i_LW(p, odd, sizeof(pte_t), ptr); /* get odd pte */
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));
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));
1215 UASM_i_MTC0(p, odd, C0_ENTRYLO1); /* load it */
1217 if (c0_scratch_reg >= 0) {
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);
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;
1234 uasm_i_eret(p); /* return from trap */
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.
1245 #define MIPS64_REFILL_INSNS 32
1247 static void build_r4000_tlb_refill_handler(void)
1249 u32 *p = tlb_handler;
1250 struct uasm_label *l = labels;
1251 struct uasm_reloc *r = relocs;
1253 unsigned int final_len;
1254 struct mips_huge_tlb_info htlb_info __maybe_unused;
1255 enum vmalloc64_mode vmalloc_mode __maybe_unused;
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));
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,
1265 vmalloc_mode = refill_scratch;
1267 htlb_info.huge_pte = K0;
1268 htlb_info.restore_scratch = 0;
1269 htlb_info.need_reload_pte = true;
1270 vmalloc_mode = refill_noscratch;
1272 * create the plain linear handler
1274 if (bcm1250_m3_war()) {
1275 unsigned int segbits = 44;
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 */
1289 build_get_pmde64(&p, &l, &r, K0, K1); /* get pmd in K1 */
1291 build_get_pgde32(&p, K0, K1); /* get pgd in K1 */
1294 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
1295 build_is_huge_pte(&p, &r, K0, K1, label_tlb_huge_update);
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 */
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);
1314 build_get_pgd_vmalloc64(&p, &l, &r, K0, K1, vmalloc_mode);
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
1324 switch (boot_cpu_type()) {
1326 if (sizeof(long) == 4) {
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");
1333 * Now fold the handler in the TLB refill handler space.
1336 /* Simplest case, just copy the handler. */
1337 uasm_copy_handler(relocs, labels, tlb_handler, p, f);
1338 final_len = p - tlb_handler;
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");
1347 * Now fold the handler in the TLB refill handler space.
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;
1355 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
1356 const enum label_id ls = label_tlb_huge_update;
1358 const enum label_id ls = label_vmalloc;
1364 for (i = 0; i < ARRAY_SIZE(labels) && labels[i].lab != ls; i++)
1366 BUG_ON(i == ARRAY_SIZE(labels));
1367 split = labels[i].addr;
1370 * See if we have overflown one way or the other.
1372 if (split > tlb_handler + MIPS64_REFILL_INSNS ||
1373 split < p - MIPS64_REFILL_INSNS)
1378 * Split two instructions before the end. One
1379 * for the branch and one for the instruction
1380 * in the delay slot.
1382 split = tlb_handler + MIPS64_REFILL_INSNS - 2;
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.
1389 if (uasm_insn_has_bdelay(relocs, split - 1))
1392 /* Copy first part of the handler. */
1393 uasm_copy_handler(relocs, labels, tlb_handler, split, f);
1394 f += split - tlb_handler;
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))
1403 uasm_copy_handler(relocs, labels,
1404 split, split + 1, f);
1405 uasm_move_labels(labels, f, f + 1, -1);
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)) +
1420 uasm_resolve_relocs(relocs, labels);
1421 pr_debug("Wrote TLB refill handler (%u instructions).\n",
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));
1429 static void setup_pw(void)
1432 unsigned long pgd_i, pgd_w;
1433 #ifndef __PAGETABLE_PMD_FOLDED
1434 unsigned long pmd_i, pmd_w;
1436 unsigned long pt_i, pt_w;
1437 unsigned long pte_i, pte_w;
1438 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
1441 psn = ilog2(_PAGE_HUGE); /* bit used to indicate huge page */
1443 pgd_i = PGDIR_SHIFT; /* 1st level PGD */
1444 #ifndef __PAGETABLE_PMD_FOLDED
1445 pgd_w = PGDIR_SHIFT - PMD_SHIFT + PGD_TABLE_ORDER;
1447 pmd_i = PMD_SHIFT; /* 2nd level PMD */
1448 pmd_w = PMD_SHIFT - PAGE_SHIFT;
1450 pgd_w = PGDIR_SHIFT - PAGE_SHIFT + PGD_TABLE_ORDER;
1453 pt_i = PAGE_SHIFT; /* 3rd level PTE */
1454 pt_w = PAGE_SHIFT - 3;
1456 pte_i = ilog2(_PAGE_GLOBAL);
1458 pwctl = 1 << 30; /* Set PWDirExt */
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);
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);
1468 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
1469 pwctl |= (1 << 6 | psn);
1471 write_c0_pwctl(pwctl);
1472 write_c0_kpgd((long)swapper_pg_dir);
1473 kscratch_used_mask |= (1 << 7); /* KScratch6 is used for KPGD */
1476 static void build_loongson3_tlb_refill_handler(void)
1478 u32 *p = tlb_handler;
1479 struct uasm_label *l = labels;
1480 struct uasm_reloc *r = relocs;
1482 memset(labels, 0, sizeof(labels));
1483 memset(relocs, 0, sizeof(relocs));
1484 memset(tlb_handler, 0, sizeof(tlb_handler));
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);
1492 uasm_il_bgez(&p, &r, K0, label_large_segbits_fault);
1494 uasm_l_vmalloc(&l, p);
1497 uasm_i_dmfc0(&p, K1, C0_PGD);
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 */
1503 uasm_i_ldpte(&p, K1, 0); /* even */
1504 uasm_i_ldpte(&p, K1, 1); /* odd */
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);
1516 uasm_i_mtc0(&p, 0, C0_PAGEMASK);
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);
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));
1535 static void build_setup_pgd(void)
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;
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;
1554 /* PGD << 11 in c0_Context */
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.
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);
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);
1572 /* PGD in c0_KScratch */
1574 UASM_i_MTC0(&p, a0, C0_PWBASE);
1576 UASM_i_MTC0(&p, a0, c0_kscratch(), pgd_reg);
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);
1589 UASM_i_LA_mostly(&p, a2, pgdc);
1590 UASM_i_SW(&p, a0, uasm_rel_lo(pgdc), a2);
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);
1603 if (p >= (u32 *)tlbmiss_handler_setup_pgd_end)
1604 panic("tlbmiss_handler_setup_pgd space exceeded");
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));
1610 dump_handler("tlbmiss_handler", tlbmiss_handler_setup_pgd,
1611 tlbmiss_handler_setup_pgd_end);
1615 iPTE_LW(u32 **p, unsigned int pte, unsigned int ptr)
1618 if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
1620 # ifdef CONFIG_PHYS_ADDR_T_64BIT
1622 uasm_i_lld(p, pte, 0, ptr);
1625 UASM_i_LL(p, pte, 0, ptr);
1627 # ifdef CONFIG_PHYS_ADDR_T_64BIT
1629 uasm_i_ld(p, pte, 0, ptr);
1632 UASM_i_LW(p, pte, 0, ptr);
1637 iPTE_SW(u32 **p, struct uasm_reloc **r, unsigned int pte, unsigned int ptr,
1638 unsigned int mode, unsigned int scratch)
1640 unsigned int hwmode = mode & (_PAGE_VALID | _PAGE_DIRTY);
1641 unsigned int swmode = mode & ~hwmode;
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);
1648 uasm_i_ori(p, pte, pte, mode);
1652 # ifdef CONFIG_PHYS_ADDR_T_64BIT
1654 uasm_i_scd(p, pte, 0, ptr);
1657 UASM_i_SC(p, pte, 0, ptr);
1659 if (r10000_llsc_war())
1660 uasm_il_beqzl(p, r, pte, label_smp_pgtable_change);
1662 uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
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);
1680 # ifdef CONFIG_PHYS_ADDR_T_64BIT
1682 uasm_i_sd(p, pte, 0, ptr);
1685 UASM_i_SW(p, pte, 0, ptr);
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);
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.
1705 build_pte_present(u32 **p, struct uasm_reloc **r,
1706 int pte, int ptr, int scratch, enum label_id lid)
1708 int t = scratch >= 0 ? scratch : pte;
1712 if (use_bbit_insns()) {
1713 uasm_il_bbit0(p, r, pte, ilog2(_PAGE_PRESENT), lid);
1716 if (_PAGE_PRESENT_SHIFT) {
1717 uasm_i_srl(p, t, cur, _PAGE_PRESENT_SHIFT);
1720 uasm_i_andi(p, t, cur, 1);
1721 uasm_il_beqz(p, r, t, lid);
1723 /* You lose the SMP race :-(*/
1724 iPTE_LW(p, pte, ptr);
1727 if (_PAGE_PRESENT_SHIFT) {
1728 uasm_i_srl(p, t, cur, _PAGE_PRESENT_SHIFT);
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);
1736 /* You lose the SMP race :-(*/
1737 iPTE_LW(p, pte, ptr);
1741 /* Make PTE valid, store result in PTR. */
1743 build_make_valid(u32 **p, struct uasm_reloc **r, unsigned int pte,
1744 unsigned int ptr, unsigned int scratch)
1746 unsigned int mode = _PAGE_VALID | _PAGE_ACCESSED;
1748 iPTE_SW(p, r, pte, ptr, mode, scratch);
1752 * Check if PTE can be written to, if not branch to LABEL. Regardless
1753 * restore PTE with value from PTR when done.
1756 build_pte_writable(u32 **p, struct uasm_reloc **r,
1757 unsigned int pte, unsigned int ptr, int scratch,
1760 int t = scratch >= 0 ? scratch : pte;
1763 if (_PAGE_PRESENT_SHIFT) {
1764 uasm_i_srl(p, t, cur, _PAGE_PRESENT_SHIFT);
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);
1773 /* You lose the SMP race :-(*/
1774 iPTE_LW(p, pte, ptr);
1779 /* Make PTE writable, update software status bits as well, then store
1783 build_make_write(u32 **p, struct uasm_reloc **r, unsigned int pte,
1784 unsigned int ptr, unsigned int scratch)
1786 unsigned int mode = (_PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID
1789 iPTE_SW(p, r, pte, ptr, mode, scratch);
1793 * Check if PTE can be modified, if not branch to LABEL. Regardless
1794 * restore PTE with value from PTR when done.
1797 build_pte_modifiable(u32 **p, struct uasm_reloc **r,
1798 unsigned int pte, unsigned int ptr, int scratch,
1801 if (use_bbit_insns()) {
1802 uasm_il_bbit0(p, r, pte, ilog2(_PAGE_WRITE), lid);
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);
1810 /* You lose the SMP race :-(*/
1811 iPTE_LW(p, pte, ptr);
1815 #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
1819 * R3000 style TLB load/store/modify handlers.
1823 * This places the pte into ENTRYLO0 and writes it with tlbwi.
1827 build_r3000_pte_reload_tlbwi(u32 **p, unsigned int pte, unsigned int tmp)
1829 uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
1830 uasm_i_mfc0(p, tmp, C0_EPC); /* cp0 delay */
1833 uasm_i_rfe(p); /* branch delay */
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.
1843 build_r3000_tlb_reload_write(u32 **p, struct uasm_label **l,
1844 struct uasm_reloc **r, unsigned int pte,
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 */
1853 uasm_i_rfe(p); /* branch delay */
1854 uasm_l_r3000_write_probe_fail(l, *p);
1855 uasm_i_tlbwr(p); /* cp0 delay */
1857 uasm_i_rfe(p); /* branch delay */
1861 build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
1864 long pgdc = (long)pgd_current;
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 */
1880 static void build_r3000_tlb_load_handler(void)
1882 u32 *p = (u32 *)handle_tlbl;
1883 struct uasm_label *l = labels;
1884 struct uasm_reloc *r = relocs;
1886 memset(p, 0, handle_tlbl_end - (char *)p);
1887 memset(labels, 0, sizeof(labels));
1888 memset(relocs, 0, sizeof(relocs));
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);
1896 uasm_l_nopage_tlbl(&l, p);
1897 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
1900 if (p >= (u32 *)handle_tlbl_end)
1901 panic("TLB load handler fastpath space exceeded");
1903 uasm_resolve_relocs(relocs, labels);
1904 pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
1905 (unsigned int)(p - (u32 *)handle_tlbl));
1907 dump_handler("r3000_tlb_load", handle_tlbl, handle_tlbl_end);
1910 static void build_r3000_tlb_store_handler(void)
1912 u32 *p = (u32 *)handle_tlbs;
1913 struct uasm_label *l = labels;
1914 struct uasm_reloc *r = relocs;
1916 memset(p, 0, handle_tlbs_end - (char *)p);
1917 memset(labels, 0, sizeof(labels));
1918 memset(relocs, 0, sizeof(relocs));
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);
1926 uasm_l_nopage_tlbs(&l, p);
1927 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1930 if (p >= (u32 *)handle_tlbs_end)
1931 panic("TLB store handler fastpath space exceeded");
1933 uasm_resolve_relocs(relocs, labels);
1934 pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
1935 (unsigned int)(p - (u32 *)handle_tlbs));
1937 dump_handler("r3000_tlb_store", handle_tlbs, handle_tlbs_end);
1940 static void build_r3000_tlb_modify_handler(void)
1942 u32 *p = (u32 *)handle_tlbm;
1943 struct uasm_label *l = labels;
1944 struct uasm_reloc *r = relocs;
1946 memset(p, 0, handle_tlbm_end - (char *)p);
1947 memset(labels, 0, sizeof(labels));
1948 memset(relocs, 0, sizeof(relocs));
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);
1956 uasm_l_nopage_tlbm(&l, p);
1957 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1960 if (p >= (u32 *)handle_tlbm_end)
1961 panic("TLB modify handler fastpath space exceeded");
1963 uasm_resolve_relocs(relocs, labels);
1964 pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
1965 (unsigned int)(p - (u32 *)handle_tlbm));
1967 dump_handler("r3000_tlb_modify", handle_tlbm, handle_tlbm_end);
1969 #endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
1971 static bool cpu_has_tlbex_tlbp_race(void)
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.
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.
1984 if (cpu_has_shared_ftlb_ram)
1987 /* In all other cases there ought to be no race condition to handle */
1992 * R4000 style TLB load/store/modify handlers.
1994 static struct work_registers
1995 build_r4000_tlbchange_handler_head(u32 **p, struct uasm_label **l,
1996 struct uasm_reloc **r)
1998 struct work_registers wr = build_get_work_registers(p);
2001 build_get_pmde64(p, l, r, wr.r1, wr.r2); /* get pmd in ptr */
2003 build_get_pgde32(p, wr.r1, wr.r2); /* get pgd in ptr */
2006 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
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.
2012 build_is_huge_pte(p, r, wr.r1, wr.r2, label_tlb_huge_update);
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);
2022 uasm_l_smp_pgtable_change(l, *p);
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 */
2030 uasm_i_mfc0(p, wr.r3, C0_INDEX);
2031 uasm_il_bltz(p, r, wr.r3, label_leave);
2039 build_r4000_tlbchange_handler_tail(u32 **p, struct uasm_label **l,
2040 struct uasm_reloc **r, unsigned int tmp,
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 */
2052 build_get_pgd_vmalloc64(p, l, r, tmp, ptr, not_refill);
2056 static void build_r4000_tlb_load_handler(void)
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;
2063 memset(p, 0, handle_tlbl_end - (char *)p);
2064 memset(labels, 0, sizeof(labels));
2065 memset(relocs, 0, sizeof(relocs));
2067 if (bcm1250_m3_war()) {
2068 unsigned int segbits = 44;
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 */
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);
2086 if (cpu_has_rixi && !cpu_has_rixiex) {
2088 * If the page is not _PAGE_VALID, RI or XI could not
2089 * have triggered it. Skip the expensive test..
2091 if (use_bbit_insns()) {
2092 uasm_il_bbit0(&p, &r, wr.r1, ilog2(_PAGE_VALID),
2093 label_tlbl_goaround1);
2095 uasm_i_andi(&p, wr.r3, wr.r1, _PAGE_VALID);
2096 uasm_il_beqz(&p, &r, wr.r3, label_tlbl_goaround1);
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
2106 WARN(cpu_has_tlbex_tlbp_race(), "Unhandled race in RiXi path");
2110 if (cpu_has_mips_r2_exec_hazard)
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);
2117 uasm_i_andi(&p, wr.r3, wr.r2, sizeof(pte_t));
2118 uasm_i_beqz(&p, wr.r3, 8);
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);
2125 * If the entryLo (now in wr.r3) is valid (bit 1), RI or
2126 * XI must have triggered it.
2128 if (use_bbit_insns()) {
2129 uasm_il_bbit1(&p, &r, wr.r3, 1, label_nopage_tlbl);
2131 uasm_l_tlbl_goaround1(&l, p);
2133 uasm_i_andi(&p, wr.r3, wr.r3, 2);
2134 uasm_il_bnez(&p, &r, wr.r3, label_nopage_tlbl);
2137 uasm_l_tlbl_goaround1(&l, p);
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);
2142 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
2144 * This is the entry point when build_r4000_tlbchange_handler_head
2145 * spots a huge page.
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);
2152 if (cpu_has_rixi && !cpu_has_rixiex) {
2154 * If the page is not _PAGE_VALID, RI or XI could not
2155 * have triggered it. Skip the expensive test..
2157 if (use_bbit_insns()) {
2158 uasm_il_bbit0(&p, &r, wr.r1, ilog2(_PAGE_VALID),
2159 label_tlbl_goaround2);
2161 uasm_i_andi(&p, wr.r3, wr.r1, _PAGE_VALID);
2162 uasm_il_beqz(&p, &r, wr.r3, label_tlbl_goaround2);
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
2172 WARN(cpu_has_tlbex_tlbp_race(), "Unhandled race in RiXi path");
2176 if (cpu_has_mips_r2_exec_hazard)
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);
2183 uasm_i_andi(&p, wr.r3, wr.r2, sizeof(pte_t));
2184 uasm_i_beqz(&p, wr.r3, 8);
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);
2191 * If the entryLo (now in wr.r3) is valid (bit 1), RI or
2192 * XI must have triggered it.
2194 if (use_bbit_insns()) {
2195 uasm_il_bbit0(&p, &r, wr.r3, 1, label_tlbl_goaround2);
2197 uasm_i_andi(&p, wr.r3, wr.r3, 2);
2198 uasm_il_beqz(&p, &r, wr.r3, label_tlbl_goaround2);
2200 if (PM_DEFAULT_MASK == 0)
2203 * We clobbered C0_PAGEMASK, restore it. On the other branch
2204 * it is restored in build_huge_tlb_write_entry.
2206 build_restore_pagemask(&p, &r, wr.r3, label_nopage_tlbl, 0);
2208 uasm_l_tlbl_goaround2(&l, p);
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);
2214 uasm_l_nopage_tlbl(&l, p);
2215 if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
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));
2225 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
2228 if (p >= (u32 *)handle_tlbl_end)
2229 panic("TLB load handler fastpath space exceeded");
2231 uasm_resolve_relocs(relocs, labels);
2232 pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
2233 (unsigned int)(p - (u32 *)handle_tlbl));
2235 dump_handler("r4000_tlb_load", handle_tlbl, handle_tlbl_end);
2238 static void build_r4000_tlb_store_handler(void)
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;
2245 memset(p, 0, handle_tlbs_end - (char *)p);
2246 memset(labels, 0, sizeof(labels));
2247 memset(relocs, 0, sizeof(relocs));
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);
2256 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
2258 * This is the entry point when
2259 * build_r4000_tlbchange_handler_head spots a huge page.
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);
2270 uasm_l_nopage_tlbs(&l, p);
2271 if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
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));
2281 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
2284 if (p >= (u32 *)handle_tlbs_end)
2285 panic("TLB store handler fastpath space exceeded");
2287 uasm_resolve_relocs(relocs, labels);
2288 pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
2289 (unsigned int)(p - (u32 *)handle_tlbs));
2291 dump_handler("r4000_tlb_store", handle_tlbs, handle_tlbs_end);
2294 static void build_r4000_tlb_modify_handler(void)
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;
2301 memset(p, 0, handle_tlbm_end - (char *)p);
2302 memset(labels, 0, sizeof(labels));
2303 memset(relocs, 0, sizeof(relocs));
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);
2313 #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
2315 * This is the entry point when
2316 * build_r4000_tlbchange_handler_head spots a huge page.
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);
2327 uasm_l_nopage_tlbm(&l, p);
2328 if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
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));
2338 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
2341 if (p >= (u32 *)handle_tlbm_end)
2342 panic("TLB modify handler fastpath space exceeded");
2344 uasm_resolve_relocs(relocs, labels);
2345 pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
2346 (unsigned int)(p - (u32 *)handle_tlbm));
2348 dump_handler("r4000_tlb_modify", handle_tlbm, handle_tlbm_end);
2351 static void flush_tlb_handlers(void)
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);
2363 static void print_htw_config(void)
2365 unsigned long config;
2367 const int field = 2 * sizeof(unsigned long);
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",
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);
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",
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);
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",
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);
2400 static void config_htw_params(void)
2402 unsigned long pwfield, pwsize, ptei;
2403 unsigned int config;
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.
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;
2425 /* Set the PTEI right shift */
2426 ptei = _PAGE_GLOBAL_SHIFT << MIPS_PWFIELD_PTEI_SHIFT;
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)
2434 pr_warn("Unsupported PTEI field value: 0x%lx. HTW will not be enabled",
2437 * Drop option to avoid HTW being enabled via another path
2440 current_cpu_data.options &= ~MIPS_CPU_HTW;
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;
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;
2456 write_c0_pwsize(pwsize);
2458 /* Make sure everything is set before we enable the HTW */
2459 back_to_back_c0_hazard();
2462 * Enable HTW (and only for XUSeg on 64-bit), and disable the rest of
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");
2474 static void config_xpa_params(void)
2477 unsigned int pagegrain;
2479 if (mips_xpa_disabled) {
2480 pr_info("Extended Physical Addressing (XPA) disabled\n");
2484 pagegrain = read_c0_pagegrain();
2485 write_c0_pagegrain(pagegrain | PG_ELPA);
2486 back_to_back_c0_hazard();
2487 pagegrain = read_c0_pagegrain();
2489 if (pagegrain & PG_ELPA)
2490 pr_info("Extended Physical Addressing (XPA) enabled\n");
2492 panic("Extended Physical Addressing (XPA) disabled");
2496 static void check_pabits(void)
2498 unsigned long entry;
2499 unsigned pabits, fillbits;
2501 if (!cpu_has_rixi || _PAGE_NO_EXEC == 0) {
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.
2510 write_c0_entrylo0(~0ul);
2511 back_to_back_c0_hazard();
2512 entry = read_c0_entrylo0();
2514 /* clear all non-PFN bits */
2515 entry &= ~((1 << MIPS_ENTRYLO_PFN_SHIFT) - 1);
2516 entry &= ~(MIPS_ENTRYLO_RI | MIPS_ENTRYLO_XI);
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);
2522 /* minus the RI & XI bits */
2523 fillbits -= min_t(unsigned, fillbits, 2);
2525 if (fillbits >= ilog2(_PAGE_NO_EXEC))
2526 fill_includes_sw_bits = true;
2528 pr_debug("Entry* registers contain %u fill bits\n", fillbits);
2531 void build_tlb_refill_handler(void)
2534 * The refill handler is generated per-CPU, multi-node systems
2535 * may have local storage for it. The other handlers are only
2538 static int run_once = 0;
2540 if (IS_ENABLED(CONFIG_XPA) && !cpu_has_rixi)
2541 panic("Kernels supporting XPA currently require CPUs with RIXI");
2543 output_pgtable_bits_defines();
2547 check_for_high_segbits = current_cpu_data.vmbits > (PGDIR_SHIFT + PGD_TABLE_ORDER + PAGE_SHIFT - 3);
2551 #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
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();
2562 panic("No R3000 TLB refill handler");
2571 scratch_reg = allocate_kscratch();
2573 build_r4000_tlb_load_handler();
2574 build_r4000_tlb_store_handler();
2575 build_r4000_tlb_modify_handler();
2577 build_loongson3_tlb_refill_handler();
2579 build_r4000_tlb_refill_handler();
2580 flush_tlb_handlers();
2584 config_xpa_params();
2586 config_htw_params();