1 // SPDX-License-Identifier: GPL-2.0
5 #include <linux/sched.h>
7 #include <asm/mmu_context.h>
9 static inline void local_flush_tlb_all_asid(unsigned long asid)
11 __asm__ __volatile__ ("sfence.vma x0, %0"
17 static inline void local_flush_tlb_page_asid(unsigned long addr,
20 __asm__ __volatile__ ("sfence.vma %0, %1"
22 : "r" (addr), "r" (asid)
26 static inline void local_flush_tlb_range(unsigned long start,
27 unsigned long size, unsigned long stride)
30 local_flush_tlb_page(start);
32 local_flush_tlb_all();
35 static inline void local_flush_tlb_range_asid(unsigned long start,
36 unsigned long size, unsigned long stride, unsigned long asid)
39 local_flush_tlb_page_asid(start, asid);
41 local_flush_tlb_all_asid(asid);
44 static void __ipi_flush_tlb_all(void *info)
46 local_flush_tlb_all();
49 void flush_tlb_all(void)
51 if (riscv_use_ipi_for_rfence())
52 on_each_cpu(__ipi_flush_tlb_all, NULL, 1);
54 sbi_remote_sfence_vma(NULL, 0, -1);
57 struct flush_tlb_range_data {
64 static void __ipi_flush_tlb_range_asid(void *info)
66 struct flush_tlb_range_data *d = info;
68 local_flush_tlb_range_asid(d->start, d->size, d->stride, d->asid);
71 static void __ipi_flush_tlb_range(void *info)
73 struct flush_tlb_range_data *d = info;
75 local_flush_tlb_range(d->start, d->size, d->stride);
78 static void __flush_tlb_range(struct mm_struct *mm, unsigned long start,
79 unsigned long size, unsigned long stride)
81 struct flush_tlb_range_data ftd;
82 struct cpumask *cmask = mm_cpumask(mm);
86 if (cpumask_empty(cmask))
90 /* check if the tlbflush needs to be sent to other CPUs */
91 broadcast = cpumask_any_but(cmask, cpuid) < nr_cpu_ids;
92 if (static_branch_unlikely(&use_asid_allocator)) {
93 unsigned long asid = atomic_long_read(&mm->context.id) & asid_mask;
96 if (riscv_use_ipi_for_rfence()) {
101 on_each_cpu_mask(cmask,
102 __ipi_flush_tlb_range_asid,
105 sbi_remote_sfence_vma_asid(cmask,
108 local_flush_tlb_range_asid(start, size, stride, asid);
112 if (riscv_use_ipi_for_rfence()) {
117 on_each_cpu_mask(cmask,
118 __ipi_flush_tlb_range,
121 sbi_remote_sfence_vma(cmask, start, size);
123 local_flush_tlb_range(start, size, stride);
130 void flush_tlb_mm(struct mm_struct *mm)
132 __flush_tlb_range(mm, 0, -1, PAGE_SIZE);
135 void flush_tlb_page(struct vm_area_struct *vma, unsigned long addr)
137 __flush_tlb_range(vma->vm_mm, addr, PAGE_SIZE, PAGE_SIZE);
140 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
143 __flush_tlb_range(vma->vm_mm, start, end - start, PAGE_SIZE);
145 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
146 void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start,
149 __flush_tlb_range(vma->vm_mm, start, end - start, PMD_SIZE);