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 Maciej W. Rozycki
10 * Copyright (C) 2006 Ralf Baechle (ralf@linux-mips.org)
12 * ... and the days got worse and worse and now you see
13 * I've gone completly out of my mind.
15 * They're coming to take me a away haha
16 * they're coming to take me a away hoho hihi haha
17 * to the funny farm where code is beautiful all the time ...
19 * (Condolences to Napoleon XIV)
22 #include <linux/kernel.h>
23 #include <linux/types.h>
24 #include <linux/string.h>
25 #include <linux/init.h>
27 #include <asm/mmu_context.h>
32 static inline int r45k_bvahwbug(void)
34 /* XXX: We should probe for the presence of this bug, but we don't. */
38 static inline int r4k_250MHZhwbug(void)
40 /* XXX: We should probe for the presence of this bug, but we don't. */
44 static inline int __maybe_unused bcm1250_m3_war(void)
46 return BCM1250_M3_WAR;
49 static inline int __maybe_unused r10000_llsc_war(void)
51 return R10000_LLSC_WAR;
55 * Found by experiment: At least some revisions of the 4kc throw under
56 * some circumstances a machine check exception, triggered by invalid
57 * values in the index register. Delaying the tlbp instruction until
58 * after the next branch, plus adding an additional nop in front of
59 * tlbwi/tlbwr avoids the invalid index register values. Nobody knows
60 * why; it's not an issue caused by the core RTL.
63 static int __cpuinit m4kc_tlbp_war(void)
65 return (current_cpu_data.processor_id & 0xffff00) ==
66 (PRID_COMP_MIPS | PRID_IMP_4KC);
69 /* Handle labels (which must be positive integers). */
71 label_second_part = 1,
83 label_smp_pgtable_change,
84 label_r3000_write_probe_fail,
87 UASM_L_LA(_second_part)
90 UASM_L_LA(_module_alloc)
93 UASM_L_LA(_vmalloc_done)
94 UASM_L_LA(_tlbw_hazard)
96 UASM_L_LA(_nopage_tlbl)
97 UASM_L_LA(_nopage_tlbs)
98 UASM_L_LA(_nopage_tlbm)
99 UASM_L_LA(_smp_pgtable_change)
100 UASM_L_LA(_r3000_write_probe_fail)
103 * For debug purposes.
105 static inline void dump_handler(const u32 *handler, int count)
109 pr_debug("\t.set push\n");
110 pr_debug("\t.set noreorder\n");
112 for (i = 0; i < count; i++)
113 pr_debug("\t%p\t.word 0x%08x\n", &handler[i], handler[i]);
115 pr_debug("\t.set pop\n");
118 /* The only general purpose registers allowed in TLB handlers. */
122 /* Some CP0 registers */
123 #define C0_INDEX 0, 0
124 #define C0_ENTRYLO0 2, 0
125 #define C0_TCBIND 2, 2
126 #define C0_ENTRYLO1 3, 0
127 #define C0_CONTEXT 4, 0
128 #define C0_BADVADDR 8, 0
129 #define C0_ENTRYHI 10, 0
131 #define C0_XCONTEXT 20, 0
134 # define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_XCONTEXT)
136 # define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_CONTEXT)
139 /* The worst case length of the handler is around 18 instructions for
140 * R3000-style TLBs and up to 63 instructions for R4000-style TLBs.
141 * Maximum space available is 32 instructions for R3000 and 64
142 * instructions for R4000.
144 * We deliberately chose a buffer size of 128, so we won't scribble
145 * over anything important on overflow before we panic.
147 static u32 tlb_handler[128] __cpuinitdata;
149 /* simply assume worst case size for labels and relocs */
150 static struct uasm_label labels[128] __cpuinitdata;
151 static struct uasm_reloc relocs[128] __cpuinitdata;
154 * The R3000 TLB handler is simple.
156 static void __cpuinit build_r3000_tlb_refill_handler(void)
158 long pgdc = (long)pgd_current;
161 memset(tlb_handler, 0, sizeof(tlb_handler));
164 uasm_i_mfc0(&p, K0, C0_BADVADDR);
165 uasm_i_lui(&p, K1, uasm_rel_hi(pgdc)); /* cp0 delay */
166 uasm_i_lw(&p, K1, uasm_rel_lo(pgdc), K1);
167 uasm_i_srl(&p, K0, K0, 22); /* load delay */
168 uasm_i_sll(&p, K0, K0, 2);
169 uasm_i_addu(&p, K1, K1, K0);
170 uasm_i_mfc0(&p, K0, C0_CONTEXT);
171 uasm_i_lw(&p, K1, 0, K1); /* cp0 delay */
172 uasm_i_andi(&p, K0, K0, 0xffc); /* load delay */
173 uasm_i_addu(&p, K1, K1, K0);
174 uasm_i_lw(&p, K0, 0, K1);
175 uasm_i_nop(&p); /* load delay */
176 uasm_i_mtc0(&p, K0, C0_ENTRYLO0);
177 uasm_i_mfc0(&p, K1, C0_EPC); /* cp0 delay */
178 uasm_i_tlbwr(&p); /* cp0 delay */
180 uasm_i_rfe(&p); /* branch delay */
182 if (p > tlb_handler + 32)
183 panic("TLB refill handler space exceeded");
185 pr_debug("Wrote TLB refill handler (%u instructions).\n",
186 (unsigned int)(p - tlb_handler));
188 memcpy((void *)ebase, tlb_handler, 0x80);
190 dump_handler((u32 *)ebase, 32);
194 * The R4000 TLB handler is much more complicated. We have two
195 * consecutive handler areas with 32 instructions space each.
196 * Since they aren't used at the same time, we can overflow in the
197 * other one.To keep things simple, we first assume linear space,
198 * then we relocate it to the final handler layout as needed.
200 static u32 final_handler[64] __cpuinitdata;
205 * From the IDT errata for the QED RM5230 (Nevada), processor revision 1.0:
206 * 2. A timing hazard exists for the TLBP instruction.
208 * stalling_instruction
211 * The JTLB is being read for the TLBP throughout the stall generated by the
212 * previous instruction. This is not really correct as the stalling instruction
213 * can modify the address used to access the JTLB. The failure symptom is that
214 * the TLBP instruction will use an address created for the stalling instruction
215 * and not the address held in C0_ENHI and thus report the wrong results.
217 * The software work-around is to not allow the instruction preceding the TLBP
218 * to stall - make it an NOP or some other instruction guaranteed not to stall.
220 * Errata 2 will not be fixed. This errata is also on the R5000.
222 * As if we MIPS hackers wouldn't know how to nop pipelines happy ...
224 static void __cpuinit __maybe_unused build_tlb_probe_entry(u32 **p)
226 switch (current_cpu_type()) {
227 /* Found by experiment: R4600 v2.0/R4700 needs this, too. */
244 * Write random or indexed TLB entry, and care about the hazards from
245 * the preceeding mtc0 and for the following eret.
247 enum tlb_write_entry { tlb_random, tlb_indexed };
249 static void __cpuinit build_tlb_write_entry(u32 **p, struct uasm_label **l,
250 struct uasm_reloc **r,
251 enum tlb_write_entry wmode)
253 void(*tlbw)(u32 **) = NULL;
256 case tlb_random: tlbw = uasm_i_tlbwr; break;
257 case tlb_indexed: tlbw = uasm_i_tlbwi; break;
260 if (cpu_has_mips_r2) {
266 switch (current_cpu_type()) {
274 * This branch uses up a mtc0 hazard nop slot and saves
275 * two nops after the tlbw instruction.
277 uasm_il_bgezl(p, r, 0, label_tlbw_hazard);
279 uasm_l_tlbw_hazard(l, *p);
313 case CPU_CAVIUM_OCTEON:
322 uasm_i_nop(p); /* QED specifies 2 nops hazard */
324 * This branch uses up a mtc0 hazard nop slot and saves
325 * a nop after the tlbw instruction.
327 uasm_il_bgezl(p, r, 0, label_tlbw_hazard);
329 uasm_l_tlbw_hazard(l, *p);
342 * When the JTLB is updated by tlbwi or tlbwr, a subsequent
343 * use of the JTLB for instructions should not occur for 4
344 * cpu cycles and use for data translations should not occur
379 panic("No TLB refill handler yet (CPU type: %d)",
380 current_cpu_data.cputype);
387 * TMP and PTR are scratch.
388 * TMP will be clobbered, PTR will hold the pmd entry.
390 static void __cpuinit
391 build_get_pmde64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
392 unsigned int tmp, unsigned int ptr)
394 long pgdc = (long)pgd_current;
397 * The vmalloc handling is not in the hotpath.
399 uasm_i_dmfc0(p, tmp, C0_BADVADDR);
401 uasm_il_bltz(p, r, tmp, label_module_alloc);
403 uasm_il_bltz(p, r, tmp, label_vmalloc);
405 /* No uasm_i_nop needed here, since the next insn doesn't touch TMP. */
408 # ifdef CONFIG_MIPS_MT_SMTC
410 * SMTC uses TCBind value as "CPU" index
412 uasm_i_mfc0(p, ptr, C0_TCBIND);
413 uasm_i_dsrl(p, ptr, ptr, 19);
416 * 64 bit SMP running in XKPHYS has smp_processor_id() << 3
419 uasm_i_dmfc0(p, ptr, C0_CONTEXT);
420 uasm_i_dsrl(p, ptr, ptr, 23);
422 UASM_i_LA_mostly(p, tmp, pgdc);
423 uasm_i_daddu(p, ptr, ptr, tmp);
424 uasm_i_dmfc0(p, tmp, C0_BADVADDR);
425 uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
427 UASM_i_LA_mostly(p, ptr, pgdc);
428 uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
431 uasm_l_vmalloc_done(l, *p);
433 if (PGDIR_SHIFT - 3 < 32) /* get pgd offset in bytes */
434 uasm_i_dsrl(p, tmp, tmp, PGDIR_SHIFT-3);
436 uasm_i_dsrl32(p, tmp, tmp, PGDIR_SHIFT - 3 - 32);
438 uasm_i_andi(p, tmp, tmp, (PTRS_PER_PGD - 1)<<3);
439 uasm_i_daddu(p, ptr, ptr, tmp); /* add in pgd offset */
440 uasm_i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
441 uasm_i_ld(p, ptr, 0, ptr); /* get pmd pointer */
442 uasm_i_dsrl(p, tmp, tmp, PMD_SHIFT-3); /* get pmd offset in bytes */
443 uasm_i_andi(p, tmp, tmp, (PTRS_PER_PMD - 1)<<3);
444 uasm_i_daddu(p, ptr, ptr, tmp); /* add in pmd offset */
448 * BVADDR is the faulting address, PTR is scratch.
449 * PTR will hold the pgd for vmalloc.
451 static void __cpuinit
452 build_get_pgd_vmalloc64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
453 unsigned int bvaddr, unsigned int ptr)
455 long swpd = (long)swapper_pg_dir;
458 long modd = (long)module_pg_dir;
460 uasm_l_module_alloc(l, *p);
463 * VMALLOC_START >= 0xc000000000000000UL
464 * MODULE_START >= 0xe000000000000000UL
466 UASM_i_SLL(p, ptr, bvaddr, 2);
467 uasm_il_bgez(p, r, ptr, label_vmalloc);
469 if (uasm_in_compat_space_p(MODULE_START) &&
470 !uasm_rel_lo(MODULE_START)) {
471 uasm_i_lui(p, ptr, uasm_rel_hi(MODULE_START)); /* delay slot */
473 /* unlikely configuration */
474 uasm_i_nop(p); /* delay slot */
475 UASM_i_LA(p, ptr, MODULE_START);
477 uasm_i_dsubu(p, bvaddr, bvaddr, ptr);
479 if (uasm_in_compat_space_p(modd) && !uasm_rel_lo(modd)) {
480 uasm_il_b(p, r, label_vmalloc_done);
481 uasm_i_lui(p, ptr, uasm_rel_hi(modd));
483 UASM_i_LA_mostly(p, ptr, modd);
484 uasm_il_b(p, r, label_vmalloc_done);
485 if (uasm_in_compat_space_p(modd))
486 uasm_i_addiu(p, ptr, ptr, uasm_rel_lo(modd));
488 uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(modd));
491 uasm_l_vmalloc(l, *p);
492 if (uasm_in_compat_space_p(MODULE_START) &&
493 !uasm_rel_lo(MODULE_START) &&
494 MODULE_START << 32 == VMALLOC_START)
495 uasm_i_dsll32(p, ptr, ptr, 0); /* typical case */
497 UASM_i_LA(p, ptr, VMALLOC_START);
499 uasm_l_vmalloc(l, *p);
500 UASM_i_LA(p, ptr, VMALLOC_START);
502 uasm_i_dsubu(p, bvaddr, bvaddr, ptr);
504 if (uasm_in_compat_space_p(swpd) && !uasm_rel_lo(swpd)) {
505 uasm_il_b(p, r, label_vmalloc_done);
506 uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
508 UASM_i_LA_mostly(p, ptr, swpd);
509 uasm_il_b(p, r, label_vmalloc_done);
510 if (uasm_in_compat_space_p(swpd))
511 uasm_i_addiu(p, ptr, ptr, uasm_rel_lo(swpd));
513 uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(swpd));
517 #else /* !CONFIG_64BIT */
520 * TMP and PTR are scratch.
521 * TMP will be clobbered, PTR will hold the pgd entry.
523 static void __cpuinit __maybe_unused
524 build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
526 long pgdc = (long)pgd_current;
528 /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
530 #ifdef CONFIG_MIPS_MT_SMTC
532 * SMTC uses TCBind value as "CPU" index
534 uasm_i_mfc0(p, ptr, C0_TCBIND);
535 UASM_i_LA_mostly(p, tmp, pgdc);
536 uasm_i_srl(p, ptr, ptr, 19);
539 * smp_processor_id() << 3 is stored in CONTEXT.
541 uasm_i_mfc0(p, ptr, C0_CONTEXT);
542 UASM_i_LA_mostly(p, tmp, pgdc);
543 uasm_i_srl(p, ptr, ptr, 23);
545 uasm_i_addu(p, ptr, tmp, ptr);
547 UASM_i_LA_mostly(p, ptr, pgdc);
549 uasm_i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
550 uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
551 uasm_i_srl(p, tmp, tmp, PGDIR_SHIFT); /* get pgd only bits */
552 uasm_i_sll(p, tmp, tmp, PGD_T_LOG2);
553 uasm_i_addu(p, ptr, ptr, tmp); /* add in pgd offset */
556 #endif /* !CONFIG_64BIT */
558 static void __cpuinit build_adjust_context(u32 **p, unsigned int ctx)
560 unsigned int shift = 4 - (PTE_T_LOG2 + 1) + PAGE_SHIFT - 12;
561 unsigned int mask = (PTRS_PER_PTE / 2 - 1) << (PTE_T_LOG2 + 1);
563 switch (current_cpu_type()) {
580 UASM_i_SRL(p, ctx, ctx, shift);
581 uasm_i_andi(p, ctx, ctx, mask);
584 static void __cpuinit build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
587 * Bug workaround for the Nevada. It seems as if under certain
588 * circumstances the move from cp0_context might produce a
589 * bogus result when the mfc0 instruction and its consumer are
590 * in a different cacheline or a load instruction, probably any
591 * memory reference, is between them.
593 switch (current_cpu_type()) {
595 UASM_i_LW(p, ptr, 0, ptr);
596 GET_CONTEXT(p, tmp); /* get context reg */
600 GET_CONTEXT(p, tmp); /* get context reg */
601 UASM_i_LW(p, ptr, 0, ptr);
605 build_adjust_context(p, tmp);
606 UASM_i_ADDU(p, ptr, ptr, tmp); /* add in offset */
609 static void __cpuinit build_update_entries(u32 **p, unsigned int tmp,
613 * 64bit address support (36bit on a 32bit CPU) in a 32bit
614 * Kernel is a special case. Only a few CPUs use it.
616 #ifdef CONFIG_64BIT_PHYS_ADDR
617 if (cpu_has_64bits) {
618 uasm_i_ld(p, tmp, 0, ptep); /* get even pte */
619 uasm_i_ld(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
620 uasm_i_dsrl(p, tmp, tmp, 6); /* convert to entrylo0 */
621 uasm_i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
622 uasm_i_dsrl(p, ptep, ptep, 6); /* convert to entrylo1 */
623 uasm_i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
625 int pte_off_even = sizeof(pte_t) / 2;
626 int pte_off_odd = pte_off_even + sizeof(pte_t);
628 /* The pte entries are pre-shifted */
629 uasm_i_lw(p, tmp, pte_off_even, ptep); /* get even pte */
630 uasm_i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
631 uasm_i_lw(p, ptep, pte_off_odd, ptep); /* get odd pte */
632 uasm_i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
635 UASM_i_LW(p, tmp, 0, ptep); /* get even pte */
636 UASM_i_LW(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
638 build_tlb_probe_entry(p);
639 UASM_i_SRL(p, tmp, tmp, 6); /* convert to entrylo0 */
640 if (r4k_250MHZhwbug())
641 uasm_i_mtc0(p, 0, C0_ENTRYLO0);
642 uasm_i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
643 UASM_i_SRL(p, ptep, ptep, 6); /* convert to entrylo1 */
645 uasm_i_mfc0(p, tmp, C0_INDEX);
646 if (r4k_250MHZhwbug())
647 uasm_i_mtc0(p, 0, C0_ENTRYLO1);
648 uasm_i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
653 * For a 64-bit kernel, we are using the 64-bit XTLB refill exception
654 * because EXL == 0. If we wrap, we can also use the 32 instruction
655 * slots before the XTLB refill exception handler which belong to the
656 * unused TLB refill exception.
658 #define MIPS64_REFILL_INSNS 32
660 static void __cpuinit build_r4000_tlb_refill_handler(void)
662 u32 *p = tlb_handler;
663 struct uasm_label *l = labels;
664 struct uasm_reloc *r = relocs;
666 unsigned int final_len;
668 memset(tlb_handler, 0, sizeof(tlb_handler));
669 memset(labels, 0, sizeof(labels));
670 memset(relocs, 0, sizeof(relocs));
671 memset(final_handler, 0, sizeof(final_handler));
674 * create the plain linear handler
676 if (bcm1250_m3_war()) {
677 UASM_i_MFC0(&p, K0, C0_BADVADDR);
678 UASM_i_MFC0(&p, K1, C0_ENTRYHI);
679 uasm_i_xor(&p, K0, K0, K1);
680 UASM_i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
681 uasm_il_bnez(&p, &r, K0, label_leave);
682 /* No need for uasm_i_nop */
686 build_get_pmde64(&p, &l, &r, K0, K1); /* get pmd in K1 */
688 build_get_pgde32(&p, K0, K1); /* get pgd in K1 */
691 build_get_ptep(&p, K0, K1);
692 build_update_entries(&p, K0, K1);
693 build_tlb_write_entry(&p, &l, &r, tlb_random);
695 uasm_i_eret(&p); /* return from trap */
698 build_get_pgd_vmalloc64(&p, &l, &r, K0, K1);
702 * Overflow check: For the 64bit handler, we need at least one
703 * free instruction slot for the wrap-around branch. In worst
704 * case, if the intended insertion point is a delay slot, we
705 * need three, with the second nop'ed and the third being
708 /* Loongson2 ebase is different than r4k, we have more space */
709 #if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
710 if ((p - tlb_handler) > 64)
711 panic("TLB refill handler space exceeded");
713 if (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 1)
714 || (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 3)
715 && uasm_insn_has_bdelay(relocs,
716 tlb_handler + MIPS64_REFILL_INSNS - 3)))
717 panic("TLB refill handler space exceeded");
721 * Now fold the handler in the TLB refill handler space.
723 #if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
725 /* Simplest case, just copy the handler. */
726 uasm_copy_handler(relocs, labels, tlb_handler, p, f);
727 final_len = p - tlb_handler;
728 #else /* CONFIG_64BIT */
729 f = final_handler + MIPS64_REFILL_INSNS;
730 if ((p - tlb_handler) <= MIPS64_REFILL_INSNS) {
731 /* Just copy the handler. */
732 uasm_copy_handler(relocs, labels, tlb_handler, p, f);
733 final_len = p - tlb_handler;
736 * Split two instructions before the end. One for the
737 * branch and one for the instruction in the delay
740 u32 *split = tlb_handler + MIPS64_REFILL_INSNS - 2;
743 * Find the split point. If the branch would fall in
744 * a delay slot, we must back up an additional
745 * instruction so that it is no longer in a delay
748 if (uasm_insn_has_bdelay(relocs, split - 1))
751 /* Copy first part of the handler. */
752 uasm_copy_handler(relocs, labels, tlb_handler, split, f);
753 f += split - tlb_handler;
756 uasm_l_split(&l, final_handler);
757 uasm_il_b(&f, &r, label_split);
758 if (uasm_insn_has_bdelay(relocs, split))
761 uasm_copy_handler(relocs, labels, split, split + 1, f);
762 uasm_move_labels(labels, f, f + 1, -1);
767 /* Copy the rest of the handler. */
768 uasm_copy_handler(relocs, labels, split, p, final_handler);
769 final_len = (f - (final_handler + MIPS64_REFILL_INSNS)) +
772 #endif /* CONFIG_64BIT */
774 uasm_resolve_relocs(relocs, labels);
775 pr_debug("Wrote TLB refill handler (%u instructions).\n",
778 memcpy((void *)ebase, final_handler, 0x100);
780 dump_handler((u32 *)ebase, 64);
784 * TLB load/store/modify handlers.
786 * Only the fastpath gets synthesized at runtime, the slowpath for
787 * do_page_fault remains normal asm.
789 extern void tlb_do_page_fault_0(void);
790 extern void tlb_do_page_fault_1(void);
793 * 128 instructions for the fastpath handler is generous and should
796 #define FASTPATH_SIZE 128
798 u32 handle_tlbl[FASTPATH_SIZE] __cacheline_aligned;
799 u32 handle_tlbs[FASTPATH_SIZE] __cacheline_aligned;
800 u32 handle_tlbm[FASTPATH_SIZE] __cacheline_aligned;
802 static void __cpuinit
803 iPTE_LW(u32 **p, struct uasm_label **l, unsigned int pte, unsigned int ptr)
806 # ifdef CONFIG_64BIT_PHYS_ADDR
808 uasm_i_lld(p, pte, 0, ptr);
811 UASM_i_LL(p, pte, 0, ptr);
813 # ifdef CONFIG_64BIT_PHYS_ADDR
815 uasm_i_ld(p, pte, 0, ptr);
818 UASM_i_LW(p, pte, 0, ptr);
822 static void __cpuinit
823 iPTE_SW(u32 **p, struct uasm_reloc **r, unsigned int pte, unsigned int ptr,
826 #ifdef CONFIG_64BIT_PHYS_ADDR
827 unsigned int hwmode = mode & (_PAGE_VALID | _PAGE_DIRTY);
830 uasm_i_ori(p, pte, pte, mode);
832 # ifdef CONFIG_64BIT_PHYS_ADDR
834 uasm_i_scd(p, pte, 0, ptr);
837 UASM_i_SC(p, pte, 0, ptr);
839 if (r10000_llsc_war())
840 uasm_il_beqzl(p, r, pte, label_smp_pgtable_change);
842 uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
844 # ifdef CONFIG_64BIT_PHYS_ADDR
845 if (!cpu_has_64bits) {
846 /* no uasm_i_nop needed */
847 uasm_i_ll(p, pte, sizeof(pte_t) / 2, ptr);
848 uasm_i_ori(p, pte, pte, hwmode);
849 uasm_i_sc(p, pte, sizeof(pte_t) / 2, ptr);
850 uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
851 /* no uasm_i_nop needed */
852 uasm_i_lw(p, pte, 0, ptr);
859 # ifdef CONFIG_64BIT_PHYS_ADDR
861 uasm_i_sd(p, pte, 0, ptr);
864 UASM_i_SW(p, pte, 0, ptr);
866 # ifdef CONFIG_64BIT_PHYS_ADDR
867 if (!cpu_has_64bits) {
868 uasm_i_lw(p, pte, sizeof(pte_t) / 2, ptr);
869 uasm_i_ori(p, pte, pte, hwmode);
870 uasm_i_sw(p, pte, sizeof(pte_t) / 2, ptr);
871 uasm_i_lw(p, pte, 0, ptr);
878 * Check if PTE is present, if not then jump to LABEL. PTR points to
879 * the page table where this PTE is located, PTE will be re-loaded
880 * with it's original value.
882 static void __cpuinit
883 build_pte_present(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
884 unsigned int pte, unsigned int ptr, enum label_id lid)
886 uasm_i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
887 uasm_i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
888 uasm_il_bnez(p, r, pte, lid);
889 iPTE_LW(p, l, pte, ptr);
892 /* Make PTE valid, store result in PTR. */
893 static void __cpuinit
894 build_make_valid(u32 **p, struct uasm_reloc **r, unsigned int pte,
897 unsigned int mode = _PAGE_VALID | _PAGE_ACCESSED;
899 iPTE_SW(p, r, pte, ptr, mode);
903 * Check if PTE can be written to, if not branch to LABEL. Regardless
904 * restore PTE with value from PTR when done.
906 static void __cpuinit
907 build_pte_writable(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
908 unsigned int pte, unsigned int ptr, enum label_id lid)
910 uasm_i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
911 uasm_i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
912 uasm_il_bnez(p, r, pte, lid);
913 iPTE_LW(p, l, pte, ptr);
916 /* Make PTE writable, update software status bits as well, then store
919 static void __cpuinit
920 build_make_write(u32 **p, struct uasm_reloc **r, unsigned int pte,
923 unsigned int mode = (_PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID
926 iPTE_SW(p, r, pte, ptr, mode);
930 * Check if PTE can be modified, if not branch to LABEL. Regardless
931 * restore PTE with value from PTR when done.
933 static void __cpuinit
934 build_pte_modifiable(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
935 unsigned int pte, unsigned int ptr, enum label_id lid)
937 uasm_i_andi(p, pte, pte, _PAGE_WRITE);
938 uasm_il_beqz(p, r, pte, lid);
939 iPTE_LW(p, l, pte, ptr);
943 * R3000 style TLB load/store/modify handlers.
947 * This places the pte into ENTRYLO0 and writes it with tlbwi.
950 static void __cpuinit
951 build_r3000_pte_reload_tlbwi(u32 **p, unsigned int pte, unsigned int tmp)
953 uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
954 uasm_i_mfc0(p, tmp, C0_EPC); /* cp0 delay */
957 uasm_i_rfe(p); /* branch delay */
961 * This places the pte into ENTRYLO0 and writes it with tlbwi
962 * or tlbwr as appropriate. This is because the index register
963 * may have the probe fail bit set as a result of a trap on a
964 * kseg2 access, i.e. without refill. Then it returns.
966 static void __cpuinit
967 build_r3000_tlb_reload_write(u32 **p, struct uasm_label **l,
968 struct uasm_reloc **r, unsigned int pte,
971 uasm_i_mfc0(p, tmp, C0_INDEX);
972 uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
973 uasm_il_bltz(p, r, tmp, label_r3000_write_probe_fail); /* cp0 delay */
974 uasm_i_mfc0(p, tmp, C0_EPC); /* branch delay */
975 uasm_i_tlbwi(p); /* cp0 delay */
977 uasm_i_rfe(p); /* branch delay */
978 uasm_l_r3000_write_probe_fail(l, *p);
979 uasm_i_tlbwr(p); /* cp0 delay */
981 uasm_i_rfe(p); /* branch delay */
984 static void __cpuinit
985 build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
988 long pgdc = (long)pgd_current;
990 uasm_i_mfc0(p, pte, C0_BADVADDR);
991 uasm_i_lui(p, ptr, uasm_rel_hi(pgdc)); /* cp0 delay */
992 uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
993 uasm_i_srl(p, pte, pte, 22); /* load delay */
994 uasm_i_sll(p, pte, pte, 2);
995 uasm_i_addu(p, ptr, ptr, pte);
996 uasm_i_mfc0(p, pte, C0_CONTEXT);
997 uasm_i_lw(p, ptr, 0, ptr); /* cp0 delay */
998 uasm_i_andi(p, pte, pte, 0xffc); /* load delay */
999 uasm_i_addu(p, ptr, ptr, pte);
1000 uasm_i_lw(p, pte, 0, ptr);
1001 uasm_i_tlbp(p); /* load delay */
1004 static void __cpuinit build_r3000_tlb_load_handler(void)
1006 u32 *p = handle_tlbl;
1007 struct uasm_label *l = labels;
1008 struct uasm_reloc *r = relocs;
1010 memset(handle_tlbl, 0, sizeof(handle_tlbl));
1011 memset(labels, 0, sizeof(labels));
1012 memset(relocs, 0, sizeof(relocs));
1014 build_r3000_tlbchange_handler_head(&p, K0, K1);
1015 build_pte_present(&p, &l, &r, K0, K1, label_nopage_tlbl);
1016 uasm_i_nop(&p); /* load delay */
1017 build_make_valid(&p, &r, K0, K1);
1018 build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
1020 uasm_l_nopage_tlbl(&l, p);
1021 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
1024 if ((p - handle_tlbl) > FASTPATH_SIZE)
1025 panic("TLB load handler fastpath space exceeded");
1027 uasm_resolve_relocs(relocs, labels);
1028 pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
1029 (unsigned int)(p - handle_tlbl));
1031 dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
1034 static void __cpuinit build_r3000_tlb_store_handler(void)
1036 u32 *p = handle_tlbs;
1037 struct uasm_label *l = labels;
1038 struct uasm_reloc *r = relocs;
1040 memset(handle_tlbs, 0, sizeof(handle_tlbs));
1041 memset(labels, 0, sizeof(labels));
1042 memset(relocs, 0, sizeof(relocs));
1044 build_r3000_tlbchange_handler_head(&p, K0, K1);
1045 build_pte_writable(&p, &l, &r, K0, K1, label_nopage_tlbs);
1046 uasm_i_nop(&p); /* load delay */
1047 build_make_write(&p, &r, K0, K1);
1048 build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
1050 uasm_l_nopage_tlbs(&l, p);
1051 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1054 if ((p - handle_tlbs) > FASTPATH_SIZE)
1055 panic("TLB store handler fastpath space exceeded");
1057 uasm_resolve_relocs(relocs, labels);
1058 pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
1059 (unsigned int)(p - handle_tlbs));
1061 dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
1064 static void __cpuinit build_r3000_tlb_modify_handler(void)
1066 u32 *p = handle_tlbm;
1067 struct uasm_label *l = labels;
1068 struct uasm_reloc *r = relocs;
1070 memset(handle_tlbm, 0, sizeof(handle_tlbm));
1071 memset(labels, 0, sizeof(labels));
1072 memset(relocs, 0, sizeof(relocs));
1074 build_r3000_tlbchange_handler_head(&p, K0, K1);
1075 build_pte_modifiable(&p, &l, &r, K0, K1, label_nopage_tlbm);
1076 uasm_i_nop(&p); /* load delay */
1077 build_make_write(&p, &r, K0, K1);
1078 build_r3000_pte_reload_tlbwi(&p, K0, K1);
1080 uasm_l_nopage_tlbm(&l, p);
1081 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1084 if ((p - handle_tlbm) > FASTPATH_SIZE)
1085 panic("TLB modify handler fastpath space exceeded");
1087 uasm_resolve_relocs(relocs, labels);
1088 pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
1089 (unsigned int)(p - handle_tlbm));
1091 dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
1095 * R4000 style TLB load/store/modify handlers.
1097 static void __cpuinit
1098 build_r4000_tlbchange_handler_head(u32 **p, struct uasm_label **l,
1099 struct uasm_reloc **r, unsigned int pte,
1103 build_get_pmde64(p, l, r, pte, ptr); /* get pmd in ptr */
1105 build_get_pgde32(p, pte, ptr); /* get pgd in ptr */
1108 UASM_i_MFC0(p, pte, C0_BADVADDR);
1109 UASM_i_LW(p, ptr, 0, ptr);
1110 UASM_i_SRL(p, pte, pte, PAGE_SHIFT + PTE_ORDER - PTE_T_LOG2);
1111 uasm_i_andi(p, pte, pte, (PTRS_PER_PTE - 1) << PTE_T_LOG2);
1112 UASM_i_ADDU(p, ptr, ptr, pte);
1115 uasm_l_smp_pgtable_change(l, *p);
1117 iPTE_LW(p, l, pte, ptr); /* get even pte */
1118 if (!m4kc_tlbp_war())
1119 build_tlb_probe_entry(p);
1122 static void __cpuinit
1123 build_r4000_tlbchange_handler_tail(u32 **p, struct uasm_label **l,
1124 struct uasm_reloc **r, unsigned int tmp,
1127 uasm_i_ori(p, ptr, ptr, sizeof(pte_t));
1128 uasm_i_xori(p, ptr, ptr, sizeof(pte_t));
1129 build_update_entries(p, tmp, ptr);
1130 build_tlb_write_entry(p, l, r, tlb_indexed);
1131 uasm_l_leave(l, *p);
1132 uasm_i_eret(p); /* return from trap */
1135 build_get_pgd_vmalloc64(p, l, r, tmp, ptr);
1139 static void __cpuinit build_r4000_tlb_load_handler(void)
1141 u32 *p = handle_tlbl;
1142 struct uasm_label *l = labels;
1143 struct uasm_reloc *r = relocs;
1145 memset(handle_tlbl, 0, sizeof(handle_tlbl));
1146 memset(labels, 0, sizeof(labels));
1147 memset(relocs, 0, sizeof(relocs));
1149 if (bcm1250_m3_war()) {
1150 UASM_i_MFC0(&p, K0, C0_BADVADDR);
1151 UASM_i_MFC0(&p, K1, C0_ENTRYHI);
1152 uasm_i_xor(&p, K0, K0, K1);
1153 UASM_i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
1154 uasm_il_bnez(&p, &r, K0, label_leave);
1155 /* No need for uasm_i_nop */
1158 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
1159 build_pte_present(&p, &l, &r, K0, K1, label_nopage_tlbl);
1160 if (m4kc_tlbp_war())
1161 build_tlb_probe_entry(&p);
1162 build_make_valid(&p, &r, K0, K1);
1163 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1165 uasm_l_nopage_tlbl(&l, p);
1166 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
1169 if ((p - handle_tlbl) > FASTPATH_SIZE)
1170 panic("TLB load handler fastpath space exceeded");
1172 uasm_resolve_relocs(relocs, labels);
1173 pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
1174 (unsigned int)(p - handle_tlbl));
1176 dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
1179 static void __cpuinit build_r4000_tlb_store_handler(void)
1181 u32 *p = handle_tlbs;
1182 struct uasm_label *l = labels;
1183 struct uasm_reloc *r = relocs;
1185 memset(handle_tlbs, 0, sizeof(handle_tlbs));
1186 memset(labels, 0, sizeof(labels));
1187 memset(relocs, 0, sizeof(relocs));
1189 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
1190 build_pte_writable(&p, &l, &r, K0, K1, label_nopage_tlbs);
1191 if (m4kc_tlbp_war())
1192 build_tlb_probe_entry(&p);
1193 build_make_write(&p, &r, K0, K1);
1194 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1196 uasm_l_nopage_tlbs(&l, p);
1197 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1200 if ((p - handle_tlbs) > FASTPATH_SIZE)
1201 panic("TLB store handler fastpath space exceeded");
1203 uasm_resolve_relocs(relocs, labels);
1204 pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
1205 (unsigned int)(p - handle_tlbs));
1207 dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
1210 static void __cpuinit build_r4000_tlb_modify_handler(void)
1212 u32 *p = handle_tlbm;
1213 struct uasm_label *l = labels;
1214 struct uasm_reloc *r = relocs;
1216 memset(handle_tlbm, 0, sizeof(handle_tlbm));
1217 memset(labels, 0, sizeof(labels));
1218 memset(relocs, 0, sizeof(relocs));
1220 build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
1221 build_pte_modifiable(&p, &l, &r, K0, K1, label_nopage_tlbm);
1222 if (m4kc_tlbp_war())
1223 build_tlb_probe_entry(&p);
1224 /* Present and writable bits set, set accessed and dirty bits. */
1225 build_make_write(&p, &r, K0, K1);
1226 build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
1228 uasm_l_nopage_tlbm(&l, p);
1229 uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
1232 if ((p - handle_tlbm) > FASTPATH_SIZE)
1233 panic("TLB modify handler fastpath space exceeded");
1235 uasm_resolve_relocs(relocs, labels);
1236 pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
1237 (unsigned int)(p - handle_tlbm));
1239 dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
1242 void __cpuinit build_tlb_refill_handler(void)
1245 * The refill handler is generated per-CPU, multi-node systems
1246 * may have local storage for it. The other handlers are only
1249 static int run_once = 0;
1251 switch (current_cpu_type()) {
1259 build_r3000_tlb_refill_handler();
1261 build_r3000_tlb_load_handler();
1262 build_r3000_tlb_store_handler();
1263 build_r3000_tlb_modify_handler();
1270 panic("No R6000 TLB refill handler yet");
1274 panic("No R8000 TLB refill handler yet");
1278 build_r4000_tlb_refill_handler();
1280 build_r4000_tlb_load_handler();
1281 build_r4000_tlb_store_handler();
1282 build_r4000_tlb_modify_handler();
1288 void __cpuinit flush_tlb_handlers(void)
1290 local_flush_icache_range((unsigned long)handle_tlbl,
1291 (unsigned long)handle_tlbl + sizeof(handle_tlbl));
1292 local_flush_icache_range((unsigned long)handle_tlbs,
1293 (unsigned long)handle_tlbs + sizeof(handle_tlbs));
1294 local_flush_icache_range((unsigned long)handle_tlbm,
1295 (unsigned long)handle_tlbm + sizeof(handle_tlbm));