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