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