Merge ../torvalds-2.6/
[pandora-kernel.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/delay.h>
16 #include <linux/spinlock.h>
17 #include <linux/smp_lock.h>
18 #include <linux/smp.h>
19 #include <linux/kernel_stat.h>
20 #include <linux/mc146818rtc.h>
21 #include <linux/interrupt.h>
22
23 #include <asm/mtrr.h>
24 #include <asm/pgalloc.h>
25 #include <asm/tlbflush.h>
26 #include <asm/mach_apic.h>
27 #include <asm/mmu_context.h>
28 #include <asm/proto.h>
29 #include <asm/apicdef.h>
30
31 #define __cpuinit __init
32
33 /*
34  *      Smarter SMP flushing macros. 
35  *              c/o Linus Torvalds.
36  *
37  *      These mean you can really definitely utterly forget about
38  *      writing to user space from interrupts. (Its not allowed anyway).
39  *
40  *      Optimizations Manfred Spraul <manfred@colorfullife.com>
41  *
42  *      More scalable flush, from Andi Kleen
43  *
44  *      To avoid global state use 8 different call vectors.
45  *      Each CPU uses a specific vector to trigger flushes on other
46  *      CPUs. Depending on the received vector the target CPUs look into
47  *      the right per cpu variable for the flush data.
48  *
49  *      With more than 8 CPUs they are hashed to the 8 available
50  *      vectors. The limited global vector space forces us to this right now.
51  *      In future when interrupts are split into per CPU domains this could be
52  *      fixed, at the cost of triggering multiple IPIs in some cases.
53  */
54
55 union smp_flush_state {
56         struct {
57                 cpumask_t flush_cpumask;
58                 struct mm_struct *flush_mm;
59                 unsigned long flush_va;
60 #define FLUSH_ALL       -1ULL
61                 spinlock_t tlbstate_lock;
62         };
63         char pad[SMP_CACHE_BYTES];
64 } ____cacheline_aligned;
65
66 /* State is put into the per CPU data section, but padded
67    to a full cache line because other CPUs can access it and we don't
68    want false sharing in the per cpu data segment. */
69 static DEFINE_PER_CPU(union smp_flush_state, flush_state);
70
71 /*
72  * We cannot call mmdrop() because we are in interrupt context, 
73  * instead update mm->cpu_vm_mask.
74  */
75 static inline void leave_mm(int cpu)
76 {
77         if (read_pda(mmu_state) == TLBSTATE_OK)
78                 BUG();
79         clear_bit(cpu, &read_pda(active_mm)->cpu_vm_mask);
80         load_cr3(swapper_pg_dir);
81 }
82
83 /*
84  *
85  * The flush IPI assumes that a thread switch happens in this order:
86  * [cpu0: the cpu that switches]
87  * 1) switch_mm() either 1a) or 1b)
88  * 1a) thread switch to a different mm
89  * 1a1) clear_bit(cpu, &old_mm->cpu_vm_mask);
90  *      Stop ipi delivery for the old mm. This is not synchronized with
91  *      the other cpus, but smp_invalidate_interrupt ignore flush ipis
92  *      for the wrong mm, and in the worst case we perform a superfluous
93  *      tlb flush.
94  * 1a2) set cpu mmu_state to TLBSTATE_OK
95  *      Now the smp_invalidate_interrupt won't call leave_mm if cpu0
96  *      was in lazy tlb mode.
97  * 1a3) update cpu active_mm
98  *      Now cpu0 accepts tlb flushes for the new mm.
99  * 1a4) set_bit(cpu, &new_mm->cpu_vm_mask);
100  *      Now the other cpus will send tlb flush ipis.
101  * 1a4) change cr3.
102  * 1b) thread switch without mm change
103  *      cpu active_mm is correct, cpu0 already handles
104  *      flush ipis.
105  * 1b1) set cpu mmu_state to TLBSTATE_OK
106  * 1b2) test_and_set the cpu bit in cpu_vm_mask.
107  *      Atomically set the bit [other cpus will start sending flush ipis],
108  *      and test the bit.
109  * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
110  * 2) switch %%esp, ie current
111  *
112  * The interrupt must handle 2 special cases:
113  * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
114  * - the cpu performs speculative tlb reads, i.e. even if the cpu only
115  *   runs in kernel space, the cpu could load tlb entries for user space
116  *   pages.
117  *
118  * The good news is that cpu mmu_state is local to each cpu, no
119  * write/read ordering problems.
120  */
121
122 /*
123  * TLB flush IPI:
124  *
125  * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
126  * 2) Leave the mm if we are in the lazy tlb mode.
127  *
128  * Interrupts are disabled.
129  */
130
131 asmlinkage void smp_invalidate_interrupt(struct pt_regs *regs)
132 {
133         int cpu;
134         int sender;
135         union smp_flush_state *f;
136
137         cpu = smp_processor_id();
138         /*
139          * orig_rax contains the interrupt vector - 256.
140          * Use that to determine where the sender put the data.
141          */
142         sender = regs->orig_rax + 256 - INVALIDATE_TLB_VECTOR_START;
143         f = &per_cpu(flush_state, sender);
144
145         if (!cpu_isset(cpu, f->flush_cpumask))
146                 goto out;
147                 /* 
148                  * This was a BUG() but until someone can quote me the
149                  * line from the intel manual that guarantees an IPI to
150                  * multiple CPUs is retried _only_ on the erroring CPUs
151                  * its staying as a return
152                  *
153                  * BUG();
154                  */
155                  
156         if (f->flush_mm == read_pda(active_mm)) {
157                 if (read_pda(mmu_state) == TLBSTATE_OK) {
158                         if (f->flush_va == FLUSH_ALL)
159                                 local_flush_tlb();
160                         else
161                                 __flush_tlb_one(f->flush_va);
162                 } else
163                         leave_mm(cpu);
164         }
165 out:
166         ack_APIC_irq();
167         cpu_clear(cpu, f->flush_cpumask);
168 }
169
170 static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
171                                                 unsigned long va)
172 {
173         int sender;
174         union smp_flush_state *f;
175
176         /* Caller has disabled preemption */
177         sender = smp_processor_id() % NUM_INVALIDATE_TLB_VECTORS;
178         f = &per_cpu(flush_state, sender);
179
180         /* Could avoid this lock when
181            num_online_cpus() <= NUM_INVALIDATE_TLB_VECTORS, but it is
182            probably not worth checking this for a cache-hot lock. */
183         spin_lock(&f->tlbstate_lock);
184
185         f->flush_mm = mm;
186         f->flush_va = va;
187         cpus_or(f->flush_cpumask, cpumask, f->flush_cpumask);
188
189         /*
190          * We have to send the IPI only to
191          * CPUs affected.
192          */
193         send_IPI_mask(cpumask, INVALIDATE_TLB_VECTOR_START + sender);
194
195         while (!cpus_empty(f->flush_cpumask))
196                 cpu_relax();
197
198         f->flush_mm = NULL;
199         f->flush_va = 0;
200         spin_unlock(&f->tlbstate_lock);
201 }
202
203 int __cpuinit init_smp_flush(void)
204 {
205         int i;
206         for_each_cpu_mask(i, cpu_possible_map) {
207                 spin_lock_init(&per_cpu(flush_state.tlbstate_lock, i));
208         }
209         return 0;
210 }
211
212 core_initcall(init_smp_flush);
213         
214 void flush_tlb_current_task(void)
215 {
216         struct mm_struct *mm = current->mm;
217         cpumask_t cpu_mask;
218
219         preempt_disable();
220         cpu_mask = mm->cpu_vm_mask;
221         cpu_clear(smp_processor_id(), cpu_mask);
222
223         local_flush_tlb();
224         if (!cpus_empty(cpu_mask))
225                 flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
226         preempt_enable();
227 }
228
229 void flush_tlb_mm (struct mm_struct * mm)
230 {
231         cpumask_t cpu_mask;
232
233         preempt_disable();
234         cpu_mask = mm->cpu_vm_mask;
235         cpu_clear(smp_processor_id(), cpu_mask);
236
237         if (current->active_mm == mm) {
238                 if (current->mm)
239                         local_flush_tlb();
240                 else
241                         leave_mm(smp_processor_id());
242         }
243         if (!cpus_empty(cpu_mask))
244                 flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
245
246         preempt_enable();
247 }
248
249 void flush_tlb_page(struct vm_area_struct * vma, unsigned long va)
250 {
251         struct mm_struct *mm = vma->vm_mm;
252         cpumask_t cpu_mask;
253
254         preempt_disable();
255         cpu_mask = mm->cpu_vm_mask;
256         cpu_clear(smp_processor_id(), cpu_mask);
257
258         if (current->active_mm == mm) {
259                 if(current->mm)
260                         __flush_tlb_one(va);
261                  else
262                         leave_mm(smp_processor_id());
263         }
264
265         if (!cpus_empty(cpu_mask))
266                 flush_tlb_others(cpu_mask, mm, va);
267
268         preempt_enable();
269 }
270
271 static void do_flush_tlb_all(void* info)
272 {
273         unsigned long cpu = smp_processor_id();
274
275         __flush_tlb_all();
276         if (read_pda(mmu_state) == TLBSTATE_LAZY)
277                 leave_mm(cpu);
278 }
279
280 void flush_tlb_all(void)
281 {
282         on_each_cpu(do_flush_tlb_all, NULL, 1, 1);
283 }
284
285 void smp_kdb_stop(void)
286 {
287         send_IPI_allbutself(KDB_VECTOR);
288 }
289
290 /*
291  * this function sends a 'reschedule' IPI to another CPU.
292  * it goes straight through and wastes no time serializing
293  * anything. Worst case is that we lose a reschedule ...
294  */
295
296 void smp_send_reschedule(int cpu)
297 {
298         send_IPI_mask(cpumask_of_cpu(cpu), RESCHEDULE_VECTOR);
299 }
300
301 /*
302  * Structure and data for smp_call_function(). This is designed to minimise
303  * static memory requirements. It also looks cleaner.
304  */
305 static DEFINE_SPINLOCK(call_lock);
306
307 struct call_data_struct {
308         void (*func) (void *info);
309         void *info;
310         atomic_t started;
311         atomic_t finished;
312         int wait;
313 };
314
315 static struct call_data_struct * call_data;
316
317 void lock_ipi_call_lock(void)
318 {
319         spin_lock_irq(&call_lock);
320 }
321
322 void unlock_ipi_call_lock(void)
323 {
324         spin_unlock_irq(&call_lock);
325 }
326
327 /*
328  * this function sends a 'generic call function' IPI to one other CPU
329  * in the system.
330  *
331  * cpu is a standard Linux logical CPU number.
332  */
333 static void
334 __smp_call_function_single(int cpu, void (*func) (void *info), void *info,
335                                 int nonatomic, int wait)
336 {
337         struct call_data_struct data;
338         int cpus = 1;
339
340         data.func = func;
341         data.info = info;
342         atomic_set(&data.started, 0);
343         data.wait = wait;
344         if (wait)
345                 atomic_set(&data.finished, 0);
346
347         call_data = &data;
348         wmb();
349         /* Send a message to all other CPUs and wait for them to respond */
350         send_IPI_mask(cpumask_of_cpu(cpu), CALL_FUNCTION_VECTOR);
351
352         /* Wait for response */
353         while (atomic_read(&data.started) != cpus)
354                 cpu_relax();
355
356         if (!wait)
357                 return;
358
359         while (atomic_read(&data.finished) != cpus)
360                 cpu_relax();
361 }
362
363 /*
364  * smp_call_function_single - Run a function on another CPU
365  * @func: The function to run. This must be fast and non-blocking.
366  * @info: An arbitrary pointer to pass to the function.
367  * @nonatomic: Currently unused.
368  * @wait: If true, wait until function has completed on other CPUs.
369  *
370  * Retrurns 0 on success, else a negative status code.
371  *
372  * Does not return until the remote CPU is nearly ready to execute <func>
373  * or is or has executed.
374  */
375
376 int smp_call_function_single (int cpu, void (*func) (void *info), void *info,
377         int nonatomic, int wait)
378 {
379         /* prevent preemption and reschedule on another processor */
380         int me = get_cpu();
381         if (cpu == me) {
382                 WARN_ON(1);
383                 put_cpu();
384                 return -EBUSY;
385         }
386         spin_lock_bh(&call_lock);
387         __smp_call_function_single(cpu, func, info, nonatomic, wait);
388         spin_unlock_bh(&call_lock);
389         put_cpu();
390         return 0;
391 }
392
393 /*
394  * this function sends a 'generic call function' IPI to all other CPUs
395  * in the system.
396  */
397 static void __smp_call_function (void (*func) (void *info), void *info,
398                                 int nonatomic, int wait)
399 {
400         struct call_data_struct data;
401         int cpus = num_online_cpus()-1;
402
403         if (!cpus)
404                 return;
405
406         data.func = func;
407         data.info = info;
408         atomic_set(&data.started, 0);
409         data.wait = wait;
410         if (wait)
411                 atomic_set(&data.finished, 0);
412
413         call_data = &data;
414         wmb();
415         /* Send a message to all other CPUs and wait for them to respond */
416         send_IPI_allbutself(CALL_FUNCTION_VECTOR);
417
418         /* Wait for response */
419         while (atomic_read(&data.started) != cpus)
420                 cpu_relax();
421
422         if (!wait)
423                 return;
424
425         while (atomic_read(&data.finished) != cpus)
426                 cpu_relax();
427 }
428
429 /*
430  * smp_call_function - run a function on all other CPUs.
431  * @func: The function to run. This must be fast and non-blocking.
432  * @info: An arbitrary pointer to pass to the function.
433  * @nonatomic: currently unused.
434  * @wait: If true, wait (atomically) until function has completed on other
435  *        CPUs.
436  *
437  * Returns 0 on success, else a negative status code. Does not return until
438  * remote CPUs are nearly ready to execute func or are or have executed.
439  *
440  * You must not call this function with disabled interrupts or from a
441  * hardware interrupt handler or from a bottom half handler.
442  * Actually there are a few legal cases, like panic.
443  */
444 int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
445                         int wait)
446 {
447         spin_lock(&call_lock);
448         __smp_call_function(func,info,nonatomic,wait);
449         spin_unlock(&call_lock);
450         return 0;
451 }
452
453 void smp_stop_cpu(void)
454 {
455         /*
456          * Remove this CPU:
457          */
458         cpu_clear(smp_processor_id(), cpu_online_map);
459         local_irq_disable();
460         disable_local_APIC();
461         local_irq_enable(); 
462 }
463
464 static void smp_really_stop_cpu(void *dummy)
465 {
466         smp_stop_cpu(); 
467         for (;;) 
468                 asm("hlt"); 
469
470
471 void smp_send_stop(void)
472 {
473         int nolock = 0;
474         if (reboot_force)
475                 return;
476         /* Don't deadlock on the call lock in panic */
477         if (!spin_trylock(&call_lock)) {
478                 /* ignore locking because we have paniced anyways */
479                 nolock = 1;
480         }
481         __smp_call_function(smp_really_stop_cpu, NULL, 0, 0);
482         if (!nolock)
483                 spin_unlock(&call_lock);
484
485         local_irq_disable();
486         disable_local_APIC();
487         local_irq_enable();
488 }
489
490 /*
491  * Reschedule call back. Nothing to do,
492  * all the work is done automatically when
493  * we return from the interrupt.
494  */
495 asmlinkage void smp_reschedule_interrupt(void)
496 {
497         ack_APIC_irq();
498 }
499
500 asmlinkage void smp_call_function_interrupt(void)
501 {
502         void (*func) (void *info) = call_data->func;
503         void *info = call_data->info;
504         int wait = call_data->wait;
505
506         ack_APIC_irq();
507         /*
508          * Notify initiating CPU that I've grabbed the data and am
509          * about to execute the function
510          */
511         mb();
512         atomic_inc(&call_data->started);
513         /*
514          * At this point the info structure may be out of scope unless wait==1
515          */
516         irq_enter();
517         (*func)(info);
518         irq_exit();
519         if (wait) {
520                 mb();
521                 atomic_inc(&call_data->finished);
522         }
523 }
524
525 int safe_smp_processor_id(void)
526 {
527         int apicid, i;
528
529         if (disable_apic)
530                 return 0;
531
532         apicid = hard_smp_processor_id();
533         if (x86_cpu_to_apicid[apicid] == apicid)
534                 return apicid;
535
536         for (i = 0; i < NR_CPUS; ++i) {
537                 if (x86_cpu_to_apicid[i] == apicid)
538                         return i;
539         }
540
541         /* No entries in x86_cpu_to_apicid?  Either no MPS|ACPI,
542          * or called too early.  Either way, we must be CPU 0. */
543         if (x86_cpu_to_apicid[0] == BAD_APICID)
544                 return 0;
545
546         return 0; /* Should not happen */
547 }