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