devices.txt 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. Device Power Management
  2. (C) 2010 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
  3. Most of the code in Linux is device drivers, so most of the Linux power
  4. management code is also driver-specific. Most drivers will do very little;
  5. others, especially for platforms with small batteries (like cell phones),
  6. will do a lot.
  7. This writeup gives an overview of how drivers interact with system-wide
  8. power management goals, emphasizing the models and interfaces that are
  9. shared by everything that hooks up to the driver model core. Read it as
  10. background for the domain-specific work you'd do with any specific driver.
  11. Two Models for Device Power Management
  12. ======================================
  13. Drivers will use one or both of these models to put devices into low-power
  14. states:
  15. System Sleep model:
  16. Drivers can enter low power states as part of entering system-wide
  17. low-power states like "suspend-to-ram", or (mostly for systems with
  18. disks) "hibernate" (suspend-to-disk).
  19. This is something that device, bus, and class drivers collaborate on
  20. by implementing various role-specific suspend and resume methods to
  21. cleanly power down hardware and software subsystems, then reactivate
  22. them without loss of data.
  23. Some drivers can manage hardware wakeup events, which make the system
  24. leave that low-power state. This feature may be enabled or disabled
  25. using the relevant /sys/devices/.../power/wakeup file (for Ethernet
  26. drivers the ioctl interface used by ethtool may also be used for this
  27. purpose); enabling it may cost some power usage, but let the whole
  28. system enter low power states more often.
  29. Runtime Power Management model:
  30. Devices may also be put into low power states while the system is
  31. running, independently of other power management activity in principle.
  32. However, devices are not generally independent of each other (for
  33. example, parent device cannot be suspended unless all of its child
  34. devices have been suspended). Moreover, depending on the bus type the
  35. device is on, it may be necessary to carry out some bus-specific
  36. operations on the device for this purpose. Also, devices put into low
  37. power states at run time may require special handling during system-wide
  38. power transitions, like suspend to RAM.
  39. For these reasons not only the device driver itself, but also the
  40. appropriate subsystem (bus type, device type or device class) driver
  41. and the PM core are involved in the runtime power management of devices.
  42. Like in the system sleep power management case, they need to collaborate
  43. by implementing various role-specific suspend and resume methods, so
  44. that the hardware is cleanly powered down and reactivated without data
  45. or service loss.
  46. There's not a lot to be said about those low power states except that they
  47. are very system-specific, and often device-specific. Also, that if enough
  48. devices have been put into low power states (at "run time"), the effect may be
  49. very similar to entering some system-wide low-power state (system sleep) ... and
  50. that synergies exist, so that several drivers using runtime PM might put the
  51. system into a state where even deeper power saving options are available.
  52. Most suspended devices will have quiesced all I/O: no more DMA or IRQs, no
  53. more data read or written, and requests from upstream drivers are no longer
  54. accepted. A given bus or platform may have different requirements though.
  55. Examples of hardware wakeup events include an alarm from a real time clock,
  56. network wake-on-LAN packets, keyboard or mouse activity, and media insertion
  57. or removal (for PCMCIA, MMC/SD, USB, and so on).
  58. Interfaces for Entering System Sleep States
  59. ===========================================
  60. There are programming interfaces provided for subsystem (bus type, device type,
  61. device class) and device drivers in order to allow them to participate in the
  62. power management of devices they are concerned with. They cover the system
  63. sleep power management as well as the runtime power management of devices.
  64. Device Power Management Operations
  65. ----------------------------------
  66. Device power management operations, at the subsystem level as well as at the
  67. device driver level, are implemented by defining and populating objects of type
  68. struct dev_pm_ops:
  69. struct dev_pm_ops {
  70. int (*prepare)(struct device *dev);
  71. void (*complete)(struct device *dev);
  72. int (*suspend)(struct device *dev);
  73. int (*resume)(struct device *dev);
  74. int (*freeze)(struct device *dev);
  75. int (*thaw)(struct device *dev);
  76. int (*poweroff)(struct device *dev);
  77. int (*restore)(struct device *dev);
  78. int (*suspend_noirq)(struct device *dev);
  79. int (*resume_noirq)(struct device *dev);
  80. int (*freeze_noirq)(struct device *dev);
  81. int (*thaw_noirq)(struct device *dev);
  82. int (*poweroff_noirq)(struct device *dev);
  83. int (*restore_noirq)(struct device *dev);
  84. int (*runtime_suspend)(struct device *dev);
  85. int (*runtime_resume)(struct device *dev);
  86. int (*runtime_idle)(struct device *dev);
  87. };
  88. This structure is defined in include/linux/pm.h and the methods included in it
  89. are also described in that file. Their roles will be explained in what follows.
  90. For now, it should be sufficient to remember that the last three of them are
  91. specific to runtime power management, while the remaining ones are used during
  92. system-wide power transitions.
  93. There also is an "old" or "legacy", deprecated way of implementing power
  94. management operations available at least for some subsystems. This approach
  95. does not use struct dev_pm_ops objects and it only is suitable for implementing
  96. system sleep power management methods. Therefore it is not described in this
  97. document, so please refer directly to the source code for more information about
  98. it.
  99. Subsystem-Level Methods
  100. -----------------------
  101. The core methods to suspend and resume devices reside in struct dev_pm_ops
  102. pointed to by the pm member of struct bus_type, struct device_type and
  103. struct class. They are mostly of interest to the people writing infrastructure
  104. for buses, like PCI or USB, or device type and device class drivers.
  105. Bus drivers implement these methods as appropriate for the hardware and
  106. the drivers using it; PCI works differently from USB, and so on. Not many
  107. people write subsystem-level drivers; most driver code is a "device driver" that
  108. builds on top of bus-specific framework code.
  109. For more information on these driver calls, see the description later;
  110. they are called in phases for every device, respecting the parent-child
  111. sequencing in the driver model tree.
  112. /sys/devices/.../power/wakeup files
  113. -----------------------------------
  114. All devices in the driver model have two flags to control handling of
  115. wakeup events, which are hardware signals that can force the device and/or
  116. system out of a low power state. These are initialized by bus or device
  117. driver code using device_init_wakeup().
  118. The "can_wakeup" flag just records whether the device (and its driver) can
  119. physically support wakeup events. When that flag is clear, the sysfs
  120. "wakeup" file is empty, and device_may_wakeup() returns false.
  121. For devices that can issue wakeup events, a separate flag controls whether
  122. that device should try to use its wakeup mechanism. The initial value of
  123. device_may_wakeup() will be false for the majority of devices, except for
  124. power buttons, keyboards, and Ethernet adapters whose WoL (wake-on-LAN) feature
  125. has been set up with ethtool. Thus in the majority of cases the device's
  126. "wakeup" file will initially hold the value "disabled". Userspace can change
  127. that to "enabled", so that device_may_wakeup() returns true, or change it back
  128. to "disabled", so that it returns false again.
  129. /sys/devices/.../power/control files
  130. ------------------------------------
  131. All devices in the driver model have a flag to control the desired behavior of
  132. its driver with respect to runtime power management. This flag, called
  133. runtime_auto, is initialized by the bus type (or generally subsystem) code using
  134. pm_runtime_allow() or pm_runtime_forbid(), depending on whether or not the
  135. driver is supposed to power manage the device at run time by default,
  136. respectively.
  137. This setting may be adjusted by user space by writing either "on" or "auto" to
  138. the device's "control" file. If "auto" is written, the device's runtime_auto
  139. flag will be set and the driver will be allowed to power manage the device if
  140. capable of doing that. If "on" is written, the driver is not allowed to power
  141. manage the device which in turn is supposed to remain in the full power state at
  142. run time. User space can check the current value of the runtime_auto flag by
  143. reading from the device's "control" file.
  144. The device's runtime_auto flag has no effect on the handling of system-wide
  145. power transitions by its driver. In particular, the device can (and in the
  146. majority of cases should and will) be put into a low power state during a
  147. system-wide transition to a sleep state (like "suspend-to-RAM") even though its
  148. runtime_auto flag is unset (in which case its "control" file contains "on").
  149. For more information about the runtime power management framework for devices
  150. refer to Documentation/power/runtime_pm.txt.
  151. Calling Drivers to Enter System Sleep States
  152. ============================================
  153. When the system goes into a sleep state, each device's driver is asked
  154. to suspend the device by putting it into state compatible with the target
  155. system state. That's usually some version of "off", but the details are
  156. system-specific. Also, wakeup-enabled devices will usually stay partly
  157. functional in order to wake the system.
  158. When the system leaves that low power state, the device's driver is asked
  159. to resume it. The suspend and resume operations always go together, and
  160. both are multi-phase operations.
  161. For simple drivers, suspend might quiesce the device using the class code
  162. and then turn its hardware as "off" as possible with late_suspend. The
  163. matching resume calls would then completely reinitialize the hardware
  164. before reactivating its class I/O queues.
  165. More power-aware drivers might prepare the devices for triggering system wakeup
  166. events.
  167. Call Sequence Guarantees
  168. ------------------------
  169. To ensure that bridges and similar links needing to talk to a device are
  170. available when the device is suspended or resumed, the device tree is
  171. walked in a bottom-up order to suspend devices. A top-down order is
  172. used to resume those devices.
  173. The ordering of the device tree is defined by the order in which devices
  174. get registered: a child can never be registered, probed or resumed before
  175. its parent; and can't be removed or suspended after that parent.
  176. The policy is that the device tree should match hardware bus topology.
  177. (Or at least the control bus, for devices which use multiple busses.)
  178. In particular, this means that a device registration may fail if the parent of
  179. the device is suspending (i.e. has been chosen by the PM core as the next
  180. device to suspend) or has already suspended, as well as after all of the other
  181. devices have been suspended. Device drivers must be prepared to cope with such
  182. situations.
  183. Suspending Devices
  184. ------------------
  185. Suspending a given device is done in several phases. Suspending the
  186. system always includes every phase, executing calls for every device
  187. before the next phase begins. Not all busses or classes support all
  188. these callbacks; and not all drivers use all the callbacks.
  189. Generally, different callbacks are used depending on whether the system is
  190. going to the standby or memory sleep state ("suspend-to-RAM") or it is going to
  191. be hibernated ("suspend-to-disk").
  192. If the system goes to the standby or memory sleep state the phases are seen by
  193. driver notifications issued in this order:
  194. 1 bus->pm.prepare(dev) is called after tasks are frozen and it is supposed
  195. to call the device driver's ->pm.prepare() method.
  196. The purpose of this method is mainly to prevent new children of the
  197. device from being registered after it has returned. It also may be used
  198. to generally prepare the device for the upcoming system transition, but
  199. it should not put the device into a low power state.
  200. 2 class->pm.suspend(dev) is called if dev is associated with a class that
  201. has such a method. It may invoke the device driver's ->pm.suspend()
  202. method, unless type->pm.suspend(dev) or bus->pm.suspend() does that.
  203. 3 type->pm.suspend(dev) is called if dev is associated with a device type
  204. that has such a method. It may invoke the device driver's
  205. ->pm.suspend() method, unless class->pm.suspend(dev) or
  206. bus->pm.suspend() does that.
  207. 4 bus->pm.suspend(dev) is called, if implemented. It usually calls the
  208. device driver's ->pm.suspend() method.
  209. This call should generally quiesce the device so that it doesn't do any
  210. I/O after the call has returned. It also may save the device registers
  211. and put it into the appropriate low power state, depending on the bus
  212. type the device is on.
  213. 5 bus->pm.suspend_noirq(dev) is called, if implemented. It may call the
  214. device driver's ->pm.suspend_noirq() method, depending on the bus type
  215. in question.
  216. This method is invoked after device interrupts have been suspended,
  217. which means that the driver's interrupt handler will not be called
  218. while it is running. It should save the values of the device's
  219. registers that weren't saved previously and finally put the device into
  220. the appropriate low power state.
  221. The majority of subsystems and device drivers need not implement this
  222. method. However, bus types allowing devices to share interrupt vectors,
  223. like PCI, generally need to use it to prevent interrupt handling issues
  224. from happening during suspend.
  225. At the end of those phases, drivers should normally have stopped all I/O
  226. transactions (DMA, IRQs), saved enough state that they can re-initialize
  227. or restore previous state (as needed by the hardware), and placed the
  228. device into a low-power state. On many platforms they will also use
  229. gate off one or more clock sources; sometimes they will also switch off power
  230. supplies, or reduce voltages. [Drivers supporting runtime PM may already have
  231. performed some or all of the steps needed to prepare for the upcoming system
  232. state transition.]
  233. If device_may_wakeup(dev) returns true, the device should be prepared for
  234. generating hardware wakeup signals when the system is in the sleep state to
  235. trigger a system wakeup event. For example, enable_irq_wake() might identify
  236. GPIO signals hooked up to a switch or other external hardware, and
  237. pci_enable_wake() does something similar for the PCI PME signal.
  238. If a driver (or subsystem) fails it suspend method, the system won't enter the
  239. desired low power state; it will resume all the devices it's suspended so far.
  240. Hibernation Phases
  241. ------------------
  242. Hibernating the system is more complicated than putting it into the standby or
  243. memory sleep state, because it involves creating a system image and saving it.
  244. Therefore there are more phases of hibernation and special device PM methods are
  245. used in this case.
  246. First, it is necessary to prepare the system for creating a hibernation image.
  247. This is similar to putting the system into the standby or memory sleep state,
  248. although it generally doesn't require that devices be put into low power states
  249. (that is even not desirable at this point). Driver notifications are then
  250. issued in the following order:
  251. 1 bus->pm.prepare(dev) is called after tasks have been frozen and enough
  252. memory has been freed.
  253. 2 class->pm.freeze(dev) is called if implemented. It may invoke the
  254. device driver's ->pm.freeze() method, unless type->pm.freeze(dev) or
  255. bus->pm.freeze() does that.
  256. 3 type->pm.freeze(dev) is called if implemented. It may invoke the device
  257. driver's ->pm.suspend() method, unless class->pm.freeze(dev) or
  258. bus->pm.freeze() does that.
  259. 4 bus->pm.freeze(dev) is called, if implemented. It usually calls the
  260. device driver's ->pm.freeze() method.
  261. 5 bus->pm.freeze_noirq(dev) is called, if implemented. It may call the
  262. device driver's ->pm.freeze_noirq() method, depending on the bus type
  263. in question.
  264. The difference between ->pm.freeze() and the corresponding ->pm.suspend() (and
  265. similarly for the "noirq" variants) is that the former should avoid preparing
  266. devices to trigger system wakeup events and putting devices into low power
  267. states, although they generally have to save the values of device registers
  268. so that it's possible to restore them during system resume.
  269. Second, after the system image has been created, the functionality of devices
  270. has to be restored so that the image can be saved. That is similar to resuming
  271. devices after the system has been woken up from the standby or memory sleep
  272. state, which is described below, and causes the following device notifications
  273. to be issued:
  274. 1 bus->pm.thaw_noirq(dev), if implemented; may call the device driver's
  275. ->pm.thaw_noirq() method, depending on the bus type in question.
  276. 2 bus->pm.thaw(dev), if implemented; usually calls the device driver's
  277. ->pm.thaw() method.
  278. 3 type->pm.thaw(dev), if implemented; may call the device driver's
  279. ->pm.thaw() method if not called by the bus type or class.
  280. 4 class->pm.thaw(dev), if implemented; may call the device driver's
  281. ->pm.thaw() method if not called by the bus type or device type.
  282. 5 bus->pm.complete(dev), if implemented; may call the device driver's
  283. ->pm.complete() method.
  284. Generally, the role of the ->pm.thaw() methods (including the "noirq" variants)
  285. is to bring the device back to the fully functional state, so that it may be
  286. used for saving the image, if necessary. The role of bus->pm.complete() is to
  287. reverse whatever bus->pm.prepare() did (likewise for the analogous device driver
  288. callbacks).
  289. After the image has been saved, the devices need to be prepared for putting the
  290. system into the low power state. That is analogous to suspending them before
  291. putting the system into the standby or memory sleep state and involves the
  292. following device notifications:
  293. 1 bus->pm.prepare(dev).
  294. 2 class->pm.poweroff(dev), if implemented; may invoke the device driver's
  295. ->pm.poweroff() method if not called by the bus type or device type.
  296. 3 type->pm.poweroff(dev), if implemented; may invoke the device driver's
  297. ->pm.poweroff() method if not called by the bus type or device class.
  298. 4 bus->pm.poweroff(dev), if implemented; usually calls the device driver's
  299. ->pm.poweroff() method (if not called by the device class or type).
  300. 5 bus->pm.poweroff_noirq(dev), if implemented; may call the device
  301. driver's ->pm.poweroff_noirq() method, depending on the bus type
  302. in question.
  303. The difference between ->pm.poweroff() and the corresponding ->pm.suspend() (and
  304. analogously for the "noirq" variants) is that the former need not save the
  305. device's registers. Still, they should prepare the device for triggering
  306. system wakeup events if necessary and finally put it into the appropriate low
  307. power state.
  308. Device Low Power (suspend) States
  309. ---------------------------------
  310. Device low-power states aren't standard. One device might only handle
  311. "on" and "off, while another might support a dozen different versions of
  312. "on" (how many engines are active?), plus a state that gets back to "on"
  313. faster than from a full "off".
  314. Some busses define rules about what different suspend states mean. PCI
  315. gives one example: after the suspend sequence completes, a non-legacy
  316. PCI device may not perform DMA or issue IRQs, and any wakeup events it
  317. issues would be issued through the PME# bus signal. Plus, there are
  318. several PCI-standard device states, some of which are optional.
  319. In contrast, integrated system-on-chip processors often use IRQs as the
  320. wakeup event sources (so drivers would call enable_irq_wake) and might
  321. be able to treat DMA completion as a wakeup event (sometimes DMA can stay
  322. active too, it'd only be the CPU and some peripherals that sleep).
  323. Some details here may be platform-specific. Systems may have devices that
  324. can be fully active in certain sleep states, such as an LCD display that's
  325. refreshed using DMA while most of the system is sleeping lightly ... and
  326. its frame buffer might even be updated by a DSP or other non-Linux CPU while
  327. the Linux control processor stays idle.
  328. Moreover, the specific actions taken may depend on the target system state.
  329. One target system state might allow a given device to be very operational;
  330. another might require a hard shut down with re-initialization on resume.
  331. And two different target systems might use the same device in different
  332. ways; the aforementioned LCD might be active in one product's "standby",
  333. but a different product using the same SOC might work differently.
  334. Resuming Devices
  335. ----------------
  336. Resuming is done in multiple phases, much like suspending, with all
  337. devices processing each phase's calls before the next phase begins.
  338. Again, however, different callbacks are used depending on whether the system is
  339. waking up from the standby or memory sleep state ("suspend-to-RAM") or from
  340. hibernation ("suspend-to-disk").
  341. If the system is waking up from the standby or memory sleep state, the phases
  342. are seen by driver notifications issued in this order:
  343. 1 bus->pm.resume_noirq(dev) is called, if implemented. It may call the
  344. device driver's ->pm.resume_noirq() method, depending on the bus type in
  345. question.
  346. The role of this method is to perform actions that need to be performed
  347. before device drivers' interrupt handlers are allowed to be invoked. If
  348. the given bus type permits devices to share interrupt vectors, like PCI,
  349. this method should bring the device and its driver into a state in which
  350. the driver can recognize if the device is the source of incoming
  351. interrupts, if any, and handle them correctly.
  352. For example, the PCI bus type's ->pm.resume_noirq() puts the device into
  353. the full power state (D0 in the PCI terminology) and restores the
  354. standard configuration registers of the device. Then, it calls the
  355. device driver's ->pm.resume_noirq() method to perform device-specific
  356. actions needed at this stage of resume.
  357. 2 bus->pm.resume(dev) is called, if implemented. It usually calls the
  358. device driver's ->pm.resume() method.
  359. This call should generally bring the the device back to the working
  360. state, so that it can do I/O as requested after the call has returned.
  361. However, it may be more convenient to use the device class or device
  362. type ->pm.resume() for this purpose, in which case the bus type's
  363. ->pm.resume() method need not be implemented at all.
  364. 3 type->pm.resume(dev) is called, if implemented. It may invoke the
  365. device driver's ->pm.resume() method, unless class->pm.resume(dev) or
  366. bus->pm.resume() does that.
  367. For devices that are not associated with any bus type or device class
  368. this method plays the role of bus->pm.resume().
  369. 4 class->pm.resume(dev) is called, if implemented. It may invoke the
  370. device driver's ->pm.resume() method, unless bus->pm.resume(dev) or
  371. type->pm.resume() does that.
  372. For devices that are not associated with any bus type or device type
  373. this method plays the role of bus->pm.resume().
  374. 5 bus->pm.complete(dev) is called, if implemented. It is supposed to
  375. invoke the device driver's ->pm.complete() method.
  376. The role of this method is to reverse whatever bus->pm.prepare(dev)
  377. (or the driver's ->pm.prepare()) did during suspend, if necessary.
  378. At the end of those phases, drivers should normally be as functional as
  379. they were before suspending: I/O can be performed using DMA and IRQs, and
  380. the relevant clocks are gated on. In principle the device need not be
  381. "fully on"; it might be in a runtime lowpower/suspend state during suspend and
  382. the resume callbacks may try to restore that state, but that need not be
  383. desirable from the user's point of view. In fact, there are multiple reasons
  384. why it's better to always put devices into the "fully working" state in the
  385. system sleep resume callbacks and they are discussed in more detail in
  386. Documentation/power/runtime_pm.txt.
  387. However, the details here may again be platform-specific. For example,
  388. some systems support multiple "run" states, and the mode in effect at
  389. the end of resume might not be the one which preceded suspension.
  390. That means availability of certain clocks or power supplies changed,
  391. which could easily affect how a driver works.
  392. Drivers need to be able to handle hardware which has been reset since the
  393. suspend methods were called, for example by complete reinitialization.
  394. This may be the hardest part, and the one most protected by NDA'd documents
  395. and chip errata. It's simplest if the hardware state hasn't changed since
  396. the suspend was carried out, but that can't be guaranteed (in fact, it ususally
  397. is not the case).
  398. Drivers must also be prepared to notice that the device has been removed
  399. while the system was powered off, whenever that's physically possible.
  400. PCMCIA, MMC, USB, Firewire, SCSI, and even IDE are common examples of busses
  401. where common Linux platforms will see such removal. Details of how drivers
  402. will notice and handle such removals are currently bus-specific, and often
  403. involve a separate thread.
  404. Resume From Hibernation
  405. -----------------------
  406. Resuming from hibernation is, again, more complicated than resuming from a sleep
  407. state in which the contents of main memory are preserved, because it requires
  408. a system image to be loaded into memory and the pre-hibernation memory contents
  409. to be restored before control can be passed back to the image kernel.
  410. In principle, the image might be loaded into memory and the pre-hibernation
  411. memory contents might be restored by the boot loader. For this purpose,
  412. however, the boot loader would need to know the image kernel's entry point and
  413. there's no protocol defined for passing that information to boot loaders. As
  414. a workaround, the boot loader loads a fresh instance of the kernel, called the
  415. boot kernel, into memory and passes control to it in a usual way. Then, the
  416. boot kernel reads the hibernation image, restores the pre-hibernation memory
  417. contents and passes control to the image kernel. Thus, in fact, two different
  418. kernels are involved in resuming from hibernation and in general they are not
  419. only different because they play different roles in this operation. Actually,
  420. the boot kernel may be completely different from the image kernel. Not only
  421. the configuration of it, but also the version of it may be different.
  422. The consequences of this are important to device drivers and their subsystems
  423. (bus types, device classes and device types) too.
  424. Namely, to be able to load the hibernation image into memory, the boot kernel
  425. needs to include at least the subset of device drivers allowing it to access the
  426. storage medium containing the image, although it generally doesn't need to
  427. include all of the drivers included into the image kernel. After the image has
  428. been loaded the devices handled by those drivers need to be prepared for passing
  429. control back to the image kernel. This is very similar to the preparation of
  430. devices for creating a hibernation image described above. In fact, it is done
  431. in the same way, with the help of the ->pm.prepare(), ->pm.freeze() and
  432. ->pm.freeze_noirq() callbacks, but only for device drivers included in the boot
  433. kernel (whose versions may generally be different from the versions of the
  434. analogous drivers from the image kernel).
  435. Should the restoration of the pre-hibernation memory contents fail, the boot
  436. kernel would carry out the procedure of "thawing" devices described above, using
  437. the ->pm.thaw_noirq(), ->pm.thaw(), and ->pm.complete() callbacks provided by
  438. subsystems and device drivers. This, however, is a very rare condition. Most
  439. often the pre-hibernation memory contents are restored successfully and control
  440. is passed to the image kernel that is now responsible for bringing the system
  441. back to the working state.
  442. To achieve this goal, among other things, the image kernel restores the
  443. pre-hibernation functionality of devices. This operation is analogous to the
  444. resuming of devices after waking up from the memory sleep state, although it
  445. involves different device notifications which are the following:
  446. 1 bus->pm.restore_noirq(dev), if implemented; may call the device driver's
  447. ->pm.restore_noirq() method, depending on the bus type in question.
  448. 2 bus->pm.restore(dev), if implemented; usually calls the device driver's
  449. ->pm.restore() method.
  450. 3 type->pm.restore(dev), if implemented; may call the device driver's
  451. ->pm.restore() method if not called by the bus type or class.
  452. 4 class->pm.restore(dev), if implemented; may call the device driver's
  453. ->pm.restore() method if not called by the bus type or device type.
  454. 5 bus->pm.complete(dev), if implemented; may call the device driver's
  455. ->pm.complete() method.
  456. The roles of the ->pm.restore_noirq() and ->pm.restore() callbacks are analogous
  457. to the roles of the corresponding resume callbacks, but they must assume that
  458. the device may have been accessed before by the boot kernel. Consequently, the
  459. state of the device before they are called may be different from the state of it
  460. right prior to calling the resume callbacks. That difference usually doesn't
  461. matter, so the majority of device drivers can set their resume and restore
  462. callback pointers to the same routine. Nevertheless, different callback
  463. pointers are used in case there is a situation where it actually matters.
  464. System Devices
  465. --------------
  466. System devices follow a slightly different API, which can be found in
  467. include/linux/sysdev.h
  468. drivers/base/sys.c
  469. System devices will only be suspended with interrupts disabled, and after
  470. all other devices have been suspended. On resume, they will be resumed
  471. before any other devices, and also with interrupts disabled.
  472. That is, when the non-boot CPUs are all offline and IRQs are disabled on the
  473. remaining online CPU, then the sysdev_driver.suspend() phase is carried out, and
  474. the system enters a sleep state (or hibernation image is created). During
  475. resume (or after the image has been created) the sysdev_driver.resume() phase
  476. is carried out, IRQs are enabled on the only online CPU, the non-boot CPUs are
  477. enabled and that is followed by the "early resume" phase (in which the "noirq"
  478. callbacks provided by subsystems and device drivers are invoked).
  479. Code to actually enter and exit the system-wide low power state sometimes
  480. involves hardware details that are only known to the boot firmware, and
  481. may leave a CPU running software (from SRAM or flash memory) that monitors
  482. the system and manages its wakeup sequence.
  483. Power Management Notifiers
  484. --------------------------
  485. As stated in Documentation/power/notifiers.txt, there are some operations that
  486. cannot be carried out by the power management callbacks discussed above, because
  487. carrying them out at these points would be too late or too early. To handle
  488. these cases subsystems and device drivers may register power management
  489. notifiers that are called before tasks are frozen and after they have been
  490. thawed.
  491. Generally speaking, the PM notifiers are suitable for performing actions that
  492. either require user space to be available, or at least won't interfere with user
  493. space in a wrong way.
  494. For details refer to Documentation/power/notifiers.txt.
  495. Runtime Power Management
  496. ========================
  497. Many devices are able to dynamically power down while the system is still
  498. running. This feature is useful for devices that are not being used, and
  499. can offer significant power savings on a running system. These devices
  500. often support a range of runtime power states, which might use names such
  501. as "off", "sleep", "idle", "active", and so on. Those states will in some
  502. cases (like PCI) be partially constrained by a bus the device uses, and will
  503. usually include hardware states that are also used in system sleep states.
  504. Note, however, that a system-wide power transition can be started while some
  505. devices are in low power states due to the runtime power management. The system
  506. sleep PM callbacks should generally recognize such situations and react to them
  507. appropriately, but the recommended actions to be taken in that cases are
  508. subsystem-specific.
  509. In some cases the decision may be made at the subsystem level while in some
  510. other cases the device driver may be left to decide. In some cases it may be
  511. desirable to leave a suspended device in that state during system-wide power
  512. transition, but in some other cases the device ought to be put back into the
  513. full power state, for example to be configured for system wakeup or so that its
  514. system wakeup capability can be disabled. That all depends on the hardware
  515. and the design of the subsystem and device driver in question.
  516. During system-wide resume from a sleep state it's better to put devices into
  517. the full power state, as explained in Documentation/power/runtime_pm.txt. Refer
  518. to that document for more information regarding this particular issue as well as
  519. for information on the device runtime power management framework in general.