Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 1 | Device Power Management |
| 2 | |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 3 | Copyright (c) 2010-2011 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc. |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 4 | Copyright (c) 2010 Alan Stern <stern@rowland.harvard.edu> |
Rafael J. Wysocki | f71495f | 2014-05-16 02:47:37 +0200 | [diff] [blame] | 5 | Copyright (c) 2014 Intel Corp., Rafael J. Wysocki <rafael.j.wysocki@intel.com> |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 6 | |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 7 | |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 8 | Most of the code in Linux is device drivers, so most of the Linux power |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 9 | management (PM) code is also driver-specific. Most drivers will do very |
| 10 | little; others, especially for platforms with small batteries (like cell |
| 11 | phones), will do a lot. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 13 | This writeup gives an overview of how drivers interact with system-wide |
| 14 | power management goals, emphasizing the models and interfaces that are |
| 15 | shared by everything that hooks up to the driver model core. Read it as |
| 16 | background for the domain-specific work you'd do with any specific driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 19 | Two Models for Device Power Management |
| 20 | ====================================== |
| 21 | Drivers will use one or both of these models to put devices into low-power |
| 22 | states: |
| 23 | |
| 24 | System Sleep model: |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 25 | Drivers can enter low-power states as part of entering system-wide |
| 26 | low-power states like "suspend" (also known as "suspend-to-RAM"), or |
| 27 | (mostly for systems with disks) "hibernation" (also known as |
| 28 | "suspend-to-disk"). |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 29 | |
| 30 | This is something that device, bus, and class drivers collaborate on |
| 31 | by implementing various role-specific suspend and resume methods to |
| 32 | cleanly power down hardware and software subsystems, then reactivate |
| 33 | them without loss of data. |
| 34 | |
| 35 | Some drivers can manage hardware wakeup events, which make the system |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 36 | leave the low-power state. This feature may be enabled or disabled |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 37 | using the relevant /sys/devices/.../power/wakeup file (for Ethernet |
| 38 | drivers the ioctl interface used by ethtool may also be used for this |
| 39 | purpose); enabling it may cost some power usage, but let the whole |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 40 | system enter low-power states more often. |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 41 | |
| 42 | Runtime Power Management model: |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 43 | Devices may also be put into low-power states while the system is |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 44 | running, independently of other power management activity in principle. |
| 45 | However, devices are not generally independent of each other (for |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 46 | example, a parent device cannot be suspended unless all of its child |
| 47 | devices have been suspended). Moreover, depending on the bus type the |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 48 | device is on, it may be necessary to carry out some bus-specific |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 49 | operations on the device for this purpose. Devices put into low power |
| 50 | states at run time may require special handling during system-wide power |
| 51 | transitions (suspend or hibernation). |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 52 | |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 53 | For these reasons not only the device driver itself, but also the |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 54 | appropriate subsystem (bus type, device type or device class) driver and |
| 55 | the PM core are involved in runtime power management. As in the system |
| 56 | sleep power management case, they need to collaborate by implementing |
| 57 | various role-specific suspend and resume methods, so that the hardware |
| 58 | is cleanly powered down and reactivated without data or service loss. |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 59 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 60 | There's not a lot to be said about those low-power states except that they are |
| 61 | very system-specific, and often device-specific. Also, that if enough devices |
| 62 | have been put into low-power states (at runtime), the effect may be very similar |
| 63 | to entering some system-wide low-power state (system sleep) ... and that |
| 64 | synergies exist, so that several drivers using runtime PM might put the system |
| 65 | into a state where even deeper power saving options are available. |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 66 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 67 | Most suspended devices will have quiesced all I/O: no more DMA or IRQs (except |
| 68 | for wakeup events), no more data read or written, and requests from upstream |
| 69 | drivers are no longer accepted. A given bus or platform may have different |
| 70 | requirements though. |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 71 | |
| 72 | Examples of hardware wakeup events include an alarm from a real time clock, |
| 73 | network wake-on-LAN packets, keyboard or mouse activity, and media insertion |
| 74 | or removal (for PCMCIA, MMC/SD, USB, and so on). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 77 | Interfaces for Entering System Sleep States |
| 78 | =========================================== |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 79 | There are programming interfaces provided for subsystems (bus type, device type, |
| 80 | device class) and device drivers to allow them to participate in the power |
| 81 | management of devices they are concerned with. These interfaces cover both |
| 82 | system sleep and runtime power management. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 84 | |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 85 | Device Power Management Operations |
| 86 | ---------------------------------- |
| 87 | Device power management operations, at the subsystem level as well as at the |
| 88 | device driver level, are implemented by defining and populating objects of type |
| 89 | struct dev_pm_ops: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 91 | struct dev_pm_ops { |
| 92 | int (*prepare)(struct device *dev); |
| 93 | void (*complete)(struct device *dev); |
| 94 | int (*suspend)(struct device *dev); |
| 95 | int (*resume)(struct device *dev); |
| 96 | int (*freeze)(struct device *dev); |
| 97 | int (*thaw)(struct device *dev); |
| 98 | int (*poweroff)(struct device *dev); |
| 99 | int (*restore)(struct device *dev); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 100 | int (*suspend_late)(struct device *dev); |
| 101 | int (*resume_early)(struct device *dev); |
| 102 | int (*freeze_late)(struct device *dev); |
| 103 | int (*thaw_early)(struct device *dev); |
| 104 | int (*poweroff_late)(struct device *dev); |
| 105 | int (*restore_early)(struct device *dev); |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 106 | int (*suspend_noirq)(struct device *dev); |
| 107 | int (*resume_noirq)(struct device *dev); |
| 108 | int (*freeze_noirq)(struct device *dev); |
| 109 | int (*thaw_noirq)(struct device *dev); |
| 110 | int (*poweroff_noirq)(struct device *dev); |
| 111 | int (*restore_noirq)(struct device *dev); |
| 112 | int (*runtime_suspend)(struct device *dev); |
| 113 | int (*runtime_resume)(struct device *dev); |
| 114 | int (*runtime_idle)(struct device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 117 | This structure is defined in include/linux/pm.h and the methods included in it |
| 118 | are also described in that file. Their roles will be explained in what follows. |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 119 | For now, it should be sufficient to remember that the last three methods are |
| 120 | specific to runtime power management while the remaining ones are used during |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 121 | system-wide power transitions. |
| 122 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 123 | There also is a deprecated "old" or "legacy" interface for power management |
| 124 | operations available at least for some subsystems. This approach does not use |
| 125 | struct dev_pm_ops objects and it is suitable only for implementing system sleep |
| 126 | power management methods. Therefore it is not described in this document, so |
| 127 | please refer directly to the source code for more information about it. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 128 | |
| 129 | |
| 130 | Subsystem-Level Methods |
| 131 | ----------------------- |
| 132 | The core methods to suspend and resume devices reside in struct dev_pm_ops |
Rafael J. Wysocki | 5841eb6 | 2011-11-23 21:18:39 +0100 | [diff] [blame] | 133 | pointed to by the ops member of struct dev_pm_domain, or by the pm member of |
| 134 | struct bus_type, struct device_type and struct class. They are mostly of |
| 135 | interest to the people writing infrastructure for platforms and buses, like PCI |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 136 | or USB, or device type and device class drivers. They also are relevant to the |
| 137 | writers of device drivers whose subsystems (PM domains, device types, device |
| 138 | classes and bus types) don't provide all power management methods. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 139 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 140 | Bus drivers implement these methods as appropriate for the hardware and the |
| 141 | drivers using it; PCI works differently from USB, and so on. Not many people |
| 142 | write subsystem-level drivers; most driver code is a "device driver" that builds |
| 143 | on top of bus-specific framework code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 145 | For more information on these driver calls, see the description later; |
| 146 | they are called in phases for every device, respecting the parent-child |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 147 | sequencing in the driver model tree. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 150 | /sys/devices/.../power/wakeup files |
| 151 | ----------------------------------- |
Rafael J. Wysocki | fafba48 | 2011-11-23 21:20:15 +0100 | [diff] [blame] | 152 | All device objects in the driver model contain fields that control the handling |
| 153 | of system wakeup events (hardware signals that can force the system out of a |
| 154 | sleep state). These fields are initialized by bus or device driver code using |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 155 | device_set_wakeup_capable() and device_set_wakeup_enable(), defined in |
| 156 | include/linux/pm_wakeup.h. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Rafael J. Wysocki | fafba48 | 2011-11-23 21:20:15 +0100 | [diff] [blame] | 158 | The "power.can_wakeup" flag just records whether the device (and its driver) can |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 159 | physically support wakeup events. The device_set_wakeup_capable() routine |
Rafael J. Wysocki | fafba48 | 2011-11-23 21:20:15 +0100 | [diff] [blame] | 160 | affects this flag. The "power.wakeup" field is a pointer to an object of type |
| 161 | struct wakeup_source used for controlling whether or not the device should use |
| 162 | its system wakeup mechanism and for notifying the PM core of system wakeup |
| 163 | events signaled by the device. This object is only present for wakeup-capable |
| 164 | devices (i.e. devices whose "can_wakeup" flags are set) and is created (or |
| 165 | removed) by device_set_wakeup_capable(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 167 | Whether or not a device is capable of issuing wakeup events is a hardware |
| 168 | matter, and the kernel is responsible for keeping track of it. By contrast, |
| 169 | whether or not a wakeup-capable device should issue wakeup events is a policy |
| 170 | decision, and it is managed by user space through a sysfs attribute: the |
Rafael J. Wysocki | fafba48 | 2011-11-23 21:20:15 +0100 | [diff] [blame] | 171 | "power/wakeup" file. User space can write the strings "enabled" or "disabled" |
| 172 | to it to indicate whether or not, respectively, the device is supposed to signal |
| 173 | system wakeup. This file is only present if the "power.wakeup" object exists |
| 174 | for the given device and is created (or removed) along with that object, by |
| 175 | device_set_wakeup_capable(). Reads from the file will return the corresponding |
| 176 | string. |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 177 | |
Rafael J. Wysocki | fafba48 | 2011-11-23 21:20:15 +0100 | [diff] [blame] | 178 | The "power/wakeup" file is supposed to contain the "disabled" string initially |
| 179 | for the majority of devices; the major exceptions are power buttons, keyboards, |
| 180 | and Ethernet adapters whose WoL (wake-on-LAN) feature has been set up with |
| 181 | ethtool. It should also default to "enabled" for devices that don't generate |
| 182 | wakeup requests on their own but merely forward wakeup requests from one bus to |
| 183 | another (like PCI Express ports). |
| 184 | |
| 185 | The device_may_wakeup() routine returns true only if the "power.wakeup" object |
| 186 | exists and the corresponding "power/wakeup" file contains the string "enabled". |
Rafael J. Wysocki | cb8f51b | 2011-02-08 23:26:02 +0100 | [diff] [blame] | 187 | This information is used by subsystems, like the PCI bus type code, to see |
| 188 | whether or not to enable the devices' wakeup mechanisms. If device wakeup |
| 189 | mechanisms are enabled or disabled directly by drivers, they also should use |
| 190 | device_may_wakeup() to decide what to do during a system sleep transition. |
Rafael J. Wysocki | fafba48 | 2011-11-23 21:20:15 +0100 | [diff] [blame] | 191 | Device drivers, however, are not supposed to call device_set_wakeup_enable() |
| 192 | directly in any case. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Rafael J. Wysocki | fafba48 | 2011-11-23 21:20:15 +0100 | [diff] [blame] | 194 | It ought to be noted that system wakeup is conceptually different from "remote |
| 195 | wakeup" used by runtime power management, although it may be supported by the |
| 196 | same physical mechanism. Remote wakeup is a feature allowing devices in |
| 197 | low-power states to trigger specific interrupts to signal conditions in which |
| 198 | they should be put into the full-power state. Those interrupts may or may not |
| 199 | be used to signal system wakeup events, depending on the hardware design. On |
| 200 | some systems it is impossible to trigger them from system sleep states. In any |
| 201 | case, remote wakeup should always be enabled for runtime power management for |
| 202 | all devices and drivers that support it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 204 | /sys/devices/.../power/control files |
| 205 | ------------------------------------ |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 206 | Each device in the driver model has a flag to control whether it is subject to |
| 207 | runtime power management. This flag, called runtime_auto, is initialized by the |
| 208 | bus type (or generally subsystem) code using pm_runtime_allow() or |
| 209 | pm_runtime_forbid(); the default is to allow runtime power management. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 211 | The setting can be adjusted by user space by writing either "on" or "auto" to |
| 212 | the device's power/control sysfs file. Writing "auto" calls pm_runtime_allow(), |
| 213 | setting the flag and allowing the device to be runtime power-managed by its |
| 214 | driver. Writing "on" calls pm_runtime_forbid(), clearing the flag, returning |
| 215 | the device to full power if it was in a low-power state, and preventing the |
| 216 | device from being runtime power-managed. User space can check the current value |
| 217 | of the runtime_auto flag by reading the file. |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 218 | |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 219 | The device's runtime_auto flag has no effect on the handling of system-wide |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 220 | power transitions. In particular, the device can (and in the majority of cases |
| 221 | should and will) be put into a low-power state during a system-wide transition |
| 222 | to a sleep state even though its runtime_auto flag is clear. |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 223 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 224 | For more information about the runtime power management framework, refer to |
| 225 | Documentation/power/runtime_pm.txt. |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 226 | |
| 227 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 228 | Calling Drivers to Enter and Leave System Sleep States |
| 229 | ====================================================== |
| 230 | When the system goes into a sleep state, each device's driver is asked to |
| 231 | suspend the device by putting it into a state compatible with the target |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 232 | system state. That's usually some version of "off", but the details are |
| 233 | system-specific. Also, wakeup-enabled devices will usually stay partly |
| 234 | functional in order to wake the system. |
| 235 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 236 | When the system leaves that low-power state, the device's driver is asked to |
| 237 | resume it by returning it to full power. The suspend and resume operations |
| 238 | always go together, and both are multi-phase operations. |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 239 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 240 | For simple drivers, suspend might quiesce the device using class code |
| 241 | and then turn its hardware as "off" as possible during suspend_noirq. The |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 242 | matching resume calls would then completely reinitialize the hardware |
| 243 | before reactivating its class I/O queues. |
| 244 | |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 245 | More power-aware drivers might prepare the devices for triggering system wakeup |
| 246 | events. |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 247 | |
| 248 | |
| 249 | Call Sequence Guarantees |
| 250 | ------------------------ |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 251 | To ensure that bridges and similar links needing to talk to a device are |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 252 | available when the device is suspended or resumed, the device tree is |
| 253 | walked in a bottom-up order to suspend devices. A top-down order is |
| 254 | used to resume those devices. |
| 255 | |
| 256 | The ordering of the device tree is defined by the order in which devices |
| 257 | get registered: a child can never be registered, probed or resumed before |
| 258 | its parent; and can't be removed or suspended after that parent. |
| 259 | |
| 260 | The policy is that the device tree should match hardware bus topology. |
| 261 | (Or at least the control bus, for devices which use multiple busses.) |
Rafael J. Wysocki | 58aca23 | 2008-03-12 00:57:22 +0100 | [diff] [blame] | 262 | In particular, this means that a device registration may fail if the parent of |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 263 | the device is suspending (i.e. has been chosen by the PM core as the next |
Rafael J. Wysocki | 58aca23 | 2008-03-12 00:57:22 +0100 | [diff] [blame] | 264 | device to suspend) or has already suspended, as well as after all of the other |
| 265 | devices have been suspended. Device drivers must be prepared to cope with such |
| 266 | situations. |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 267 | |
| 268 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 269 | System Power Management Phases |
| 270 | ------------------------------ |
| 271 | Suspending or resuming the system is done in several phases. Different phases |
Zhang Rui | dc5aeae | 2013-05-13 02:42:11 +0000 | [diff] [blame] | 272 | are used for freeze, standby, and memory sleep states ("suspend-to-RAM") and the |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 273 | hibernation state ("suspend-to-disk"). Each phase involves executing callbacks |
| 274 | for every device before the next phase begins. Not all busses or classes |
| 275 | support all these callbacks and not all drivers use all the callbacks. The |
| 276 | various phases always run after tasks have been frozen and before they are |
| 277 | unfrozen. Furthermore, the *_noirq phases run at a time when IRQ handlers have |
Rafael J. Wysocki | fa8ce72 | 2011-11-23 21:19:57 +0100 | [diff] [blame] | 278 | been disabled (except for those marked with the IRQF_NO_SUSPEND flag). |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 279 | |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 280 | All phases use PM domain, bus, type, class or driver callbacks (that is, methods |
| 281 | defined in dev->pm_domain->ops, dev->bus->pm, dev->type->pm, dev->class->pm or |
| 282 | dev->driver->pm). These callbacks are regarded by the PM core as mutually |
| 283 | exclusive. Moreover, PM domain callbacks always take precedence over all of the |
| 284 | other callbacks and, for example, type callbacks take precedence over bus, class |
| 285 | and driver callbacks. To be precise, the following rules are used to determine |
| 286 | which callback to execute in the given phase: |
Rafael J. Wysocki | 5841eb6 | 2011-11-23 21:18:39 +0100 | [diff] [blame] | 287 | |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 288 | 1. If dev->pm_domain is present, the PM core will choose the callback |
| 289 | included in dev->pm_domain->ops for execution |
Rafael J. Wysocki | 5841eb6 | 2011-11-23 21:18:39 +0100 | [diff] [blame] | 290 | |
| 291 | 2. Otherwise, if both dev->type and dev->type->pm are present, the callback |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 292 | included in dev->type->pm will be chosen for execution. |
Rafael J. Wysocki | 5841eb6 | 2011-11-23 21:18:39 +0100 | [diff] [blame] | 293 | |
| 294 | 3. Otherwise, if both dev->class and dev->class->pm are present, the |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 295 | callback included in dev->class->pm will be chosen for execution. |
Rafael J. Wysocki | 5841eb6 | 2011-11-23 21:18:39 +0100 | [diff] [blame] | 296 | |
| 297 | 4. Otherwise, if both dev->bus and dev->bus->pm are present, the callback |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 298 | included in dev->bus->pm will be chosen for execution. |
Rafael J. Wysocki | 5841eb6 | 2011-11-23 21:18:39 +0100 | [diff] [blame] | 299 | |
| 300 | This allows PM domains and device types to override callbacks provided by bus |
| 301 | types or device classes if necessary. |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 302 | |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 303 | The PM domain, type, class and bus callbacks may in turn invoke device- or |
| 304 | driver-specific methods stored in dev->driver->pm, but they don't have to do |
| 305 | that. |
| 306 | |
| 307 | If the subsystem callback chosen for execution is not present, the PM core will |
| 308 | execute the corresponding method from dev->driver->pm instead if there is one. |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 309 | |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 310 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 311 | Entering System Suspend |
| 312 | ----------------------- |
Zhang Rui | dc5aeae | 2013-05-13 02:42:11 +0000 | [diff] [blame] | 313 | When the system goes into the freeze, standby or memory sleep state, |
| 314 | the phases are: |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 315 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 316 | prepare, suspend, suspend_late, suspend_noirq. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 317 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 318 | 1. The prepare phase is meant to prevent races by preventing new devices |
| 319 | from being registered; the PM core would never know that all the |
| 320 | children of a device had been suspended if new children could be |
| 321 | registered at will. (By contrast, devices may be unregistered at any |
| 322 | time.) Unlike the other suspend-related phases, during the prepare |
| 323 | phase the device tree is traversed top-down. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 324 | |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 325 | After the prepare callback method returns, no new children may be |
| 326 | registered below the device. The method may also prepare the device or |
Rafael J. Wysocki | fa8ce72 | 2011-11-23 21:19:57 +0100 | [diff] [blame] | 327 | driver in some way for the upcoming system power transition, but it |
| 328 | should not put the device into a low-power state. |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 329 | |
Rafael J. Wysocki | f71495f | 2014-05-16 02:47:37 +0200 | [diff] [blame] | 330 | For devices supporting runtime power management, the return value of the |
| 331 | prepare callback can be used to indicate to the PM core that it may |
| 332 | safely leave the device in runtime suspend (if runtime-suspended |
| 333 | already), provided that all of the device's descendants are also left in |
| 334 | runtime suspend. Namely, if the prepare callback returns a positive |
| 335 | number and that happens for all of the descendants of the device too, |
| 336 | and all of them (including the device itself) are runtime-suspended, the |
| 337 | PM core will skip the suspend, suspend_late and suspend_noirq suspend |
| 338 | phases as well as the resume_noirq, resume_early and resume phases of |
| 339 | the following system resume for all of these devices. In that case, |
| 340 | the complete callback will be called directly after the prepare callback |
| 341 | and is entirely responsible for bringing the device back to the |
| 342 | functional state as appropriate. |
| 343 | |
Alan Stern | 019d881 | 2015-07-15 14:40:06 +0200 | [diff] [blame] | 344 | Note that this direct-complete procedure applies even if the device is |
| 345 | disabled for runtime PM; only the runtime-PM status matters. It follows |
| 346 | that if a device has system-sleep callbacks but does not support runtime |
| 347 | PM, then its prepare callback must never return a positive value. This |
| 348 | is because all devices are initially set to runtime-suspended with |
| 349 | runtime PM disabled. |
| 350 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 351 | 2. The suspend methods should quiesce the device to stop it from performing |
| 352 | I/O. They also may save the device registers and put it into the |
| 353 | appropriate low-power state, depending on the bus type the device is on, |
| 354 | and they may enable wakeup events. |
| 355 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 356 | 3 For a number of devices it is convenient to split suspend into the |
| 357 | "quiesce device" and "save device state" phases, in which cases |
| 358 | suspend_late is meant to do the latter. It is always executed after |
| 359 | runtime power management has been disabled for all devices. |
| 360 | |
| 361 | 4. The suspend_noirq phase occurs after IRQ handlers have been disabled, |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 362 | which means that the driver's interrupt handler will not be called while |
| 363 | the callback method is running. The methods should save the values of |
| 364 | the device's registers that weren't saved previously and finally put the |
| 365 | device into the appropriate low-power state. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 366 | |
| 367 | The majority of subsystems and device drivers need not implement this |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 368 | callback. However, bus types allowing devices to share interrupt |
| 369 | vectors, like PCI, generally need it; otherwise a driver might encounter |
| 370 | an error during the suspend phase by fielding a shared interrupt |
| 371 | generated by some other device after its own device had been set to low |
| 372 | power. |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 373 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 374 | At the end of these phases, drivers should have stopped all I/O transactions |
| 375 | (DMA, IRQs), saved enough state that they can re-initialize or restore previous |
| 376 | state (as needed by the hardware), and placed the device into a low-power state. |
| 377 | On many platforms they will gate off one or more clock sources; sometimes they |
| 378 | will also switch off power supplies or reduce voltages. (Drivers supporting |
| 379 | runtime PM may already have performed some or all of these steps.) |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 380 | |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 381 | If device_may_wakeup(dev) returns true, the device should be prepared for |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 382 | generating hardware wakeup signals to trigger a system wakeup event when the |
| 383 | system is in the sleep state. For example, enable_irq_wake() might identify |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 384 | GPIO signals hooked up to a switch or other external hardware, and |
| 385 | pci_enable_wake() does something similar for the PCI PME signal. |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 386 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 387 | If any of these callbacks returns an error, the system won't enter the desired |
| 388 | low-power state. Instead the PM core will unwind its actions by resuming all |
| 389 | the devices that were suspended. |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 390 | |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 391 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 392 | Leaving System Suspend |
| 393 | ---------------------- |
Zhang Rui | dc5aeae | 2013-05-13 02:42:11 +0000 | [diff] [blame] | 394 | When resuming from freeze, standby or memory sleep, the phases are: |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 395 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 396 | resume_noirq, resume_early, resume, complete. |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 397 | |
| 398 | 1. The resume_noirq callback methods should perform any actions needed |
| 399 | before the driver's interrupt handlers are invoked. This generally |
| 400 | means undoing the actions of the suspend_noirq phase. If the bus type |
| 401 | permits devices to share interrupt vectors, like PCI, the method should |
| 402 | bring the device and its driver into a state in which the driver can |
| 403 | recognize if the device is the source of incoming interrupts, if any, |
| 404 | and handle them correctly. |
| 405 | |
| 406 | For example, the PCI bus type's ->pm.resume_noirq() puts the device into |
| 407 | the full-power state (D0 in the PCI terminology) and restores the |
| 408 | standard configuration registers of the device. Then it calls the |
| 409 | device driver's ->pm.resume_noirq() method to perform device-specific |
| 410 | actions. |
| 411 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 412 | 2. The resume_early methods should prepare devices for the execution of |
| 413 | the resume methods. This generally involves undoing the actions of the |
| 414 | preceding suspend_late phase. |
| 415 | |
Masanari Iida | df5cbb2 | 2014-03-21 10:04:30 +0900 | [diff] [blame] | 416 | 3 The resume methods should bring the device back to its operating |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 417 | state, so that it can perform normal I/O. This generally involves |
| 418 | undoing the actions of the suspend phase. |
| 419 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 420 | 4. The complete phase should undo the actions of the prepare phase. Note, |
| 421 | however, that new children may be registered below the device as soon as |
| 422 | the resume callbacks occur; it's not necessary to wait until the |
| 423 | complete phase. |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 424 | |
Rafael J. Wysocki | f71495f | 2014-05-16 02:47:37 +0200 | [diff] [blame] | 425 | Moreover, if the preceding prepare callback returned a positive number, |
| 426 | the device may have been left in runtime suspend throughout the whole |
| 427 | system suspend and resume (the suspend, suspend_late, suspend_noirq |
| 428 | phases of system suspend and the resume_noirq, resume_early, resume |
| 429 | phases of system resume may have been skipped for it). In that case, |
| 430 | the complete callback is entirely responsible for bringing the device |
| 431 | back to the functional state after system suspend if necessary. [For |
| 432 | example, it may need to queue up a runtime resume request for the device |
| 433 | for this purpose.] To check if that is the case, the complete callback |
| 434 | can consult the device's power.direct_complete flag. Namely, if that |
| 435 | flag is set when the complete callback is being run, it has been called |
| 436 | directly after the preceding prepare and special action may be required |
| 437 | to make the device work correctly afterward. |
| 438 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 439 | At the end of these phases, drivers should be as functional as they were before |
| 440 | suspending: I/O can be performed using DMA and IRQs, and the relevant clocks are |
Rafael J. Wysocki | f71495f | 2014-05-16 02:47:37 +0200 | [diff] [blame] | 441 | gated on. |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 442 | |
| 443 | However, the details here may again be platform-specific. For example, |
| 444 | some systems support multiple "run" states, and the mode in effect at |
| 445 | the end of resume might not be the one which preceded suspension. |
| 446 | That means availability of certain clocks or power supplies changed, |
| 447 | which could easily affect how a driver works. |
| 448 | |
| 449 | Drivers need to be able to handle hardware which has been reset since the |
| 450 | suspend methods were called, for example by complete reinitialization. |
| 451 | This may be the hardest part, and the one most protected by NDA'd documents |
| 452 | and chip errata. It's simplest if the hardware state hasn't changed since |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 453 | the suspend was carried out, but that can't be guaranteed (in fact, it usually |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 454 | is not the case). |
| 455 | |
| 456 | Drivers must also be prepared to notice that the device has been removed |
| 457 | while the system was powered down, whenever that's physically possible. |
| 458 | PCMCIA, MMC, USB, Firewire, SCSI, and even IDE are common examples of busses |
| 459 | where common Linux platforms will see such removal. Details of how drivers |
| 460 | will notice and handle such removals are currently bus-specific, and often |
| 461 | involve a separate thread. |
| 462 | |
| 463 | These callbacks may return an error value, but the PM core will ignore such |
| 464 | errors since there's nothing it can do about them other than printing them in |
| 465 | the system log. |
| 466 | |
| 467 | |
| 468 | Entering Hibernation |
| 469 | -------------------- |
Zhang Rui | dc5aeae | 2013-05-13 02:42:11 +0000 | [diff] [blame] | 470 | Hibernating the system is more complicated than putting it into the other |
| 471 | sleep states, because it involves creating and saving a system image. |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 472 | Therefore there are more phases for hibernation, with a different set of |
| 473 | callbacks. These phases always run after tasks have been frozen and memory has |
| 474 | been freed. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 475 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 476 | The general procedure for hibernation is to quiesce all devices (freeze), create |
| 477 | an image of the system memory while everything is stable, reactivate all |
| 478 | devices (thaw), write the image to permanent storage, and finally shut down the |
| 479 | system (poweroff). The phases used to accomplish this are: |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 480 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 481 | prepare, freeze, freeze_late, freeze_noirq, thaw_noirq, thaw_early, |
| 482 | thaw, complete, prepare, poweroff, poweroff_late, poweroff_noirq |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 483 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 484 | 1. The prepare phase is discussed in the "Entering System Suspend" section |
| 485 | above. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 486 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 487 | 2. The freeze methods should quiesce the device so that it doesn't generate |
| 488 | IRQs or DMA, and they may need to save the values of device registers. |
| 489 | However the device does not have to be put in a low-power state, and to |
| 490 | save time it's best not to do so. Also, the device should not be |
| 491 | prepared to generate wakeup events. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 492 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 493 | 3. The freeze_late phase is analogous to the suspend_late phase described |
| 494 | above, except that the device should not be put in a low-power state and |
| 495 | should not be allowed to generate wakeup events by it. |
| 496 | |
| 497 | 4. The freeze_noirq phase is analogous to the suspend_noirq phase discussed |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 498 | above, except again that the device should not be put in a low-power |
| 499 | state and should not be allowed to generate wakeup events. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 500 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 501 | At this point the system image is created. All devices should be inactive and |
| 502 | the contents of memory should remain undisturbed while this happens, so that the |
| 503 | image forms an atomic snapshot of the system state. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 504 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 505 | 5. The thaw_noirq phase is analogous to the resume_noirq phase discussed |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 506 | above. The main difference is that its methods can assume the device is |
| 507 | in the same state as at the end of the freeze_noirq phase. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 508 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 509 | 6. The thaw_early phase is analogous to the resume_early phase described |
| 510 | above. Its methods should undo the actions of the preceding |
| 511 | freeze_late, if necessary. |
| 512 | |
| 513 | 7. The thaw phase is analogous to the resume phase discussed above. Its |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 514 | methods should bring the device back to an operating state, so that it |
| 515 | can be used for saving the image if necessary. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 516 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 517 | 8. The complete phase is discussed in the "Leaving System Suspend" section |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 518 | above. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 519 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 520 | At this point the system image is saved, and the devices then need to be |
| 521 | prepared for the upcoming system shutdown. This is much like suspending them |
Zhang Rui | dc5aeae | 2013-05-13 02:42:11 +0000 | [diff] [blame] | 522 | before putting the system into the freeze, standby or memory sleep state, |
| 523 | and the phases are similar. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 524 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 525 | 9. The prepare phase is discussed above. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 526 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 527 | 10. The poweroff phase is analogous to the suspend phase. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 528 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 529 | 11. The poweroff_late phase is analogous to the suspend_late phase. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 530 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 531 | 12. The poweroff_noirq phase is analogous to the suspend_noirq phase. |
| 532 | |
| 533 | The poweroff, poweroff_late and poweroff_noirq callbacks should do essentially |
| 534 | the same things as the suspend, suspend_late and suspend_noirq callbacks, |
| 535 | respectively. The only notable difference is that they need not store the |
| 536 | device register values, because the registers should already have been stored |
| 537 | during the freeze, freeze_late or freeze_noirq phases. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 538 | |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 539 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 540 | Leaving Hibernation |
| 541 | ------------------- |
| 542 | Resuming from hibernation is, again, more complicated than resuming from a sleep |
| 543 | state in which the contents of main memory are preserved, because it requires |
| 544 | a system image to be loaded into memory and the pre-hibernation memory contents |
| 545 | to be restored before control can be passed back to the image kernel. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 546 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 547 | Although in principle, the image might be loaded into memory and the |
| 548 | pre-hibernation memory contents restored by the boot loader, in practice this |
| 549 | can't be done because boot loaders aren't smart enough and there is no |
| 550 | established protocol for passing the necessary information. So instead, the |
| 551 | boot loader loads a fresh instance of the kernel, called the boot kernel, into |
| 552 | memory and passes control to it in the usual way. Then the boot kernel reads |
| 553 | the system image, restores the pre-hibernation memory contents, and passes |
| 554 | control to the image kernel. Thus two different kernels are involved in |
| 555 | resuming from hibernation. In fact, the boot kernel may be completely different |
| 556 | from the image kernel: a different configuration and even a different version. |
| 557 | This has important consequences for device drivers and their subsystems. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 558 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 559 | To be able to load the system image into memory, the boot kernel needs to |
| 560 | include at least a subset of device drivers allowing it to access the storage |
| 561 | medium containing the image, although it doesn't need to include all of the |
| 562 | drivers present in the image kernel. After the image has been loaded, the |
| 563 | devices managed by the boot kernel need to be prepared for passing control back |
| 564 | to the image kernel. This is very similar to the initial steps involved in |
| 565 | creating a system image, and it is accomplished in the same way, using prepare, |
| 566 | freeze, and freeze_noirq phases. However the devices affected by these phases |
| 567 | are only those having drivers in the boot kernel; other devices will still be in |
| 568 | whatever state the boot loader left them. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 569 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 570 | Should the restoration of the pre-hibernation memory contents fail, the boot |
| 571 | kernel would go through the "thawing" procedure described above, using the |
| 572 | thaw_noirq, thaw, and complete phases, and then continue running normally. This |
| 573 | happens only rarely. Most often the pre-hibernation memory contents are |
| 574 | restored successfully and control is passed to the image kernel, which then |
| 575 | becomes responsible for bringing the system back to the working state. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 576 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 577 | To achieve this, the image kernel must restore the devices' pre-hibernation |
| 578 | functionality. The operation is much like waking up from the memory sleep |
| 579 | state, although it involves different phases: |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 580 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 581 | restore_noirq, restore_early, restore, complete |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 582 | |
| 583 | 1. The restore_noirq phase is analogous to the resume_noirq phase. |
| 584 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 585 | 2. The restore_early phase is analogous to the resume_early phase. |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 586 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 587 | 3. The restore phase is analogous to the resume phase. |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 588 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 589 | 4. The complete phase is discussed above. |
| 590 | |
| 591 | The main difference from resume[_early|_noirq] is that restore[_early|_noirq] |
| 592 | must assume the device has been accessed and reconfigured by the boot loader or |
| 593 | the boot kernel. Consequently the state of the device may be different from the |
| 594 | state remembered from the freeze, freeze_late and freeze_noirq phases. The |
| 595 | device may even need to be reset and completely re-initialized. In many cases |
| 596 | this difference doesn't matter, so the resume[_early|_noirq] and |
| 597 | restore[_early|_norq] method pointers can be set to the same routines. |
| 598 | Nevertheless, different callback pointers are used in case there is a situation |
| 599 | where it actually does matter. |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 600 | |
| 601 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 602 | Device Power Management Domains |
| 603 | ------------------------------- |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 604 | Sometimes devices share reference clocks or other power resources. In those |
| 605 | cases it generally is not possible to put devices into low-power states |
| 606 | individually. Instead, a set of devices sharing a power resource can be put |
| 607 | into a low-power state together at the same time by turning off the shared |
| 608 | power resource. Of course, they also need to be put into the full-power state |
| 609 | together, by turning the shared power resource on. A set of devices with this |
| 610 | property is often referred to as a power domain. |
| 611 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 612 | Support for power domains is provided through the pm_domain field of struct |
| 613 | device. This field is a pointer to an object of type struct dev_pm_domain, |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 614 | defined in include/linux/pm.h, providing a set of power management callbacks |
| 615 | analogous to the subsystem-level and device driver callbacks that are executed |
Rafael J. Wysocki | ca9c6890 | 2011-06-21 23:25:32 +0200 | [diff] [blame] | 616 | for the given device during all power transitions, instead of the respective |
| 617 | subsystem-level callbacks. Specifically, if a device's pm_domain pointer is |
| 618 | not NULL, the ->suspend() callback from the object pointed to by it will be |
| 619 | executed instead of its subsystem's (e.g. bus type's) ->suspend() callback and |
Oskar Schirmer | 8d2c794 | 2012-07-03 09:27:24 +0000 | [diff] [blame] | 620 | analogously for all of the remaining callbacks. In other words, power |
| 621 | management domain callbacks, if defined for the given device, always take |
| 622 | precedence over the callbacks provided by the device's subsystem (e.g. bus |
| 623 | type). |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 624 | |
Rafael J. Wysocki | ca9c6890 | 2011-06-21 23:25:32 +0200 | [diff] [blame] | 625 | The support for device power management domains is only relevant to platforms |
| 626 | needing to use the same device driver power management callbacks in many |
| 627 | different power domain configurations and wanting to avoid incorporating the |
| 628 | support for power domains into subsystem-level callbacks, for example by |
| 629 | modifying the platform bus type. Other platforms need not implement it or take |
| 630 | it into account in any way. |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 631 | |
| 632 | |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 633 | Device Low Power (suspend) States |
| 634 | --------------------------------- |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 635 | Device low-power states aren't standard. One device might only handle |
Oskar Schirmer | 8d2c794 | 2012-07-03 09:27:24 +0000 | [diff] [blame] | 636 | "on" and "off", while another might support a dozen different versions of |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 637 | "on" (how many engines are active?), plus a state that gets back to "on" |
| 638 | faster than from a full "off". |
| 639 | |
| 640 | Some busses define rules about what different suspend states mean. PCI |
| 641 | gives one example: after the suspend sequence completes, a non-legacy |
| 642 | PCI device may not perform DMA or issue IRQs, and any wakeup events it |
| 643 | issues would be issued through the PME# bus signal. Plus, there are |
| 644 | several PCI-standard device states, some of which are optional. |
| 645 | |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 646 | In contrast, integrated system-on-chip processors often use IRQs as the |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 647 | wakeup event sources (so drivers would call enable_irq_wake) and might |
| 648 | be able to treat DMA completion as a wakeup event (sometimes DMA can stay |
| 649 | active too, it'd only be the CPU and some peripherals that sleep). |
| 650 | |
| 651 | Some details here may be platform-specific. Systems may have devices that |
| 652 | can be fully active in certain sleep states, such as an LCD display that's |
| 653 | refreshed using DMA while most of the system is sleeping lightly ... and |
| 654 | its frame buffer might even be updated by a DSP or other non-Linux CPU while |
| 655 | the Linux control processor stays idle. |
| 656 | |
| 657 | Moreover, the specific actions taken may depend on the target system state. |
| 658 | One target system state might allow a given device to be very operational; |
| 659 | another might require a hard shut down with re-initialization on resume. |
| 660 | And two different target systems might use the same device in different |
| 661 | ways; the aforementioned LCD might be active in one product's "standby", |
| 662 | but a different product using the same SOC might work differently. |
| 663 | |
| 664 | |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 665 | Power Management Notifiers |
| 666 | -------------------------- |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 667 | There are some operations that cannot be carried out by the power management |
| 668 | callbacks discussed above, because the callbacks occur too late or too early. |
| 669 | To handle these cases, subsystems and device drivers may register power |
| 670 | management notifiers that are called before tasks are frozen and after they have |
| 671 | been thawed. Generally speaking, the PM notifiers are suitable for performing |
| 672 | actions that either require user space to be available, or at least won't |
| 673 | interfere with user space. |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 674 | |
| 675 | For details refer to Documentation/power/notifiers.txt. |
| 676 | |
| 677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | Runtime Power Management |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 679 | ======================== |
| 680 | Many devices are able to dynamically power down while the system is still |
| 681 | running. This feature is useful for devices that are not being used, and |
| 682 | can offer significant power savings on a running system. These devices |
| 683 | often support a range of runtime power states, which might use names such |
| 684 | as "off", "sleep", "idle", "active", and so on. Those states will in some |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 685 | cases (like PCI) be partially constrained by the bus the device uses, and will |
David Brownell | 4fc0840 | 2006-08-10 16:38:28 -0700 | [diff] [blame] | 686 | usually include hardware states that are also used in system sleep states. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 688 | A system-wide power transition can be started while some devices are in low |
| 689 | power states due to runtime power management. The system sleep PM callbacks |
| 690 | should recognize such situations and react to them appropriately, but the |
| 691 | necessary actions are subsystem-specific. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | |
Alan Stern | d6f9cda | 2010-03-26 23:53:55 +0100 | [diff] [blame] | 693 | In some cases the decision may be made at the subsystem level while in other |
| 694 | cases the device driver may be left to decide. In some cases it may be |
| 695 | desirable to leave a suspended device in that state during a system-wide power |
| 696 | transition, but in other cases the device must be put back into the full-power |
| 697 | state temporarily, for example so that its system wakeup capability can be |
| 698 | disabled. This all depends on the hardware and the design of the subsystem and |
| 699 | device driver in question. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
Rafael J. Wysocki | 455716e | 2011-07-01 22:29:05 +0200 | [diff] [blame] | 701 | During system-wide resume from a sleep state it's easiest to put devices into |
| 702 | the full-power state, as explained in Documentation/power/runtime_pm.txt. Refer |
| 703 | to that document for more information regarding this particular issue as well as |
Rafael J. Wysocki | 624f6ec | 2010-03-26 23:53:42 +0100 | [diff] [blame] | 704 | for information on the device runtime power management framework in general. |