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.
13 * ... and the days got worse and worse and now you see
14 * I've gone completly out of my mind.
16 * They're coming to take me a away haha
17 * they're coming to take me a away hoho hihi haha
18 * to the funny farm where code is beautiful all the time ...
20 * (Condolences to Napoleon XIV)
23 #include <linux/bug.h>
24 #include <linux/kernel.h>
25 #include <linux/types.h>
26 #include <linux/smp.h>
27 #include <linux/string.h>
28 #include <linux/init.h>
29 #include <linux/cache.h>
31 #include <asm/cacheflush.h>
32 #include <asm/pgtable.h>
37 * TLB load/store/modify handlers.
39 * Only the fastpath gets synthesized at runtime, the slowpath for
40 * do_page_fault remains normal asm.
42 extern void tlb_do_page_fault_0(void);
43 extern void tlb_do_page_fault_1(void);
46 static inline int r45k_bvahwbug(void)
48 /* XXX: We should probe for the presence of this bug, but we don't. */
52 static inline int r4k_250MHZhwbug(void)
54 /* XXX: We should probe for the presence of this bug, but we don't. */
58 static inline int __maybe_unused bcm1250_m3_war(void)
60 return BCM1250_M3_WAR;
63 static inline int __maybe_unused r10000_llsc_war(void)
65 return R10000_LLSC_WAR;
68 static int use_bbit_insns(void)
70 switch (current_cpu_type()) {
71 case CPU_CAVIUM_OCTEON:
72 case CPU_CAVIUM_OCTEON_PLUS:
73 case CPU_CAVIUM_OCTEON2:
80 static int use_lwx_insns(void)
82 switch (current_cpu_type()) {
83 case CPU_CAVIUM_OCTEON2:
89 #if defined(CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE) && \
90 CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE > 0
91 static bool scratchpad_available(void)
95 static int scratchpad_offset(int i)
98 * CVMSEG starts at address -32768 and extends for
99 * CAVIUM_OCTEON_CVMSEG_SIZE 128 byte cache lines.
101 i += 1; /* Kernel use starts at the top and works down. */
102 return CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE * 128 - (8 * i) - 32768;
105 static bool scratchpad_available(void)
109 static int scratchpad_offset(int i)
115 * Found by experiment: At least some revisions of the 4kc throw under
116 * some circumstances a machine check exception, triggered by invalid
117 * values in the index register. Delaying the tlbp instruction until
118 * after the next branch, plus adding an additional nop in front of
119 * tlbwi/tlbwr avoids the invalid index register values. Nobody knows
120 * why; it's not an issue caused by the core RTL.
123 static int __cpuinit m4kc_tlbp_war(void)
125 return (current_cpu_data.processor_id & 0xffff00) ==
126 (PRID_COMP_MIPS | PRID_IMP_4KC);
129 /* Handle labels (which must be positive integers). */
131 label_second_part = 1,
137 label_tlbl_goaround1,
138 label_tlbl_goaround2,
142 label_smp_pgtable_change,
143 label_r3000_write_probe_fail,
144 label_large_segbits_fault,
145 #ifdef CONFIG_HUGETLB_PAGE
146 label_tlb_huge_update,
150 UASM_L_LA(_second_part)
153 UASM_L_LA(_vmalloc_done)
154 UASM_L_LA(_tlbw_hazard)
156 UASM_L_LA(_tlbl_goaround1)
157 UASM_L_LA(_tlbl_goaround2)
158 UASM_L_LA(_nopage_tlbl)
159 UASM_L_LA(_nopage_tlbs)
160 UASM_L_LA(_nopage_tlbm)
161 UASM_L_LA(_smp_pgtable_change)
162 UASM_L_LA(_r3000_write_probe_fail)
163 UASM_L_LA(_large_segbits_fault)
164 #ifdef CONFIG_HUGETLB_PAGE
165 UASM_L_LA(_tlb_huge_update)
169 * For debug purposes.
171 static inline void dump_handler(const u32 *handler, int count)
175 pr_debug("\t.set push\n");
176 pr_debug("\t.set noreorder\n");
178 for (i = 0; i < count; i++)
179 pr_debug("\t%p\t.word 0x%08x\n", &handler[i], handler[i]);
181 pr_debug("\t.set pop\n");
184 /* The only general purpose registers allowed in TLB handlers. */
188 /* Some CP0 registers */
189 #define C0_INDEX 0, 0
190 #define C0_ENTRYLO0 2, 0
191 #define C0_TCBIND 2, 2
192 #define C0_ENTRYLO1 3, 0
193 #define C0_CONTEXT 4, 0
194 #define C0_PAGEMASK 5, 0
195 #define C0_BADVADDR 8, 0
196 #define C0_ENTRYHI 10, 0
198 #define C0_XCONTEXT 20, 0
201 # define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_XCONTEXT)
203 # define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_CONTEXT)
206 /* The worst case length of the handler is around 18 instructions for
207 * R3000-style TLBs and up to 63 instructions for R4000-style TLBs.
208 * Maximum space available is 32 instructions for R3000 and 64
209 * instructions for R4000.
211 * We deliberately chose a buffer size of 128, so we won't scribble
212 * over anything important on overflow before we panic.
214 static u32 tlb_handler[128] __cpuinitdata;
216 /* simply assume worst case size for labels and relocs */
217 static struct uasm_label labels[128] __cpuinitdata;
218 static struct uasm_reloc relocs[128] __cpuinitdata;
221 static int check_for_high_segbits __cpuinitdata;
224 static int check_for_high_segbits __cpuinitdata;
226 static unsigned int kscratch_used_mask __cpuinitdata;
228 static int __cpuinit allocate_kscratch(void)
231 unsigned int a = cpu_data[0].kscratch_mask & ~kscratch_used_mask;
238 r--; /* make it zero based */
240 kscratch_used_mask |= (1 << r);
245 static int scratch_reg __cpuinitdata;
246 static int pgd_reg __cpuinitdata;
247 enum vmalloc64_mode {not_refill, refill_scratch, refill_noscratch};
249 #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
252 * CONFIG_MIPS_PGD_C0_CONTEXT implies 64 bit and lack of pgd_current,
253 * we cannot do r3000 under these circumstances.
255 * Declare pgd_current here instead of including mmu_context.h to avoid type
256 * conflicts for tlbmiss_handler_setup_pgd
258 extern unsigned long pgd_current[];
261 * The R3000 TLB handler is simple.
263 static void __cpuinit build_r3000_tlb_refill_handler(void)
265 long pgdc = (long)pgd_current;
268 memset(tlb_handler, 0, sizeof(tlb_handler));
271 uasm_i_mfc0(&p, K0, C0_BADVADDR);
272 uasm_i_lui(&p, K1, uasm_rel_hi(pgdc)); /* cp0 delay */
273 uasm_i_lw(&p, K1, uasm_rel_lo(pgdc), K1);
274 uasm_i_srl(&p, K0, K0, 22); /* load delay */
275 uasm_i_sll(&p, K0, K0, 2);
276 uasm_i_addu(&p, K1, K1, K0);
277 uasm_i_mfc0(&p, K0, C0_CONTEXT);
278 uasm_i_lw(&p, K1, 0, K1); /* cp0 delay */
279 uasm_i_andi(&p, K0, K0, 0xffc); /* load delay */
280 uasm_i_addu(&p, K1, K1, K0);
281 uasm_i_lw(&p, K0, 0, K1);
282 uasm_i_nop(&p); /* load delay */
283 uasm_i_mtc0(&p, K0, C0_ENTRYLO0);
284 uasm_i_mfc0(&p, K1, C0_EPC); /* cp0 delay */
285 uasm_i_tlbwr(&p); /* cp0 delay */
287 uasm_i_rfe(&p); /* branch delay */
289 if (p > tlb_handler + 32)
290 panic("TLB refill handler space exceeded");
292 pr_debug("Wrote TLB refill handler (%u instructions).\n",
293 (unsigned int)(p - tlb_handler));
295 memcpy((void *)ebase, tlb_handler, 0x80);
297 dump_handler((u32 *)ebase, 32);
299 #endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
302 * The R4000 TLB handler is much more complicated. We have two
303 * consecutive handler areas with 32 instructions space each.
304 * Since they aren't used at the same time, we can overflow in the
305 * other one.To keep things simple, we first assume linear space,
306 * then we relocate it to the final handler layout as needed.
308 static u32 final_handler[64] __cpuinitdata;
313 * From the IDT errata for the QED RM5230 (Nevada), processor revision 1.0:
314 * 2. A timing hazard exists for the TLBP instruction.
316 * stalling_instruction
319 * The JTLB is being read for the TLBP throughout the stall generated by the
320 * previous instruction. This is not really correct as the stalling instruction
321 * can modify the address used to access the JTLB. The failure symptom is that
322 * the TLBP instruction will use an address created for the stalling instruction
323 * and not the address held in C0_ENHI and thus report the wrong results.
325 * The software work-around is to not allow the instruction preceding the TLBP
326 * to stall - make it an NOP or some other instruction guaranteed not to stall.
328 * Errata 2 will not be fixed. This errata is also on the R5000.
330 * As if we MIPS hackers wouldn't know how to nop pipelines happy ...
332 static void __cpuinit __maybe_unused build_tlb_probe_entry(u32 **p)
334 switch (current_cpu_type()) {
335 /* Found by experiment: R4600 v2.0/R4700 needs this, too. */
352 * Write random or indexed TLB entry, and care about the hazards from
353 * the preceeding mtc0 and for the following eret.
355 enum tlb_write_entry { tlb_random, tlb_indexed };
357 static void __cpuinit build_tlb_write_entry(u32 **p, struct uasm_label **l,
358 struct uasm_reloc **r,
359 enum tlb_write_entry wmode)
361 void(*tlbw)(u32 **) = NULL;
364 case tlb_random: tlbw = uasm_i_tlbwr; break;
365 case tlb_indexed: tlbw = uasm_i_tlbwi; break;
368 if (cpu_has_mips_r2) {
369 if (cpu_has_mips_r2_exec_hazard)
375 switch (current_cpu_type()) {
383 * This branch uses up a mtc0 hazard nop slot and saves
384 * two nops after the tlbw instruction.
386 uasm_il_bgezl(p, r, 0, label_tlbw_hazard);
388 uasm_l_tlbw_hazard(l, *p);
433 uasm_i_nop(p); /* QED specifies 2 nops hazard */
435 * This branch uses up a mtc0 hazard nop slot and saves
436 * a nop after the tlbw instruction.
438 uasm_il_bgezl(p, r, 0, label_tlbw_hazard);
440 uasm_l_tlbw_hazard(l, *p);
453 * When the JTLB is updated by tlbwi or tlbwr, a subsequent
454 * use of the JTLB for instructions should not occur for 4
455 * cpu cycles and use for data translations should not occur
495 panic("No TLB refill handler yet (CPU type: %d)",
496 current_cpu_data.cputype);
501 static __cpuinit __maybe_unused void build_convert_pte_to_entrylo(u32 **p,
504 if (kernel_uses_smartmips_rixi) {
505 UASM_i_SRL(p, reg, reg, ilog2(_PAGE_NO_EXEC));
506 UASM_i_ROTR(p, reg, reg, ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
508 #ifdef CONFIG_64BIT_PHYS_ADDR
509 uasm_i_dsrl_safe(p, reg, reg, ilog2(_PAGE_GLOBAL));
511 UASM_i_SRL(p, reg, reg, ilog2(_PAGE_GLOBAL));
516 #ifdef CONFIG_HUGETLB_PAGE
518 static __cpuinit void build_restore_pagemask(u32 **p,
519 struct uasm_reloc **r,
524 if (restore_scratch) {
525 /* Reset default page size */
526 if (PM_DEFAULT_MASK >> 16) {
527 uasm_i_lui(p, tmp, PM_DEFAULT_MASK >> 16);
528 uasm_i_ori(p, tmp, tmp, PM_DEFAULT_MASK & 0xffff);
529 uasm_i_mtc0(p, tmp, C0_PAGEMASK);
530 uasm_il_b(p, r, lid);
531 } else if (PM_DEFAULT_MASK) {
532 uasm_i_ori(p, tmp, 0, PM_DEFAULT_MASK);
533 uasm_i_mtc0(p, tmp, C0_PAGEMASK);
534 uasm_il_b(p, r, lid);
536 uasm_i_mtc0(p, 0, C0_PAGEMASK);
537 uasm_il_b(p, r, lid);
540 UASM_i_MFC0(p, 1, 31, scratch_reg);
542 UASM_i_LW(p, 1, scratchpad_offset(0), 0);
544 /* Reset default page size */
545 if (PM_DEFAULT_MASK >> 16) {
546 uasm_i_lui(p, tmp, PM_DEFAULT_MASK >> 16);
547 uasm_i_ori(p, tmp, tmp, PM_DEFAULT_MASK & 0xffff);
548 uasm_il_b(p, r, lid);
549 uasm_i_mtc0(p, tmp, C0_PAGEMASK);
550 } else if (PM_DEFAULT_MASK) {
551 uasm_i_ori(p, tmp, 0, PM_DEFAULT_MASK);
552 uasm_il_b(p, r, lid);
553 uasm_i_mtc0(p, tmp, C0_PAGEMASK);
555 uasm_il_b(p, r, lid);
556 uasm_i_mtc0(p, 0, C0_PAGEMASK);
561 static __cpuinit void build_huge_tlb_write_entry(u32 **p,
562 struct uasm_label **l,
563 struct uasm_reloc **r,
565 enum tlb_write_entry wmode,
568 /* Set huge page tlb entry size */
569 uasm_i_lui(p, tmp, PM_HUGE_MASK >> 16);
570 uasm_i_ori(p, tmp, tmp, PM_HUGE_MASK & 0xffff);
571 uasm_i_mtc0(p, tmp, C0_PAGEMASK);
573 build_tlb_write_entry(p, l, r, wmode);
575 build_restore_pagemask(p, r, tmp, label_leave, restore_scratch);
579 * Check if Huge PTE is present, if so then jump to LABEL.
581 static void __cpuinit
582 build_is_huge_pte(u32 **p, struct uasm_reloc **r, unsigned int tmp,
583 unsigned int pmd, int lid)
585 UASM_i_LW(p, tmp, 0, pmd);
586 if (use_bbit_insns()) {
587 uasm_il_bbit1(p, r, tmp, ilog2(_PAGE_HUGE), lid);
589 uasm_i_andi(p, tmp, tmp, _PAGE_HUGE);
590 uasm_il_bnez(p, r, tmp, lid);
594 static __cpuinit void build_huge_update_entries(u32 **p,
601 * A huge PTE describes an area the size of the
602 * configured huge page size. This is twice the
603 * of the large TLB entry size we intend to use.
604 * A TLB entry half the size of the configured
605 * huge page size is configured into entrylo0
606 * and entrylo1 to cover the contiguous huge PTE
609 small_sequence = (HPAGE_SIZE >> 7) < 0x10000;
611 /* We can clobber tmp. It isn't used after this.*/
613 uasm_i_lui(p, tmp, HPAGE_SIZE >> (7 + 16));
615 build_convert_pte_to_entrylo(p, pte);
616 UASM_i_MTC0(p, pte, C0_ENTRYLO0); /* load it */
617 /* convert to entrylo1 */
619 UASM_i_ADDIU(p, pte, pte, HPAGE_SIZE >> 7);
621 UASM_i_ADDU(p, pte, pte, tmp);
623 UASM_i_MTC0(p, pte, C0_ENTRYLO1); /* load it */
626 static __cpuinit void build_huge_handler_tail(u32 **p,
627 struct uasm_reloc **r,
628 struct uasm_label **l,
633 UASM_i_SC(p, pte, 0, ptr);
634 uasm_il_beqz(p, r, pte, label_tlb_huge_update);
635 UASM_i_LW(p, pte, 0, ptr); /* Needed because SC killed our PTE */
637 UASM_i_SW(p, pte, 0, ptr);
639 build_huge_update_entries(p, pte, ptr);
640 build_huge_tlb_write_entry(p, l, r, pte, tlb_indexed, 0);
642 #endif /* CONFIG_HUGETLB_PAGE */
646 * TMP and PTR are scratch.
647 * TMP will be clobbered, PTR will hold the pmd entry.
649 static void __cpuinit
650 build_get_pmde64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
651 unsigned int tmp, unsigned int ptr)
653 #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
654 long pgdc = (long)pgd_current;
657 * The vmalloc handling is not in the hotpath.
659 uasm_i_dmfc0(p, tmp, C0_BADVADDR);
661 if (check_for_high_segbits) {
663 * The kernel currently implicitely assumes that the
664 * MIPS SEGBITS parameter for the processor is
665 * (PGDIR_SHIFT+PGDIR_BITS) or less, and will never
666 * allocate virtual addresses outside the maximum
667 * range for SEGBITS = (PGDIR_SHIFT+PGDIR_BITS). But
668 * that doesn't prevent user code from accessing the
669 * higher xuseg addresses. Here, we make sure that
670 * everything but the lower xuseg addresses goes down
671 * the module_alloc/vmalloc path.
673 uasm_i_dsrl_safe(p, ptr, tmp, PGDIR_SHIFT + PGD_ORDER + PAGE_SHIFT - 3);
674 uasm_il_bnez(p, r, ptr, label_vmalloc);
676 uasm_il_bltz(p, r, tmp, label_vmalloc);
678 /* No uasm_i_nop needed here, since the next insn doesn't touch TMP. */
680 #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
682 /* pgd is in pgd_reg */
683 UASM_i_MFC0(p, ptr, 31, pgd_reg);
686 * &pgd << 11 stored in CONTEXT [23..63].
688 UASM_i_MFC0(p, ptr, C0_CONTEXT);
690 /* Clear lower 23 bits of context. */
691 uasm_i_dins(p, ptr, 0, 0, 23);
693 /* 1 0 1 0 1 << 6 xkphys cached */
694 uasm_i_ori(p, ptr, ptr, 0x540);
695 uasm_i_drotr(p, ptr, ptr, 11);
697 #elif defined(CONFIG_SMP)
698 # ifdef CONFIG_MIPS_MT_SMTC
700 * SMTC uses TCBind value as "CPU" index
702 uasm_i_mfc0(p, ptr, C0_TCBIND);
703 uasm_i_dsrl_safe(p, ptr, ptr, 19);
706 * 64 bit SMP running in XKPHYS has smp_processor_id() << 3
709 uasm_i_dmfc0(p, ptr, C0_CONTEXT);
710 uasm_i_dsrl_safe(p, ptr, ptr, 23);
712 UASM_i_LA_mostly(p, tmp, pgdc);
713 uasm_i_daddu(p, ptr, ptr, tmp);
714 uasm_i_dmfc0(p, tmp, C0_BADVADDR);
715 uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
717 UASM_i_LA_mostly(p, ptr, pgdc);
718 uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
721 uasm_l_vmalloc_done(l, *p);
723 /* get pgd offset in bytes */
724 uasm_i_dsrl_safe(p, tmp, tmp, PGDIR_SHIFT - 3);
726 uasm_i_andi(p, tmp, tmp, (PTRS_PER_PGD - 1)<<3);
727 uasm_i_daddu(p, ptr, ptr, tmp); /* add in pgd offset */
728 #ifndef __PAGETABLE_PMD_FOLDED
729 uasm_i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
730 uasm_i_ld(p, ptr, 0, ptr); /* get pmd pointer */
731 uasm_i_dsrl_safe(p, tmp, tmp, PMD_SHIFT-3); /* get pmd offset in bytes */
732 uasm_i_andi(p, tmp, tmp, (PTRS_PER_PMD - 1)<<3);
733 uasm_i_daddu(p, ptr, ptr, tmp); /* add in pmd offset */
738 * BVADDR is the faulting address, PTR is scratch.
739 * PTR will hold the pgd for vmalloc.
741 static void __cpuinit
742 build_get_pgd_vmalloc64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
743 unsigned int bvaddr, unsigned int ptr,
744 enum vmalloc64_mode mode)
746 long swpd = (long)swapper_pg_dir;
747 int single_insn_swpd;
748 int did_vmalloc_branch = 0;
750 single_insn_swpd = uasm_in_compat_space_p(swpd) && !uasm_rel_lo(swpd);
752 uasm_l_vmalloc(l, *p);
754 if (mode != not_refill && check_for_high_segbits) {
755 if (single_insn_swpd) {
756 uasm_il_bltz(p, r, bvaddr, label_vmalloc_done);
757 uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
758 did_vmalloc_branch = 1;
761 uasm_il_bgez(p, r, bvaddr, label_large_segbits_fault);
764 if (!did_vmalloc_branch) {
765 if (uasm_in_compat_space_p(swpd) && !uasm_rel_lo(swpd)) {
766 uasm_il_b(p, r, label_vmalloc_done);
767 uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
769 UASM_i_LA_mostly(p, ptr, swpd);
770 uasm_il_b(p, r, label_vmalloc_done);
771 if (uasm_in_compat_space_p(swpd))
772 uasm_i_addiu(p, ptr, ptr, uasm_rel_lo(swpd));
774 uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(swpd));
777 if (mode != not_refill && check_for_high_segbits) {
778 uasm_l_large_segbits_fault(l, *p);
780 * We get here if we are an xsseg address, or if we are
781 * an xuseg address above (PGDIR_SHIFT+PGDIR_BITS) boundary.
783 * Ignoring xsseg (assume disabled so would generate
784 * (address errors?), the only remaining possibility
785 * is the upper xuseg addresses. On processors with
786 * TLB_SEGBITS <= PGDIR_SHIFT+PGDIR_BITS, these
787 * addresses would have taken an address error. We try
788 * to mimic that here by taking a load/istream page
791 UASM_i_LA(p, ptr, (unsigned long)tlb_do_page_fault_0);
794 if (mode == refill_scratch) {
796 UASM_i_MFC0(p, 1, 31, scratch_reg);
798 UASM_i_LW(p, 1, scratchpad_offset(0), 0);
805 #else /* !CONFIG_64BIT */
808 * TMP and PTR are scratch.
809 * TMP will be clobbered, PTR will hold the pgd entry.
811 static void __cpuinit __maybe_unused
812 build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
814 long pgdc = (long)pgd_current;
816 /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
818 #ifdef CONFIG_MIPS_MT_SMTC
820 * SMTC uses TCBind value as "CPU" index
822 uasm_i_mfc0(p, ptr, C0_TCBIND);
823 UASM_i_LA_mostly(p, tmp, pgdc);
824 uasm_i_srl(p, ptr, ptr, 19);
827 * smp_processor_id() << 3 is stored in CONTEXT.
829 uasm_i_mfc0(p, ptr, C0_CONTEXT);
830 UASM_i_LA_mostly(p, tmp, pgdc);
831 uasm_i_srl(p, ptr, ptr, 23);
833 uasm_i_addu(p, ptr, tmp, ptr);
835 UASM_i_LA_mostly(p, ptr, pgdc);
837 uasm_i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
838 uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
839 uasm_i_srl(p, tmp, tmp, PGDIR_SHIFT); /* get pgd only bits */
840 uasm_i_sll(p, tmp, tmp, PGD_T_LOG2);
841 uasm_i_addu(p, ptr, ptr, tmp); /* add in pgd offset */
844 #endif /* !CONFIG_64BIT */
846 static void __cpuinit build_adjust_context(u32 **p, unsigned int ctx)
848 unsigned int shift = 4 - (PTE_T_LOG2 + 1) + PAGE_SHIFT - 12;
849 unsigned int mask = (PTRS_PER_PTE / 2 - 1) << (PTE_T_LOG2 + 1);
851 switch (current_cpu_type()) {
868 UASM_i_SRL(p, ctx, ctx, shift);
869 uasm_i_andi(p, ctx, ctx, mask);
872 static void __cpuinit build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
875 * Bug workaround for the Nevada. It seems as if under certain
876 * circumstances the move from cp0_context might produce a
877 * bogus result when the mfc0 instruction and its consumer are
878 * in a different cacheline or a load instruction, probably any
879 * memory reference, is between them.
881 switch (current_cpu_type()) {
883 UASM_i_LW(p, ptr, 0, ptr);
884 GET_CONTEXT(p, tmp); /* get context reg */
888 GET_CONTEXT(p, tmp); /* get context reg */
889 UASM_i_LW(p, ptr, 0, ptr);
893 build_adjust_context(p, tmp);
894 UASM_i_ADDU(p, ptr, ptr, tmp); /* add in offset */
897 static void __cpuinit build_update_entries(u32 **p, unsigned int tmp,
901 * 64bit address support (36bit on a 32bit CPU) in a 32bit
902 * Kernel is a special case. Only a few CPUs use it.
904 #ifdef CONFIG_64BIT_PHYS_ADDR
905 if (cpu_has_64bits) {
906 uasm_i_ld(p, tmp, 0, ptep); /* get even pte */
907 uasm_i_ld(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
908 if (kernel_uses_smartmips_rixi) {
909 UASM_i_SRL(p, tmp, tmp, ilog2(_PAGE_NO_EXEC));
910 UASM_i_SRL(p, ptep, ptep, ilog2(_PAGE_NO_EXEC));
911 UASM_i_ROTR(p, tmp, tmp, ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
912 UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
913 UASM_i_ROTR(p, ptep, ptep, ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
915 uasm_i_dsrl_safe(p, tmp, tmp, ilog2(_PAGE_GLOBAL)); /* convert to entrylo0 */
916 UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
917 uasm_i_dsrl_safe(p, ptep, ptep, ilog2(_PAGE_GLOBAL)); /* convert to entrylo1 */
919 UASM_i_MTC0(p, ptep, C0_ENTRYLO1); /* load it */
921 int pte_off_even = sizeof(pte_t) / 2;
922 int pte_off_odd = pte_off_even + sizeof(pte_t);
924 /* The pte entries are pre-shifted */
925 uasm_i_lw(p, tmp, pte_off_even, ptep); /* get even pte */
926 UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
927 uasm_i_lw(p, ptep, pte_off_odd, ptep); /* get odd pte */
928 UASM_i_MTC0(p, ptep, C0_ENTRYLO1); /* load it */
931 UASM_i_LW(p, tmp, 0, ptep); /* get even pte */
932 UASM_i_LW(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
934 build_tlb_probe_entry(p);
935 if (kernel_uses_smartmips_rixi) {
936 UASM_i_SRL(p, tmp, tmp, ilog2(_PAGE_NO_EXEC));
937 UASM_i_SRL(p, ptep, ptep, ilog2(_PAGE_NO_EXEC));
938 UASM_i_ROTR(p, tmp, tmp, ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
939 if (r4k_250MHZhwbug())
940 UASM_i_MTC0(p, 0, C0_ENTRYLO0);
941 UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
942 UASM_i_ROTR(p, ptep, ptep, ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
944 UASM_i_SRL(p, tmp, tmp, ilog2(_PAGE_GLOBAL)); /* convert to entrylo0 */
945 if (r4k_250MHZhwbug())
946 UASM_i_MTC0(p, 0, C0_ENTRYLO0);
947 UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
948 UASM_i_SRL(p, ptep, ptep, ilog2(_PAGE_GLOBAL)); /* convert to entrylo1 */
950 uasm_i_mfc0(p, tmp, C0_INDEX);
952 if (r4k_250MHZhwbug())
953 UASM_i_MTC0(p, 0, C0_ENTRYLO1);
954 UASM_i_MTC0(p, ptep, C0_ENTRYLO1); /* load it */
958 struct mips_huge_tlb_info {
963 static struct mips_huge_tlb_info __cpuinit
964 build_fast_tlb_refill_handler (u32 **p, struct uasm_label **l,
965 struct uasm_reloc **r, unsigned int tmp,
966 unsigned int ptr, int c0_scratch)
968 struct mips_huge_tlb_info rv;
969 unsigned int even, odd;
970 int vmalloc_branch_delay_filled = 0;
971 const int scratch = 1; /* Our extra working register */
973 rv.huge_pte = scratch;
974 rv.restore_scratch = 0;
976 if (check_for_high_segbits) {
977 UASM_i_MFC0(p, tmp, C0_BADVADDR);
980 UASM_i_MFC0(p, ptr, 31, pgd_reg);
982 UASM_i_MFC0(p, ptr, C0_CONTEXT);
985 UASM_i_MTC0(p, scratch, 31, c0_scratch);
987 UASM_i_SW(p, scratch, scratchpad_offset(0), 0);
989 uasm_i_dsrl_safe(p, scratch, tmp,
990 PGDIR_SHIFT + PGD_ORDER + PAGE_SHIFT - 3);
991 uasm_il_bnez(p, r, scratch, label_vmalloc);
994 vmalloc_branch_delay_filled = 1;
995 /* Clear lower 23 bits of context. */
996 uasm_i_dins(p, ptr, 0, 0, 23);
1000 UASM_i_MFC0(p, ptr, 31, pgd_reg);
1002 UASM_i_MFC0(p, ptr, C0_CONTEXT);
1004 UASM_i_MFC0(p, tmp, C0_BADVADDR);
1006 if (c0_scratch >= 0)
1007 UASM_i_MTC0(p, scratch, 31, c0_scratch);
1009 UASM_i_SW(p, scratch, scratchpad_offset(0), 0);
1012 /* Clear lower 23 bits of context. */
1013 uasm_i_dins(p, ptr, 0, 0, 23);
1015 uasm_il_bltz(p, r, tmp, label_vmalloc);
1018 if (pgd_reg == -1) {
1019 vmalloc_branch_delay_filled = 1;
1020 /* 1 0 1 0 1 << 6 xkphys cached */
1021 uasm_i_ori(p, ptr, ptr, 0x540);
1022 uasm_i_drotr(p, ptr, ptr, 11);
1025 #ifdef __PAGETABLE_PMD_FOLDED
1026 #define LOC_PTEP scratch
1028 #define LOC_PTEP ptr
1031 if (!vmalloc_branch_delay_filled)
1032 /* get pgd offset in bytes */
1033 uasm_i_dsrl_safe(p, scratch, tmp, PGDIR_SHIFT - 3);
1035 uasm_l_vmalloc_done(l, *p);
1039 * fall-through case = badvaddr *pgd_current
1040 * vmalloc case = badvaddr swapper_pg_dir
1043 if (vmalloc_branch_delay_filled)
1044 /* get pgd offset in bytes */
1045 uasm_i_dsrl_safe(p, scratch, tmp, PGDIR_SHIFT - 3);
1047 #ifdef __PAGETABLE_PMD_FOLDED
1048 GET_CONTEXT(p, tmp); /* get context reg */
1050 uasm_i_andi(p, scratch, scratch, (PTRS_PER_PGD - 1) << 3);
1052 if (use_lwx_insns()) {
1053 UASM_i_LWX(p, LOC_PTEP, scratch, ptr);
1055 uasm_i_daddu(p, ptr, ptr, scratch); /* add in pgd offset */
1056 uasm_i_ld(p, LOC_PTEP, 0, ptr); /* get pmd pointer */
1059 #ifndef __PAGETABLE_PMD_FOLDED
1060 /* get pmd offset in bytes */
1061 uasm_i_dsrl_safe(p, scratch, tmp, PMD_SHIFT - 3);
1062 uasm_i_andi(p, scratch, scratch, (PTRS_PER_PMD - 1) << 3);
1063 GET_CONTEXT(p, tmp); /* get context reg */
1065 if (use_lwx_insns()) {
1066 UASM_i_LWX(p, scratch, scratch, ptr);
1068 uasm_i_daddu(p, ptr, ptr, scratch); /* add in pmd offset */
1069 UASM_i_LW(p, scratch, 0, ptr);
1072 /* Adjust the context during the load latency. */
1073 build_adjust_context(p, tmp);
1075 #ifdef CONFIG_HUGETLB_PAGE
1076 uasm_il_bbit1(p, r, scratch, ilog2(_PAGE_HUGE), label_tlb_huge_update);
1078 * The in the LWX case we don't want to do the load in the
1079 * delay slot. It cannot issue in the same cycle and may be
1080 * speculative and unneeded.
1082 if (use_lwx_insns())
1084 #endif /* CONFIG_HUGETLB_PAGE */
1087 /* build_update_entries */
1088 if (use_lwx_insns()) {
1091 UASM_i_LWX(p, even, scratch, tmp);
1092 UASM_i_ADDIU(p, tmp, tmp, sizeof(pte_t));
1093 UASM_i_LWX(p, odd, scratch, tmp);
1095 UASM_i_ADDU(p, ptr, scratch, tmp); /* add in offset */
1098 UASM_i_LW(p, even, 0, ptr); /* get even pte */
1099 UASM_i_LW(p, odd, sizeof(pte_t), ptr); /* get odd pte */
1101 if (kernel_uses_smartmips_rixi) {
1102 uasm_i_dsrl_safe(p, even, even, ilog2(_PAGE_NO_EXEC));
1103 uasm_i_dsrl_safe(p, odd, odd, ilog2(_PAGE_NO_EXEC));
1104 uasm_i_drotr(p, even, even,
1105 ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
1106 UASM_i_MTC0(p, even, C0_ENTRYLO0); /* load it */
1107 uasm_i_drotr(p, odd, odd,
1108 ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
1110 uasm_i_dsrl_safe(p, even, even, ilog2(_PAGE_GLOBAL));
1111 UASM_i_MTC0(p, even, C0_ENTRYLO0); /* load it */
1112 uasm_i_dsrl_safe(p, odd, odd, ilog2(_PAGE_GLOBAL));
1114 UASM_i_MTC0(p, odd, C0_ENTRYLO1); /* load it */
1116 if (c0_scratch >= 0) {
1117 UASM_i_MFC0(p, scratch, 31, c0_scratch);
1118 build_tlb_write_entry(p, l, r, tlb_random);
1119 uasm_l_leave(l, *p);
1120 rv.restore_scratch = 1;
1121 } else if (PAGE_SHIFT == 14 || PAGE_SHIFT == 13) {
1122 build_tlb_write_entry(p, l, r, tlb_random);
1123 uasm_l_leave(l, *p);
1124 UASM_i_LW(p, scratch, scratchpad_offset(0), 0);
1126 UASM_i_LW(p, scratch, scratchpad_offset(0), 0);
1127 build_tlb_write_entry(p, l, r, tlb_random);
1128 uasm_l_leave(l, *p);
1129 rv.restore_scratch = 1;
1132 uasm_i_eret(p); /* return from trap */
1138 * For a 64-bit kernel, we are using the 64-bit XTLB refill exception
1139 * because EXL == 0. If we wrap, we can also use the 32 instruction
1140 * slots before the XTLB refill exception handler which belong to the
1141 * unused TLB refill exception.
1143 #define MIPS64_REFILL_INSNS 32
1145 static void __cpuinit build_r4000_tlb_refill_handler(void)
1147 u32 *p = tlb_handler;
1148 struct uasm_label *l = labels;
1149 struct uasm_reloc *r = relocs;
1151 unsigned int final_len;
1152 struct mips_huge_tlb_info htlb_info;
1153 enum vmalloc64_mode vmalloc_mode;
1155 memset(tlb_handler, 0, sizeof(tlb_handler));
1156 memset(labels, 0, sizeof(labels));
1157 memset(relocs, 0, sizeof(relocs));
1158 memset(final_handler, 0, sizeof(final_handler));
1160 if (scratch_reg == 0)
1161 scratch_reg = allocate_kscratch();
1163 if ((scratch_reg > 0 || scratchpad_available()) && use_bbit_insns()) {
1164 htlb_info = build_fast_tlb_refill_handler(&p, &l, &r, K0, K1,
1166 vmalloc_mode = refill_scratch;
1168 htlb_info.huge_pte = K0;
1169 htlb_info.restore_scratch = 0;
1170 vmalloc_mode = refill_noscratch;
1172 * create the plain linear handler
1174 if (bcm1250_m3_war()) {
1175 unsigned int segbits = 44;
1177 uasm_i_dmfc0(&p, K0, C0_BADVADDR);
1178 uasm_i_dmfc0(&p, K1, C0_ENTRYHI);
1179 uasm_i_xor(&p, K0, K0, K1);
1180 uasm_i_dsrl_safe(&p, K1, K0, 62);
1181 uasm_i_dsrl_safe(&p, K0, K0, 12 + 1);
1182 uasm_i_dsll_safe(&p, K0, K0, 64 + 12 + 1 - segbits);
1183 uasm_i_or(&p, K0, K0, K1);
1184 uasm_il_bnez(&p, &r, K0, label_leave);
1185 /* No need for uasm_i_nop */
1189 build_get_pmde64(&p, &l, &r, K0, K1); /* get pmd in K1 */
1191 build_get_pgde32(&p, K0, K1); /* get pgd in K1 */
1194 #ifdef CONFIG_HUGETLB_PAGE
1195 build_is_huge_pte(&p, &r, K0, K1, label_tlb_huge_update);
1198 build_get_ptep(&p, K0, K1);
1199 build_update_entries(&p, K0, K1);
1200 build_tlb_write_entry(&p, &l, &r, tlb_random);
1201 uasm_l_leave(&l, p);
1202 uasm_i_eret(&p); /* return from trap */
1204 #ifdef CONFIG_HUGETLB_PAGE
1205 uasm_l_tlb_huge_update(&l, p);
1206 build_huge_update_entries(&p, htlb_info.huge_pte, K1);
1207 build_huge_tlb_write_entry(&p, &l, &r, K0, tlb_random,
1208 htlb_info.restore_scratch);
1212 build_get_pgd_vmalloc64(&p, &l, &r, K0, K1, vmalloc_mode);
1216 * Overflow check: For the 64bit handler, we need at least one
1217 * free instruction slot for the wrap-around branch. In worst
1218 * case, if the intended insertion point is a delay slot, we
1219 * need three, with the second nop'ed and the third being
1222 /* Loongson2 ebase is different than r4k, we have more space */
1223 #if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
1224 if ((p - tlb_handler) > 64)
1225 panic("TLB refill handler space exceeded");
1227 if (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 1)
1228 || (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 3)
1229 && uasm_insn_has_bdelay(relocs,
1230 tlb_handler + MIPS64_REFILL_INSNS - 3)))
1231 panic("TLB refill handler space exceeded");
1235 * Now fold the handler in the TLB refill handler space.
1237 #if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
1239 /* Simplest case, just copy the handler. */
1240 uasm_copy_handler(relocs, labels, tlb_handler, p, f);
1241 final_len = p - tlb_handler;
1242 #else /* CONFIG_64BIT */
1243 f = final_handler + MIPS64_REFILL_INSNS;
1244 if ((p - tlb_handler) <= MIPS64_REFILL_INSNS) {
1245 /* Just copy the handler. */
1246 uasm_copy_handler(relocs, labels, tlb_handler, p, f);
1247 final_len = p - tlb_handler;
1249 #if defined(CONFIG_HUGETLB_PAGE)
1250 const enum label_id ls = label_tlb_huge_update;
1252 const enum label_id ls = label_vmalloc;
1258 for (i = 0; i < ARRAY_SIZE(labels) && labels[i].lab != ls; i++)
1260 BUG_ON(i == ARRAY_SIZE(labels));
1261 split = labels[i].addr;
1264 * See if we have overflown one way or the other.
1266 if (split > tlb_handler + MIPS64_REFILL_INSNS ||
1267 split < p - MIPS64_REFILL_INSNS)
1272 * Split two instructions before the end. One
1273 * for the branch and one for the instruction
1274 * in the delay slot.
1276 split = tlb_handler + MIPS64_REFILL_INSNS - 2;
1279 * If the branch would fall in a delay slot,
1280 * we must back up an additional instruction
1281 * so that it is no longer in a delay slot.
1283 if (uasm_insn_has_bdelay(relocs, split - 1))
1286 /* Copy first part of the handler. */
1287 uasm_copy_handler(relocs, labels, tlb_handler, split, f);
1288 f += split - tlb_handler;
1291 /* Insert branch. */
1292 uasm_l_split(&l, final_handler);
1293 uasm_il_b(&f, &r, label_split);
1294 if (uasm_insn_has_bdelay(relocs, split))
1297 uasm_copy_handler(relocs, labels,
1298 split, split + 1, f);
1299 uasm_move_labels(labels, f, f + 1, -1);
1305 /* Copy the rest of the handler. */
1306 uasm_copy_handler(relocs, labels, split, p, final_handler);
1307 final_len = (f - (final_handler + MIPS64_REFILL_INSNS)) +
1310 #endif /* CONFIG_64BIT */
1312 uasm_resolve_relocs(relocs, labels);
1313 pr_debug("Wrote TLB refill handler (%u instructions).\n",
1316 memcpy((void *)ebase, final_handler, 0x100);
1318 dump_handler((u32 *)ebase, 64);
1322 * 128 instructions for the fastpath handler is generous and should
1323 * never be exceeded.
1325 #define FASTPATH_SIZE 128
1327 u32 handle_tlbl[FASTPATH_SIZE] __cacheline_aligned;
1328 u32 handle_tlbs[FASTPATH_SIZE] __cacheline_aligned;
1329 u32 handle_tlbm[FASTPATH_SIZE] __cacheline_aligned;
1330 #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
1331 u32 tlbmiss_handler_setup_pgd[16] __cacheline_aligned;
1333 static void __cpuinit build_r4000_setup_pgd(void)
1337 u32 *p = tlbmiss_handler_setup_pgd;
1338 struct uasm_label *l = labels;
1339 struct uasm_reloc *r = relocs;
1341 memset(tlbmiss_handler_setup_pgd, 0, sizeof(tlbmiss_handler_setup_pgd));
1342 memset(labels, 0, sizeof(labels));
1343 memset(relocs, 0, sizeof(relocs));
1345 pgd_reg = allocate_kscratch();
1347 if (pgd_reg == -1) {
1348 /* PGD << 11 in c0_Context */
1350 * If it is a ckseg0 address, convert to a physical
1351 * address. Shifting right by 29 and adding 4 will
1352 * result in zero for these addresses.
1355 UASM_i_SRA(&p, a1, a0, 29);
1356 UASM_i_ADDIU(&p, a1, a1, 4);
1357 uasm_il_bnez(&p, &r, a1, label_tlbl_goaround1);
1359 uasm_i_dinsm(&p, a0, 0, 29, 64 - 29);
1360 uasm_l_tlbl_goaround1(&l, p);
1361 UASM_i_SLL(&p, a0, a0, 11);
1363 UASM_i_MTC0(&p, a0, C0_CONTEXT);
1365 /* PGD in c0_KScratch */
1367 UASM_i_MTC0(&p, a0, 31, pgd_reg);
1369 if (p - tlbmiss_handler_setup_pgd > ARRAY_SIZE(tlbmiss_handler_setup_pgd))
1370 panic("tlbmiss_handler_setup_pgd space exceeded");
1371 uasm_resolve_relocs(relocs, labels);
1372 pr_debug("Wrote tlbmiss_handler_setup_pgd (%u instructions).\n",
1373 (unsigned int)(p - tlbmiss_handler_setup_pgd));
1375 dump_handler(tlbmiss_handler_setup_pgd,
1376 ARRAY_SIZE(tlbmiss_handler_setup_pgd));
1380 static void __cpuinit
1381 iPTE_LW(u32 **p, unsigned int pte, unsigned int ptr)
1384 # ifdef CONFIG_64BIT_PHYS_ADDR
1386 uasm_i_lld(p, pte, 0, ptr);
1389 UASM_i_LL(p, pte, 0, ptr);
1391 # ifdef CONFIG_64BIT_PHYS_ADDR
1393 uasm_i_ld(p, pte, 0, ptr);
1396 UASM_i_LW(p, pte, 0, ptr);
1400 static void __cpuinit
1401 iPTE_SW(u32 **p, struct uasm_reloc **r, unsigned int pte, unsigned int ptr,
1404 #ifdef CONFIG_64BIT_PHYS_ADDR
1405 unsigned int hwmode = mode & (_PAGE_VALID | _PAGE_DIRTY);
1408 uasm_i_ori(p, pte, pte, mode);
1410 # ifdef CONFIG_64BIT_PHYS_ADDR
1412 uasm_i_scd(p, pte, 0, ptr);
1415 UASM_i_SC(p, pte, 0, ptr);
1417 if (r10000_llsc_war())
1418 uasm_il_beqzl(p, r, pte, label_smp_pgtable_change);
1420 uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
1422 # ifdef CONFIG_64BIT_PHYS_ADDR
1423 if (!cpu_has_64bits) {
1424 /* no uasm_i_nop needed */
1425 uasm_i_ll(p, pte, sizeof(pte_t) / 2, ptr);
1426 uasm_i_ori(p, pte, pte, hwmode);
1427 uasm_i_sc(p, pte, sizeof(pte_t) / 2, ptr);
1428 uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
1429 /* no uasm_i_nop needed */
1430 uasm_i_lw(p, pte, 0, ptr);
1437 # ifdef CONFIG_64BIT_PHYS_ADDR
1439 uasm_i_sd(p, pte, 0, ptr);
1442 UASM_i_SW(p, pte, 0, ptr);
1444 # ifdef CONFIG_64BIT_PHYS_ADDR
1445 if (!cpu_has_64bits) {
1446 uasm_i_lw(p, pte, sizeof(pte_t) / 2, ptr);
1447 uasm_i_ori(p, pte, pte, hwmode);
1448 uasm_i_sw(p, pte, sizeof(pte_t) / 2, ptr);
1449 uasm_i_lw(p, pte, 0, ptr);
1456 * Check if PTE is present, if not then jump to LABEL. PTR points to
1457 * the page table where this PTE is located, PTE will be re-loaded
1458 * with it's original value.
1460 static void __cpuinit
1461 build_pte_present(u32 **p, struct uasm_reloc **r,
1462 unsigned int pte, unsigned int ptr, enum label_id lid)
1464 if (kernel_uses_smartmips_rixi) {
1465 if (use_bbit_insns()) {
1466 uasm_il_bbit0(p, r, pte, ilog2(_PAGE_PRESENT), lid);
1469 uasm_i_andi(p, pte, pte, _PAGE_PRESENT);
1470 uasm_il_beqz(p, r, pte, lid);
1471 iPTE_LW(p, pte, ptr);
1474 uasm_i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
1475 uasm_i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
1476 uasm_il_bnez(p, r, pte, lid);
1477 iPTE_LW(p, pte, ptr);
1481 /* Make PTE valid, store result in PTR. */
1482 static void __cpuinit
1483 build_make_valid(u32 **p, struct uasm_reloc **r, unsigned int pte,
1486 unsigned int mode = _PAGE_VALID | _PAGE_ACCESSED;
1488 iPTE_SW(p, r, pte, ptr, mode);
1492 * Check if PTE can be written to, if not branch to LABEL. Regardless
1493 * restore PTE with value from PTR when done.
1495 static void __cpuinit
1496 build_pte_writable(u32 **p, struct uasm_reloc **r,
1497 unsigned int pte, unsigned int ptr, enum label_id lid)
1499 if (use_bbit_insns()) {
1500 uasm_il_bbit0(p, r, pte, ilog2(_PAGE_PRESENT), lid);
1502 uasm_il_bbit0(p, r, pte, ilog2(_PAGE_WRITE), lid);
1505 uasm_i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
1506 uasm_i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
1507 uasm_il_bnez(p, r, pte, lid);
1508 iPTE_LW(p, pte, ptr);
1512 /* Make PTE writable, update software status bits as well, then store
1515 static void __cpuinit
1516 build_make_write(u32 **p, struct uasm_reloc **r, unsigned int pte,
1519 unsigned int mode = (_PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID
1522 iPTE_SW(p, r, pte, ptr, mode);
1526 * Check if PTE can be modified, if not branch to LABEL. Regardless
1527 * restore PTE with value from PTR when done.
1529 static void __cpuinit
1530 build_pte_modifiable(u32 **p, struct uasm_reloc **r,
1531 unsigned int pte, unsigned int ptr, enum label_id lid)
1533 if (use_bbit_insns()) {
1534 uasm_il_bbit0(p, r, pte, ilog2(_PAGE_WRITE), lid);
1537 uasm_i_andi(p, pte, pte, _PAGE_WRITE);
1538 uasm_il_beqz(p, r, pte, lid);
1539 iPTE_LW(p, pte, ptr);
1543 #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
1547 * R3000 style TLB load/store/modify handlers.
1551 * This places the pte into ENTRYLO0 and writes it with tlbwi.
1554 static void __cpuinit
1555 build_r3000_pte_reload_tlbwi(u32 **p, unsigned int pte, unsigned int tmp)
1557 uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
1558 uasm_i_mfc0(p, tmp, C0_EPC); /* cp0 delay */
1561 uasm_i_rfe(p); /* branch delay */
1565 * This places the pte into ENTRYLO0 and writes it with tlbwi
1566 * or tlbwr as appropriate. This is because the index register
1567 * may have the probe fail bit set as a result of a trap on a
1568 * kseg2 access, i.e. without refill. Then it returns.
1570 static void __cpuinit
1571 build_r3000_tlb_reload_write(u32 **p, struct uasm_label **l,
1572 struct uasm_reloc **r, unsigned int pte,
1575 uasm_i_mfc0(p, tmp, C0_INDEX);
1576 uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
1577 uasm_il_bltz(p, r, tmp, label_r3000_write_probe_fail); /* cp0 delay */
1578 uasm_i_mfc0(p, tmp, C0_EPC); /* branch delay */
1579 uasm_i_tlbwi(p); /* cp0 delay */
1581 uasm_i_rfe(p); /* branch delay */
1582 uasm_l_r3000_write_probe_fail(l, *p);
1583 uasm_i_tlbwr(p); /* cp0 delay */
1585 uasm_i_rfe(p); /* branch delay */
1588 static void __cpuinit
1589 build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
1592 long pgdc = (long)pgd_current;
1594 uasm_i_mfc0(p, pte, C0_BADVADDR);
1595 uasm_i_lui(p, ptr, uasm_rel_hi(pgdc)); /* cp0 delay */
1596 uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
1597 uasm_i_srl(p, pte, pte, 22); /* load delay */
1598 uasm_i_sll(p, pte, pte, 2);
1599 uasm_i_addu(p, ptr, ptr, pte);
1600 uasm_i_mfc0(p, pte, C0_CONTEXT);
1601 uasm_i_lw(p, ptr, 0, ptr); /* cp0 delay */
1602 uasm_i_andi(p, pte, pte, 0xffc); /* load delay */
1603 uasm_i_addu(p, ptr, ptr, pte);
1604 uasm_i_lw(p, pte, 0, ptr);
1605 uasm_i_tlbp(p); /* load delay */
1608 static void __cpuinit build_r3000_tlb_load_handler(void)
1610 u32 *p = handle_tlbl;
1611 struct uasm_label *l = labels;
1612 struct uasm_reloc *r = relocs;
1614 memset(handle_tlbl, 0, sizeof(handle_tlbl));
1615 memset(labels, 0, sizeof(labels));
1616 memset(relocs, 0, sizeof(relocs));
1618 build_r3000_tlbchange_handler_head(&p, K0, K1);
1619 build_pte_present(&p, &r, K0, K1, label_nopage_tlbl);
1620 uasm_i_nop(&p); /* load delay */
1621 build_make_valid(&p, &r, K0, K1);
1622 build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
1624 uasm_l_nopage_tlbl(&l, p);
1625 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
1628 if ((p - handle_tlbl) > FASTPATH_SIZE)
1629 panic("TLB load handler fastpath space exceeded");
1631 uasm_resolve_relocs(relocs, labels);
1632 pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
1633 (unsigned int)(p - handle_tlbl));
1635 dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
1638 static void __cpuinit build_r3000_tlb_store_handler(void)
1640 u32 *p = handle_tlbs;
1641 struct uasm_label *l = labels;
1642 struct uasm_reloc *r = relocs;
1644 memset(handle_tlbs, 0, sizeof(handle_tlbs));
1645 memset(labels, 0, sizeof(labels));
1646 memset(relocs, 0, sizeof(relocs));
1648 build_r3000_tlbchange_handler_head(&p, K0, K1);
1649 build_pte_writable(&p, &r, K0, K1, label_nopage_tlbs);
1650 uasm_i_nop(&p); /* load delay */
1651 build_make_write(&p, &r, K0, K1);
1652 build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
1654 uasm_l_nopage_tlbs(&l, p);
1655 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1658 if ((p - handle_tlbs) > FASTPATH_SIZE)
1659 panic("TLB store handler fastpath space exceeded");
1661 uasm_resolve_relocs(relocs, labels);
1662 pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
1663 (unsigned int)(p - handle_tlbs));
1665 dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
1668 static void __cpuinit build_r3000_tlb_modify_handler(void)
1670 u32 *p = handle_tlbm;
1671 struct uasm_label *l = labels;
1672 struct uasm_reloc *r = relocs;
1674 memset(handle_tlbm, 0, sizeof(handle_tlbm));
1675 memset(labels, 0, sizeof(labels));
1676 memset(relocs, 0, sizeof(relocs));
1678 build_r3000_tlbchange_handler_head(&p, K0, K1);
1679 build_pte_modifiable(&p, &r, K0, K1, label_nopage_tlbm);
1680 uasm_i_nop(&p); /* load delay */
1681 build_make_write(&p, &r, K0, K1);
1682 build_r3000_pte_reload_tlbwi(&p, K0, K1);
1684 uasm_l_nopage_tlbm(&l, p);
1685 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1688 if ((p - handle_tlbm) > FASTPATH_SIZE)
1689 panic("TLB modify handler fastpath space exceeded");
1691 uasm_resolve_relocs(relocs, labels);
1692 pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
1693 (unsigned int)(p - handle_tlbm));
1695 dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
1697 #endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
1700 * R4000 style TLB load/store/modify handlers.
1702 static void __cpuinit
1703 build_r4000_tlbchange_handler_head(u32 **p, struct uasm_label **l,
1704 struct uasm_reloc **r, unsigned int pte,
1708 build_get_pmde64(p, l, r, pte, ptr); /* get pmd in ptr */
1710 build_get_pgde32(p, pte, ptr); /* get pgd in ptr */
1713 #ifdef CONFIG_HUGETLB_PAGE
1715 * For huge tlb entries, pmd doesn't contain an address but
1716 * instead contains the tlb pte. Check the PAGE_HUGE bit and
1717 * see if we need to jump to huge tlb processing.
1719 build_is_huge_pte(p, r, pte, ptr, label_tlb_huge_update);
1722 UASM_i_MFC0(p, pte, C0_BADVADDR);
1723 UASM_i_LW(p, ptr, 0, ptr);
1724 UASM_i_SRL(p, pte, pte, PAGE_SHIFT + PTE_ORDER - PTE_T_LOG2);
1725 uasm_i_andi(p, pte, pte, (PTRS_PER_PTE - 1) << PTE_T_LOG2);
1726 UASM_i_ADDU(p, ptr, ptr, pte);
1729 uasm_l_smp_pgtable_change(l, *p);
1731 iPTE_LW(p, pte, ptr); /* get even pte */
1732 if (!m4kc_tlbp_war())
1733 build_tlb_probe_entry(p);
1736 static void __cpuinit
1737 build_r4000_tlbchange_handler_tail(u32 **p, struct uasm_label **l,
1738 struct uasm_reloc **r, unsigned int tmp,
1741 uasm_i_ori(p, ptr, ptr, sizeof(pte_t));
1742 uasm_i_xori(p, ptr, ptr, sizeof(pte_t));
1743 build_update_entries(p, tmp, ptr);
1744 build_tlb_write_entry(p, l, r, tlb_indexed);
1745 uasm_l_leave(l, *p);
1746 uasm_i_eret(p); /* return from trap */
1749 build_get_pgd_vmalloc64(p, l, r, tmp, ptr, not_refill);
1753 static void __cpuinit build_r4000_tlb_load_handler(void)
1755 u32 *p = handle_tlbl;
1756 struct uasm_label *l = labels;
1757 struct uasm_reloc *r = relocs;
1759 memset(handle_tlbl, 0, sizeof(handle_tlbl));
1760 memset(labels, 0, sizeof(labels));
1761 memset(relocs, 0, sizeof(relocs));
1763 if (bcm1250_m3_war()) {
1764 unsigned int segbits = 44;
1766 uasm_i_dmfc0(&p, K0, C0_BADVADDR);
1767 uasm_i_dmfc0(&p, K1, C0_ENTRYHI);
1768 uasm_i_xor(&p, K0, K0, K1);
1769 uasm_i_dsrl_safe(&p, K1, K0, 62);
1770 uasm_i_dsrl_safe(&p, K0, K0, 12 + 1);
1771 uasm_i_dsll_safe(&p, K0, K0, 64 + 12 + 1 - segbits);
1772 uasm_i_or(&p, K0, K0, K1);
1773 uasm_il_bnez(&p, &r, K0, label_leave);
1774 /* No need for uasm_i_nop */
1777 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
1778 build_pte_present(&p, &r, K0, K1, label_nopage_tlbl);
1779 if (m4kc_tlbp_war())
1780 build_tlb_probe_entry(&p);
1782 if (kernel_uses_smartmips_rixi) {
1784 * If the page is not _PAGE_VALID, RI or XI could not
1785 * have triggered it. Skip the expensive test..
1787 if (use_bbit_insns()) {
1788 uasm_il_bbit0(&p, &r, K0, ilog2(_PAGE_VALID),
1789 label_tlbl_goaround1);
1791 uasm_i_andi(&p, K0, K0, _PAGE_VALID);
1792 uasm_il_beqz(&p, &r, K0, label_tlbl_goaround1);
1797 /* Examine entrylo 0 or 1 based on ptr. */
1798 if (use_bbit_insns()) {
1799 uasm_i_bbit0(&p, K1, ilog2(sizeof(pte_t)), 8);
1801 uasm_i_andi(&p, K0, K1, sizeof(pte_t));
1802 uasm_i_beqz(&p, K0, 8);
1805 UASM_i_MFC0(&p, K0, C0_ENTRYLO0); /* load it in the delay slot*/
1806 UASM_i_MFC0(&p, K0, C0_ENTRYLO1); /* load it if ptr is odd */
1808 * If the entryLo (now in K0) is valid (bit 1), RI or
1809 * XI must have triggered it.
1811 if (use_bbit_insns()) {
1812 uasm_il_bbit1(&p, &r, K0, 1, label_nopage_tlbl);
1813 /* Reload the PTE value */
1814 iPTE_LW(&p, K0, K1);
1815 uasm_l_tlbl_goaround1(&l, p);
1817 uasm_i_andi(&p, K0, K0, 2);
1818 uasm_il_bnez(&p, &r, K0, label_nopage_tlbl);
1819 uasm_l_tlbl_goaround1(&l, p);
1820 /* Reload the PTE value */
1821 iPTE_LW(&p, K0, K1);
1824 build_make_valid(&p, &r, K0, K1);
1825 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1827 #ifdef CONFIG_HUGETLB_PAGE
1829 * This is the entry point when build_r4000_tlbchange_handler_head
1830 * spots a huge page.
1832 uasm_l_tlb_huge_update(&l, p);
1833 iPTE_LW(&p, K0, K1);
1834 build_pte_present(&p, &r, K0, K1, label_nopage_tlbl);
1835 build_tlb_probe_entry(&p);
1837 if (kernel_uses_smartmips_rixi) {
1839 * If the page is not _PAGE_VALID, RI or XI could not
1840 * have triggered it. Skip the expensive test..
1842 if (use_bbit_insns()) {
1843 uasm_il_bbit0(&p, &r, K0, ilog2(_PAGE_VALID),
1844 label_tlbl_goaround2);
1846 uasm_i_andi(&p, K0, K0, _PAGE_VALID);
1847 uasm_il_beqz(&p, &r, K0, label_tlbl_goaround2);
1852 /* Examine entrylo 0 or 1 based on ptr. */
1853 if (use_bbit_insns()) {
1854 uasm_i_bbit0(&p, K1, ilog2(sizeof(pte_t)), 8);
1856 uasm_i_andi(&p, K0, K1, sizeof(pte_t));
1857 uasm_i_beqz(&p, K0, 8);
1859 UASM_i_MFC0(&p, K0, C0_ENTRYLO0); /* load it in the delay slot*/
1860 UASM_i_MFC0(&p, K0, C0_ENTRYLO1); /* load it if ptr is odd */
1862 * If the entryLo (now in K0) is valid (bit 1), RI or
1863 * XI must have triggered it.
1865 if (use_bbit_insns()) {
1866 uasm_il_bbit0(&p, &r, K0, 1, label_tlbl_goaround2);
1868 uasm_i_andi(&p, K0, K0, 2);
1869 uasm_il_beqz(&p, &r, K0, label_tlbl_goaround2);
1871 /* Reload the PTE value */
1872 iPTE_LW(&p, K0, K1);
1875 * We clobbered C0_PAGEMASK, restore it. On the other branch
1876 * it is restored in build_huge_tlb_write_entry.
1878 build_restore_pagemask(&p, &r, K0, label_nopage_tlbl, 0);
1880 uasm_l_tlbl_goaround2(&l, p);
1882 uasm_i_ori(&p, K0, K0, (_PAGE_ACCESSED | _PAGE_VALID));
1883 build_huge_handler_tail(&p, &r, &l, K0, K1);
1886 uasm_l_nopage_tlbl(&l, p);
1887 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
1890 if ((p - handle_tlbl) > FASTPATH_SIZE)
1891 panic("TLB load handler fastpath space exceeded");
1893 uasm_resolve_relocs(relocs, labels);
1894 pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
1895 (unsigned int)(p - handle_tlbl));
1897 dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
1900 static void __cpuinit build_r4000_tlb_store_handler(void)
1902 u32 *p = handle_tlbs;
1903 struct uasm_label *l = labels;
1904 struct uasm_reloc *r = relocs;
1906 memset(handle_tlbs, 0, sizeof(handle_tlbs));
1907 memset(labels, 0, sizeof(labels));
1908 memset(relocs, 0, sizeof(relocs));
1910 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
1911 build_pte_writable(&p, &r, K0, K1, label_nopage_tlbs);
1912 if (m4kc_tlbp_war())
1913 build_tlb_probe_entry(&p);
1914 build_make_write(&p, &r, K0, K1);
1915 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1917 #ifdef CONFIG_HUGETLB_PAGE
1919 * This is the entry point when
1920 * build_r4000_tlbchange_handler_head spots a huge page.
1922 uasm_l_tlb_huge_update(&l, p);
1923 iPTE_LW(&p, K0, K1);
1924 build_pte_writable(&p, &r, K0, K1, label_nopage_tlbs);
1925 build_tlb_probe_entry(&p);
1926 uasm_i_ori(&p, K0, K0,
1927 _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
1928 build_huge_handler_tail(&p, &r, &l, K0, K1);
1931 uasm_l_nopage_tlbs(&l, p);
1932 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1935 if ((p - handle_tlbs) > FASTPATH_SIZE)
1936 panic("TLB store handler fastpath space exceeded");
1938 uasm_resolve_relocs(relocs, labels);
1939 pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
1940 (unsigned int)(p - handle_tlbs));
1942 dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
1945 static void __cpuinit build_r4000_tlb_modify_handler(void)
1947 u32 *p = handle_tlbm;
1948 struct uasm_label *l = labels;
1949 struct uasm_reloc *r = relocs;
1951 memset(handle_tlbm, 0, sizeof(handle_tlbm));
1952 memset(labels, 0, sizeof(labels));
1953 memset(relocs, 0, sizeof(relocs));
1955 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
1956 build_pte_modifiable(&p, &r, K0, K1, label_nopage_tlbm);
1957 if (m4kc_tlbp_war())
1958 build_tlb_probe_entry(&p);
1959 /* Present and writable bits set, set accessed and dirty bits. */
1960 build_make_write(&p, &r, K0, K1);
1961 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1963 #ifdef CONFIG_HUGETLB_PAGE
1965 * This is the entry point when
1966 * build_r4000_tlbchange_handler_head spots a huge page.
1968 uasm_l_tlb_huge_update(&l, p);
1969 iPTE_LW(&p, K0, K1);
1970 build_pte_modifiable(&p, &r, K0, K1, label_nopage_tlbm);
1971 build_tlb_probe_entry(&p);
1972 uasm_i_ori(&p, K0, K0,
1973 _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
1974 build_huge_handler_tail(&p, &r, &l, K0, K1);
1977 uasm_l_nopage_tlbm(&l, p);
1978 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1981 if ((p - handle_tlbm) > FASTPATH_SIZE)
1982 panic("TLB modify handler fastpath space exceeded");
1984 uasm_resolve_relocs(relocs, labels);
1985 pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
1986 (unsigned int)(p - handle_tlbm));
1988 dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
1991 void __cpuinit build_tlb_refill_handler(void)
1994 * The refill handler is generated per-CPU, multi-node systems
1995 * may have local storage for it. The other handlers are only
1998 static int run_once = 0;
2001 check_for_high_segbits = current_cpu_data.vmbits > (PGDIR_SHIFT + PGD_ORDER + PAGE_SHIFT - 3);
2004 switch (current_cpu_type()) {
2012 #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
2013 build_r3000_tlb_refill_handler();
2015 build_r3000_tlb_load_handler();
2016 build_r3000_tlb_store_handler();
2017 build_r3000_tlb_modify_handler();
2021 panic("No R3000 TLB refill handler");
2027 panic("No R6000 TLB refill handler yet");
2031 panic("No R8000 TLB refill handler yet");
2036 #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
2037 build_r4000_setup_pgd();
2039 build_r4000_tlb_load_handler();
2040 build_r4000_tlb_store_handler();
2041 build_r4000_tlb_modify_handler();
2044 build_r4000_tlb_refill_handler();
2048 void __cpuinit flush_tlb_handlers(void)
2050 local_flush_icache_range((unsigned long)handle_tlbl,
2051 (unsigned long)handle_tlbl + sizeof(handle_tlbl));
2052 local_flush_icache_range((unsigned long)handle_tlbs,
2053 (unsigned long)handle_tlbs + sizeof(handle_tlbs));
2054 local_flush_icache_range((unsigned long)handle_tlbm,
2055 (unsigned long)handle_tlbm + sizeof(handle_tlbm));
2056 #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
2057 local_flush_icache_range((unsigned long)tlbmiss_handler_setup_pgd,
2058 (unsigned long)tlbmiss_handler_setup_pgd + sizeof(handle_tlbm));