xref: /openbmc/linux/arch/x86/include/asm/tlbflush.h (revision 861e10be)
1 #ifndef _ASM_X86_TLBFLUSH_H
2 #define _ASM_X86_TLBFLUSH_H
3 
4 #include <linux/mm.h>
5 #include <linux/sched.h>
6 
7 #include <asm/processor.h>
8 #include <asm/special_insns.h>
9 
10 #ifdef CONFIG_PARAVIRT
11 #include <asm/paravirt.h>
12 #else
13 #define __flush_tlb() __native_flush_tlb()
14 #define __flush_tlb_global() __native_flush_tlb_global()
15 #define __flush_tlb_single(addr) __native_flush_tlb_single(addr)
16 #endif
17 
18 static inline void __native_flush_tlb(void)
19 {
20 	native_write_cr3(native_read_cr3());
21 }
22 
23 static inline void __native_flush_tlb_global(void)
24 {
25 	unsigned long flags;
26 	unsigned long cr4;
27 
28 	/*
29 	 * Read-modify-write to CR4 - protect it from preemption and
30 	 * from interrupts. (Use the raw variant because this code can
31 	 * be called from deep inside debugging code.)
32 	 */
33 	raw_local_irq_save(flags);
34 
35 	cr4 = native_read_cr4();
36 	/* clear PGE */
37 	native_write_cr4(cr4 & ~X86_CR4_PGE);
38 	/* write old PGE again and flush TLBs */
39 	native_write_cr4(cr4);
40 
41 	raw_local_irq_restore(flags);
42 }
43 
44 static inline void __native_flush_tlb_single(unsigned long addr)
45 {
46 	asm volatile("invlpg (%0)" ::"r" (addr) : "memory");
47 }
48 
49 static inline void __flush_tlb_all(void)
50 {
51 	if (cpu_has_pge)
52 		__flush_tlb_global();
53 	else
54 		__flush_tlb();
55 }
56 
57 static inline void __flush_tlb_one(unsigned long addr)
58 {
59 		__flush_tlb_single(addr);
60 }
61 
62 #define TLB_FLUSH_ALL	-1UL
63 
64 /*
65  * TLB flushing:
66  *
67  *  - flush_tlb() flushes the current mm struct TLBs
68  *  - flush_tlb_all() flushes all processes TLBs
69  *  - flush_tlb_mm(mm) flushes the specified mm context TLB's
70  *  - flush_tlb_page(vma, vmaddr) flushes one page
71  *  - flush_tlb_range(vma, start, end) flushes a range of pages
72  *  - flush_tlb_kernel_range(start, end) flushes a range of kernel pages
73  *  - flush_tlb_others(cpumask, mm, start, end) flushes TLBs on other cpus
74  *
75  * ..but the i386 has somewhat limited tlb flushing capabilities,
76  * and page-granular flushes are available only on i486 and up.
77  */
78 
79 #ifndef CONFIG_SMP
80 
81 #define flush_tlb() __flush_tlb()
82 #define flush_tlb_all() __flush_tlb_all()
83 #define local_flush_tlb() __flush_tlb()
84 
85 static inline void flush_tlb_mm(struct mm_struct *mm)
86 {
87 	if (mm == current->active_mm)
88 		__flush_tlb();
89 }
90 
91 static inline void flush_tlb_page(struct vm_area_struct *vma,
92 				  unsigned long addr)
93 {
94 	if (vma->vm_mm == current->active_mm)
95 		__flush_tlb_one(addr);
96 }
97 
98 static inline void flush_tlb_range(struct vm_area_struct *vma,
99 				   unsigned long start, unsigned long end)
100 {
101 	if (vma->vm_mm == current->active_mm)
102 		__flush_tlb();
103 }
104 
105 static inline void flush_tlb_mm_range(struct mm_struct *mm,
106 	   unsigned long start, unsigned long end, unsigned long vmflag)
107 {
108 	if (mm == current->active_mm)
109 		__flush_tlb();
110 }
111 
112 static inline void native_flush_tlb_others(const struct cpumask *cpumask,
113 					   struct mm_struct *mm,
114 					   unsigned long start,
115 					   unsigned long end)
116 {
117 }
118 
119 static inline void reset_lazy_tlbstate(void)
120 {
121 }
122 
123 static inline void flush_tlb_kernel_range(unsigned long start,
124 					  unsigned long end)
125 {
126 	flush_tlb_all();
127 }
128 
129 #else  /* SMP */
130 
131 #include <asm/smp.h>
132 
133 #define local_flush_tlb() __flush_tlb()
134 
135 #define flush_tlb_mm(mm)	flush_tlb_mm_range(mm, 0UL, TLB_FLUSH_ALL, 0UL)
136 
137 #define flush_tlb_range(vma, start, end)	\
138 		flush_tlb_mm_range(vma->vm_mm, start, end, vma->vm_flags)
139 
140 extern void flush_tlb_all(void);
141 extern void flush_tlb_current_task(void);
142 extern void flush_tlb_page(struct vm_area_struct *, unsigned long);
143 extern void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
144 				unsigned long end, unsigned long vmflag);
145 extern void flush_tlb_kernel_range(unsigned long start, unsigned long end);
146 
147 #define flush_tlb()	flush_tlb_current_task()
148 
149 void native_flush_tlb_others(const struct cpumask *cpumask,
150 				struct mm_struct *mm,
151 				unsigned long start, unsigned long end);
152 
153 #define TLBSTATE_OK	1
154 #define TLBSTATE_LAZY	2
155 
156 struct tlb_state {
157 	struct mm_struct *active_mm;
158 	int state;
159 };
160 DECLARE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate);
161 
162 static inline void reset_lazy_tlbstate(void)
163 {
164 	this_cpu_write(cpu_tlbstate.state, 0);
165 	this_cpu_write(cpu_tlbstate.active_mm, &init_mm);
166 }
167 
168 #endif	/* SMP */
169 
170 #ifndef CONFIG_PARAVIRT
171 #define flush_tlb_others(mask, mm, start, end)	\
172 	native_flush_tlb_others(mask, mm, start, end)
173 #endif
174 
175 #endif /* _ASM_X86_TLBFLUSH_H */
176