1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * This file contains the routines for TLB flushing.
4 * On machines where the MMU does not use a hash table to store virtual to
5 * physical translations (ie, SW loaded TLBs or Book3E compilant processors,
6 * this does -not- include 603 however which shares the implementation with
7 * hash based processors)
14 * Derived from arch/ppc/mm/init.c:
19 * Copyright (C) 1996 Paul Mackerras
21 * Derived from "arch/i386/mm/init.c"
22 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
25 #include <linux/kernel.h>
26 #include <linux/export.h>
28 #include <linux/init.h>
29 #include <linux/highmem.h>
30 #include <linux/pagemap.h>
31 #include <linux/preempt.h>
32 #include <linux/spinlock.h>
33 #include <linux/memblock.h>
34 #include <linux/of_fdt.h>
35 #include <linux/hugetlb.h>
37 #include <asm/pgalloc.h>
38 #include <asm/tlbflush.h>
40 #include <asm/text-patching.h>
41 #include <asm/cputhreads.h>
42 #include <asm/hugetlb.h>
45 #include <mm/mmu_decl.h>
48 * This struct lists the sw-supported page sizes. The hardawre MMU may support
49 * other sizes not listed here. The .ind field is only used on MMUs that have
50 * indirect page table entries.
52 #ifdef CONFIG_PPC_E500
53 struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT] = {
77 static inline int mmu_get_tsize(int psize)
79 return mmu_psize_defs[psize].shift - 10;
82 static inline int mmu_get_tsize(int psize)
84 /* This isn't used on !Book3E for now */
90 struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT] = {
106 #ifdef CONFIG_PPC_E500
107 /* next_tlbcam_idx is used to round-robin tlbcam entry assignment */
108 DEFINE_PER_CPU(int, next_tlbcam_idx);
109 EXPORT_PER_CPU_SYMBOL(next_tlbcam_idx);
113 * Base TLB flushing operations:
115 * - flush_tlb_mm(mm) flushes the specified mm context TLB's
116 * - flush_tlb_page(vma, vmaddr) flushes one page
117 * - flush_tlb_range(vma, start, end) flushes a range of pages
118 * - flush_tlb_kernel_range(start, end) flushes kernel pages
120 * - local_* variants of page and mm only apply to the current
124 #ifndef CONFIG_PPC_8xx
126 * These are the base non-SMP variants of page and mm flushing
128 void local_flush_tlb_mm(struct mm_struct *mm)
133 pid = mm->context.id;
134 if (pid != MMU_NO_CONTEXT)
138 EXPORT_SYMBOL(local_flush_tlb_mm);
140 void __local_flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
146 pid = mm ? mm->context.id : 0;
147 if (pid != MMU_NO_CONTEXT)
148 _tlbil_va(vmaddr, pid, tsize, ind);
152 void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
154 __local_flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
155 mmu_get_tsize(mmu_virtual_psize), 0);
157 EXPORT_SYMBOL(local_flush_tlb_page);
159 void local_flush_tlb_page_psize(struct mm_struct *mm,
160 unsigned long vmaddr, int psize)
162 __local_flush_tlb_page(mm, vmaddr, mmu_get_tsize(psize), 0);
164 EXPORT_SYMBOL(local_flush_tlb_page_psize);
169 * And here are the SMP non-local implementations
173 static DEFINE_RAW_SPINLOCK(tlbivax_lock);
175 struct tlb_flush_param {
182 static void do_flush_tlb_mm_ipi(void *param)
184 struct tlb_flush_param *p = param;
186 _tlbil_pid(p ? p->pid : 0);
189 static void do_flush_tlb_page_ipi(void *param)
191 struct tlb_flush_param *p = param;
193 _tlbil_va(p->addr, p->pid, p->tsize, p->ind);
197 /* Note on invalidations and PID:
199 * We snapshot the PID with preempt disabled. At this point, it can still
200 * change either because:
201 * - our context is being stolen (PID -> NO_CONTEXT) on another CPU
202 * - we are invaliating some target that isn't currently running here
203 * and is concurrently acquiring a new PID on another CPU
204 * - some other CPU is re-acquiring a lost PID for this mm
207 * However, this shouldn't be a problem as we only guarantee
208 * invalidation of TLB entries present prior to this call, so we
209 * don't care about the PID changing, and invalidating a stale PID
210 * is generally harmless.
213 void flush_tlb_mm(struct mm_struct *mm)
218 pid = mm->context.id;
219 if (unlikely(pid == MMU_NO_CONTEXT))
221 if (!mm_is_core_local(mm)) {
222 struct tlb_flush_param p = { .pid = pid };
223 /* Ignores smp_processor_id() even if set. */
224 smp_call_function_many(mm_cpumask(mm),
225 do_flush_tlb_mm_ipi, &p, 1);
231 EXPORT_SYMBOL(flush_tlb_mm);
233 void __flush_tlb_page(struct mm_struct *mm, unsigned long vmaddr,
236 struct cpumask *cpu_mask;
240 * This function as well as __local_flush_tlb_page() must only be called
247 pid = mm->context.id;
248 if (unlikely(pid == MMU_NO_CONTEXT))
250 cpu_mask = mm_cpumask(mm);
251 if (!mm_is_core_local(mm)) {
252 /* If broadcast tlbivax is supported, use it */
253 if (mmu_has_feature(MMU_FTR_USE_TLBIVAX_BCAST)) {
254 int lock = mmu_has_feature(MMU_FTR_LOCK_BCAST_INVAL);
256 raw_spin_lock(&tlbivax_lock);
257 _tlbivax_bcast(vmaddr, pid, tsize, ind);
259 raw_spin_unlock(&tlbivax_lock);
262 struct tlb_flush_param p = {
268 /* Ignores smp_processor_id() even if set in cpu_mask */
269 smp_call_function_many(cpu_mask,
270 do_flush_tlb_page_ipi, &p, 1);
273 _tlbil_va(vmaddr, pid, tsize, ind);
278 void flush_tlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
280 #ifdef CONFIG_HUGETLB_PAGE
281 if (vma && is_vm_hugetlb_page(vma))
282 flush_hugetlb_page(vma, vmaddr);
285 __flush_tlb_page(vma ? vma->vm_mm : NULL, vmaddr,
286 mmu_get_tsize(mmu_virtual_psize), 0);
288 EXPORT_SYMBOL(flush_tlb_page);
290 #endif /* CONFIG_SMP */
293 * Flush kernel TLB entries in the given range
295 #ifndef CONFIG_PPC_8xx
296 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
300 smp_call_function(do_flush_tlb_mm_ipi, NULL, 1);
307 EXPORT_SYMBOL(flush_tlb_kernel_range);
311 * Currently, for range flushing, we just do a full mm flush. This should
312 * be optimized based on a threshold on the size of the range, since
313 * some implementation can stack multiple tlbivax before a tlbsync but
314 * for now, we keep it that way
316 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
320 if (end - start == PAGE_SIZE && !(start & ~PAGE_MASK))
321 flush_tlb_page(vma, start);
323 flush_tlb_mm(vma->vm_mm);
325 EXPORT_SYMBOL(flush_tlb_range);
327 void tlb_flush(struct mmu_gather *tlb)
329 flush_tlb_mm(tlb->mm);
333 void __init early_init_mmu(void)
335 unsigned long root = of_get_flat_dt_root();
337 if (IS_ENABLED(CONFIG_PPC_47x) && IS_ENABLED(CONFIG_SMP) &&
338 of_get_flat_dt_prop(root, "cooperative-partition", NULL))
339 mmu_clear_feature(MMU_FTR_USE_TLBIVAX_BCAST);
341 #endif /* CONFIG_PPC64 */