x86: provide __smp_call_function
[sfrench/cifs-2.6.git] / arch / x86 / kernel / smp_64.c
index df4a82812adbbad89fe7d491c3ac7347553cb281..aa2edb7f3a5147de61bf9ca0dad6719d90609908 100644 (file)
@@ -29,7 +29,7 @@
 #include <asm/idle.h>
 
 /*
- *     Smarter SMP flushing macros. 
+ *     Smarter SMP flushing macros.
  *             c/o Linus Torvalds.
  *
  *     These mean you can really definitely utterly forget about
  *
  *     Optimizations Manfred Spraul <manfred@colorfullife.com>
  *
- *     More scalable flush, from Andi Kleen
+ *     More scalable flush, from Andi Kleen
  *
- *     To avoid global state use 8 different call vectors.
- *     Each CPU uses a specific vector to trigger flushes on other
- *     CPUs. Depending on the received vector the target CPUs look into
+ *     To avoid global state use 8 different call vectors.
+ *     Each CPU uses a specific vector to trigger flushes on other
+ *     CPUs. Depending on the received vector the target CPUs look into
  *     the right per cpu variable for the flush data.
  *
- *     With more than 8 CPUs they are hashed to the 8 available
- *     vectors. The limited global vector space forces us to this right now.
+ *     With more than 8 CPUs they are hashed to the 8 available
+ *     vectors. The limited global vector space forces us to this right now.
  *     In future when interrupts are split into per CPU domains this could be
  *     fixed, at the cost of triggering multiple IPIs in some cases.
  */
@@ -55,7 +55,6 @@ union smp_flush_state {
                cpumask_t flush_cpumask;
                struct mm_struct *flush_mm;
                unsigned long flush_va;
-#define FLUSH_ALL      -1ULL
                spinlock_t tlbstate_lock;
        };
        char pad[SMP_CACHE_BYTES];
@@ -67,16 +66,17 @@ union smp_flush_state {
 static DEFINE_PER_CPU(union smp_flush_state, flush_state);
 
 /*
- * We cannot call mmdrop() because we are in interrupt context, 
+ * We cannot call mmdrop() because we are in interrupt context,
  * instead update mm->cpu_vm_mask.
  */
-static inline void leave_mm(int cpu)
+void leave_mm(int cpu)
 {
        if (read_pda(mmu_state) == TLBSTATE_OK)
                BUG();
        cpu_clear(cpu, read_pda(active_mm)->cpu_vm_mask);
        load_cr3(swapper_pg_dir);
 }
+EXPORT_SYMBOL_GPL(leave_mm);
 
 /*
  *
@@ -85,25 +85,25 @@ static inline void leave_mm(int cpu)
  * 1) switch_mm() either 1a) or 1b)
  * 1a) thread switch to a different mm
  * 1a1) cpu_clear(cpu, old_mm->cpu_vm_mask);
- *     Stop ipi delivery for the old mm. This is not synchronized with
- *     the other cpus, but smp_invalidate_interrupt ignore flush ipis
- *     for the wrong mm, and in the worst case we perform a superfluous
- *     tlb flush.
+ *     Stop ipi delivery for the old mm. This is not synchronized with
+ *     the other cpus, but smp_invalidate_interrupt ignore flush ipis
+ *     for the wrong mm, and in the worst case we perform a superfluous
+ *     tlb flush.
  * 1a2) set cpu mmu_state to TLBSTATE_OK
- *     Now the smp_invalidate_interrupt won't call leave_mm if cpu0
+ *     Now the smp_invalidate_interrupt won't call leave_mm if cpu0
  *     was in lazy tlb mode.
  * 1a3) update cpu active_mm
- *     Now cpu0 accepts tlb flushes for the new mm.
+ *     Now cpu0 accepts tlb flushes for the new mm.
  * 1a4) cpu_set(cpu, new_mm->cpu_vm_mask);
- *     Now the other cpus will send tlb flush ipis.
+ *     Now the other cpus will send tlb flush ipis.
  * 1a4) change cr3.
  * 1b) thread switch without mm change
  *     cpu active_mm is correct, cpu0 already handles
  *     flush ipis.
  * 1b1) set cpu mmu_state to TLBSTATE_OK
  * 1b2) test_and_set the cpu bit in cpu_vm_mask.
- *     Atomically set the bit [other cpus will start sending flush ipis],
- *     and test the bit.
+ *     Atomically set the bit [other cpus will start sending flush ipis],
+ *     and test the bit.
  * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
  * 2) switch %%esp, ie current
  *
@@ -137,12 +137,12 @@ asmlinkage void smp_invalidate_interrupt(struct pt_regs *regs)
         * orig_rax contains the negated interrupt vector.
         * Use that to determine where the sender put the data.
         */
-       sender = ~regs->orig_rax - INVALIDATE_TLB_VECTOR_START;
+       sender = ~regs->orig_ax - INVALIDATE_TLB_VECTOR_START;
        f = &per_cpu(flush_state, sender);
 
        if (!cpu_isset(cpu, f->flush_cpumask))
                goto out;
-               /* 
+               /*
                 * This was a BUG() but until someone can quote me the
                 * line from the intel manual that guarantees an IPI to
                 * multiple CPUs is retried _only_ on the erroring CPUs
@@ -150,10 +150,10 @@ asmlinkage void smp_invalidate_interrupt(struct pt_regs *regs)
                 *
                 * BUG();
                 */
-                
+
        if (f->flush_mm == read_pda(active_mm)) {
                if (read_pda(mmu_state) == TLBSTATE_OK) {
-                       if (f->flush_va == FLUSH_ALL)
+                       if (f->flush_va == TLB_FLUSH_ALL)
                                local_flush_tlb();
                        else
                                __flush_tlb_one(f->flush_va);
@@ -163,21 +163,25 @@ asmlinkage void smp_invalidate_interrupt(struct pt_regs *regs)
 out:
        ack_APIC_irq();
        cpu_clear(cpu, f->flush_cpumask);
+       add_pda(irq_tlb_count, 1);
 }
 
-static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
-                                               unsigned long va)
+void native_flush_tlb_others(const cpumask_t *cpumaskp, struct mm_struct *mm,
+                            unsigned long va)
 {
        int sender;
        union smp_flush_state *f;
+       cpumask_t cpumask = *cpumaskp;
 
        /* Caller has disabled preemption */
        sender = smp_processor_id() % NUM_INVALIDATE_TLB_VECTORS;
        f = &per_cpu(flush_state, sender);
 
-       /* Could avoid this lock when
-          num_online_cpus() <= NUM_INVALIDATE_TLB_VECTORS, but it is
-          probably not worth checking this for a cache-hot lock. */
+       /*
+        * Could avoid this lock when
+        * num_online_cpus() <= NUM_INVALIDATE_TLB_VECTORS, but it is
+        * probably not worth checking this for a cache-hot lock.
+        */
        spin_lock(&f->tlbstate_lock);
 
        f->flush_mm = mm;
@@ -201,14 +205,14 @@ static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
 int __cpuinit init_smp_flush(void)
 {
        int i;
+
        for_each_cpu_mask(i, cpu_possible_map) {
                spin_lock_init(&per_cpu(flush_state, i).tlbstate_lock);
        }
        return 0;
 }
-
 core_initcall(init_smp_flush);
-       
+
 void flush_tlb_current_task(void)
 {
        struct mm_struct *mm = current->mm;
@@ -220,10 +224,9 @@ void flush_tlb_current_task(void)
 
        local_flush_tlb();
        if (!cpus_empty(cpu_mask))
-               flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
+               flush_tlb_others(cpu_mask, mm, TLB_FLUSH_ALL);
        preempt_enable();
 }
-EXPORT_SYMBOL(flush_tlb_current_task);
 
 void flush_tlb_mm (struct mm_struct * mm)
 {
@@ -240,11 +243,10 @@ void flush_tlb_mm (struct mm_struct * mm)
                        leave_mm(smp_processor_id());
        }
        if (!cpus_empty(cpu_mask))
-               flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
+               flush_tlb_others(cpu_mask, mm, TLB_FLUSH_ALL);
 
        preempt_enable();
 }
-EXPORT_SYMBOL(flush_tlb_mm);
 
 void flush_tlb_page(struct vm_area_struct * vma, unsigned long va)
 {
@@ -258,8 +260,8 @@ void flush_tlb_page(struct vm_area_struct * vma, unsigned long va)
        if (current->active_mm == mm) {
                if(current->mm)
                        __flush_tlb_one(va);
-                else
-                       leave_mm(smp_processor_id());
+               else
+                       leave_mm(smp_processor_id());
        }
 
        if (!cpus_empty(cpu_mask))
@@ -267,7 +269,6 @@ void flush_tlb_page(struct vm_area_struct * vma, unsigned long va)
 
        preempt_enable();
 }
-EXPORT_SYMBOL(flush_tlb_page);
 
 static void do_flush_tlb_all(void* info)
 {
@@ -289,8 +290,9 @@ void flush_tlb_all(void)
  * anything. Worst case is that we lose a reschedule ...
  */
 
-void smp_send_reschedule(int cpu)
+static void native_smp_send_reschedule(int cpu)
 {
+       WARN_ON(cpu_is_offline(cpu));
        send_IPI_mask(cpumask_of_cpu(cpu), RESCHEDULE_VECTOR);
 }
 
@@ -320,18 +322,14 @@ void unlock_ipi_call_lock(void)
        spin_unlock_irq(&call_lock);
 }
 
-/*
- * this function sends a 'generic call function' IPI to one other CPU
- * in the system.
- *
- * cpu is a standard Linux logical CPU number.
- */
-static void
-__smp_call_function_single(int cpu, void (*func) (void *info), void *info,
+static void __smp_call_function(void (*func) (void *info), void *info,
                                int nonatomic, int wait)
 {
        struct call_data_struct data;
-       int cpus = 1;
+       int cpus = num_online_cpus() - 1;
+
+       if (!cpus)
+               return;
 
        data.func = func;
        data.info = info;
@@ -341,71 +339,41 @@ __smp_call_function_single(int cpu, void (*func) (void *info), void *info,
                atomic_set(&data.finished, 0);
 
        call_data = &data;
-       wmb();
+       mb();
+
        /* Send a message to all other CPUs and wait for them to respond */
-       send_IPI_mask(cpumask_of_cpu(cpu), CALL_FUNCTION_VECTOR);
+       send_IPI_allbutself(CALL_FUNCTION_VECTOR);
 
        /* Wait for response */
        while (atomic_read(&data.started) != cpus)
                cpu_relax();
 
-       if (!wait)
-               return;
-
-       while (atomic_read(&data.finished) != cpus)
-               cpu_relax();
+       if (wait)
+               while (atomic_read(&data.finished) != cpus)
+                       cpu_relax();
 }
 
-/*
- * smp_call_function_single - Run a function on a specific CPU
- * @func: The function to run. This must be fast and non-blocking.
- * @info: An arbitrary pointer to pass to the function.
- * @nonatomic: Currently unused.
- * @wait: If true, wait until function has completed on other CPUs.
- *
- * Retrurns 0 on success, else a negative status code.
- *
- * Does not return until the remote CPU is nearly ready to execute <func>
- * or is or has executed.
- */
-
-int smp_call_function_single (int cpu, void (*func) (void *info), void *info,
-       int nonatomic, int wait)
-{
-       /* prevent preemption and reschedule on another processor */
-       int me = get_cpu();
-
-       /* Can deadlock when called with interrupts disabled */
-       WARN_ON(irqs_disabled());
-
-       if (cpu == me) {
-               local_irq_disable();
-               func(info);
-               local_irq_enable();
-               put_cpu();
-               return 0;
-       }
-
-       spin_lock(&call_lock);
-       __smp_call_function_single(cpu, func, info, nonatomic, wait);
-       spin_unlock(&call_lock);
-       put_cpu();
-       return 0;
-}
-EXPORT_SYMBOL(smp_call_function_single);
 
 /*
- * this function sends a 'generic call function' IPI to all other CPUs
- * in the system.
+ * this function sends a 'generic call function' IPI to all other CPU
+ * of the system defined in the mask.
  */
-static void __smp_call_function (void (*func) (void *info), void *info,
-                               int nonatomic, int wait)
+static int __smp_call_function_mask(cpumask_t mask,
+                                   void (*func)(void *), void *info,
+                                   int wait)
 {
        struct call_data_struct data;
-       int cpus = num_online_cpus()-1;
+       cpumask_t allbutself;
+       int cpus;
+
+       allbutself = cpu_online_map;
+       cpu_clear(smp_processor_id(), allbutself);
+
+       cpus_and(mask, mask, allbutself);
+       cpus = cpus_weight(mask);
 
        if (!cpus)
-               return;
+               return 0;
 
        data.func = func;
        data.info = info;
@@ -416,44 +384,54 @@ static void __smp_call_function (void (*func) (void *info), void *info,
 
        call_data = &data;
        wmb();
-       /* Send a message to all other CPUs and wait for them to respond */
-       send_IPI_allbutself(CALL_FUNCTION_VECTOR);
+
+       /* Send a message to other CPUs */
+       if (cpus_equal(mask, allbutself))
+               send_IPI_allbutself(CALL_FUNCTION_VECTOR);
+       else
+               send_IPI_mask(mask, CALL_FUNCTION_VECTOR);
 
        /* Wait for response */
        while (atomic_read(&data.started) != cpus)
                cpu_relax();
 
        if (!wait)
-               return;
+               return 0;
 
        while (atomic_read(&data.finished) != cpus)
                cpu_relax();
-}
 
-/*
- * smp_call_function - run a function on all other CPUs.
+       return 0;
+}
+/**
+ * smp_call_function_mask(): Run a function on a set of other CPUs.
+ * @mask: The set of cpus to run on.  Must not include the current cpu.
  * @func: The function to run. This must be fast and non-blocking.
  * @info: An arbitrary pointer to pass to the function.
- * @nonatomic: currently unused.
- * @wait: If true, wait (atomically) until function has completed on other
- *        CPUs.
+ * @wait: If true, wait (atomically) until function has completed on other CPUs.
+ *
+ * Returns 0 on success, else a negative status code.
  *
- * Returns 0 on success, else a negative status code. Does not return until
- * remote CPUs are nearly ready to execute func or are or have executed.
+ * If @wait is true, then returns once @func has returned; otherwise
+ * it returns just before the target cpu calls @func.
  *
  * You must not call this function with disabled interrupts or from a
  * hardware interrupt handler or from a bottom half handler.
- * Actually there are a few legal cases, like panic.
  */
-int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
-                       int wait)
+int native_smp_call_function_mask(cpumask_t mask,
+                                 void (*func)(void *), void *info,
+                                 int wait)
 {
+       int ret;
+
+       /* Can deadlock when called with interrupts disabled */
+       WARN_ON(irqs_disabled());
+
        spin_lock(&call_lock);
-       __smp_call_function(func,info,nonatomic,wait);
+       ret = __smp_call_function_mask(mask, func, info, wait);
        spin_unlock(&call_lock);
-       return 0;
+       return ret;
 }
-EXPORT_SYMBOL(smp_call_function);
 
 static void stop_this_cpu(void *dummy)
 {
@@ -463,9 +441,9 @@ static void stop_this_cpu(void *dummy)
         */
        cpu_clear(smp_processor_id(), cpu_online_map);
        disable_local_APIC();
-       for (;;) 
+       for (;;)
                halt();
-} 
+}
 
 void smp_send_stop(void)
 {
@@ -493,6 +471,7 @@ void smp_send_stop(void)
 asmlinkage void smp_reschedule_interrupt(void)
 {
        ack_APIC_irq();
+       add_pda(irq_resched_count, 1);
 }
 
 asmlinkage void smp_call_function_interrupt(void)
@@ -514,6 +493,7 @@ asmlinkage void smp_call_function_interrupt(void)
        exit_idle();
        irq_enter();
        (*func)(info);
+       add_pda(irq_call_count, 1);
        irq_exit();
        if (wait) {
                mb();
@@ -521,3 +501,13 @@ asmlinkage void smp_call_function_interrupt(void)
        }
 }
 
+struct smp_ops smp_ops = {
+       .smp_prepare_boot_cpu = native_smp_prepare_boot_cpu,
+       .smp_prepare_cpus = native_smp_prepare_cpus,
+       .smp_cpus_done = native_smp_cpus_done,
+
+       .smp_send_reschedule = native_smp_send_reschedule,
+       .smp_call_function_mask = native_smp_call_function_mask,
+       .cpu_up = native_cpu_up,
+};
+EXPORT_SYMBOL_GPL(smp_ops);