devices.txt 36 KB

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