]> rtime.felk.cvut.cz Git - linux-imx.git/blobdiff - arch/x86/kernel/cpu/mtrr/main.c
x86, mtrr: lock stop machine during MTRR rendezvous sequence
[linux-imx.git] / arch / x86 / kernel / cpu / mtrr / main.c
index 79556bd9b602a41b3ef3bc5502eccb05ed8dba1d..172891ca9d651e28a002dba5a8cc2b4e4035285e 100644 (file)
@@ -35,6 +35,7 @@
 
 #include <linux/types.h> /* FIXME: kvm_para.h needs this */
 
+#include <linux/stop_machine.h>
 #include <linux/kvm_para.h>
 #include <linux/uaccess.h>
 #include <linux/module.h>
@@ -143,22 +144,28 @@ struct set_mtrr_data {
        mtrr_type       smp_type;
 };
 
+static DEFINE_PER_CPU(struct cpu_stop_work, mtrr_work);
+
 /**
- * ipi_handler - Synchronisation handler. Executed by "other" CPUs.
+ * mtrr_work_handler - Synchronisation handler. Executed by "other" CPUs.
  * @info: pointer to mtrr configuration data
  *
  * Returns nothing.
  */
-static void ipi_handler(void *info)
+static int mtrr_work_handler(void *info)
 {
 #ifdef CONFIG_SMP
        struct set_mtrr_data *data = info;
        unsigned long flags;
 
+       atomic_dec(&data->count);
+       while (!atomic_read(&data->gate))
+               cpu_relax();
+
        local_irq_save(flags);
 
        atomic_dec(&data->count);
-       while (!atomic_read(&data->gate))
+       while (atomic_read(&data->gate))
                cpu_relax();
 
        /*  The master has cleared me to execute  */
@@ -173,12 +180,13 @@ static void ipi_handler(void *info)
        }
 
        atomic_dec(&data->count);
-       while (atomic_read(&data->gate))
+       while (!atomic_read(&data->gate))
                cpu_relax();
 
        atomic_dec(&data->count);
        local_irq_restore(flags);
 #endif
+       return 0;
 }
 
 static inline int types_compatible(mtrr_type type1, mtrr_type type2)
@@ -198,7 +206,7 @@ static inline int types_compatible(mtrr_type type1, mtrr_type type2)
  *
  * This is kinda tricky, but fortunately, Intel spelled it out for us cleanly:
  *
- * 1. Send IPI to do the following:
+ * 1. Queue work to do the following on all processors:
  * 2. Disable Interrupts
  * 3. Wait for all procs to do so
  * 4. Enter no-fill cache mode
@@ -215,14 +223,17 @@ static inline int types_compatible(mtrr_type type1, mtrr_type type2)
  * 15. Enable interrupts.
  *
  * What does that mean for us? Well, first we set data.count to the number
- * of CPUs. As each CPU disables interrupts, it'll decrement it once. We wait
- * until it hits 0 and proceed. We set the data.gate flag and reset data.count.
- * Meanwhile, they are waiting for that flag to be set. Once it's set, each
+ * of CPUs. As each CPU announces that it started the rendezvous handler by
+ * decrementing the count, We reset data.count and set the data.gate flag
+ * allowing all the cpu's to proceed with the work. As each cpu disables
+ * interrupts, it'll decrement data.count once. We wait until it hits 0 and
+ * proceed. We clear the data.gate flag and reset data.count. Meanwhile, they
+ * are waiting for that flag to be cleared. Once it's cleared, each
  * CPU goes through the transition of updating MTRRs.
  * The CPU vendors may each do it differently,
  * so we call mtrr_if->set() callback and let them take care of it.
  * When they're done, they again decrement data->count and wait for data.gate
- * to be reset.
+ * to be set.
  * When we finish, we wait for data.count to hit 0 and toggle the data.gate flag
  * Everyone then enables interrupts and we all continue on.
  *
@@ -234,6 +245,28 @@ set_mtrr(unsigned int reg, unsigned long base, unsigned long size, mtrr_type typ
 {
        struct set_mtrr_data data;
        unsigned long flags;
+       int cpu;
+
+#ifdef CONFIG_SMP
+       /*
+        * If this cpu is not yet active, we are in the cpu online path. There
+        * can be no stop_machine() in parallel, as stop machine ensures this
+        * by using get_online_cpus(). We can skip taking the stop_cpus_mutex,
+        * as we don't need it and also we can't afford to block while waiting
+        * for the mutex.
+        *
+        * If this cpu is active, we need to prevent stop_machine() happening
+        * in parallel by taking the stop cpus mutex.
+        *
+        * Also, this is called in the context of cpu online path or in the
+        * context where cpu hotplug is prevented. So checking the active status
+        * of the raw_smp_processor_id() is safe.
+        */
+       if (cpu_active(raw_smp_processor_id()))
+               mutex_lock(&stop_cpus_mutex);
+#endif
+
+       preempt_disable();
 
        data.smp_reg = reg;
        data.smp_base = base;
@@ -246,10 +279,15 @@ set_mtrr(unsigned int reg, unsigned long base, unsigned long size, mtrr_type typ
        atomic_set(&data.gate, 0);
 
        /* Start the ball rolling on other CPUs */
-       if (smp_call_function(ipi_handler, &data, 0) != 0)
-               panic("mtrr: timed out waiting for other CPUs\n");
+       for_each_online_cpu(cpu) {
+               struct cpu_stop_work *work = &per_cpu(mtrr_work, cpu);
+
+               if (cpu == smp_processor_id())
+                       continue;
+
+               stop_one_cpu_nowait(cpu, mtrr_work_handler, &data, work);
+       }
 
-       local_irq_save(flags);
 
        while (atomic_read(&data.count))
                cpu_relax();
@@ -259,18 +297,38 @@ set_mtrr(unsigned int reg, unsigned long base, unsigned long size, mtrr_type typ
        smp_wmb();
        atomic_set(&data.gate, 1);
 
+       local_irq_save(flags);
+
+       while (atomic_read(&data.count))
+               cpu_relax();
+
+       /* Ok, reset count and toggle gate */
+       atomic_set(&data.count, num_booting_cpus() - 1);
+       smp_wmb();
+       atomic_set(&data.gate, 0);
+
        /* Do our MTRR business */
 
        /*
         * HACK!
-        * We use this same function to initialize the mtrrs on boot.
-        * The state of the boot cpu's mtrrs has been saved, and we want
-        * to replicate across all the APs.
-        * If we're doing that @reg is set to something special...
+        *
+        * We use this same function to initialize the mtrrs during boot,
+        * resume, runtime cpu online and on an explicit request to set a
+        * specific MTRR.
+        *
+        * During boot or suspend, the state of the boot cpu's mtrrs has been
+        * saved, and we want to replicate that across all the cpus that come
+        * online (either at the end of boot or resume or during a runtime cpu
+        * online). If we're doing that, @reg is set to something special and on
+        * this cpu we still do mtrr_if->set_all(). During boot/resume, this
+        * is unnecessary if at this point we are still on the cpu that started
+        * the boot/resume sequence. But there is no guarantee that we are still
+        * on the same cpu. So we do mtrr_if->set_all() on this cpu aswell to be
+        * sure that we are in sync with everyone else.
         */
        if (reg != ~0U)
                mtrr_if->set(reg, base, size, type);
-       else if (!mtrr_aps_delayed_init)
+       else
                mtrr_if->set_all();
 
        /* Wait for the others */
@@ -279,7 +337,7 @@ set_mtrr(unsigned int reg, unsigned long base, unsigned long size, mtrr_type typ
 
        atomic_set(&data.count, num_booting_cpus() - 1);
        smp_wmb();
-       atomic_set(&data.gate, 0);
+       atomic_set(&data.gate, 1);
 
        /*
         * Wait here for everyone to have seen the gate change
@@ -289,6 +347,11 @@ set_mtrr(unsigned int reg, unsigned long base, unsigned long size, mtrr_type typ
                cpu_relax();
 
        local_irq_restore(flags);
+       preempt_enable();
+#ifdef CONFIG_SMP
+       if (cpu_active(raw_smp_processor_id()))
+               mutex_unlock(&stop_cpus_mutex);
+#endif
 }
 
 /**
@@ -763,13 +826,21 @@ void set_mtrr_aps_delayed_init(void)
 }
 
 /*
- * MTRR initialization for all AP's
+ * Delayed MTRR initialization for all AP's
  */
 void mtrr_aps_init(void)
 {
        if (!use_intel())
                return;
 
+       /*
+        * Check if someone has requested the delay of AP MTRR initialization,
+        * by doing set_mtrr_aps_delayed_init(), prior to this point. If not,
+        * then we are done.
+        */
+       if (!mtrr_aps_delayed_init)
+               return;
+
        set_mtrr(~0U, 0, 0, 0);
        mtrr_aps_delayed_init = false;
 }