]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - arch/arm/common/bL_switcher.c
Merge remote-tracking branch 'usb/usb-next'
[karo-tx-linux.git] / arch / arm / common / bL_switcher.c
index 7d98629aa446053a3ba909b6bbf8c80b36ac0056..63bbc4f70564ae8449d156be7dcdb00ab342f378 100644 (file)
@@ -9,6 +9,7 @@
  * published by the Free Software Foundation.
  */
 
+#include <linux/atomic.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/cpumask.h>
 #include <linux/kthread.h>
 #include <linux/wait.h>
+#include <linux/time.h>
 #include <linux/clockchips.h>
 #include <linux/hrtimer.h>
 #include <linux/tick.h>
+#include <linux/notifier.h>
 #include <linux/mm.h>
 #include <linux/mutex.h>
+#include <linux/smp.h>
+#include <linux/spinlock.h>
 #include <linux/string.h>
 #include <linux/sysfs.h>
 #include <linux/irqchip/arm-gic.h>
 #include <linux/moduleparam.h>
 
 #include <asm/smp_plat.h>
+#include <asm/cputype.h>
 #include <asm/suspend.h>
 #include <asm/mcpm.h>
 #include <asm/bL_switcher.h>
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/power_cpu_migrate.h>
+
 
 /*
  * Use our own MPIDR accessors as the generic ones in asm/cputype.h have
@@ -48,13 +57,24 @@ static int read_mpidr(void)
        return id & MPIDR_HWID_BITMASK;
 }
 
+/*
+ * Get a global nanosecond time stamp for tracing.
+ */
+static s64 get_ns(void)
+{
+       struct timespec ts;
+       getnstimeofday(&ts);
+       return timespec_to_ns(&ts);
+}
+
 /*
  * bL switcher core code.
  */
 
-static void bL_do_switch(void *_unused)
+static void bL_do_switch(void *_arg)
 {
        unsigned ib_mpidr, ib_cpu, ib_cluster;
+       long volatile handshake, **handshake_ptr = _arg;
 
        pr_debug("%s\n", __func__);
 
@@ -62,6 +82,13 @@ static void bL_do_switch(void *_unused)
        ib_cpu = MPIDR_AFFINITY_LEVEL(ib_mpidr, 0);
        ib_cluster = MPIDR_AFFINITY_LEVEL(ib_mpidr, 1);
 
+       /* Advertise our handshake location */
+       if (handshake_ptr) {
+               handshake = 0;
+               *handshake_ptr = &handshake;
+       } else
+               handshake = -1;
+
        /*
         * Our state has been saved at this point.  Let's release our
         * inbound CPU.
@@ -80,6 +107,14 @@ static void bL_do_switch(void *_unused)
         * we have none.
         */
 
+       /*
+        * Let's wait until our inbound is alive.
+        */
+       while (!handshake) {
+               wfe();
+               smp_mb();
+       }
+
        /* Let's put ourself down. */
        mcpm_cpu_power_down();
 
@@ -125,9 +160,11 @@ static int bL_switch_to(unsigned int new_cluster_id)
 {
        unsigned int mpidr, this_cpu, that_cpu;
        unsigned int ob_mpidr, ob_cpu, ob_cluster, ib_mpidr, ib_cpu, ib_cluster;
+       struct completion inbound_alive;
        struct tick_device *tdev;
        enum clock_event_mode tdev_mode;
-       int ret;
+       long volatile *handshake_ptr;
+       int ipi_nr, ret;
 
        this_cpu = smp_processor_id();
        ob_mpidr = read_mpidr();
@@ -146,10 +183,18 @@ static int bL_switch_to(unsigned int new_cluster_id)
        pr_debug("before switch: CPU %d MPIDR %#x -> %#x\n",
                 this_cpu, ob_mpidr, ib_mpidr);
 
+       this_cpu = smp_processor_id();
+
        /* Close the gate for our entry vectors */
        mcpm_set_entry_vector(ob_cpu, ob_cluster, NULL);
        mcpm_set_entry_vector(ib_cpu, ib_cluster, NULL);
 
+       /* Install our "inbound alive" notifier. */
+       init_completion(&inbound_alive);
+       ipi_nr = register_ipi_completion(&inbound_alive, this_cpu);
+       ipi_nr |= ((1 << 16) << bL_gic_id[ob_cpu][ob_cluster]);
+       mcpm_set_early_poke(ib_cpu, ib_cluster, gic_get_sgir_physaddr(), ipi_nr);
+
        /*
         * Let's wake up the inbound CPU now in case it requires some delay
         * to come online, but leave it gated in our entry vector code.
@@ -160,22 +205,30 @@ static int bL_switch_to(unsigned int new_cluster_id)
                return ret;
        }
 
+       /*
+        * Raise a SGI on the inbound CPU to make sure it doesn't stall
+        * in a possible WFI, such as in bL_power_down().
+        */
+       gic_send_sgi(bL_gic_id[ib_cpu][ib_cluster], 0);
+
+       /*
+        * Wait for the inbound to come up.  This allows for other
+        * tasks to be scheduled in the mean time.
+        */
+       wait_for_completion(&inbound_alive);
+       mcpm_set_early_poke(ib_cpu, ib_cluster, 0, 0);
+
        /*
         * From this point we are entering the switch critical zone
         * and can't take any interrupts anymore.
         */
        local_irq_disable();
        local_fiq_disable();
+       trace_cpu_migrate_begin(get_ns(), ob_mpidr);
 
        /* redirect GIC's SGIs to our counterpart */
        gic_migrate_target(bL_gic_id[ib_cpu][ib_cluster]);
 
-       /*
-        * Raise a SGI on the inbound CPU to make sure it doesn't stall
-        * in a possible WFI, such as in mcpm_power_down().
-        */
-       arch_send_wakeup_ipi_mask(cpumask_of(this_cpu));
-
        tdev = tick_get_device(this_cpu);
        if (tdev && !cpumask_equal(tdev->evtdev->cpumask, cpumask_of(this_cpu)))
                tdev = NULL;
@@ -195,7 +248,7 @@ static int bL_switch_to(unsigned int new_cluster_id)
        cpu_logical_map(that_cpu) = ob_mpidr;
 
        /* Let's do the actual CPU switch. */
-       ret = cpu_suspend(0, bL_switchpoint);
+       ret = cpu_suspend((unsigned long)&handshake_ptr, bL_switchpoint);
        if (ret > 0)
                panic("%s: cpu_suspend() returned %d\n", __func__, ret);
 
@@ -214,19 +267,26 @@ static int bL_switch_to(unsigned int new_cluster_id)
                                          tdev->evtdev->next_event, 1);
        }
 
+       trace_cpu_migrate_finish(get_ns(), ib_mpidr);
        local_fiq_enable();
        local_irq_enable();
 
+       *handshake_ptr = 1;
+       dsb_sev();
+
        if (ret)
                pr_err("%s exiting with error %d\n", __func__, ret);
        return ret;
 }
 
 struct bL_thread {
+       spinlock_t lock;
        struct task_struct *task;
        wait_queue_head_t wq;
        int wanted_cluster;
        struct completion started;
+       bL_switch_completion_handler completer;
+       void *completer_cookie;
 };
 
 static struct bL_thread bL_threads[NR_CPUS];
@@ -236,6 +296,8 @@ static int bL_switcher_thread(void *arg)
        struct bL_thread *t = arg;
        struct sched_param param = { .sched_priority = 1 };
        int cluster;
+       bL_switch_completion_handler completer;
+       void *completer_cookie;
 
        sched_setscheduler_nocheck(current, SCHED_FIFO, &param);
        complete(&t->started);
@@ -246,9 +308,21 @@ static int bL_switcher_thread(void *arg)
                wait_event_interruptible(t->wq,
                                t->wanted_cluster != -1 ||
                                kthread_should_stop());
-               cluster = xchg(&t->wanted_cluster, -1);
-               if (cluster != -1)
+
+               spin_lock(&t->lock);
+               cluster = t->wanted_cluster;
+               completer = t->completer;
+               completer_cookie = t->completer_cookie;
+               t->wanted_cluster = -1;
+               t->completer = NULL;
+               spin_unlock(&t->lock);
+
+               if (cluster != -1) {
                        bL_switch_to(cluster);
+
+                       if (completer)
+                               completer(completer_cookie);
+               }
        } while (!kthread_should_stop());
 
        return 0;
@@ -269,16 +343,30 @@ static struct task_struct *bL_switcher_thread_create(int cpu, void *arg)
 }
 
 /*
- * bL_switch_request - Switch to a specific cluster for the given CPU
+ * bL_switch_request_cb - Switch to a specific cluster for the given CPU,
+ *      with completion notification via a callback
  *
  * @cpu: the CPU to switch
  * @new_cluster_id: the ID of the cluster to switch to.
+ * @completer: switch completion callback.  if non-NULL,
+ *     @completer(@completer_cookie) will be called on completion of
+ *     the switch, in non-atomic context.
+ * @completer_cookie: opaque context argument for @completer.
  *
  * This function causes a cluster switch on the given CPU by waking up
  * the appropriate switcher thread.  This function may or may not return
  * before the switch has occurred.
+ *
+ * If a @completer callback function is supplied, it will be called when
+ * the switch is complete.  This can be used to determine asynchronously
+ * when the switch is complete, regardless of when bL_switch_request()
+ * returns.  When @completer is supplied, no new switch request is permitted
+ * for the affected CPU until after the switch is complete, and @completer
+ * has returned.
  */
-int bL_switch_request(unsigned int cpu, unsigned int new_cluster_id)
+int bL_switch_request_cb(unsigned int cpu, unsigned int new_cluster_id,
+                        bL_switch_completion_handler completer,
+                        void *completer_cookie)
 {
        struct bL_thread *t;
 
@@ -288,26 +376,59 @@ int bL_switch_request(unsigned int cpu, unsigned int new_cluster_id)
        }
 
        t = &bL_threads[cpu];
+
        if (IS_ERR(t->task))
                return PTR_ERR(t->task);
        if (!t->task)
                return -ESRCH;
 
+       spin_lock(&t->lock);
+       if (t->completer) {
+               spin_unlock(&t->lock);
+               return -EBUSY;
+       }
+       t->completer = completer;
+       t->completer_cookie = completer_cookie;
        t->wanted_cluster = new_cluster_id;
+       spin_unlock(&t->lock);
        wake_up(&t->wq);
        return 0;
 }
-EXPORT_SYMBOL_GPL(bL_switch_request);
+EXPORT_SYMBOL_GPL(bL_switch_request_cb);
 
 /*
  * Activation and configuration code.
  */
 
 static DEFINE_MUTEX(bL_switcher_activation_lock);
+static BLOCKING_NOTIFIER_HEAD(bL_activation_notifier);
 static unsigned int bL_switcher_active;
 static unsigned int bL_switcher_cpu_original_cluster[NR_CPUS];
 static cpumask_t bL_switcher_removed_logical_cpus;
 
+int bL_switcher_register_notifier(struct notifier_block *nb)
+{
+       return blocking_notifier_chain_register(&bL_activation_notifier, nb);
+}
+EXPORT_SYMBOL_GPL(bL_switcher_register_notifier);
+
+int bL_switcher_unregister_notifier(struct notifier_block *nb)
+{
+       return blocking_notifier_chain_unregister(&bL_activation_notifier, nb);
+}
+EXPORT_SYMBOL_GPL(bL_switcher_unregister_notifier);
+
+static int bL_activation_notify(unsigned long val)
+{
+       int ret;
+
+       ret = blocking_notifier_call_chain(&bL_activation_notifier, val, NULL);
+       if (ret & NOTIFY_STOP_MASK)
+               pr_err("%s: notifier chain failed with status 0x%x\n",
+                       __func__, ret);
+       return notifier_to_errno(ret);
+}
+
 static void bL_switcher_restore_cpus(void)
 {
        int i;
@@ -411,6 +532,46 @@ static int bL_switcher_halve_cpus(void)
        return 0;
 }
 
+/* Determine the logical CPU a given physical CPU is grouped on. */
+int bL_switcher_get_logical_index(u32 mpidr)
+{
+       int cpu;
+
+       if (!bL_switcher_active)
+               return -EUNATCH;
+
+       mpidr &= MPIDR_HWID_BITMASK;
+       for_each_online_cpu(cpu) {
+               int pairing = bL_switcher_cpu_pairing[cpu];
+               if (pairing == -1)
+                       continue;
+               if ((mpidr == cpu_logical_map(cpu)) ||
+                   (mpidr == cpu_logical_map(pairing)))
+                       return cpu;
+       }
+       return -EINVAL;
+}
+
+static void bL_switcher_trace_trigger_cpu(void *__always_unused info)
+{
+       trace_cpu_migrate_current(get_ns(), read_mpidr());
+}
+
+int bL_switcher_trace_trigger(void)
+{
+       int ret;
+
+       preempt_disable();
+
+       bL_switcher_trace_trigger_cpu(NULL);
+       ret = smp_call_function(bL_switcher_trace_trigger_cpu, NULL, true);
+
+       preempt_enable();
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(bL_switcher_trace_trigger);
+
 static int bL_switcher_enable(void)
 {
        int cpu, ret;
@@ -425,15 +586,19 @@ static int bL_switcher_enable(void)
 
        pr_info("big.LITTLE switcher initializing\n");
 
+       ret = bL_activation_notify(BL_NOTIFY_PRE_ENABLE);
+       if (ret)
+               goto error;
+
        ret = bL_switcher_halve_cpus();
-       if (ret) {
-               cpu_hotplug_driver_unlock();
-               mutex_unlock(&bL_switcher_activation_lock);
-               return ret;
-       }
+       if (ret)
+               goto error;
+
+       bL_switcher_trace_trigger();
 
        for_each_online_cpu(cpu) {
                struct bL_thread *t = &bL_threads[cpu];
+               spin_lock_init(&t->lock);
                init_waitqueue_head(&t->wq);
                init_completion(&t->started);
                t->wanted_cluster = -1;
@@ -441,11 +606,18 @@ static int bL_switcher_enable(void)
        }
 
        bL_switcher_active = 1;
+       bL_activation_notify(BL_NOTIFY_POST_ENABLE);
        pr_info("big.LITTLE switcher initialized\n");
+       goto out;
+
+error:
+       pr_warn("big.LITTLE switcher initialization failed\n");
+       bL_activation_notify(BL_NOTIFY_POST_DISABLE);
 
+out:
        cpu_hotplug_driver_unlock();
        mutex_unlock(&bL_switcher_activation_lock);
-       return 0;
+       return ret;
 }
 
 #ifdef CONFIG_SYSFS
@@ -458,11 +630,15 @@ static void bL_switcher_disable(void)
 
        mutex_lock(&bL_switcher_activation_lock);
        cpu_hotplug_driver_lock();
-       if (!bL_switcher_active) {
-               cpu_hotplug_driver_unlock();
-               mutex_unlock(&bL_switcher_activation_lock);
-               return;
+
+       if (!bL_switcher_active)
+               goto out;
+
+       if (bL_activation_notify(BL_NOTIFY_PRE_DISABLE) != 0) {
+               bL_activation_notify(BL_NOTIFY_POST_ENABLE);
+               goto out;
        }
+
        bL_switcher_active = 0;
 
        /*
@@ -504,6 +680,11 @@ static void bL_switcher_disable(void)
        }
 
        bL_switcher_restore_cpus();
+       bL_switcher_trace_trigger();
+
+       bL_activation_notify(BL_NOTIFY_POST_DISABLE);
+
+out:
        cpu_hotplug_driver_unlock();
        mutex_unlock(&bL_switcher_activation_lock);
 }
@@ -534,11 +715,23 @@ static ssize_t bL_switcher_active_store(struct kobject *kobj,
        return (ret >= 0) ? count : ret;
 }
 
+static ssize_t bL_switcher_trace_trigger_store(struct kobject *kobj,
+               struct kobj_attribute *attr, const char *buf, size_t count)
+{
+       int ret = bL_switcher_trace_trigger();
+
+       return ret ? ret : count;
+}
+
 static struct kobj_attribute bL_switcher_active_attr =
        __ATTR(active, 0644, bL_switcher_active_show, bL_switcher_active_store);
 
+static struct kobj_attribute bL_switcher_trace_trigger_attr =
+       __ATTR(trace_trigger, 0200, NULL, bL_switcher_trace_trigger_store);
+
 static struct attribute *bL_switcher_attrs[] = {
        &bL_switcher_active_attr.attr,
+       &bL_switcher_trace_trigger_attr.attr,
        NULL,
 };