1 // SPDX-License-Identifier: GPL-2.0
3 * PPC Huge TLB Page Support for Book3E MMU
5 * Copyright (C) 2009 David Gibson, IBM Corporation.
6 * Copyright (C) 2011 Becky Bruce, Freescale Semiconductor
10 #include <linux/hugetlb.h>
17 static inline int tlb1_next(void)
19 struct paca_struct *paca = get_paca();
20 struct tlb_core_data *tcd;
24 this = tcd->esel_next;
27 if (next >= tcd->esel_max)
28 next = tcd->esel_first;
30 tcd->esel_next = next;
34 static inline void book3e_tlb_lock(void)
36 struct paca_struct *paca = get_paca();
38 int token = smp_processor_id() + 1;
41 * Besides being unnecessary in the absence of SMT, this
42 * check prevents trying to do lbarx/stbcx. on e5500 which
43 * doesn't implement either feature.
45 if (!cpu_has_feature(CPU_FTR_SMT))
48 asm volatile("1: lbarx %0, 0, %1;"
60 : "r" (&paca->tcd_ptr->lock), "r" (token)
64 static inline void book3e_tlb_unlock(void)
66 struct paca_struct *paca = get_paca();
68 if (!cpu_has_feature(CPU_FTR_SMT))
72 paca->tcd_ptr->lock = 0;
75 static inline int tlb1_next(void)
79 ncams = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
81 index = this_cpu_read(next_tlbcam_idx);
83 /* Just round-robin the entries and wrap when we hit the end */
84 if (unlikely(index == ncams - 1))
85 __this_cpu_write(next_tlbcam_idx, tlbcam_index);
87 __this_cpu_inc(next_tlbcam_idx);
92 static inline void book3e_tlb_lock(void)
96 static inline void book3e_tlb_unlock(void)
101 static inline int book3e_tlb_exists(unsigned long ea, unsigned long pid)
105 mtspr(SPRN_MAS6, pid << 16);
110 : "=&r"(found) : "r"(ea));
116 book3e_hugetlb_preload(struct vm_area_struct *vma, unsigned long ea, pte_t pte)
118 unsigned long mas1, mas2;
120 unsigned long psize, tsize, shift;
122 struct mm_struct *mm;
125 if (unlikely(is_kernel_addr(ea)))
130 psize = vma_mmu_pagesize(vma);
131 shift = __ilog2(psize);
134 * We can't be interrupted while we're setting up the MAS
135 * registers or after we've confirmed that no tlb exists.
137 local_irq_save(flags);
141 if (unlikely(book3e_tlb_exists(ea, mm->context.id))) {
143 local_irq_restore(flags);
147 /* We have to use the CAM(TLB1) on FSL parts for hugepages */
149 mtspr(SPRN_MAS0, MAS0_ESEL(index) | MAS0_TLBSEL(1));
151 mas1 = MAS1_VALID | MAS1_TID(mm->context.id) | MAS1_TSIZE(tsize);
152 mas2 = ea & ~((1UL << shift) - 1);
153 mas2 |= (pte_val(pte) >> PTE_WIMGE_SHIFT) & MAS2_WIMGE_MASK;
154 mas7_3 = (u64)pte_pfn(pte) << PAGE_SHIFT;
155 mas7_3 |= (pte_val(pte) >> PTE_BAP_SHIFT) & MAS3_BAP_MASK;
157 mas7_3 &= ~(MAS3_SW|MAS3_UW);
159 mtspr(SPRN_MAS1, mas1);
160 mtspr(SPRN_MAS2, mas2);
162 if (mmu_has_feature(MMU_FTR_BIG_PHYS))
163 mtspr(SPRN_MAS7, upper_32_bits(mas7_3));
164 mtspr(SPRN_MAS3, lower_32_bits(mas7_3));
166 asm volatile ("tlbwe");
169 local_irq_restore(flags);
173 * This is called at the end of handling a user page fault, when the
174 * fault has been handled by updating a PTE in the linux page tables.
176 * This must always be called with the pte lock held.
178 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t *ptep)
180 if (is_vm_hugetlb_page(vma))
181 book3e_hugetlb_preload(vma, address, *ptep);
184 void flush_hugetlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
186 struct hstate *hstate = hstate_file(vma->vm_file);
187 unsigned long tsize = huge_page_shift(hstate) - 10;
189 __flush_tlb_page(vma->vm_mm, vmaddr, tsize, 0);