upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / arch / x86_64 / kernel / smp.c
1 /*
2  *      Intel SMP support routines.
3  *
4  *      (c) 1995 Alan Cox, Building #3 <alan@redhat.com>
5  *      (c) 1998-99, 2000 Ingo Molnar <mingo@redhat.com>
6  *      (c) 2002,2003 Andi Kleen, SuSE Labs.
7  *
8  *      This code is released under the GNU General Public License version 2 or
9  *      later.
10  */
11
12 #include <linux/init.h>
13
14 #include <linux/mm.h>
15 #include <linux/irq.h>
16 #include <linux/delay.h>
17 #include <linux/spinlock.h>
18 #include <linux/smp_lock.h>
19 #include <linux/smp.h>
20 #include <linux/kernel_stat.h>
21 #include <linux/mc146818rtc.h>
22 #include <linux/interrupt.h>
23
24 #include <asm/mtrr.h>
25 #include <asm/pgalloc.h>
26 #include <asm/tlbflush.h>
27 #include <asm/mach_apic.h>
28
29 /*
30  *      Smarter SMP flushing macros. 
31  *              c/o Linus Torvalds.
32  *
33  *      These mean you can really definitely utterly forget about
34  *      writing to user space from interrupts. (Its not allowed anyway).
35  *
36  *      Optimizations Manfred Spraul <manfred@colorfullife.com>
37  */
38
39 static cpumask_t flush_cpumask;
40 static struct mm_struct * flush_mm;
41 static unsigned long flush_va;
42 static spinlock_t tlbstate_lock = SPIN_LOCK_UNLOCKED;
43 #define FLUSH_ALL       0xffffffff
44
45 /*
46  * We cannot call mmdrop() because we are in interrupt context, 
47  * instead update mm->cpu_vm_mask.
48  */
49 static inline void leave_mm (unsigned long cpu)
50 {
51         if (read_pda(mmu_state) == TLBSTATE_OK)
52                 BUG();
53         clear_bit(cpu, &read_pda(active_mm)->cpu_vm_mask);
54         __flush_tlb();
55 }
56
57 /*
58  *
59  * The flush IPI assumes that a thread switch happens in this order:
60  * [cpu0: the cpu that switches]
61  * 1) switch_mm() either 1a) or 1b)
62  * 1a) thread switch to a different mm
63  * 1a1) clear_bit(cpu, &old_mm->cpu_vm_mask);
64  *      Stop ipi delivery for the old mm. This is not synchronized with
65  *      the other cpus, but smp_invalidate_interrupt ignore flush ipis
66  *      for the wrong mm, and in the worst case we perform a superfluous
67  *      tlb flush.
68  * 1a2) set cpu mmu_state to TLBSTATE_OK
69  *      Now the smp_invalidate_interrupt won't call leave_mm if cpu0
70  *      was in lazy tlb mode.
71  * 1a3) update cpu active_mm
72  *      Now cpu0 accepts tlb flushes for the new mm.
73  * 1a4) set_bit(cpu, &new_mm->cpu_vm_mask);
74  *      Now the other cpus will send tlb flush ipis.
75  * 1a4) change cr3.
76  * 1b) thread switch without mm change
77  *      cpu active_mm is correct, cpu0 already handles
78  *      flush ipis.
79  * 1b1) set cpu mmu_state to TLBSTATE_OK
80  * 1b2) test_and_set the cpu bit in cpu_vm_mask.
81  *      Atomically set the bit [other cpus will start sending flush ipis],
82  *      and test the bit.
83  * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
84  * 2) switch %%esp, ie current
85  *
86  * The interrupt must handle 2 special cases:
87  * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
88  * - the cpu performs speculative tlb reads, i.e. even if the cpu only
89  *   runs in kernel space, the cpu could load tlb entries for user space
90  *   pages.
91  *
92  * The good news is that cpu mmu_state is local to each cpu, no
93  * write/read ordering problems.
94  */
95
96 /*
97  * TLB flush IPI:
98  *
99  * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
100  * 2) Leave the mm if we are in the lazy tlb mode.
101  */
102
103 asmlinkage void smp_invalidate_interrupt (void)
104 {
105         unsigned long cpu;
106
107         cpu = get_cpu();
108
109         if (!cpu_isset(cpu, flush_cpumask))
110                 goto out;
111                 /* 
112                  * This was a BUG() but until someone can quote me the
113                  * line from the intel manual that guarantees an IPI to
114                  * multiple CPUs is retried _only_ on the erroring CPUs
115                  * its staying as a return
116                  *
117                  * BUG();
118                  */
119                  
120         if (flush_mm == read_pda(active_mm)) {
121                 if (read_pda(mmu_state) == TLBSTATE_OK) {
122                         if (flush_va == FLUSH_ALL)
123                                 local_flush_tlb();
124                         else
125                                 __flush_tlb_one(flush_va);
126                 } else
127                         leave_mm(cpu);
128         }
129         ack_APIC_irq();
130         cpu_clear(cpu, flush_cpumask);
131
132 out:
133         put_cpu_no_resched();
134 }
135
136 static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
137                                                 unsigned long va)
138 {
139         cpumask_t tmp;
140         /*
141          * A couple of (to be removed) sanity checks:
142          *
143          * - we do not send IPIs to not-yet booted CPUs.
144          * - current CPU must not be in mask
145          * - mask must exist :)
146          */
147         BUG_ON(cpus_empty(cpumask));
148         cpus_and(tmp, cpumask, cpu_online_map);
149         BUG_ON(!cpus_equal(tmp, cpumask));
150         BUG_ON(cpu_isset(smp_processor_id(), cpumask));
151         if (!mm)
152                 BUG();
153
154         /*
155          * I'm not happy about this global shared spinlock in the
156          * MM hot path, but we'll see how contended it is.
157          * Temporarily this turns IRQs off, so that lockups are
158          * detected by the NMI watchdog.
159          */
160         spin_lock(&tlbstate_lock);
161         
162         flush_mm = mm;
163         flush_va = va;
164         cpus_or(flush_cpumask, cpumask, flush_cpumask);
165
166         /*
167          * We have to send the IPI only to
168          * CPUs affected.
169          */
170         send_IPI_mask(cpumask, INVALIDATE_TLB_VECTOR);
171
172         while (!cpus_empty(flush_cpumask))
173                 mb();   /* nothing. lockup detection does not belong here */;
174
175         flush_mm = NULL;
176         flush_va = 0;
177         spin_unlock(&tlbstate_lock);
178 }
179         
180 void flush_tlb_current_task(void)
181 {
182         struct mm_struct *mm = current->mm;
183         cpumask_t cpu_mask;
184
185         preempt_disable();
186         cpu_mask = mm->cpu_vm_mask;
187         cpu_clear(smp_processor_id(), cpu_mask);
188
189         local_flush_tlb();
190         if (!cpus_empty(cpu_mask))
191                 flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
192         preempt_enable();
193 }
194
195 void flush_tlb_mm (struct mm_struct * mm)
196 {
197         cpumask_t cpu_mask;
198
199         preempt_disable();
200         cpu_mask = mm->cpu_vm_mask;
201         cpu_clear(smp_processor_id(), cpu_mask);
202
203         if (current->active_mm == mm) {
204                 if (current->mm)
205                         local_flush_tlb();
206                 else
207                         leave_mm(smp_processor_id());
208         }
209         if (!cpus_empty(cpu_mask))
210                 flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
211
212         preempt_enable();
213 }
214
215 void flush_tlb_page(struct vm_area_struct * vma, unsigned long va)
216 {
217         struct mm_struct *mm = vma->vm_mm;
218         cpumask_t cpu_mask;
219
220         preempt_disable();
221         cpu_mask = mm->cpu_vm_mask;
222         cpu_clear(smp_processor_id(), cpu_mask);
223
224         if (current->active_mm == mm) {
225                 if(current->mm)
226                         __flush_tlb_one(va);
227                  else
228                         leave_mm(smp_processor_id());
229         }
230
231         if (!cpus_empty(cpu_mask))
232                 flush_tlb_others(cpu_mask, mm, va);
233
234         preempt_enable();
235 }
236
237 static void do_flush_tlb_all(void* info)
238 {
239         unsigned long cpu = smp_processor_id();
240
241         __flush_tlb_all();
242         if (read_pda(mmu_state) == TLBSTATE_LAZY)
243                 leave_mm(cpu);
244 }
245
246 void flush_tlb_all(void)
247 {
248         on_each_cpu(do_flush_tlb_all, NULL, 1, 1);
249 }
250
251 void smp_kdb_stop(void)
252 {
253         send_IPI_allbutself(KDB_VECTOR);
254 }
255
256 /*
257  * this function sends a 'reschedule' IPI to another CPU.
258  * it goes straight through and wastes no time serializing
259  * anything. Worst case is that we lose a reschedule ...
260  */
261
262 void smp_send_reschedule(int cpu)
263 {
264         send_IPI_mask(cpumask_of_cpu(cpu), RESCHEDULE_VECTOR);
265 }
266
267 /*
268  * Structure and data for smp_call_function(). This is designed to minimise
269  * static memory requirements. It also looks cleaner.
270  */
271 static spinlock_t call_lock = SPIN_LOCK_UNLOCKED;
272
273 struct call_data_struct {
274         void (*func) (void *info);
275         void *info;
276         atomic_t started;
277         atomic_t finished;
278         int wait;
279 };
280
281 static struct call_data_struct * call_data;
282
283 /*
284  * this function sends a 'generic call function' IPI to all other CPUs
285  * in the system.
286  */
287 static void __smp_call_function (void (*func) (void *info), void *info,
288                                 int nonatomic, int wait)
289 {
290         static struct call_data_struct dumpdata;
291         struct call_data_struct normaldata;
292         struct call_data_struct *data;
293         int cpus = num_online_cpus()-1;
294
295         if (!cpus)
296                 return;
297
298         if (wait == -1) {
299                 /* if another cpu beat us, they win! */
300                 if (dumpdata.func) {
301                         spin_unlock(&call_lock);
302                         return;
303                 }
304                 data = &dumpdata;
305         } else
306                 data = &normaldata;
307
308         data->func = func;
309         data->info = info;
310         atomic_set(&data->started, 0);
311         data->wait = wait > 0 ? wait : 0;
312         if (wait > 0)
313                 atomic_set(&data->finished, 0);
314
315         call_data = data;
316         wmb();
317         /* Send a message to all other CPUs and wait for them to respond */
318         send_IPI_allbutself(CALL_FUNCTION_VECTOR);
319
320         /* Wait for response */
321         if (wait >= 0)
322                 while (atomic_read(&data->started) != cpus)
323                         cpu_relax();
324
325         if (wait > 0)
326                 while (atomic_read(&data->finished) != cpus)
327                         cpu_relax();
328
329         if (wait >= 0)
330                 call_data = NULL;
331 }
332
333 /*
334  * smp_call_function - run a function on all other CPUs.
335  * @func: The function to run. This must be fast and non-blocking.
336  * @info: An arbitrary pointer to pass to the function.
337  * @nonatomic: currently unused.
338  * @wait: If 1, wait (atomically) until function has complete on other CPUs.
339  *        If 0, wait for the IPI to be received by other CPUs, but do not wait
340  *        for the completion of the IPI on each CPU.  If -1, do not wait for
341  *        other CPUs to receive IPI.
342  *
343  * Returns 0 on success, else a negative status code. Does not return until
344  * remote CPUs are nearly ready to execute func or are or have executed.
345  *
346  * You must not call this function with disabled interrupts or from a
347  * hardware interrupt handler or from a bottom half handler.
348  * Actually there are a few legal cases, like panic.
349  */
350 int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
351                         int wait)
352 {
353         spin_lock(&call_lock);
354         __smp_call_function(func,info,nonatomic,wait);
355         spin_unlock(&call_lock);
356         return 0;
357 }
358
359 void smp_stop_cpu(void)
360 {
361         /*
362          * Remove this CPU:
363          */
364         cpu_clear(smp_processor_id(), cpu_online_map);
365         local_irq_disable();
366         disable_local_APIC();
367         local_irq_enable(); 
368 }
369
370 static void smp_really_stop_cpu(void *dummy)
371 {
372         smp_stop_cpu(); 
373         for (;;) 
374                 asm("hlt"); 
375
376
377 void smp_send_stop(void)
378 {
379         int nolock = 0;
380         /* Don't deadlock on the call lock in panic */
381         if (!spin_trylock(&call_lock)) {
382                 /* ignore locking because we have paniced anyways */
383                 nolock = 1;
384         }
385         __smp_call_function(smp_really_stop_cpu, NULL, 0, 0);
386         if (!nolock)
387                 spin_unlock(&call_lock);
388         smp_stop_cpu();
389 }
390
391 /*
392  * Reschedule call back. Nothing to do,
393  * all the work is done automatically when
394  * we return from the interrupt.
395  */
396 asmlinkage void smp_reschedule_interrupt(void)
397 {
398         ack_APIC_irq();
399 }
400
401 asmlinkage void smp_call_function_interrupt(void)
402 {
403         void (*func) (void *info) = call_data->func;
404         void *info = call_data->info;
405         int wait = call_data->wait;
406
407         ack_APIC_irq();
408         /*
409          * Notify initiating CPU that I've grabbed the data and am
410          * about to execute the function
411          */
412         mb();
413         atomic_inc(&call_data->started);
414         /*
415          * At this point the info structure may be out of scope unless wait==1
416          */
417         irq_enter();
418         (*func)(info);
419         irq_exit();
420         if (wait) {
421                 mb();
422                 atomic_inc(&call_data->finished);
423         }
424 }