devices.rst 46 KB

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