xref: /linux/arch/s390/include/asm/tlbflush.h (revision 92ce4c3ea7c44e61ca2b6ef3e5682bfcea851d87)
1 #ifndef _S390_TLBFLUSH_H
2 #define _S390_TLBFLUSH_H
3 
4 #include <linux/mm.h>
5 #include <linux/sched.h>
6 #include <asm/processor.h>
7 #include <asm/pgalloc.h>
8 #include <asm/pgtable.h>
9 
10 /*
11  * Flush all TLB entries on the local CPU.
12  */
13 static inline void __tlb_flush_local(void)
14 {
15 	asm volatile("ptlb" : : : "memory");
16 }
17 
18 /*
19  * Flush TLB entries for a specific ASCE on all CPUs
20  */
21 static inline void __tlb_flush_idte(unsigned long asce)
22 {
23 	unsigned long opt;
24 
25 	opt = IDTE_PTOA;
26 	if (MACHINE_HAS_TLB_GUEST)
27 		opt |= IDTE_GUEST_ASCE;
28 	/* Global TLB flush for the mm */
29 	asm volatile(
30 		"	.insn	rrf,0xb98e0000,0,%0,%1,0"
31 		: : "a" (opt), "a" (asce) : "cc");
32 }
33 
34 #ifdef CONFIG_SMP
35 void smp_ptlb_all(void);
36 
37 /*
38  * Flush all TLB entries on all CPUs.
39  */
40 static inline void __tlb_flush_global(void)
41 {
42 	unsigned int dummy = 0;
43 
44 	csp(&dummy, 0, 0);
45 }
46 
47 /*
48  * Flush TLB entries for a specific mm on all CPUs (in case gmap is used
49  * this implicates multiple ASCEs!).
50  */
51 static inline void __tlb_flush_full(struct mm_struct *mm)
52 {
53 	preempt_disable();
54 	atomic_inc(&mm->context.flush_count);
55 	if (cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id()))) {
56 		/* Local TLB flush */
57 		__tlb_flush_local();
58 	} else {
59 		/* Global TLB flush */
60 		__tlb_flush_global();
61 		/* Reset TLB flush mask */
62 		cpumask_copy(mm_cpumask(mm), &mm->context.cpu_attach_mask);
63 	}
64 	atomic_dec(&mm->context.flush_count);
65 	preempt_enable();
66 }
67 
68 static inline void __tlb_flush_mm(struct mm_struct *mm)
69 {
70 	unsigned long gmap_asce;
71 
72 	/*
73 	 * If the machine has IDTE we prefer to do a per mm flush
74 	 * on all cpus instead of doing a local flush if the mm
75 	 * only ran on the local cpu.
76 	 */
77 	preempt_disable();
78 	atomic_inc(&mm->context.flush_count);
79 	gmap_asce = READ_ONCE(mm->context.gmap_asce);
80 	if (MACHINE_HAS_IDTE && gmap_asce != -1UL) {
81 		if (gmap_asce)
82 			__tlb_flush_idte(gmap_asce);
83 		__tlb_flush_idte(mm->context.asce);
84 	} else {
85 		__tlb_flush_full(mm);
86 	}
87 	/* Reset TLB flush mask */
88 	cpumask_copy(mm_cpumask(mm), &mm->context.cpu_attach_mask);
89 	atomic_dec(&mm->context.flush_count);
90 	preempt_enable();
91 }
92 
93 static inline void __tlb_flush_kernel(void)
94 {
95 	if (MACHINE_HAS_IDTE)
96 		__tlb_flush_idte(init_mm.context.asce);
97 	else
98 		__tlb_flush_global();
99 }
100 #else
101 #define __tlb_flush_global()	__tlb_flush_local()
102 #define __tlb_flush_full(mm)	__tlb_flush_local()
103 
104 /*
105  * Flush TLB entries for a specific ASCE on all CPUs.
106  */
107 static inline void __tlb_flush_mm(struct mm_struct *mm)
108 {
109 	__tlb_flush_local();
110 }
111 
112 static inline void __tlb_flush_kernel(void)
113 {
114 	__tlb_flush_local();
115 }
116 #endif
117 
118 static inline void __tlb_flush_mm_lazy(struct mm_struct * mm)
119 {
120 	if (mm->context.flush_mm) {
121 		__tlb_flush_mm(mm);
122 		mm->context.flush_mm = 0;
123 	}
124 }
125 
126 /*
127  * TLB flushing:
128  *  flush_tlb() - flushes the current mm struct TLBs
129  *  flush_tlb_all() - flushes all processes TLBs
130  *  flush_tlb_mm(mm) - flushes the specified mm context TLB's
131  *  flush_tlb_page(vma, vmaddr) - flushes one page
132  *  flush_tlb_range(vma, start, end) - flushes a range of pages
133  *  flush_tlb_kernel_range(start, end) - flushes a range of kernel pages
134  */
135 
136 /*
137  * flush_tlb_mm goes together with ptep_set_wrprotect for the
138  * copy_page_range operation and flush_tlb_range is related to
139  * ptep_get_and_clear for change_protection. ptep_set_wrprotect and
140  * ptep_get_and_clear do not flush the TLBs directly if the mm has
141  * only one user. At the end of the update the flush_tlb_mm and
142  * flush_tlb_range functions need to do the flush.
143  */
144 #define flush_tlb()				do { } while (0)
145 #define flush_tlb_all()				do { } while (0)
146 #define flush_tlb_page(vma, addr)		do { } while (0)
147 
148 static inline void flush_tlb_mm(struct mm_struct *mm)
149 {
150 	__tlb_flush_mm_lazy(mm);
151 }
152 
153 static inline void flush_tlb_range(struct vm_area_struct *vma,
154 				   unsigned long start, unsigned long end)
155 {
156 	__tlb_flush_mm_lazy(vma->vm_mm);
157 }
158 
159 static inline void flush_tlb_kernel_range(unsigned long start,
160 					  unsigned long end)
161 {
162 	__tlb_flush_kernel();
163 }
164 
165 #endif /* _S390_TLBFLUSH_H */
166