blob: d172bce0fd49845e4b9dd5a76b068e28e22055b5 [file] [log] [blame]
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +01001Device Power Management
2
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +01003Copyright (c) 2010-2011 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
Alan Sternd6f9cda2010-03-26 23:53:55 +01004Copyright (c) 2010 Alan Stern <stern@rowland.harvard.edu>
Rafael J. Wysockif71495f2014-05-16 02:47:37 +02005Copyright (c) 2014 Intel Corp., Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Alan Sternd6f9cda2010-03-26 23:53:55 +01006
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +01007
David Brownell4fc08402006-08-10 16:38:28 -07008Most of the code in Linux is device drivers, so most of the Linux power
Alan Sternd6f9cda2010-03-26 23:53:55 +01009management (PM) code is also driver-specific. Most drivers will do very
10little; others, especially for platforms with small batteries (like cell
11phones), will do a lot.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
David Brownell4fc08402006-08-10 16:38:28 -070013This writeup gives an overview of how drivers interact with system-wide
14power management goals, emphasizing the models and interfaces that are
15shared by everything that hooks up to the driver model core. Read it as
16background for the domain-specific work you'd do with any specific driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18
David Brownell4fc08402006-08-10 16:38:28 -070019Two Models for Device Power Management
20======================================
21Drivers will use one or both of these models to put devices into low-power
22states:
23
24 System Sleep model:
Alan Sternd6f9cda2010-03-26 23:53:55 +010025 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 Brownell4fc08402006-08-10 16:38:28 -070029
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 Sternd6f9cda2010-03-26 23:53:55 +010036 leave the low-power state. This feature may be enabled or disabled
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +010037 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 Sternd6f9cda2010-03-26 23:53:55 +010040 system enter low-power states more often.
David Brownell4fc08402006-08-10 16:38:28 -070041
42 Runtime Power Management model:
Alan Sternd6f9cda2010-03-26 23:53:55 +010043 Devices may also be put into low-power states while the system is
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +010044 running, independently of other power management activity in principle.
45 However, devices are not generally independent of each other (for
Alan Sternd6f9cda2010-03-26 23:53:55 +010046 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. Wysocki624f6ec2010-03-26 23:53:42 +010048 device is on, it may be necessary to carry out some bus-specific
Alan Sternd6f9cda2010-03-26 23:53:55 +010049 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 Brownell4fc08402006-08-10 16:38:28 -070052
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +010053 For these reasons not only the device driver itself, but also the
Alan Sternd6f9cda2010-03-26 23:53:55 +010054 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 Brownell4fc08402006-08-10 16:38:28 -070059
Alan Sternd6f9cda2010-03-26 23:53:55 +010060There's not a lot to be said about those low-power states except that they are
61very system-specific, and often device-specific. Also, that if enough devices
62have been put into low-power states (at runtime), the effect may be very similar
63to entering some system-wide low-power state (system sleep) ... and that
64synergies exist, so that several drivers using runtime PM might put the system
65into a state where even deeper power saving options are available.
David Brownell4fc08402006-08-10 16:38:28 -070066
Alan Sternd6f9cda2010-03-26 23:53:55 +010067Most suspended devices will have quiesced all I/O: no more DMA or IRQs (except
68for wakeup events), no more data read or written, and requests from upstream
69drivers are no longer accepted. A given bus or platform may have different
70requirements though.
David Brownell4fc08402006-08-10 16:38:28 -070071
72Examples of hardware wakeup events include an alarm from a real time clock,
73network wake-on-LAN packets, keyboard or mouse activity, and media insertion
74or removal (for PCMCIA, MMC/SD, USB, and so on).
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76
David Brownell4fc08402006-08-10 16:38:28 -070077Interfaces for Entering System Sleep States
78===========================================
Alan Sternd6f9cda2010-03-26 23:53:55 +010079There are programming interfaces provided for subsystems (bus type, device type,
80device class) and device drivers to allow them to participate in the power
81management of devices they are concerned with. These interfaces cover both
82system sleep and runtime power management.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
David Brownell4fc08402006-08-10 16:38:28 -070084
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +010085Device Power Management Operations
86----------------------------------
87Device power management operations, at the subsystem level as well as at the
88device driver level, are implemented by defining and populating objects of type
89struct dev_pm_ops:
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +010091struct 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. Wysockicf579df2012-01-29 20:38:29 +0100100 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. Wysocki624f6ec2010-03-26 23:53:42 +0100106 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 Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100117This structure is defined in include/linux/pm.h and the methods included in it
118are also described in that file. Their roles will be explained in what follows.
Alan Sternd6f9cda2010-03-26 23:53:55 +0100119For now, it should be sufficient to remember that the last three methods are
120specific to runtime power management while the remaining ones are used during
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100121system-wide power transitions.
122
Alan Sternd6f9cda2010-03-26 23:53:55 +0100123There also is a deprecated "old" or "legacy" interface for power management
124operations available at least for some subsystems. This approach does not use
125struct dev_pm_ops objects and it is suitable only for implementing system sleep
126power management methods. Therefore it is not described in this document, so
127please refer directly to the source code for more information about it.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100128
129
130Subsystem-Level Methods
131-----------------------
132The core methods to suspend and resume devices reside in struct dev_pm_ops
Rafael J. Wysocki5841eb62011-11-23 21:18:39 +0100133pointed to by the ops member of struct dev_pm_domain, or by the pm member of
134struct bus_type, struct device_type and struct class. They are mostly of
135interest to the people writing infrastructure for platforms and buses, like PCI
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100136or USB, or device type and device class drivers. They also are relevant to the
137writers of device drivers whose subsystems (PM domains, device types, device
138classes and bus types) don't provide all power management methods.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100139
Alan Sternd6f9cda2010-03-26 23:53:55 +0100140Bus drivers implement these methods as appropriate for the hardware and the
141drivers using it; PCI works differently from USB, and so on. Not many people
142write subsystem-level drivers; most driver code is a "device driver" that builds
143on top of bus-specific framework code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
David Brownell4fc08402006-08-10 16:38:28 -0700145For more information on these driver calls, see the description later;
146they are called in phases for every device, respecting the parent-child
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100147sequencing in the driver model tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149
David Brownell4fc08402006-08-10 16:38:28 -0700150/sys/devices/.../power/wakeup files
151-----------------------------------
Rafael J. Wysockifafba482011-11-23 21:20:15 +0100152All device objects in the driver model contain fields that control the handling
153of system wakeup events (hardware signals that can force the system out of a
154sleep state). These fields are initialized by bus or device driver code using
Alan Sternd6f9cda2010-03-26 23:53:55 +0100155device_set_wakeup_capable() and device_set_wakeup_enable(), defined in
156include/linux/pm_wakeup.h.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Rafael J. Wysockifafba482011-11-23 21:20:15 +0100158The "power.can_wakeup" flag just records whether the device (and its driver) can
Alan Sternd6f9cda2010-03-26 23:53:55 +0100159physically support wakeup events. The device_set_wakeup_capable() routine
Rafael J. Wysockifafba482011-11-23 21:20:15 +0100160affects this flag. The "power.wakeup" field is a pointer to an object of type
161struct wakeup_source used for controlling whether or not the device should use
162its system wakeup mechanism and for notifying the PM core of system wakeup
163events signaled by the device. This object is only present for wakeup-capable
164devices (i.e. devices whose "can_wakeup" flags are set) and is created (or
165removed) by device_set_wakeup_capable().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Alan Sternd6f9cda2010-03-26 23:53:55 +0100167Whether or not a device is capable of issuing wakeup events is a hardware
168matter, and the kernel is responsible for keeping track of it. By contrast,
169whether or not a wakeup-capable device should issue wakeup events is a policy
170decision, and it is managed by user space through a sysfs attribute: the
Rafael J. Wysockifafba482011-11-23 21:20:15 +0100171"power/wakeup" file. User space can write the strings "enabled" or "disabled"
172to it to indicate whether or not, respectively, the device is supposed to signal
173system wakeup. This file is only present if the "power.wakeup" object exists
174for the given device and is created (or removed) along with that object, by
175device_set_wakeup_capable(). Reads from the file will return the corresponding
176string.
Alan Sternd6f9cda2010-03-26 23:53:55 +0100177
Rafael J. Wysockifafba482011-11-23 21:20:15 +0100178The "power/wakeup" file is supposed to contain the "disabled" string initially
179for the majority of devices; the major exceptions are power buttons, keyboards,
180and Ethernet adapters whose WoL (wake-on-LAN) feature has been set up with
181ethtool. It should also default to "enabled" for devices that don't generate
182wakeup requests on their own but merely forward wakeup requests from one bus to
183another (like PCI Express ports).
184
185The device_may_wakeup() routine returns true only if the "power.wakeup" object
186exists and the corresponding "power/wakeup" file contains the string "enabled".
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100187This information is used by subsystems, like the PCI bus type code, to see
188whether or not to enable the devices' wakeup mechanisms. If device wakeup
189mechanisms are enabled or disabled directly by drivers, they also should use
190device_may_wakeup() to decide what to do during a system sleep transition.
Rafael J. Wysockifafba482011-11-23 21:20:15 +0100191Device drivers, however, are not supposed to call device_set_wakeup_enable()
192directly in any case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Rafael J. Wysockifafba482011-11-23 21:20:15 +0100194It ought to be noted that system wakeup is conceptually different from "remote
195wakeup" used by runtime power management, although it may be supported by the
196same physical mechanism. Remote wakeup is a feature allowing devices in
197low-power states to trigger specific interrupts to signal conditions in which
198they should be put into the full-power state. Those interrupts may or may not
199be used to signal system wakeup events, depending on the hardware design. On
200some systems it is impossible to trigger them from system sleep states. In any
201case, remote wakeup should always be enabled for runtime power management for
202all devices and drivers that support it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100204/sys/devices/.../power/control files
205------------------------------------
Alan Sternd6f9cda2010-03-26 23:53:55 +0100206Each device in the driver model has a flag to control whether it is subject to
207runtime power management. This flag, called runtime_auto, is initialized by the
208bus type (or generally subsystem) code using pm_runtime_allow() or
209pm_runtime_forbid(); the default is to allow runtime power management.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Alan Sternd6f9cda2010-03-26 23:53:55 +0100211The setting can be adjusted by user space by writing either "on" or "auto" to
212the device's power/control sysfs file. Writing "auto" calls pm_runtime_allow(),
213setting the flag and allowing the device to be runtime power-managed by its
214driver. Writing "on" calls pm_runtime_forbid(), clearing the flag, returning
215the device to full power if it was in a low-power state, and preventing the
216device from being runtime power-managed. User space can check the current value
217of the runtime_auto flag by reading the file.
David Brownell4fc08402006-08-10 16:38:28 -0700218
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100219The device's runtime_auto flag has no effect on the handling of system-wide
Alan Sternd6f9cda2010-03-26 23:53:55 +0100220power transitions. In particular, the device can (and in the majority of cases
221should and will) be put into a low-power state during a system-wide transition
222to a sleep state even though its runtime_auto flag is clear.
David Brownell4fc08402006-08-10 16:38:28 -0700223
Alan Sternd6f9cda2010-03-26 23:53:55 +0100224For more information about the runtime power management framework, refer to
225Documentation/power/runtime_pm.txt.
David Brownell4fc08402006-08-10 16:38:28 -0700226
227
Alan Sternd6f9cda2010-03-26 23:53:55 +0100228Calling Drivers to Enter and Leave System Sleep States
229======================================================
230When the system goes into a sleep state, each device's driver is asked to
231suspend the device by putting it into a state compatible with the target
David Brownell4fc08402006-08-10 16:38:28 -0700232system state. That's usually some version of "off", but the details are
233system-specific. Also, wakeup-enabled devices will usually stay partly
234functional in order to wake the system.
235
Alan Sternd6f9cda2010-03-26 23:53:55 +0100236When the system leaves that low-power state, the device's driver is asked to
237resume it by returning it to full power. The suspend and resume operations
238always go together, and both are multi-phase operations.
David Brownell4fc08402006-08-10 16:38:28 -0700239
Alan Sternd6f9cda2010-03-26 23:53:55 +0100240For simple drivers, suspend might quiesce the device using class code
241and then turn its hardware as "off" as possible during suspend_noirq. The
David Brownell4fc08402006-08-10 16:38:28 -0700242matching resume calls would then completely reinitialize the hardware
243before reactivating its class I/O queues.
244
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100245More power-aware drivers might prepare the devices for triggering system wakeup
246events.
David Brownell4fc08402006-08-10 16:38:28 -0700247
248
249Call Sequence Guarantees
250------------------------
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100251To ensure that bridges and similar links needing to talk to a device are
David Brownell4fc08402006-08-10 16:38:28 -0700252available when the device is suspended or resumed, the device tree is
253walked in a bottom-up order to suspend devices. A top-down order is
254used to resume those devices.
255
256The ordering of the device tree is defined by the order in which devices
257get registered: a child can never be registered, probed or resumed before
258its parent; and can't be removed or suspended after that parent.
259
260The 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. Wysocki58aca232008-03-12 00:57:22 +0100262In particular, this means that a device registration may fail if the parent of
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100263the device is suspending (i.e. has been chosen by the PM core as the next
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100264device to suspend) or has already suspended, as well as after all of the other
265devices have been suspended. Device drivers must be prepared to cope with such
266situations.
David Brownell4fc08402006-08-10 16:38:28 -0700267
268
Alan Sternd6f9cda2010-03-26 23:53:55 +0100269System Power Management Phases
270------------------------------
271Suspending or resuming the system is done in several phases. Different phases
Zhang Ruidc5aeae2013-05-13 02:42:11 +0000272are used for freeze, standby, and memory sleep states ("suspend-to-RAM") and the
Alan Sternd6f9cda2010-03-26 23:53:55 +0100273hibernation state ("suspend-to-disk"). Each phase involves executing callbacks
274for every device before the next phase begins. Not all busses or classes
275support all these callbacks and not all drivers use all the callbacks. The
276various phases always run after tasks have been frozen and before they are
277unfrozen. Furthermore, the *_noirq phases run at a time when IRQ handlers have
Rafael J. Wysockifa8ce722011-11-23 21:19:57 +0100278been disabled (except for those marked with the IRQF_NO_SUSPEND flag).
David Brownell4fc08402006-08-10 16:38:28 -0700279
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100280All phases use PM domain, bus, type, class or driver callbacks (that is, methods
281defined in dev->pm_domain->ops, dev->bus->pm, dev->type->pm, dev->class->pm or
282dev->driver->pm). These callbacks are regarded by the PM core as mutually
283exclusive. Moreover, PM domain callbacks always take precedence over all of the
284other callbacks and, for example, type callbacks take precedence over bus, class
285and driver callbacks. To be precise, the following rules are used to determine
286which callback to execute in the given phase:
Rafael J. Wysocki5841eb62011-11-23 21:18:39 +0100287
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100288 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. Wysocki5841eb62011-11-23 21:18:39 +0100290
291 2. Otherwise, if both dev->type and dev->type->pm are present, the callback
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100292 included in dev->type->pm will be chosen for execution.
Rafael J. Wysocki5841eb62011-11-23 21:18:39 +0100293
294 3. Otherwise, if both dev->class and dev->class->pm are present, the
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100295 callback included in dev->class->pm will be chosen for execution.
Rafael J. Wysocki5841eb62011-11-23 21:18:39 +0100296
297 4. Otherwise, if both dev->bus and dev->bus->pm are present, the callback
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100298 included in dev->bus->pm will be chosen for execution.
Rafael J. Wysocki5841eb62011-11-23 21:18:39 +0100299
300This allows PM domains and device types to override callbacks provided by bus
301types or device classes if necessary.
David Brownell4fc08402006-08-10 16:38:28 -0700302
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100303The PM domain, type, class and bus callbacks may in turn invoke device- or
304driver-specific methods stored in dev->driver->pm, but they don't have to do
305that.
306
307If the subsystem callback chosen for execution is not present, the PM core will
308execute the corresponding method from dev->driver->pm instead if there is one.
David Brownell4fc08402006-08-10 16:38:28 -0700309
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100310
Alan Sternd6f9cda2010-03-26 23:53:55 +0100311Entering System Suspend
312-----------------------
Zhang Ruidc5aeae2013-05-13 02:42:11 +0000313When the system goes into the freeze, standby or memory sleep state,
314the phases are:
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100315
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100316 prepare, suspend, suspend_late, suspend_noirq.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100317
Alan Sternd6f9cda2010-03-26 23:53:55 +0100318 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. Wysocki624f6ec2010-03-26 23:53:42 +0100324
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200325 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. Wysockifa8ce722011-11-23 21:19:57 +0100327 driver in some way for the upcoming system power transition, but it
328 should not put the device into a low-power state.
Alan Sternd6f9cda2010-03-26 23:53:55 +0100329
Rafael J. Wysockif71495f2014-05-16 02:47:37 +0200330 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 Sternd6f9cda2010-03-26 23:53:55 +0100344 2. The suspend methods should quiesce the device to stop it from performing
345 I/O. They also may save the device registers and put it into the
346 appropriate low-power state, depending on the bus type the device is on,
347 and they may enable wakeup events.
348
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100349 3 For a number of devices it is convenient to split suspend into the
350 "quiesce device" and "save device state" phases, in which cases
351 suspend_late is meant to do the latter. It is always executed after
352 runtime power management has been disabled for all devices.
353
354 4. The suspend_noirq phase occurs after IRQ handlers have been disabled,
Alan Sternd6f9cda2010-03-26 23:53:55 +0100355 which means that the driver's interrupt handler will not be called while
356 the callback method is running. The methods should save the values of
357 the device's registers that weren't saved previously and finally put the
358 device into the appropriate low-power state.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100359
360 The majority of subsystems and device drivers need not implement this
Alan Sternd6f9cda2010-03-26 23:53:55 +0100361 callback. However, bus types allowing devices to share interrupt
362 vectors, like PCI, generally need it; otherwise a driver might encounter
363 an error during the suspend phase by fielding a shared interrupt
364 generated by some other device after its own device had been set to low
365 power.
David Brownell4fc08402006-08-10 16:38:28 -0700366
Alan Sternd6f9cda2010-03-26 23:53:55 +0100367At the end of these phases, drivers should have stopped all I/O transactions
368(DMA, IRQs), saved enough state that they can re-initialize or restore previous
369state (as needed by the hardware), and placed the device into a low-power state.
370On many platforms they will gate off one or more clock sources; sometimes they
371will also switch off power supplies or reduce voltages. (Drivers supporting
372runtime PM may already have performed some or all of these steps.)
David Brownell4fc08402006-08-10 16:38:28 -0700373
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100374If device_may_wakeup(dev) returns true, the device should be prepared for
Alan Sternd6f9cda2010-03-26 23:53:55 +0100375generating hardware wakeup signals to trigger a system wakeup event when the
376system is in the sleep state. For example, enable_irq_wake() might identify
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100377GPIO signals hooked up to a switch or other external hardware, and
378pci_enable_wake() does something similar for the PCI PME signal.
David Brownell4fc08402006-08-10 16:38:28 -0700379
Alan Sternd6f9cda2010-03-26 23:53:55 +0100380If any of these callbacks returns an error, the system won't enter the desired
381low-power state. Instead the PM core will unwind its actions by resuming all
382the devices that were suspended.
David Brownell4fc08402006-08-10 16:38:28 -0700383
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100384
Alan Sternd6f9cda2010-03-26 23:53:55 +0100385Leaving System Suspend
386----------------------
Zhang Ruidc5aeae2013-05-13 02:42:11 +0000387When resuming from freeze, standby or memory sleep, the phases are:
Alan Sternd6f9cda2010-03-26 23:53:55 +0100388
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100389 resume_noirq, resume_early, resume, complete.
Alan Sternd6f9cda2010-03-26 23:53:55 +0100390
391 1. The resume_noirq callback methods should perform any actions needed
392 before the driver's interrupt handlers are invoked. This generally
393 means undoing the actions of the suspend_noirq phase. If the bus type
394 permits devices to share interrupt vectors, like PCI, the method should
395 bring the device and its driver into a state in which the driver can
396 recognize if the device is the source of incoming interrupts, if any,
397 and handle them correctly.
398
399 For example, the PCI bus type's ->pm.resume_noirq() puts the device into
400 the full-power state (D0 in the PCI terminology) and restores the
401 standard configuration registers of the device. Then it calls the
402 device driver's ->pm.resume_noirq() method to perform device-specific
403 actions.
404
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100405 2. The resume_early methods should prepare devices for the execution of
406 the resume methods. This generally involves undoing the actions of the
407 preceding suspend_late phase.
408
Masanari Iidadf5cbb22014-03-21 10:04:30 +0900409 3 The resume methods should bring the device back to its operating
Alan Sternd6f9cda2010-03-26 23:53:55 +0100410 state, so that it can perform normal I/O. This generally involves
411 undoing the actions of the suspend phase.
412
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100413 4. The complete phase should undo the actions of the prepare phase. Note,
414 however, that new children may be registered below the device as soon as
415 the resume callbacks occur; it's not necessary to wait until the
416 complete phase.
Alan Sternd6f9cda2010-03-26 23:53:55 +0100417
Rafael J. Wysockif71495f2014-05-16 02:47:37 +0200418 Moreover, if the preceding prepare callback returned a positive number,
419 the device may have been left in runtime suspend throughout the whole
420 system suspend and resume (the suspend, suspend_late, suspend_noirq
421 phases of system suspend and the resume_noirq, resume_early, resume
422 phases of system resume may have been skipped for it). In that case,
423 the complete callback is entirely responsible for bringing the device
424 back to the functional state after system suspend if necessary. [For
425 example, it may need to queue up a runtime resume request for the device
426 for this purpose.] To check if that is the case, the complete callback
427 can consult the device's power.direct_complete flag. Namely, if that
428 flag is set when the complete callback is being run, it has been called
429 directly after the preceding prepare and special action may be required
430 to make the device work correctly afterward.
431
Alan Sternd6f9cda2010-03-26 23:53:55 +0100432At the end of these phases, drivers should be as functional as they were before
433suspending: I/O can be performed using DMA and IRQs, and the relevant clocks are
Rafael J. Wysockif71495f2014-05-16 02:47:37 +0200434gated on.
Alan Sternd6f9cda2010-03-26 23:53:55 +0100435
436However, the details here may again be platform-specific. For example,
437some systems support multiple "run" states, and the mode in effect at
438the end of resume might not be the one which preceded suspension.
439That means availability of certain clocks or power supplies changed,
440which could easily affect how a driver works.
441
442Drivers need to be able to handle hardware which has been reset since the
443suspend methods were called, for example by complete reinitialization.
444This may be the hardest part, and the one most protected by NDA'd documents
445and chip errata. It's simplest if the hardware state hasn't changed since
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300446the suspend was carried out, but that can't be guaranteed (in fact, it usually
Alan Sternd6f9cda2010-03-26 23:53:55 +0100447is not the case).
448
449Drivers must also be prepared to notice that the device has been removed
450while the system was powered down, whenever that's physically possible.
451PCMCIA, MMC, USB, Firewire, SCSI, and even IDE are common examples of busses
452where common Linux platforms will see such removal. Details of how drivers
453will notice and handle such removals are currently bus-specific, and often
454involve a separate thread.
455
456These callbacks may return an error value, but the PM core will ignore such
457errors since there's nothing it can do about them other than printing them in
458the system log.
459
460
461Entering Hibernation
462--------------------
Zhang Ruidc5aeae2013-05-13 02:42:11 +0000463Hibernating the system is more complicated than putting it into the other
464sleep states, because it involves creating and saving a system image.
Alan Sternd6f9cda2010-03-26 23:53:55 +0100465Therefore there are more phases for hibernation, with a different set of
466callbacks. These phases always run after tasks have been frozen and memory has
467been freed.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100468
Alan Sternd6f9cda2010-03-26 23:53:55 +0100469The general procedure for hibernation is to quiesce all devices (freeze), create
470an image of the system memory while everything is stable, reactivate all
471devices (thaw), write the image to permanent storage, and finally shut down the
472system (poweroff). The phases used to accomplish this are:
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100473
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100474 prepare, freeze, freeze_late, freeze_noirq, thaw_noirq, thaw_early,
475 thaw, complete, prepare, poweroff, poweroff_late, poweroff_noirq
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100476
Alan Sternd6f9cda2010-03-26 23:53:55 +0100477 1. The prepare phase is discussed in the "Entering System Suspend" section
478 above.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100479
Alan Sternd6f9cda2010-03-26 23:53:55 +0100480 2. The freeze methods should quiesce the device so that it doesn't generate
481 IRQs or DMA, and they may need to save the values of device registers.
482 However the device does not have to be put in a low-power state, and to
483 save time it's best not to do so. Also, the device should not be
484 prepared to generate wakeup events.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100485
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100486 3. The freeze_late phase is analogous to the suspend_late phase described
487 above, except that the device should not be put in a low-power state and
488 should not be allowed to generate wakeup events by it.
489
490 4. The freeze_noirq phase is analogous to the suspend_noirq phase discussed
Alan Sternd6f9cda2010-03-26 23:53:55 +0100491 above, except again that the device should not be put in a low-power
492 state and should not be allowed to generate wakeup events.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100493
Alan Sternd6f9cda2010-03-26 23:53:55 +0100494At this point the system image is created. All devices should be inactive and
495the contents of memory should remain undisturbed while this happens, so that the
496image forms an atomic snapshot of the system state.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100497
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100498 5. The thaw_noirq phase is analogous to the resume_noirq phase discussed
Alan Sternd6f9cda2010-03-26 23:53:55 +0100499 above. The main difference is that its methods can assume the device is
500 in the same state as at the end of the freeze_noirq phase.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100501
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100502 6. The thaw_early phase is analogous to the resume_early phase described
503 above. Its methods should undo the actions of the preceding
504 freeze_late, if necessary.
505
506 7. The thaw phase is analogous to the resume phase discussed above. Its
Alan Sternd6f9cda2010-03-26 23:53:55 +0100507 methods should bring the device back to an operating state, so that it
508 can be used for saving the image if necessary.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100509
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100510 8. The complete phase is discussed in the "Leaving System Suspend" section
Alan Sternd6f9cda2010-03-26 23:53:55 +0100511 above.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100512
Alan Sternd6f9cda2010-03-26 23:53:55 +0100513At this point the system image is saved, and the devices then need to be
514prepared for the upcoming system shutdown. This is much like suspending them
Zhang Ruidc5aeae2013-05-13 02:42:11 +0000515before putting the system into the freeze, standby or memory sleep state,
516and the phases are similar.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100517
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100518 9. The prepare phase is discussed above.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100519
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100520 10. The poweroff phase is analogous to the suspend phase.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100521
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100522 11. The poweroff_late phase is analogous to the suspend_late phase.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100523
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100524 12. The poweroff_noirq phase is analogous to the suspend_noirq phase.
525
526The poweroff, poweroff_late and poweroff_noirq callbacks should do essentially
527the same things as the suspend, suspend_late and suspend_noirq callbacks,
528respectively. The only notable difference is that they need not store the
529device register values, because the registers should already have been stored
530during the freeze, freeze_late or freeze_noirq phases.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100531
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100532
Alan Sternd6f9cda2010-03-26 23:53:55 +0100533Leaving Hibernation
534-------------------
535Resuming from hibernation is, again, more complicated than resuming from a sleep
536state in which the contents of main memory are preserved, because it requires
537a system image to be loaded into memory and the pre-hibernation memory contents
538to be restored before control can be passed back to the image kernel.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100539
Alan Sternd6f9cda2010-03-26 23:53:55 +0100540Although in principle, the image might be loaded into memory and the
541pre-hibernation memory contents restored by the boot loader, in practice this
542can't be done because boot loaders aren't smart enough and there is no
543established protocol for passing the necessary information. So instead, the
544boot loader loads a fresh instance of the kernel, called the boot kernel, into
545memory and passes control to it in the usual way. Then the boot kernel reads
546the system image, restores the pre-hibernation memory contents, and passes
547control to the image kernel. Thus two different kernels are involved in
548resuming from hibernation. In fact, the boot kernel may be completely different
549from the image kernel: a different configuration and even a different version.
550This has important consequences for device drivers and their subsystems.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100551
Alan Sternd6f9cda2010-03-26 23:53:55 +0100552To be able to load the system image into memory, the boot kernel needs to
553include at least a subset of device drivers allowing it to access the storage
554medium containing the image, although it doesn't need to include all of the
555drivers present in the image kernel. After the image has been loaded, the
556devices managed by the boot kernel need to be prepared for passing control back
557to the image kernel. This is very similar to the initial steps involved in
558creating a system image, and it is accomplished in the same way, using prepare,
559freeze, and freeze_noirq phases. However the devices affected by these phases
560are only those having drivers in the boot kernel; other devices will still be in
561whatever state the boot loader left them.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100562
Alan Sternd6f9cda2010-03-26 23:53:55 +0100563Should the restoration of the pre-hibernation memory contents fail, the boot
564kernel would go through the "thawing" procedure described above, using the
565thaw_noirq, thaw, and complete phases, and then continue running normally. This
566happens only rarely. Most often the pre-hibernation memory contents are
567restored successfully and control is passed to the image kernel, which then
568becomes responsible for bringing the system back to the working state.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100569
Alan Sternd6f9cda2010-03-26 23:53:55 +0100570To achieve this, the image kernel must restore the devices' pre-hibernation
571functionality. The operation is much like waking up from the memory sleep
572state, although it involves different phases:
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100573
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100574 restore_noirq, restore_early, restore, complete
Alan Sternd6f9cda2010-03-26 23:53:55 +0100575
576 1. The restore_noirq phase is analogous to the resume_noirq phase.
577
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100578 2. The restore_early phase is analogous to the resume_early phase.
Alan Sternd6f9cda2010-03-26 23:53:55 +0100579
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100580 3. The restore phase is analogous to the resume phase.
Alan Sternd6f9cda2010-03-26 23:53:55 +0100581
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100582 4. The complete phase is discussed above.
583
584The main difference from resume[_early|_noirq] is that restore[_early|_noirq]
585must assume the device has been accessed and reconfigured by the boot loader or
586the boot kernel. Consequently the state of the device may be different from the
587state remembered from the freeze, freeze_late and freeze_noirq phases. The
588device may even need to be reset and completely re-initialized. In many cases
589this difference doesn't matter, so the resume[_early|_noirq] and
590restore[_early|_norq] method pointers can be set to the same routines.
591Nevertheless, different callback pointers are used in case there is a situation
592where it actually does matter.
Alan Sternd6f9cda2010-03-26 23:53:55 +0100593
594
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200595Device Power Management Domains
596-------------------------------
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100597Sometimes devices share reference clocks or other power resources. In those
598cases it generally is not possible to put devices into low-power states
599individually. Instead, a set of devices sharing a power resource can be put
600into a low-power state together at the same time by turning off the shared
601power resource. Of course, they also need to be put into the full-power state
602together, by turning the shared power resource on. A set of devices with this
603property is often referred to as a power domain.
604
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200605Support for power domains is provided through the pm_domain field of struct
606device. This field is a pointer to an object of type struct dev_pm_domain,
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100607defined in include/linux/pm.h, providing a set of power management callbacks
608analogous to the subsystem-level and device driver callbacks that are executed
Rafael J. Wysockica9c6892011-06-21 23:25:32 +0200609for the given device during all power transitions, instead of the respective
610subsystem-level callbacks. Specifically, if a device's pm_domain pointer is
611not NULL, the ->suspend() callback from the object pointed to by it will be
612executed instead of its subsystem's (e.g. bus type's) ->suspend() callback and
Oskar Schirmer8d2c7942012-07-03 09:27:24 +0000613analogously for all of the remaining callbacks. In other words, power
614management domain callbacks, if defined for the given device, always take
615precedence over the callbacks provided by the device's subsystem (e.g. bus
616type).
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100617
Rafael J. Wysockica9c6892011-06-21 23:25:32 +0200618The support for device power management domains is only relevant to platforms
619needing to use the same device driver power management callbacks in many
620different power domain configurations and wanting to avoid incorporating the
621support for power domains into subsystem-level callbacks, for example by
622modifying the platform bus type. Other platforms need not implement it or take
623it into account in any way.
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100624
625
David Brownell4fc08402006-08-10 16:38:28 -0700626Device Low Power (suspend) States
627---------------------------------
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100628Device low-power states aren't standard. One device might only handle
Oskar Schirmer8d2c7942012-07-03 09:27:24 +0000629"on" and "off", while another might support a dozen different versions of
David Brownell4fc08402006-08-10 16:38:28 -0700630"on" (how many engines are active?), plus a state that gets back to "on"
631faster than from a full "off".
632
633Some busses define rules about what different suspend states mean. PCI
634gives one example: after the suspend sequence completes, a non-legacy
635PCI device may not perform DMA or issue IRQs, and any wakeup events it
636issues would be issued through the PME# bus signal. Plus, there are
637several PCI-standard device states, some of which are optional.
638
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100639In contrast, integrated system-on-chip processors often use IRQs as the
David Brownell4fc08402006-08-10 16:38:28 -0700640wakeup event sources (so drivers would call enable_irq_wake) and might
641be able to treat DMA completion as a wakeup event (sometimes DMA can stay
642active too, it'd only be the CPU and some peripherals that sleep).
643
644Some details here may be platform-specific. Systems may have devices that
645can be fully active in certain sleep states, such as an LCD display that's
646refreshed using DMA while most of the system is sleeping lightly ... and
647its frame buffer might even be updated by a DSP or other non-Linux CPU while
648the Linux control processor stays idle.
649
650Moreover, the specific actions taken may depend on the target system state.
651One target system state might allow a given device to be very operational;
652another might require a hard shut down with re-initialization on resume.
653And two different target systems might use the same device in different
654ways; the aforementioned LCD might be active in one product's "standby",
655but a different product using the same SOC might work differently.
656
657
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100658Power Management Notifiers
659--------------------------
Alan Sternd6f9cda2010-03-26 23:53:55 +0100660There are some operations that cannot be carried out by the power management
661callbacks discussed above, because the callbacks occur too late or too early.
662To handle these cases, subsystems and device drivers may register power
663management notifiers that are called before tasks are frozen and after they have
664been thawed. Generally speaking, the PM notifiers are suitable for performing
665actions that either require user space to be available, or at least won't
666interfere with user space.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100667
668For details refer to Documentation/power/notifiers.txt.
669
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671Runtime Power Management
David Brownell4fc08402006-08-10 16:38:28 -0700672========================
673Many devices are able to dynamically power down while the system is still
674running. This feature is useful for devices that are not being used, and
675can offer significant power savings on a running system. These devices
676often support a range of runtime power states, which might use names such
677as "off", "sleep", "idle", "active", and so on. Those states will in some
Alan Sternd6f9cda2010-03-26 23:53:55 +0100678cases (like PCI) be partially constrained by the bus the device uses, and will
David Brownell4fc08402006-08-10 16:38:28 -0700679usually include hardware states that are also used in system sleep states.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Alan Sternd6f9cda2010-03-26 23:53:55 +0100681A system-wide power transition can be started while some devices are in low
682power states due to runtime power management. The system sleep PM callbacks
683should recognize such situations and react to them appropriately, but the
684necessary actions are subsystem-specific.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Alan Sternd6f9cda2010-03-26 23:53:55 +0100686In some cases the decision may be made at the subsystem level while in other
687cases the device driver may be left to decide. In some cases it may be
688desirable to leave a suspended device in that state during a system-wide power
689transition, but in other cases the device must be put back into the full-power
690state temporarily, for example so that its system wakeup capability can be
691disabled. This all depends on the hardware and the design of the subsystem and
692device driver in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Rafael J. Wysocki455716e2011-07-01 22:29:05 +0200694During system-wide resume from a sleep state it's easiest to put devices into
695the full-power state, as explained in Documentation/power/runtime_pm.txt. Refer
696to that document for more information regarding this particular issue as well as
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100697for information on the device runtime power management framework in general.