drm/radeon/kms: init rdev->num_crtc at asic init
[safe/jmp/linux-2.6] / drivers / base / power / main.c
index 7b76fd3..a5142bd 100644 (file)
  * and add it to the list of power-controlled devices. sysfs entries for
  * controlling device power management will also be added.
  *
- * A different set of lists than the global subsystem list are used to
- * keep track of power info because we use different lists to hold
- * devices based on what stage of the power management process they
- * are in. The power domain dependencies may also differ from the
- * ancestral dependencies that the subsystem list maintains.
+ * A separate list is used for keeping track of power info, because the power
+ * domain dependencies may differ from the ancestral dependencies that the
+ * subsystem list maintains.
  */
 
 #include <linux/device.h>
 #include <linux/kallsyms.h>
 #include <linux/mutex.h>
 #include <linux/pm.h>
+#include <linux/pm_runtime.h>
 #include <linux/resume-trace.h>
-#include <linux/rwsem.h>
+#include <linux/interrupt.h>
+#include <linux/sched.h>
 
 #include "../base.h"
 #include "power.h"
 
 /*
- * The entries in the dpm_active list are in a depth first order, simply
+ * The entries in the dpm_list list are in a depth first order, simply
  * because children are guaranteed to be discovered after parents, and
  * are inserted at the back of the list on discovery.
  *
- * All the other lists are kept in the same order, for consistency.
- * However the lists aren't always traversed in the same order.
- * Semaphores must be acquired from the top (i.e., front) down
- * and released in the opposite order.  Devices must be suspended
- * from the bottom (i.e., end) up and resumed in the opposite order.
- * That way no parent will be suspended while it still has an active
- * child.
- *
  * Since device_pm_add() may be called with a device semaphore held,
  * we must never try to acquire a device semaphore while holding
  * dpm_list_mutex.
  */
 
-LIST_HEAD(dpm_active);
-static LIST_HEAD(dpm_off);
-static LIST_HEAD(dpm_off_irq);
+LIST_HEAD(dpm_list);
 
 static DEFINE_MUTEX(dpm_list_mtx);
 
-/* 'true' if all devices have been suspended, protected by dpm_list_mtx */
-static bool all_sleeping;
+/*
+ * Set once the preparation of devices for a PM transition has started, reset
+ * before starting to resume devices.  Protected by dpm_list_mtx.
+ */
+static bool transition_started;
 
 /**
- *     device_pm_add - add a device to the list of active devices
- *     @dev:   Device to be added to the list
+ * device_pm_init - Initialize the PM-related part of a device object.
+ * @dev: Device object being initialized.
  */
-int device_pm_add(struct device *dev)
+void device_pm_init(struct device *dev)
 {
-       int error;
+       dev->power.status = DPM_ON;
+       pm_runtime_init(dev);
+}
+
+/**
+ * device_pm_lock - Lock the list of active devices used by the PM core.
+ */
+void device_pm_lock(void)
+{
+       mutex_lock(&dpm_list_mtx);
+}
+
+/**
+ * device_pm_unlock - Unlock the list of active devices used by the PM core.
+ */
+void device_pm_unlock(void)
+{
+       mutex_unlock(&dpm_list_mtx);
+}
 
+/**
+ * device_pm_add - Add a device to the PM core's list of active devices.
+ * @dev: Device to add to the list.
+ */
+void device_pm_add(struct device *dev)
+{
        pr_debug("PM: Adding info for %s:%s\n",
                 dev->bus ? dev->bus->name : "No Bus",
                 kobject_name(&dev->kobj));
        mutex_lock(&dpm_list_mtx);
-       if ((dev->parent && dev->parent->power.sleeping) || all_sleeping) {
-               if (dev->parent->power.sleeping)
-                       dev_warn(dev, "parent %s is sleeping\n",
-                               dev->parent->bus_id);
-               else
-                       dev_warn(dev, "all devices are sleeping\n");
-               WARN_ON(true);
+       if (dev->parent) {
+               if (dev->parent->power.status >= DPM_SUSPENDING)
+                       dev_warn(dev, "parent %s should not be sleeping\n",
+                                dev_name(dev->parent));
+       } else if (transition_started) {
+               /*
+                * We refuse to register parentless devices while a PM
+                * transition is in progress in order to avoid leaving them
+                * unhandled down the road
+                */
+               dev_WARN(dev, "Parentless device registered during a PM transaction\n");
        }
-       error = dpm_sysfs_add(dev);
-       if (!error)
-               list_add_tail(&dev->power.entry, &dpm_active);
+
+       list_add_tail(&dev->power.entry, &dpm_list);
        mutex_unlock(&dpm_list_mtx);
-       return error;
 }
 
 /**
- *     device_pm_remove - remove a device from the list of active devices
- *     @dev:   Device to be removed from the list
- *
- *     This function also removes the device's PM-related sysfs attributes.
+ * device_pm_remove - Remove a device from the PM core's list of active devices.
+ * @dev: Device to be removed from the list.
  */
 void device_pm_remove(struct device *dev)
 {
@@ -95,29 +112,303 @@ void device_pm_remove(struct device *dev)
                 dev->bus ? dev->bus->name : "No Bus",
                 kobject_name(&dev->kobj));
        mutex_lock(&dpm_list_mtx);
-       dpm_sysfs_remove(dev);
        list_del_init(&dev->power.entry);
        mutex_unlock(&dpm_list_mtx);
+       pm_runtime_remove(dev);
+}
+
+/**
+ * device_pm_move_before - Move device in the PM core's list of active devices.
+ * @deva: Device to move in dpm_list.
+ * @devb: Device @deva should come before.
+ */
+void device_pm_move_before(struct device *deva, struct device *devb)
+{
+       pr_debug("PM: Moving %s:%s before %s:%s\n",
+                deva->bus ? deva->bus->name : "No Bus",
+                kobject_name(&deva->kobj),
+                devb->bus ? devb->bus->name : "No Bus",
+                kobject_name(&devb->kobj));
+       /* Delete deva from dpm_list and reinsert before devb. */
+       list_move_tail(&deva->power.entry, &devb->power.entry);
+}
+
+/**
+ * device_pm_move_after - Move device in the PM core's list of active devices.
+ * @deva: Device to move in dpm_list.
+ * @devb: Device @deva should come after.
+ */
+void device_pm_move_after(struct device *deva, struct device *devb)
+{
+       pr_debug("PM: Moving %s:%s after %s:%s\n",
+                deva->bus ? deva->bus->name : "No Bus",
+                kobject_name(&deva->kobj),
+                devb->bus ? devb->bus->name : "No Bus",
+                kobject_name(&devb->kobj));
+       /* Delete deva from dpm_list and reinsert after devb. */
+       list_move(&deva->power.entry, &devb->power.entry);
+}
+
+/**
+ * device_pm_move_last - Move device to end of the PM core's list of devices.
+ * @dev: Device to move in dpm_list.
+ */
+void device_pm_move_last(struct device *dev)
+{
+       pr_debug("PM: Moving %s:%s to end of list\n",
+                dev->bus ? dev->bus->name : "No Bus",
+                kobject_name(&dev->kobj));
+       list_move_tail(&dev->power.entry, &dpm_list);
+}
+
+static ktime_t initcall_debug_start(struct device *dev)
+{
+       ktime_t calltime = ktime_set(0, 0);
+
+       if (initcall_debug) {
+               pr_info("calling  %s+ @ %i\n",
+                               dev_name(dev), task_pid_nr(current));
+               calltime = ktime_get();
+       }
+
+       return calltime;
+}
+
+static void initcall_debug_report(struct device *dev, ktime_t calltime,
+                                 int error)
+{
+       ktime_t delta, rettime;
+
+       if (initcall_debug) {
+               rettime = ktime_get();
+               delta = ktime_sub(rettime, calltime);
+               pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
+                       error, (unsigned long long)ktime_to_ns(delta) >> 10);
+       }
+}
+
+/**
+ * pm_op - Execute the PM operation appropriate for given PM event.
+ * @dev: Device to handle.
+ * @ops: PM operations to choose from.
+ * @state: PM transition of the system being carried out.
+ */
+static int pm_op(struct device *dev,
+                const struct dev_pm_ops *ops,
+                pm_message_t state)
+{
+       int error = 0;
+       ktime_t calltime;
+
+       calltime = initcall_debug_start(dev);
+
+       switch (state.event) {
+#ifdef CONFIG_SUSPEND
+       case PM_EVENT_SUSPEND:
+               if (ops->suspend) {
+                       error = ops->suspend(dev);
+                       suspend_report_result(ops->suspend, error);
+               }
+               break;
+       case PM_EVENT_RESUME:
+               if (ops->resume) {
+                       error = ops->resume(dev);
+                       suspend_report_result(ops->resume, error);
+               }
+               break;
+#endif /* CONFIG_SUSPEND */
+#ifdef CONFIG_HIBERNATION
+       case PM_EVENT_FREEZE:
+       case PM_EVENT_QUIESCE:
+               if (ops->freeze) {
+                       error = ops->freeze(dev);
+                       suspend_report_result(ops->freeze, error);
+               }
+               break;
+       case PM_EVENT_HIBERNATE:
+               if (ops->poweroff) {
+                       error = ops->poweroff(dev);
+                       suspend_report_result(ops->poweroff, error);
+               }
+               break;
+       case PM_EVENT_THAW:
+       case PM_EVENT_RECOVER:
+               if (ops->thaw) {
+                       error = ops->thaw(dev);
+                       suspend_report_result(ops->thaw, error);
+               }
+               break;
+       case PM_EVENT_RESTORE:
+               if (ops->restore) {
+                       error = ops->restore(dev);
+                       suspend_report_result(ops->restore, error);
+               }
+               break;
+#endif /* CONFIG_HIBERNATION */
+       default:
+               error = -EINVAL;
+       }
+
+       initcall_debug_report(dev, calltime, error);
+
+       return error;
+}
+
+/**
+ * pm_noirq_op - Execute the PM operation appropriate for given PM event.
+ * @dev: Device to handle.
+ * @ops: PM operations to choose from.
+ * @state: PM transition of the system being carried out.
+ *
+ * The driver of @dev will not receive interrupts while this function is being
+ * executed.
+ */
+static int pm_noirq_op(struct device *dev,
+                       const struct dev_pm_ops *ops,
+                       pm_message_t state)
+{
+       int error = 0;
+       ktime_t calltime, delta, rettime;
+
+       if (initcall_debug) {
+               pr_info("calling  %s_i+ @ %i\n",
+                               dev_name(dev), task_pid_nr(current));
+               calltime = ktime_get();
+       }
+
+       switch (state.event) {
+#ifdef CONFIG_SUSPEND
+       case PM_EVENT_SUSPEND:
+               if (ops->suspend_noirq) {
+                       error = ops->suspend_noirq(dev);
+                       suspend_report_result(ops->suspend_noirq, error);
+               }
+               break;
+       case PM_EVENT_RESUME:
+               if (ops->resume_noirq) {
+                       error = ops->resume_noirq(dev);
+                       suspend_report_result(ops->resume_noirq, error);
+               }
+               break;
+#endif /* CONFIG_SUSPEND */
+#ifdef CONFIG_HIBERNATION
+       case PM_EVENT_FREEZE:
+       case PM_EVENT_QUIESCE:
+               if (ops->freeze_noirq) {
+                       error = ops->freeze_noirq(dev);
+                       suspend_report_result(ops->freeze_noirq, error);
+               }
+               break;
+       case PM_EVENT_HIBERNATE:
+               if (ops->poweroff_noirq) {
+                       error = ops->poweroff_noirq(dev);
+                       suspend_report_result(ops->poweroff_noirq, error);
+               }
+               break;
+       case PM_EVENT_THAW:
+       case PM_EVENT_RECOVER:
+               if (ops->thaw_noirq) {
+                       error = ops->thaw_noirq(dev);
+                       suspend_report_result(ops->thaw_noirq, error);
+               }
+               break;
+       case PM_EVENT_RESTORE:
+               if (ops->restore_noirq) {
+                       error = ops->restore_noirq(dev);
+                       suspend_report_result(ops->restore_noirq, error);
+               }
+               break;
+#endif /* CONFIG_HIBERNATION */
+       default:
+               error = -EINVAL;
+       }
+
+       if (initcall_debug) {
+               rettime = ktime_get();
+               delta = ktime_sub(rettime, calltime);
+               printk("initcall %s_i+ returned %d after %Ld usecs\n",
+                       dev_name(dev), error,
+                       (unsigned long long)ktime_to_ns(delta) >> 10);
+       }
+
+       return error;
+}
+
+static char *pm_verb(int event)
+{
+       switch (event) {
+       case PM_EVENT_SUSPEND:
+               return "suspend";
+       case PM_EVENT_RESUME:
+               return "resume";
+       case PM_EVENT_FREEZE:
+               return "freeze";
+       case PM_EVENT_QUIESCE:
+               return "quiesce";
+       case PM_EVENT_HIBERNATE:
+               return "hibernate";
+       case PM_EVENT_THAW:
+               return "thaw";
+       case PM_EVENT_RESTORE:
+               return "restore";
+       case PM_EVENT_RECOVER:
+               return "recover";
+       default:
+               return "(unknown PM event)";
+       }
+}
+
+static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
+{
+       dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
+               ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
+               ", may wakeup" : "");
+}
+
+static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
+                       int error)
+{
+       printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
+               kobject_name(&dev->kobj), pm_verb(state.event), info, error);
+}
+
+static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
+{
+       ktime_t calltime;
+       s64 usecs64;
+       int usecs;
+
+       calltime = ktime_get();
+       usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
+       do_div(usecs64, NSEC_PER_USEC);
+       usecs = usecs64;
+       if (usecs == 0)
+               usecs = 1;
+       pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
+               info ?: "", info ? " " : "", pm_verb(state.event),
+               usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
 }
 
 /*------------------------- Resume routines -------------------------*/
 
 /**
- *     resume_device_early - Power on one device (early resume).
- *     @dev:   Device.
+ * device_resume_noirq - Execute an "early resume" callback for given device.
+ * @dev: Device to handle.
+ * @state: PM transition of the system being carried out.
  *
- *     Must be called with interrupts disabled.
+ * The driver of @dev will not receive interrupts while this function is being
+ * executed.
  */
-static int resume_device_early(struct device *dev)
+static int device_resume_noirq(struct device *dev, pm_message_t state)
 {
        int error = 0;
 
        TRACE_DEVICE(dev);
        TRACE_RESUME(0);
 
-       if (dev->bus && dev->bus->resume_early) {
-               dev_dbg(dev, "EARLY resume\n");
-               error = dev->bus->resume_early(dev);
+       if (dev->bus && dev->bus->pm) {
+               pm_dev_dbg(dev, state, "EARLY ");
+               error = pm_noirq_op(dev, dev->bus->pm, state);
        }
 
        TRACE_RESUME(error);
@@ -125,48 +416,60 @@ static int resume_device_early(struct device *dev)
 }
 
 /**
- *     dpm_power_up - Power on all regular (non-sysdev) devices.
- *
- *     Walk the dpm_off_irq list and power each device up. This
- *     is used for devices that required they be powered down with
- *     interrupts disabled. As devices are powered on, they are moved
- *     to the dpm_off list.
+ * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
+ * @state: PM transition of the system being carried out.
  *
- *     Must be called with interrupts disabled and only one CPU running.
+ * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
+ * enable device drivers to receive interrupts.
  */
-static void dpm_power_up(void)
+void dpm_resume_noirq(pm_message_t state)
 {
+       struct device *dev;
+       ktime_t starttime = ktime_get();
 
-       while (!list_empty(&dpm_off_irq)) {
-               struct list_head *entry = dpm_off_irq.next;
-               struct device *dev = to_device(entry);
-
-               list_move_tail(entry, &dpm_off);
-               resume_device_early(dev);
-       }
+       mutex_lock(&dpm_list_mtx);
+       transition_started = false;
+       list_for_each_entry(dev, &dpm_list, power.entry)
+               if (dev->power.status > DPM_OFF) {
+                       int error;
+
+                       dev->power.status = DPM_OFF;
+                       error = device_resume_noirq(dev, state);
+                       if (error)
+                               pm_dev_err(dev, state, " early", error);
+               }
+       mutex_unlock(&dpm_list_mtx);
+       dpm_show_time(starttime, state, "early");
+       resume_device_irqs();
 }
+EXPORT_SYMBOL_GPL(dpm_resume_noirq);
 
 /**
- *     device_power_up - Turn on all devices that need special attention.
- *
- *     Power on system devices, then devices that required we shut them down
- *     with interrupts disabled.
- *
- *     Must be called with interrupts disabled.
+ * legacy_resume - Execute a legacy (bus or class) resume callback for device.
+ * @dev: Device to resume.
+ * @cb: Resume callback to execute.
  */
-void device_power_up(void)
+static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
 {
-       sysdev_resume();
-       dpm_power_up();
+       int error;
+       ktime_t calltime;
+
+       calltime = initcall_debug_start(dev);
+
+       error = cb(dev);
+       suspend_report_result(cb, error);
+
+       initcall_debug_report(dev, calltime, error);
+
+       return error;
 }
-EXPORT_SYMBOL_GPL(device_power_up);
 
 /**
- *     resume_device - Restore state for one device.
- *     @dev:   Device.
- *
+ * device_resume - Execute "resume" callbacks for given device.
+ * @dev: Device to handle.
+ * @state: PM transition of the system being carried out.
  */
-static int resume_device(struct device *dev)
+static int device_resume(struct device *dev, pm_message_t state)
 {
        int error = 0;
 
@@ -175,21 +478,37 @@ static int resume_device(struct device *dev)
 
        down(&dev->sem);
 
-       if (dev->bus && dev->bus->resume) {
-               dev_dbg(dev,"resuming\n");
-               error = dev->bus->resume(dev);
+       if (dev->bus) {
+               if (dev->bus->pm) {
+                       pm_dev_dbg(dev, state, "");
+                       error = pm_op(dev, dev->bus->pm, state);
+               } else if (dev->bus->resume) {
+                       pm_dev_dbg(dev, state, "legacy ");
+                       error = legacy_resume(dev, dev->bus->resume);
+               }
+               if (error)
+                       goto End;
        }
 
-       if (!error && dev->type && dev->type->resume) {
-               dev_dbg(dev,"resuming\n");
-               error = dev->type->resume(dev);
+       if (dev->type) {
+               if (dev->type->pm) {
+                       pm_dev_dbg(dev, state, "type ");
+                       error = pm_op(dev, dev->type->pm, state);
+               }
+               if (error)
+                       goto End;
        }
 
-       if (!error && dev->class && dev->class->resume) {
-               dev_dbg(dev,"class resume\n");
-               error = dev->class->resume(dev);
+       if (dev->class) {
+               if (dev->class->pm) {
+                       pm_dev_dbg(dev, state, "class ");
+                       error = pm_op(dev, dev->class->pm, state);
+               } else if (dev->class->resume) {
+                       pm_dev_dbg(dev, state, "legacy class ");
+                       error = legacy_resume(dev, dev->class->resume);
+               }
        }
-
+ End:
        up(&dev->sem);
 
        TRACE_RESUME(error);
@@ -197,229 +516,423 @@ static int resume_device(struct device *dev)
 }
 
 /**
- *     dpm_resume - Resume every device.
+ * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
+ * @state: PM transition of the system being carried out.
  *
- *     Resume the devices that have either not gone through
- *     the late suspend, or that did go through it but also
- *     went through the early resume.
+ * Execute the appropriate "resume" callback for all devices whose status
+ * indicates that they are suspended.
+ */
+static void dpm_resume(pm_message_t state)
+{
+       struct list_head list;
+       ktime_t starttime = ktime_get();
+
+       INIT_LIST_HEAD(&list);
+       mutex_lock(&dpm_list_mtx);
+       while (!list_empty(&dpm_list)) {
+               struct device *dev = to_device(dpm_list.next);
+
+               get_device(dev);
+               if (dev->power.status >= DPM_OFF) {
+                       int error;
+
+                       dev->power.status = DPM_RESUMING;
+                       mutex_unlock(&dpm_list_mtx);
+
+                       error = device_resume(dev, state);
+
+                       mutex_lock(&dpm_list_mtx);
+                       if (error)
+                               pm_dev_err(dev, state, "", error);
+               } else if (dev->power.status == DPM_SUSPENDING) {
+                       /* Allow new children of the device to be registered */
+                       dev->power.status = DPM_RESUMING;
+               }
+               if (!list_empty(&dev->power.entry))
+                       list_move_tail(&dev->power.entry, &list);
+               put_device(dev);
+       }
+       list_splice(&list, &dpm_list);
+       mutex_unlock(&dpm_list_mtx);
+       dpm_show_time(starttime, state, NULL);
+}
+
+/**
+ * device_complete - Complete a PM transition for given device.
+ * @dev: Device to handle.
+ * @state: PM transition of the system being carried out.
+ */
+static void device_complete(struct device *dev, pm_message_t state)
+{
+       down(&dev->sem);
+
+       if (dev->class && dev->class->pm && dev->class->pm->complete) {
+               pm_dev_dbg(dev, state, "completing class ");
+               dev->class->pm->complete(dev);
+       }
+
+       if (dev->type && dev->type->pm && dev->type->pm->complete) {
+               pm_dev_dbg(dev, state, "completing type ");
+               dev->type->pm->complete(dev);
+       }
+
+       if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
+               pm_dev_dbg(dev, state, "completing ");
+               dev->bus->pm->complete(dev);
+       }
+
+       up(&dev->sem);
+}
+
+/**
+ * dpm_complete - Complete a PM transition for all non-sysdev devices.
+ * @state: PM transition of the system being carried out.
  *
- *     Take devices from the dpm_off_list, resume them,
- *     and put them on the dpm_locked list.
+ * Execute the ->complete() callbacks for all devices whose PM status is not
+ * DPM_ON (this allows new devices to be registered).
  */
-static void dpm_resume(void)
+static void dpm_complete(pm_message_t state)
 {
+       struct list_head list;
+
+       INIT_LIST_HEAD(&list);
        mutex_lock(&dpm_list_mtx);
-       all_sleeping = false;
-       while(!list_empty(&dpm_off)) {
-               struct list_head *entry = dpm_off.next;
-               struct device *dev = to_device(entry);
+       transition_started = false;
+       while (!list_empty(&dpm_list)) {
+               struct device *dev = to_device(dpm_list.prev);
 
-               list_move_tail(entry, &dpm_active);
-               dev->power.sleeping = false;
-               mutex_unlock(&dpm_list_mtx);
-               resume_device(dev);
-               mutex_lock(&dpm_list_mtx);
+               get_device(dev);
+               if (dev->power.status > DPM_ON) {
+                       dev->power.status = DPM_ON;
+                       mutex_unlock(&dpm_list_mtx);
+
+                       device_complete(dev, state);
+                       pm_runtime_put_sync(dev);
+
+                       mutex_lock(&dpm_list_mtx);
+               }
+               if (!list_empty(&dev->power.entry))
+                       list_move(&dev->power.entry, &list);
+               put_device(dev);
        }
+       list_splice(&list, &dpm_list);
        mutex_unlock(&dpm_list_mtx);
 }
 
 /**
- *     device_resume - Restore state of each device in system.
+ * dpm_resume_end - Execute "resume" callbacks and complete system transition.
+ * @state: PM transition of the system being carried out.
  *
- *     Resume all the devices, unlock them all, and allow new
- *     devices to be registered once again.
+ * Execute "resume" callbacks for all devices and complete the PM transition of
+ * the system.
  */
-void device_resume(void)
+void dpm_resume_end(pm_message_t state)
 {
        might_sleep();
-       dpm_resume();
+       dpm_resume(state);
+       dpm_complete(state);
 }
-EXPORT_SYMBOL_GPL(device_resume);
+EXPORT_SYMBOL_GPL(dpm_resume_end);
 
 
 /*------------------------- Suspend routines -------------------------*/
 
-static inline char *suspend_verb(u32 event)
+/**
+ * resume_event - Return a "resume" message for given "suspend" sleep state.
+ * @sleep_state: PM message representing a sleep state.
+ *
+ * Return a PM message representing the resume event corresponding to given
+ * sleep state.
+ */
+static pm_message_t resume_event(pm_message_t sleep_state)
 {
-       switch (event) {
-       case PM_EVENT_SUSPEND:  return "suspend";
-       case PM_EVENT_FREEZE:   return "freeze";
-       case PM_EVENT_PRETHAW:  return "prethaw";
-       default:                return "(unknown suspend event)";
+       switch (sleep_state.event) {
+       case PM_EVENT_SUSPEND:
+               return PMSG_RESUME;
+       case PM_EVENT_FREEZE:
+       case PM_EVENT_QUIESCE:
+               return PMSG_RECOVER;
+       case PM_EVENT_HIBERNATE:
+               return PMSG_RESTORE;
        }
+       return PMSG_ON;
 }
 
-static void
-suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
+/**
+ * device_suspend_noirq - Execute a "late suspend" callback for given device.
+ * @dev: Device to handle.
+ * @state: PM transition of the system being carried out.
+ *
+ * The driver of @dev will not receive interrupts while this function is being
+ * executed.
+ */
+static int device_suspend_noirq(struct device *dev, pm_message_t state)
 {
-       dev_dbg(dev, "%s%s%s\n", info, suspend_verb(state.event),
-               ((state.event == PM_EVENT_SUSPEND) && device_may_wakeup(dev)) ?
-               ", may wakeup" : "");
+       int error = 0;
+
+       if (dev->bus && dev->bus->pm) {
+               pm_dev_dbg(dev, state, "LATE ");
+               error = pm_noirq_op(dev, dev->bus->pm, state);
+       }
+       return error;
 }
 
 /**
- *     suspend_device_late - Shut down one device (late suspend).
- *     @dev:   Device.
- *     @state: Power state device is entering.
+ * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
+ * @state: PM transition of the system being carried out.
  *
- *     This is called with interrupts off and only a single CPU running.
+ * Prevent device drivers from receiving interrupts and call the "noirq" suspend
+ * handlers for all non-sysdev devices.
  */
-static int suspend_device_late(struct device *dev, pm_message_t state)
+int dpm_suspend_noirq(pm_message_t state)
 {
+       struct device *dev;
+       ktime_t starttime = ktime_get();
        int error = 0;
 
-       if (dev->bus && dev->bus->suspend_late) {
-               suspend_device_dbg(dev, state, "LATE ");
-               error = dev->bus->suspend_late(dev, state);
-               suspend_report_result(dev->bus->suspend_late, error);
+       suspend_device_irqs();
+       mutex_lock(&dpm_list_mtx);
+       list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
+               error = device_suspend_noirq(dev, state);
+               if (error) {
+                       pm_dev_err(dev, state, " late", error);
+                       break;
+               }
+               dev->power.status = DPM_OFF_IRQ;
        }
+       mutex_unlock(&dpm_list_mtx);
+       if (error)
+               dpm_resume_noirq(resume_event(state));
+       else
+               dpm_show_time(starttime, state, "late");
        return error;
 }
+EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
 
 /**
- *     device_power_down - Shut down special devices.
- *     @state:         Power state to enter.
- *
- *     Power down devices that require interrupts to be disabled
- *     and move them from the dpm_off list to the dpm_off_irq list.
- *     Then power down system devices.
- *
- *     Must be called with interrupts disabled and only one CPU running.
+ * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
+ * @dev: Device to suspend.
+ * @state: PM transition of the system being carried out.
+ * @cb: Suspend callback to execute.
+ */
+static int legacy_suspend(struct device *dev, pm_message_t state,
+                         int (*cb)(struct device *dev, pm_message_t state))
+{
+       int error;
+       ktime_t calltime;
+
+       calltime = initcall_debug_start(dev);
+
+       error = cb(dev, state);
+       suspend_report_result(cb, error);
+
+       initcall_debug_report(dev, calltime, error);
+
+       return error;
+}
+
+/**
+ * device_suspend - Execute "suspend" callbacks for given device.
+ * @dev: Device to handle.
+ * @state: PM transition of the system being carried out.
+ */
+static int device_suspend(struct device *dev, pm_message_t state)
+{
+       int error = 0;
+
+       down(&dev->sem);
+
+       if (dev->class) {
+               if (dev->class->pm) {
+                       pm_dev_dbg(dev, state, "class ");
+                       error = pm_op(dev, dev->class->pm, state);
+               } else if (dev->class->suspend) {
+                       pm_dev_dbg(dev, state, "legacy class ");
+                       error = legacy_suspend(dev, state, dev->class->suspend);
+               }
+               if (error)
+                       goto End;
+       }
+
+       if (dev->type) {
+               if (dev->type->pm) {
+                       pm_dev_dbg(dev, state, "type ");
+                       error = pm_op(dev, dev->type->pm, state);
+               }
+               if (error)
+                       goto End;
+       }
+
+       if (dev->bus) {
+               if (dev->bus->pm) {
+                       pm_dev_dbg(dev, state, "");
+                       error = pm_op(dev, dev->bus->pm, state);
+               } else if (dev->bus->suspend) {
+                       pm_dev_dbg(dev, state, "legacy ");
+                       error = legacy_suspend(dev, state, dev->bus->suspend);
+               }
+       }
+ End:
+       up(&dev->sem);
+
+       return error;
+}
+
+/**
+ * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
+ * @state: PM transition of the system being carried out.
  */
-int device_power_down(pm_message_t state)
+static int dpm_suspend(pm_message_t state)
 {
+       struct list_head list;
+       ktime_t starttime = ktime_get();
        int error = 0;
 
-       while (!list_empty(&dpm_off)) {
-               struct list_head *entry = dpm_off.prev;
-               struct device *dev = to_device(entry);
+       INIT_LIST_HEAD(&list);
+       mutex_lock(&dpm_list_mtx);
+       while (!list_empty(&dpm_list)) {
+               struct device *dev = to_device(dpm_list.prev);
+
+               get_device(dev);
+               mutex_unlock(&dpm_list_mtx);
+
+               error = device_suspend(dev, state);
 
-               error = suspend_device_late(dev, state);
+               mutex_lock(&dpm_list_mtx);
                if (error) {
-                       printk(KERN_ERR "Could not power down device %s: "
-                                       "error %d\n",
-                                       kobject_name(&dev->kobj), error);
+                       pm_dev_err(dev, state, "", error);
+                       put_device(dev);
                        break;
                }
+               dev->power.status = DPM_OFF;
                if (!list_empty(&dev->power.entry))
-                       list_move(&dev->power.entry, &dpm_off_irq);
+                       list_move(&dev->power.entry, &list);
+               put_device(dev);
        }
-
+       list_splice(&list, dpm_list.prev);
+       mutex_unlock(&dpm_list_mtx);
        if (!error)
-               error = sysdev_suspend(state);
-       if (error)
-               dpm_power_up();
+               dpm_show_time(starttime, state, NULL);
        return error;
 }
-EXPORT_SYMBOL_GPL(device_power_down);
 
 /**
- *     suspend_device - Save state of one device.
- *     @dev:   Device.
- *     @state: Power state device is entering.
+ * device_prepare - Prepare a device for system power transition.
+ * @dev: Device to handle.
+ * @state: PM transition of the system being carried out.
+ *
+ * Execute the ->prepare() callback(s) for given device.  No new children of the
+ * device may be registered after this function has returned.
  */
-static int suspend_device(struct device *dev, pm_message_t state)
+static int device_prepare(struct device *dev, pm_message_t state)
 {
        int error = 0;
 
        down(&dev->sem);
 
-       if (dev->class && dev->class->suspend) {
-               suspend_device_dbg(dev, state, "class ");
-               error = dev->class->suspend(dev, state);
-               suspend_report_result(dev->class->suspend, error);
+       if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
+               pm_dev_dbg(dev, state, "preparing ");
+               error = dev->bus->pm->prepare(dev);
+               suspend_report_result(dev->bus->pm->prepare, error);
+               if (error)
+                       goto End;
        }
 
-       if (!error && dev->type && dev->type->suspend) {
-               suspend_device_dbg(dev, state, "type ");
-               error = dev->type->suspend(dev, state);
-               suspend_report_result(dev->type->suspend, error);
+       if (dev->type && dev->type->pm && dev->type->pm->prepare) {
+               pm_dev_dbg(dev, state, "preparing type ");
+               error = dev->type->pm->prepare(dev);
+               suspend_report_result(dev->type->pm->prepare, error);
+               if (error)
+                       goto End;
        }
 
-       if (!error && dev->bus && dev->bus->suspend) {
-               suspend_device_dbg(dev, state, "");
-               error = dev->bus->suspend(dev, state);
-               suspend_report_result(dev->bus->suspend, error);
+       if (dev->class && dev->class->pm && dev->class->pm->prepare) {
+               pm_dev_dbg(dev, state, "preparing class ");
+               error = dev->class->pm->prepare(dev);
+               suspend_report_result(dev->class->pm->prepare, error);
        }
-
+ End:
        up(&dev->sem);
 
        return error;
 }
 
 /**
- *     dpm_suspend - Suspend every device.
- *     @state: Power state to put each device in.
- *
- *     Walk the dpm_locked list.  Suspend each device and move it
- *     to the dpm_off list.
+ * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
+ * @state: PM transition of the system being carried out.
  *
- *     (For historical reasons, if it returns -EAGAIN, that used to mean
- *     that the device would be called again with interrupts disabled.
- *     These days, we use the "suspend_late()" callback for that, so we
- *     print a warning and consider it an error).
+ * Execute the ->prepare() callback(s) for all devices.
  */
-static int dpm_suspend(pm_message_t state)
+static int dpm_prepare(pm_message_t state)
 {
+       struct list_head list;
        int error = 0;
 
+       INIT_LIST_HEAD(&list);
        mutex_lock(&dpm_list_mtx);
-       while (!list_empty(&dpm_active)) {
-               struct list_head *entry = dpm_active.prev;
-               struct device *dev = to_device(entry);
-
-               WARN_ON(dev->parent && dev->parent->power.sleeping);
+       transition_started = true;
+       while (!list_empty(&dpm_list)) {
+               struct device *dev = to_device(dpm_list.next);
 
-               dev->power.sleeping = true;
+               get_device(dev);
+               dev->power.status = DPM_PREPARING;
                mutex_unlock(&dpm_list_mtx);
-               error = suspend_device(dev, state);
+
+               pm_runtime_get_noresume(dev);
+               if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) {
+                       /* Wake-up requested during system sleep transition. */
+                       pm_runtime_put_sync(dev);
+                       error = -EBUSY;
+               } else {
+                       error = device_prepare(dev, state);
+               }
+
                mutex_lock(&dpm_list_mtx);
                if (error) {
-                       printk(KERN_ERR "Could not suspend device %s: "
-                                       "error %d%s\n",
-                                       kobject_name(&dev->kobj),
-                                       error,
-                                       (error == -EAGAIN ?
-                                       " (please convert to suspend_late)" :
-                                       ""));
-                       dev->power.sleeping = false;
+                       dev->power.status = DPM_ON;
+                       if (error == -EAGAIN) {
+                               put_device(dev);
+                               error = 0;
+                               continue;
+                       }
+                       printk(KERN_ERR "PM: Failed to prepare device %s "
+                               "for power transition: error %d\n",
+                               kobject_name(&dev->kobj), error);
+                       put_device(dev);
                        break;
                }
+               dev->power.status = DPM_SUSPENDING;
                if (!list_empty(&dev->power.entry))
-                       list_move(&dev->power.entry, &dpm_off);
+                       list_move_tail(&dev->power.entry, &list);
+               put_device(dev);
        }
-       if (!error)
-               all_sleeping = true;
+       list_splice(&list, &dpm_list);
        mutex_unlock(&dpm_list_mtx);
-
        return error;
 }
 
 /**
- *     device_suspend - Save state and stop all devices in system.
- *     @state: new power management state
+ * dpm_suspend_start - Prepare devices for PM transition and suspend them.
+ * @state: PM transition of the system being carried out.
  *
- *     Prevent new devices from being registered, then lock all devices
- *     and suspend them.
+ * Prepare all non-sysdev devices for system PM transition and execute "suspend"
+ * callbacks for them.
  */
-int device_suspend(pm_message_t state)
+int dpm_suspend_start(pm_message_t state)
 {
        int error;
 
        might_sleep();
-       error = dpm_suspend(state);
-       if (error)
-               device_resume();
+       error = dpm_prepare(state);
+       if (!error)
+               error = dpm_suspend(state);
        return error;
 }
-EXPORT_SYMBOL_GPL(device_suspend);
+EXPORT_SYMBOL_GPL(dpm_suspend_start);
 
 void __suspend_report_result(const char *function, void *fn, int ret)
 {
-       if (ret) {
-               printk(KERN_ERR "%s(): ", function);
-               print_fn_descriptor_symbol("%s() returns ", (unsigned long)fn);
-               printk("%d\n", ret);
-       }
+       if (ret)
+               printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
 }
 EXPORT_SYMBOL_GPL(__suspend_report_result);