1#include <linux/init.h>
2
3#include <linux/mm.h>
4#include <linux/spinlock.h>
5#include <linux/smp.h>
6#include <linux/interrupt.h>
7#include <linux/module.h>
8
9#include <asm/tlbflush.h>
10#include <asm/mmu_context.h>
11#include <asm/cache.h>
12#include <asm/apic.h>
13#include <asm/uv/uv.h>
14
15DEFINE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate)
16 = { &init_mm, 0, };
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40union smp_flush_state {
41 struct {
42 struct mm_struct *flush_mm;
43 unsigned long flush_va;
44 raw_spinlock_t tlbstate_lock;
45 DECLARE_BITMAP(flush_cpumask, NR_CPUS);
46 };
47 char pad[INTERNODE_CACHE_BYTES];
48} ____cacheline_internodealigned_in_smp;
49
50
51
52
53static union smp_flush_state flush_state[NUM_INVALIDATE_TLB_VECTORS];
54
55
56
57
58
59void leave_mm(int cpu)
60{
61 if (percpu_read(cpu_tlbstate.state) == TLBSTATE_OK)
62 BUG();
63 cpumask_clear_cpu(cpu,
64 mm_cpumask(percpu_read(cpu_tlbstate.active_mm)));
65 load_cr3(swapper_pg_dir);
66}
67EXPORT_SYMBOL_GPL(leave_mm);
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124#ifdef CONFIG_X86_64
125asmlinkage
126#endif
127void smp_invalidate_interrupt(struct pt_regs *regs)
128{
129 unsigned int cpu;
130 unsigned int sender;
131 union smp_flush_state *f;
132
133 cpu = smp_processor_id();
134
135
136
137
138 sender = ~regs->orig_ax - INVALIDATE_TLB_VECTOR_START;
139 f = &flush_state[sender];
140
141 if (!cpumask_test_cpu(cpu, to_cpumask(f->flush_cpumask)))
142 goto out;
143
144
145
146
147
148
149
150
151
152 if (f->flush_mm == percpu_read(cpu_tlbstate.active_mm)) {
153 if (percpu_read(cpu_tlbstate.state) == TLBSTATE_OK) {
154 if (f->flush_va == TLB_FLUSH_ALL)
155 local_flush_tlb();
156 else
157 __flush_tlb_one(f->flush_va);
158 } else
159 leave_mm(cpu);
160 }
161out:
162 ack_APIC_irq();
163 smp_mb__before_clear_bit();
164 cpumask_clear_cpu(cpu, to_cpumask(f->flush_cpumask));
165 smp_mb__after_clear_bit();
166 inc_irq_stat(irq_tlb_count);
167}
168
169static void flush_tlb_others_ipi(const struct cpumask *cpumask,
170 struct mm_struct *mm, unsigned long va)
171{
172 unsigned int sender;
173 union smp_flush_state *f;
174
175
176 sender = smp_processor_id() % NUM_INVALIDATE_TLB_VECTORS;
177 f = &flush_state[sender];
178
179
180
181
182
183
184 raw_spin_lock(&f->tlbstate_lock);
185
186 f->flush_mm = mm;
187 f->flush_va = va;
188 if (cpumask_andnot(to_cpumask(f->flush_cpumask), cpumask, cpumask_of(smp_processor_id()))) {
189
190
191
192
193 apic->send_IPI_mask(to_cpumask(f->flush_cpumask),
194 INVALIDATE_TLB_VECTOR_START + sender);
195
196 while (!cpumask_empty(to_cpumask(f->flush_cpumask)))
197 cpu_relax();
198 }
199
200 f->flush_mm = NULL;
201 f->flush_va = 0;
202 raw_spin_unlock(&f->tlbstate_lock);
203}
204
205void native_flush_tlb_others(const struct cpumask *cpumask,
206 struct mm_struct *mm, unsigned long va)
207{
208 if (is_uv_system()) {
209 unsigned int cpu;
210
211 cpu = get_cpu();
212 cpumask = uv_flush_tlb_others(cpumask, mm, va, cpu);
213 if (cpumask)
214 flush_tlb_others_ipi(cpumask, mm, va);
215 put_cpu();
216 return;
217 }
218 flush_tlb_others_ipi(cpumask, mm, va);
219}
220
221static int __cpuinit init_smp_flush(void)
222{
223 int i;
224
225 for (i = 0; i < ARRAY_SIZE(flush_state); i++)
226 raw_spin_lock_init(&flush_state[i].tlbstate_lock);
227
228 return 0;
229}
230core_initcall(init_smp_flush);
231
232void flush_tlb_current_task(void)
233{
234 struct mm_struct *mm = current->mm;
235
236 preempt_disable();
237
238 local_flush_tlb();
239 if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
240 flush_tlb_others(mm_cpumask(mm), mm, TLB_FLUSH_ALL);
241 preempt_enable();
242}
243
244void flush_tlb_mm(struct mm_struct *mm)
245{
246 preempt_disable();
247
248 if (current->active_mm == mm) {
249 if (current->mm)
250 local_flush_tlb();
251 else
252 leave_mm(smp_processor_id());
253 }
254 if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
255 flush_tlb_others(mm_cpumask(mm), mm, TLB_FLUSH_ALL);
256
257 preempt_enable();
258}
259
260void flush_tlb_page(struct vm_area_struct *vma, unsigned long va)
261{
262 struct mm_struct *mm = vma->vm_mm;
263
264 preempt_disable();
265
266 if (current->active_mm == mm) {
267 if (current->mm)
268 __flush_tlb_one(va);
269 else
270 leave_mm(smp_processor_id());
271 }
272
273 if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
274 flush_tlb_others(mm_cpumask(mm), mm, va);
275
276 preempt_enable();
277}
278
279static void do_flush_tlb_all(void *info)
280{
281 unsigned long cpu = smp_processor_id();
282
283 __flush_tlb_all();
284 if (percpu_read(cpu_tlbstate.state) == TLBSTATE_LAZY)
285 leave_mm(cpu);
286}
287
288void flush_tlb_all(void)
289{
290 on_each_cpu(do_flush_tlb_all, NULL, 1);
291}
292