blob: 646a89e0c07d50c682912ef0200dc087363cd37f [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>
5
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +01006
David Brownell4fc08402006-08-10 16:38:28 -07007Most of the code in Linux is device drivers, so most of the Linux power
Alan Sternd6f9cda2010-03-26 23:53:55 +01008management (PM) code is also driver-specific. Most drivers will do very
9little; others, especially for platforms with small batteries (like cell
10phones), will do a lot.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
David Brownell4fc08402006-08-10 16:38:28 -070012This writeup gives an overview of how drivers interact with system-wide
13power management goals, emphasizing the models and interfaces that are
14shared by everything that hooks up to the driver model core. Read it as
15background for the domain-specific work you'd do with any specific driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17
David Brownell4fc08402006-08-10 16:38:28 -070018Two Models for Device Power Management
19======================================
20Drivers will use one or both of these models to put devices into low-power
21states:
22
23 System Sleep model:
Alan Sternd6f9cda2010-03-26 23:53:55 +010024 Drivers can enter low-power states as part of entering system-wide
25 low-power states like "suspend" (also known as "suspend-to-RAM"), or
26 (mostly for systems with disks) "hibernation" (also known as
27 "suspend-to-disk").
David Brownell4fc08402006-08-10 16:38:28 -070028
29 This is something that device, bus, and class drivers collaborate on
30 by implementing various role-specific suspend and resume methods to
31 cleanly power down hardware and software subsystems, then reactivate
32 them without loss of data.
33
34 Some drivers can manage hardware wakeup events, which make the system
Alan Sternd6f9cda2010-03-26 23:53:55 +010035 leave the low-power state. This feature may be enabled or disabled
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +010036 using the relevant /sys/devices/.../power/wakeup file (for Ethernet
37 drivers the ioctl interface used by ethtool may also be used for this
38 purpose); enabling it may cost some power usage, but let the whole
Alan Sternd6f9cda2010-03-26 23:53:55 +010039 system enter low-power states more often.
David Brownell4fc08402006-08-10 16:38:28 -070040
41 Runtime Power Management model:
Alan Sternd6f9cda2010-03-26 23:53:55 +010042 Devices may also be put into low-power states while the system is
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +010043 running, independently of other power management activity in principle.
44 However, devices are not generally independent of each other (for
Alan Sternd6f9cda2010-03-26 23:53:55 +010045 example, a parent device cannot be suspended unless all of its child
46 devices have been suspended). Moreover, depending on the bus type the
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +010047 device is on, it may be necessary to carry out some bus-specific
Alan Sternd6f9cda2010-03-26 23:53:55 +010048 operations on the device for this purpose. Devices put into low power
49 states at run time may require special handling during system-wide power
50 transitions (suspend or hibernation).
David Brownell4fc08402006-08-10 16:38:28 -070051
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +010052 For these reasons not only the device driver itself, but also the
Alan Sternd6f9cda2010-03-26 23:53:55 +010053 appropriate subsystem (bus type, device type or device class) driver and
54 the PM core are involved in runtime power management. As in the system
55 sleep power management case, they need to collaborate by implementing
56 various role-specific suspend and resume methods, so that the hardware
57 is cleanly powered down and reactivated without data or service loss.
David Brownell4fc08402006-08-10 16:38:28 -070058
Alan Sternd6f9cda2010-03-26 23:53:55 +010059There's not a lot to be said about those low-power states except that they are
60very system-specific, and often device-specific. Also, that if enough devices
61have been put into low-power states (at runtime), the effect may be very similar
62to entering some system-wide low-power state (system sleep) ... and that
63synergies exist, so that several drivers using runtime PM might put the system
64into a state where even deeper power saving options are available.
David Brownell4fc08402006-08-10 16:38:28 -070065
Alan Sternd6f9cda2010-03-26 23:53:55 +010066Most suspended devices will have quiesced all I/O: no more DMA or IRQs (except
67for wakeup events), no more data read or written, and requests from upstream
68drivers are no longer accepted. A given bus or platform may have different
69requirements though.
David Brownell4fc08402006-08-10 16:38:28 -070070
71Examples of hardware wakeup events include an alarm from a real time clock,
72network wake-on-LAN packets, keyboard or mouse activity, and media insertion
73or removal (for PCMCIA, MMC/SD, USB, and so on).
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75
David Brownell4fc08402006-08-10 16:38:28 -070076Interfaces for Entering System Sleep States
77===========================================
Alan Sternd6f9cda2010-03-26 23:53:55 +010078There are programming interfaces provided for subsystems (bus type, device type,
79device class) and device drivers to allow them to participate in the power
80management of devices they are concerned with. These interfaces cover both
81system sleep and runtime power management.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
David Brownell4fc08402006-08-10 16:38:28 -070083
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +010084Device Power Management Operations
85----------------------------------
86Device power management operations, at the subsystem level as well as at the
87device driver level, are implemented by defining and populating objects of type
88struct dev_pm_ops:
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +010090struct dev_pm_ops {
91 int (*prepare)(struct device *dev);
92 void (*complete)(struct device *dev);
93 int (*suspend)(struct device *dev);
94 int (*resume)(struct device *dev);
95 int (*freeze)(struct device *dev);
96 int (*thaw)(struct device *dev);
97 int (*poweroff)(struct device *dev);
98 int (*restore)(struct device *dev);
99 int (*suspend_noirq)(struct device *dev);
100 int (*resume_noirq)(struct device *dev);
101 int (*freeze_noirq)(struct device *dev);
102 int (*thaw_noirq)(struct device *dev);
103 int (*poweroff_noirq)(struct device *dev);
104 int (*restore_noirq)(struct device *dev);
105 int (*runtime_suspend)(struct device *dev);
106 int (*runtime_resume)(struct device *dev);
107 int (*runtime_idle)(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100110This structure is defined in include/linux/pm.h and the methods included in it
111are also described in that file. Their roles will be explained in what follows.
Alan Sternd6f9cda2010-03-26 23:53:55 +0100112For now, it should be sufficient to remember that the last three methods are
113specific to runtime power management while the remaining ones are used during
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100114system-wide power transitions.
115
Alan Sternd6f9cda2010-03-26 23:53:55 +0100116There also is a deprecated "old" or "legacy" interface for power management
117operations available at least for some subsystems. This approach does not use
118struct dev_pm_ops objects and it is suitable only for implementing system sleep
119power management methods. Therefore it is not described in this document, so
120please refer directly to the source code for more information about it.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100121
122
123Subsystem-Level Methods
124-----------------------
125The core methods to suspend and resume devices reside in struct dev_pm_ops
126pointed to by the pm member of struct bus_type, struct device_type and
127struct class. They are mostly of interest to the people writing infrastructure
128for buses, like PCI or USB, or device type and device class drivers.
129
Alan Sternd6f9cda2010-03-26 23:53:55 +0100130Bus drivers implement these methods as appropriate for the hardware and the
131drivers using it; PCI works differently from USB, and so on. Not many people
132write subsystem-level drivers; most driver code is a "device driver" that builds
133on top of bus-specific framework code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
David Brownell4fc08402006-08-10 16:38:28 -0700135For more information on these driver calls, see the description later;
136they are called in phases for every device, respecting the parent-child
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100137sequencing in the driver model tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139
David Brownell4fc08402006-08-10 16:38:28 -0700140/sys/devices/.../power/wakeup files
141-----------------------------------
Alan Sternd6f9cda2010-03-26 23:53:55 +0100142All devices in the driver model have two flags to control handling of wakeup
143events (hardware signals that can force the device and/or system out of a low
144power state). These flags are initialized by bus or device driver code using
145device_set_wakeup_capable() and device_set_wakeup_enable(), defined in
146include/linux/pm_wakeup.h.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
David Brownell4fc08402006-08-10 16:38:28 -0700148The "can_wakeup" flag just records whether the device (and its driver) can
Alan Sternd6f9cda2010-03-26 23:53:55 +0100149physically support wakeup events. The device_set_wakeup_capable() routine
150affects this flag. The "should_wakeup" flag controls whether the device should
151try to use its wakeup mechanism. device_set_wakeup_enable() affects this flag;
152for the most part drivers should not change its value. The initial value of
153should_wakeup is supposed to be false for the majority of devices; the major
154exceptions are power buttons, keyboards, and Ethernet adapters whose WoL
Alan Stern8f888932011-09-26 17:38:50 +0200155(wake-on-LAN) feature has been set up with ethtool. It should also default
156to true for devices that don't generate wakeup requests on their own but merely
157forward wakeup requests from one bus to another (like PCI bridges).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Alan Sternd6f9cda2010-03-26 23:53:55 +0100159Whether or not a device is capable of issuing wakeup events is a hardware
160matter, and the kernel is responsible for keeping track of it. By contrast,
161whether or not a wakeup-capable device should issue wakeup events is a policy
162decision, and it is managed by user space through a sysfs attribute: the
163power/wakeup file. User space can write the strings "enabled" or "disabled" to
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100164set or clear the "should_wakeup" flag, respectively. This file is only present
165for wakeup-capable devices (i.e. devices whose "can_wakeup" flags are set)
166and is created (or removed) by device_set_wakeup_capable(). Reads from the
167file will return the corresponding string.
Alan Sternd6f9cda2010-03-26 23:53:55 +0100168
169The device_may_wakeup() routine returns true only if both flags are set.
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100170This information is used by subsystems, like the PCI bus type code, to see
171whether or not to enable the devices' wakeup mechanisms. If device wakeup
172mechanisms are enabled or disabled directly by drivers, they also should use
173device_may_wakeup() to decide what to do during a system sleep transition.
174However for runtime power management, wakeup events should be enabled whenever
175the device and driver both support them, regardless of the should_wakeup flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100178/sys/devices/.../power/control files
179------------------------------------
Alan Sternd6f9cda2010-03-26 23:53:55 +0100180Each device in the driver model has a flag to control whether it is subject to
181runtime power management. This flag, called runtime_auto, is initialized by the
182bus type (or generally subsystem) code using pm_runtime_allow() or
183pm_runtime_forbid(); the default is to allow runtime power management.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Alan Sternd6f9cda2010-03-26 23:53:55 +0100185The setting can be adjusted by user space by writing either "on" or "auto" to
186the device's power/control sysfs file. Writing "auto" calls pm_runtime_allow(),
187setting the flag and allowing the device to be runtime power-managed by its
188driver. Writing "on" calls pm_runtime_forbid(), clearing the flag, returning
189the device to full power if it was in a low-power state, and preventing the
190device from being runtime power-managed. User space can check the current value
191of the runtime_auto flag by reading the file.
David Brownell4fc08402006-08-10 16:38:28 -0700192
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100193The device's runtime_auto flag has no effect on the handling of system-wide
Alan Sternd6f9cda2010-03-26 23:53:55 +0100194power transitions. In particular, the device can (and in the majority of cases
195should and will) be put into a low-power state during a system-wide transition
196to a sleep state even though its runtime_auto flag is clear.
David Brownell4fc08402006-08-10 16:38:28 -0700197
Alan Sternd6f9cda2010-03-26 23:53:55 +0100198For more information about the runtime power management framework, refer to
199Documentation/power/runtime_pm.txt.
David Brownell4fc08402006-08-10 16:38:28 -0700200
201
Alan Sternd6f9cda2010-03-26 23:53:55 +0100202Calling Drivers to Enter and Leave System Sleep States
203======================================================
204When the system goes into a sleep state, each device's driver is asked to
205suspend the device by putting it into a state compatible with the target
David Brownell4fc08402006-08-10 16:38:28 -0700206system state. That's usually some version of "off", but the details are
207system-specific. Also, wakeup-enabled devices will usually stay partly
208functional in order to wake the system.
209
Alan Sternd6f9cda2010-03-26 23:53:55 +0100210When the system leaves that low-power state, the device's driver is asked to
211resume it by returning it to full power. The suspend and resume operations
212always go together, and both are multi-phase operations.
David Brownell4fc08402006-08-10 16:38:28 -0700213
Alan Sternd6f9cda2010-03-26 23:53:55 +0100214For simple drivers, suspend might quiesce the device using class code
215and then turn its hardware as "off" as possible during suspend_noirq. The
David Brownell4fc08402006-08-10 16:38:28 -0700216matching resume calls would then completely reinitialize the hardware
217before reactivating its class I/O queues.
218
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100219More power-aware drivers might prepare the devices for triggering system wakeup
220events.
David Brownell4fc08402006-08-10 16:38:28 -0700221
222
223Call Sequence Guarantees
224------------------------
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100225To ensure that bridges and similar links needing to talk to a device are
David Brownell4fc08402006-08-10 16:38:28 -0700226available when the device is suspended or resumed, the device tree is
227walked in a bottom-up order to suspend devices. A top-down order is
228used to resume those devices.
229
230The ordering of the device tree is defined by the order in which devices
231get registered: a child can never be registered, probed or resumed before
232its parent; and can't be removed or suspended after that parent.
233
234The policy is that the device tree should match hardware bus topology.
235(Or at least the control bus, for devices which use multiple busses.)
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100236In particular, this means that a device registration may fail if the parent of
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100237the device is suspending (i.e. has been chosen by the PM core as the next
Rafael J. Wysocki58aca232008-03-12 00:57:22 +0100238device to suspend) or has already suspended, as well as after all of the other
239devices have been suspended. Device drivers must be prepared to cope with such
240situations.
David Brownell4fc08402006-08-10 16:38:28 -0700241
242
Alan Sternd6f9cda2010-03-26 23:53:55 +0100243System Power Management Phases
244------------------------------
245Suspending or resuming the system is done in several phases. Different phases
246are used for standby or memory sleep states ("suspend-to-RAM") and the
247hibernation state ("suspend-to-disk"). Each phase involves executing callbacks
248for every device before the next phase begins. Not all busses or classes
249support all these callbacks and not all drivers use all the callbacks. The
250various phases always run after tasks have been frozen and before they are
251unfrozen. Furthermore, the *_noirq phases run at a time when IRQ handlers have
252been disabled (except for those marked with the IRQ_WAKEUP flag).
David Brownell4fc08402006-08-10 16:38:28 -0700253
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100254All phases use bus, type, or class callbacks (that is, methods defined in
255dev->bus->pm, dev->type->pm, or dev->class->pm). These callbacks are mutually
256exclusive, so if the device type provides a struct dev_pm_ops object pointed to
257by its pm field (i.e. both dev->type and dev->type->pm are defined), the
258callbacks included in that object (i.e. dev->type->pm) will be used. Otherwise,
259if the class provides a struct dev_pm_ops object pointed to by its pm field
260(i.e. both dev->class and dev->class->pm are defined), the PM core will use the
261callbacks from that object (i.e. dev->class->pm). Finally, if the pm fields of
262both the device type and class objects are NULL (or those objects do not exist),
263the callbacks provided by the bus (that is, the callbacks from dev->bus->pm)
264will be used (this allows device types to override callbacks provided by bus
265types or classes if necessary).
David Brownell4fc08402006-08-10 16:38:28 -0700266
Alan Sternd6f9cda2010-03-26 23:53:55 +0100267These callbacks may in turn invoke device- or driver-specific methods stored in
268dev->driver->pm, but they don't have to.
David Brownell4fc08402006-08-10 16:38:28 -0700269
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100270
Alan Sternd6f9cda2010-03-26 23:53:55 +0100271Entering System Suspend
272-----------------------
273When the system goes into the standby or memory sleep state, the phases are:
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100274
Alan Sternd6f9cda2010-03-26 23:53:55 +0100275 prepare, suspend, suspend_noirq.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100276
Alan Sternd6f9cda2010-03-26 23:53:55 +0100277 1. The prepare phase is meant to prevent races by preventing new devices
278 from being registered; the PM core would never know that all the
279 children of a device had been suspended if new children could be
280 registered at will. (By contrast, devices may be unregistered at any
281 time.) Unlike the other suspend-related phases, during the prepare
282 phase the device tree is traversed top-down.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100283
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200284 After the prepare callback method returns, no new children may be
285 registered below the device. The method may also prepare the device or
286 driver in some way for the upcoming system power transition (for
287 example, by allocating additional memory required for this purpose), but
288 it should not put the device into a low-power state.
Alan Sternd6f9cda2010-03-26 23:53:55 +0100289
290 2. The suspend methods should quiesce the device to stop it from performing
291 I/O. They also may save the device registers and put it into the
292 appropriate low-power state, depending on the bus type the device is on,
293 and they may enable wakeup events.
294
295 3. The suspend_noirq phase occurs after IRQ handlers have been disabled,
296 which means that the driver's interrupt handler will not be called while
297 the callback method is running. The methods should save the values of
298 the device's registers that weren't saved previously and finally put the
299 device into the appropriate low-power state.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100300
301 The majority of subsystems and device drivers need not implement this
Alan Sternd6f9cda2010-03-26 23:53:55 +0100302 callback. However, bus types allowing devices to share interrupt
303 vectors, like PCI, generally need it; otherwise a driver might encounter
304 an error during the suspend phase by fielding a shared interrupt
305 generated by some other device after its own device had been set to low
306 power.
David Brownell4fc08402006-08-10 16:38:28 -0700307
Alan Sternd6f9cda2010-03-26 23:53:55 +0100308At the end of these phases, drivers should have stopped all I/O transactions
309(DMA, IRQs), saved enough state that they can re-initialize or restore previous
310state (as needed by the hardware), and placed the device into a low-power state.
311On many platforms they will gate off one or more clock sources; sometimes they
312will also switch off power supplies or reduce voltages. (Drivers supporting
313runtime PM may already have performed some or all of these steps.)
David Brownell4fc08402006-08-10 16:38:28 -0700314
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100315If device_may_wakeup(dev) returns true, the device should be prepared for
Alan Sternd6f9cda2010-03-26 23:53:55 +0100316generating hardware wakeup signals to trigger a system wakeup event when the
317system is in the sleep state. For example, enable_irq_wake() might identify
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100318GPIO signals hooked up to a switch or other external hardware, and
319pci_enable_wake() does something similar for the PCI PME signal.
David Brownell4fc08402006-08-10 16:38:28 -0700320
Alan Sternd6f9cda2010-03-26 23:53:55 +0100321If any of these callbacks returns an error, the system won't enter the desired
322low-power state. Instead the PM core will unwind its actions by resuming all
323the devices that were suspended.
David Brownell4fc08402006-08-10 16:38:28 -0700324
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100325
Alan Sternd6f9cda2010-03-26 23:53:55 +0100326Leaving System Suspend
327----------------------
328When resuming from standby or memory sleep, the phases are:
329
330 resume_noirq, resume, complete.
331
332 1. The resume_noirq callback methods should perform any actions needed
333 before the driver's interrupt handlers are invoked. This generally
334 means undoing the actions of the suspend_noirq phase. If the bus type
335 permits devices to share interrupt vectors, like PCI, the method should
336 bring the device and its driver into a state in which the driver can
337 recognize if the device is the source of incoming interrupts, if any,
338 and handle them correctly.
339
340 For example, the PCI bus type's ->pm.resume_noirq() puts the device into
341 the full-power state (D0 in the PCI terminology) and restores the
342 standard configuration registers of the device. Then it calls the
343 device driver's ->pm.resume_noirq() method to perform device-specific
344 actions.
345
346 2. The resume methods should bring the the device back to its operating
347 state, so that it can perform normal I/O. This generally involves
348 undoing the actions of the suspend phase.
349
350 3. The complete phase uses only a bus callback. The method should undo the
351 actions of the prepare phase. Note, however, that new children may be
352 registered below the device as soon as the resume callbacks occur; it's
353 not necessary to wait until the complete phase.
354
355At the end of these phases, drivers should be as functional as they were before
356suspending: I/O can be performed using DMA and IRQs, and the relevant clocks are
357gated on. Even if the device was in a low-power state before the system sleep
358because of runtime power management, afterwards it should be back in its
359full-power state. There are multiple reasons why it's best to do this; they are
360discussed in more detail in Documentation/power/runtime_pm.txt.
361
362However, the details here may again be platform-specific. For example,
363some systems support multiple "run" states, and the mode in effect at
364the end of resume might not be the one which preceded suspension.
365That means availability of certain clocks or power supplies changed,
366which could easily affect how a driver works.
367
368Drivers need to be able to handle hardware which has been reset since the
369suspend methods were called, for example by complete reinitialization.
370This may be the hardest part, and the one most protected by NDA'd documents
371and chip errata. It's simplest if the hardware state hasn't changed since
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300372the suspend was carried out, but that can't be guaranteed (in fact, it usually
Alan Sternd6f9cda2010-03-26 23:53:55 +0100373is not the case).
374
375Drivers must also be prepared to notice that the device has been removed
376while the system was powered down, whenever that's physically possible.
377PCMCIA, MMC, USB, Firewire, SCSI, and even IDE are common examples of busses
378where common Linux platforms will see such removal. Details of how drivers
379will notice and handle such removals are currently bus-specific, and often
380involve a separate thread.
381
382These callbacks may return an error value, but the PM core will ignore such
383errors since there's nothing it can do about them other than printing them in
384the system log.
385
386
387Entering Hibernation
388--------------------
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100389Hibernating the system is more complicated than putting it into the standby or
Alan Sternd6f9cda2010-03-26 23:53:55 +0100390memory sleep state, because it involves creating and saving a system image.
391Therefore there are more phases for hibernation, with a different set of
392callbacks. These phases always run after tasks have been frozen and memory has
393been freed.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100394
Alan Sternd6f9cda2010-03-26 23:53:55 +0100395The general procedure for hibernation is to quiesce all devices (freeze), create
396an image of the system memory while everything is stable, reactivate all
397devices (thaw), write the image to permanent storage, and finally shut down the
398system (poweroff). The phases used to accomplish this are:
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100399
Alan Sternd6f9cda2010-03-26 23:53:55 +0100400 prepare, freeze, freeze_noirq, thaw_noirq, thaw, complete,
401 prepare, poweroff, poweroff_noirq
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100402
Alan Sternd6f9cda2010-03-26 23:53:55 +0100403 1. The prepare phase is discussed in the "Entering System Suspend" section
404 above.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100405
Alan Sternd6f9cda2010-03-26 23:53:55 +0100406 2. The freeze methods should quiesce the device so that it doesn't generate
407 IRQs or DMA, and they may need to save the values of device registers.
408 However the device does not have to be put in a low-power state, and to
409 save time it's best not to do so. Also, the device should not be
410 prepared to generate wakeup events.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100411
Alan Sternd6f9cda2010-03-26 23:53:55 +0100412 3. The freeze_noirq phase is analogous to the suspend_noirq phase discussed
413 above, except again that the device should not be put in a low-power
414 state and should not be allowed to generate wakeup events.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100415
Alan Sternd6f9cda2010-03-26 23:53:55 +0100416At this point the system image is created. All devices should be inactive and
417the contents of memory should remain undisturbed while this happens, so that the
418image forms an atomic snapshot of the system state.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100419
Alan Sternd6f9cda2010-03-26 23:53:55 +0100420 4. The thaw_noirq phase is analogous to the resume_noirq phase discussed
421 above. The main difference is that its methods can assume the device is
422 in the same state as at the end of the freeze_noirq phase.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100423
Alan Sternd6f9cda2010-03-26 23:53:55 +0100424 5. The thaw phase is analogous to the resume phase discussed above. Its
425 methods should bring the device back to an operating state, so that it
426 can be used for saving the image if necessary.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100427
Alan Sternd6f9cda2010-03-26 23:53:55 +0100428 6. The complete phase is discussed in the "Leaving System Suspend" section
429 above.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100430
Alan Sternd6f9cda2010-03-26 23:53:55 +0100431At this point the system image is saved, and the devices then need to be
432prepared for the upcoming system shutdown. This is much like suspending them
433before putting the system into the standby or memory sleep state, and the phases
434are similar.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100435
Alan Sternd6f9cda2010-03-26 23:53:55 +0100436 7. The prepare phase is discussed above.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100437
Alan Sternd6f9cda2010-03-26 23:53:55 +0100438 8. The poweroff phase is analogous to the suspend phase.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100439
Alan Sternd6f9cda2010-03-26 23:53:55 +0100440 9. The poweroff_noirq phase is analogous to the suspend_noirq phase.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100441
Alan Sternd6f9cda2010-03-26 23:53:55 +0100442The poweroff and poweroff_noirq callbacks should do essentially the same things
443as the suspend and suspend_noirq callbacks. The only notable difference is that
444they need not store the device register values, because the registers should
445already have been stored during the freeze or freeze_noirq phases.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100446
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100447
Alan Sternd6f9cda2010-03-26 23:53:55 +0100448Leaving Hibernation
449-------------------
450Resuming from hibernation is, again, more complicated than resuming from a sleep
451state in which the contents of main memory are preserved, because it requires
452a system image to be loaded into memory and the pre-hibernation memory contents
453to be restored before control can be passed back to the image kernel.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100454
Alan Sternd6f9cda2010-03-26 23:53:55 +0100455Although in principle, the image might be loaded into memory and the
456pre-hibernation memory contents restored by the boot loader, in practice this
457can't be done because boot loaders aren't smart enough and there is no
458established protocol for passing the necessary information. So instead, the
459boot loader loads a fresh instance of the kernel, called the boot kernel, into
460memory and passes control to it in the usual way. Then the boot kernel reads
461the system image, restores the pre-hibernation memory contents, and passes
462control to the image kernel. Thus two different kernels are involved in
463resuming from hibernation. In fact, the boot kernel may be completely different
464from the image kernel: a different configuration and even a different version.
465This has important consequences for device drivers and their subsystems.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100466
Alan Sternd6f9cda2010-03-26 23:53:55 +0100467To be able to load the system image into memory, the boot kernel needs to
468include at least a subset of device drivers allowing it to access the storage
469medium containing the image, although it doesn't need to include all of the
470drivers present in the image kernel. After the image has been loaded, the
471devices managed by the boot kernel need to be prepared for passing control back
472to the image kernel. This is very similar to the initial steps involved in
473creating a system image, and it is accomplished in the same way, using prepare,
474freeze, and freeze_noirq phases. However the devices affected by these phases
475are only those having drivers in the boot kernel; other devices will still be in
476whatever state the boot loader left them.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100477
Alan Sternd6f9cda2010-03-26 23:53:55 +0100478Should the restoration of the pre-hibernation memory contents fail, the boot
479kernel would go through the "thawing" procedure described above, using the
480thaw_noirq, thaw, and complete phases, and then continue running normally. This
481happens only rarely. Most often the pre-hibernation memory contents are
482restored successfully and control is passed to the image kernel, which then
483becomes responsible for bringing the system back to the working state.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100484
Alan Sternd6f9cda2010-03-26 23:53:55 +0100485To achieve this, the image kernel must restore the devices' pre-hibernation
486functionality. The operation is much like waking up from the memory sleep
487state, although it involves different phases:
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100488
Alan Sternd6f9cda2010-03-26 23:53:55 +0100489 restore_noirq, restore, complete
490
491 1. The restore_noirq phase is analogous to the resume_noirq phase.
492
493 2. The restore phase is analogous to the resume phase.
494
495 3. The complete phase is discussed above.
496
497The main difference from resume[_noirq] is that restore[_noirq] must assume the
498device has been accessed and reconfigured by the boot loader or the boot kernel.
499Consequently the state of the device may be different from the state remembered
500from the freeze and freeze_noirq phases. The device may even need to be reset
501and completely re-initialized. In many cases this difference doesn't matter, so
502the resume[_noirq] and restore[_norq] method pointers can be set to the same
503routines. Nevertheless, different callback pointers are used in case there is a
504situation where it actually matters.
505
506
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200507Device Power Management Domains
508-------------------------------
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100509Sometimes devices share reference clocks or other power resources. In those
510cases it generally is not possible to put devices into low-power states
511individually. Instead, a set of devices sharing a power resource can be put
512into a low-power state together at the same time by turning off the shared
513power resource. Of course, they also need to be put into the full-power state
514together, by turning the shared power resource on. A set of devices with this
515property is often referred to as a power domain.
516
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200517Support for power domains is provided through the pm_domain field of struct
518device. This field is a pointer to an object of type struct dev_pm_domain,
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100519defined in include/linux/pm.h, providing a set of power management callbacks
520analogous to the subsystem-level and device driver callbacks that are executed
Rafael J. Wysockica9c6892011-06-21 23:25:32 +0200521for the given device during all power transitions, instead of the respective
522subsystem-level callbacks. Specifically, if a device's pm_domain pointer is
523not NULL, the ->suspend() callback from the object pointed to by it will be
524executed instead of its subsystem's (e.g. bus type's) ->suspend() callback and
525anlogously for all of the remaining callbacks. In other words, power management
526domain callbacks, if defined for the given device, always take precedence over
527the callbacks provided by the device's subsystem (e.g. bus type).
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100528
Rafael J. Wysockica9c6892011-06-21 23:25:32 +0200529The support for device power management domains is only relevant to platforms
530needing to use the same device driver power management callbacks in many
531different power domain configurations and wanting to avoid incorporating the
532support for power domains into subsystem-level callbacks, for example by
533modifying the platform bus type. Other platforms need not implement it or take
534it into account in any way.
Rafael J. Wysocki7538e3d2011-02-16 21:53:17 +0100535
536
David Brownell4fc08402006-08-10 16:38:28 -0700537Device Low Power (suspend) States
538---------------------------------
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100539Device low-power states aren't standard. One device might only handle
David Brownell4fc08402006-08-10 16:38:28 -0700540"on" and "off, while another might support a dozen different versions of
541"on" (how many engines are active?), plus a state that gets back to "on"
542faster than from a full "off".
543
544Some busses define rules about what different suspend states mean. PCI
545gives one example: after the suspend sequence completes, a non-legacy
546PCI device may not perform DMA or issue IRQs, and any wakeup events it
547issues would be issued through the PME# bus signal. Plus, there are
548several PCI-standard device states, some of which are optional.
549
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100550In contrast, integrated system-on-chip processors often use IRQs as the
David Brownell4fc08402006-08-10 16:38:28 -0700551wakeup event sources (so drivers would call enable_irq_wake) and might
552be able to treat DMA completion as a wakeup event (sometimes DMA can stay
553active too, it'd only be the CPU and some peripherals that sleep).
554
555Some details here may be platform-specific. Systems may have devices that
556can be fully active in certain sleep states, such as an LCD display that's
557refreshed using DMA while most of the system is sleeping lightly ... and
558its frame buffer might even be updated by a DSP or other non-Linux CPU while
559the Linux control processor stays idle.
560
561Moreover, the specific actions taken may depend on the target system state.
562One target system state might allow a given device to be very operational;
563another might require a hard shut down with re-initialization on resume.
564And two different target systems might use the same device in different
565ways; the aforementioned LCD might be active in one product's "standby",
566but a different product using the same SOC might work differently.
567
568
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100569Power Management Notifiers
570--------------------------
Alan Sternd6f9cda2010-03-26 23:53:55 +0100571There are some operations that cannot be carried out by the power management
572callbacks discussed above, because the callbacks occur too late or too early.
573To handle these cases, subsystems and device drivers may register power
574management notifiers that are called before tasks are frozen and after they have
575been thawed. Generally speaking, the PM notifiers are suitable for performing
576actions that either require user space to be available, or at least won't
577interfere with user space.
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100578
579For details refer to Documentation/power/notifiers.txt.
580
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582Runtime Power Management
David Brownell4fc08402006-08-10 16:38:28 -0700583========================
584Many devices are able to dynamically power down while the system is still
585running. This feature is useful for devices that are not being used, and
586can offer significant power savings on a running system. These devices
587often support a range of runtime power states, which might use names such
588as "off", "sleep", "idle", "active", and so on. Those states will in some
Alan Sternd6f9cda2010-03-26 23:53:55 +0100589cases (like PCI) be partially constrained by the bus the device uses, and will
David Brownell4fc08402006-08-10 16:38:28 -0700590usually include hardware states that are also used in system sleep states.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Alan Sternd6f9cda2010-03-26 23:53:55 +0100592A system-wide power transition can be started while some devices are in low
593power states due to runtime power management. The system sleep PM callbacks
594should recognize such situations and react to them appropriately, but the
595necessary actions are subsystem-specific.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Alan Sternd6f9cda2010-03-26 23:53:55 +0100597In some cases the decision may be made at the subsystem level while in other
598cases the device driver may be left to decide. In some cases it may be
599desirable to leave a suspended device in that state during a system-wide power
600transition, but in other cases the device must be put back into the full-power
601state temporarily, for example so that its system wakeup capability can be
602disabled. This all depends on the hardware and the design of the subsystem and
603device driver in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Rafael J. Wysocki455716e2011-07-01 22:29:05 +0200605During system-wide resume from a sleep state it's easiest to put devices into
606the full-power state, as explained in Documentation/power/runtime_pm.txt. Refer
607to that document for more information regarding this particular issue as well as
Rafael J. Wysocki624f6ec2010-03-26 23:53:42 +0100608for information on the device runtime power management framework in general.