Merge branch 'linux-linaro-lsk' into linux-linaro-lsk-android
[firefly-linux-kernel-4.4.55.git] / drivers / base / power / main.c
index 220ec3a3ca750907bb26c3f2866e1c012b04db52..a42c3548bdd34bcfe4d3e47390c438333c7d35cc 100644 (file)
@@ -31,6 +31,8 @@
 #include <trace/events/power.h>
 #include <linux/cpufreq.h>
 #include <linux/cpuidle.h>
+#include <linux/timer.h>
+
 #include "../base.h"
 #include "power.h"
 
@@ -56,6 +58,12 @@ struct suspend_stats suspend_stats;
 static DEFINE_MUTEX(dpm_list_mtx);
 static pm_message_t pm_transition;
 
+struct dpm_watchdog {
+       struct device           *dev;
+       struct task_struct      *tsk;
+       struct timer_list       timer;
+};
+
 static int async_error;
 
 /**
@@ -386,6 +394,56 @@ static int dpm_run_callback(pm_callback_t cb, struct device *dev,
        return error;
 }
 
+/**
+ * dpm_wd_handler - Driver suspend / resume watchdog handler.
+ *
+ * Called when a driver has timed out suspending or resuming.
+ * There's not much we can do here to recover so BUG() out for
+ * a crash-dump
+ */
+static void dpm_wd_handler(unsigned long data)
+{
+       struct dpm_watchdog *wd = (void *)data;
+       struct device *dev      = wd->dev;
+       struct task_struct *tsk = wd->tsk;
+
+       dev_emerg(dev, "**** DPM device timeout ****\n");
+       show_stack(tsk, NULL);
+
+       BUG();
+}
+
+/**
+ * dpm_wd_set - Enable pm watchdog for given device.
+ * @wd: Watchdog. Must be allocated on the stack.
+ * @dev: Device to handle.
+ */
+static void dpm_wd_set(struct dpm_watchdog *wd, struct device *dev)
+{
+       struct timer_list *timer = &wd->timer;
+
+       wd->dev = dev;
+       wd->tsk = get_current();
+
+       init_timer_on_stack(timer);
+       timer->expires = jiffies + HZ * 12;
+       timer->function = dpm_wd_handler;
+       timer->data = (unsigned long)wd;
+       add_timer(timer);
+}
+
+/**
+ * dpm_wd_clear - Disable pm watchdog.
+ * @wd: Watchdog to disable.
+ */
+static void dpm_wd_clear(struct dpm_watchdog *wd)
+{
+       struct timer_list *timer = &wd->timer;
+
+       del_timer_sync(timer);
+       destroy_timer_on_stack(timer);
+}
+
 /*------------------------- Resume routines -------------------------*/
 
 /**
@@ -572,6 +630,7 @@ static int device_resume(struct device *dev, pm_message_t state, bool async)
        pm_callback_t callback = NULL;
        char *info = NULL;
        int error = 0;
+       struct dpm_watchdog wd;
 
        TRACE_DEVICE(dev);
        TRACE_RESUME(0);
@@ -587,6 +646,7 @@ static int device_resume(struct device *dev, pm_message_t state, bool async)
         * a resumed device, even if the device hasn't been completed yet.
         */
        dev->power.is_prepared = false;
+       dpm_wd_set(&wd, dev);
 
        if (!dev->power.is_suspended)
                goto Unlock;
@@ -638,6 +698,7 @@ static int device_resume(struct device *dev, pm_message_t state, bool async)
 
  Unlock:
        device_unlock(dev);
+       dpm_wd_clear(&wd);
 
  Complete:
        complete_all(&dev->power.completion);
@@ -1057,6 +1118,7 @@ static int __device_suspend(struct device *dev, pm_message_t state, bool async)
        pm_callback_t callback = NULL;
        char *info = NULL;
        int error = 0;
+       struct dpm_watchdog wd;
 
        dpm_wait_for_children(dev, async);
 
@@ -1079,6 +1141,8 @@ static int __device_suspend(struct device *dev, pm_message_t state, bool async)
 
        if (dev->power.syscore)
                goto Complete;
+       
+       dpm_wd_set(&wd, dev);
 
        device_lock(dev);
 
@@ -1135,6 +1199,8 @@ static int __device_suspend(struct device *dev, pm_message_t state, bool async)
 
        device_unlock(dev);
 
+       dpm_wd_clear(&wd);
+
  Complete:
        complete_all(&dev->power.completion);
        if (error)