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