devices.txt 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. Most of the code in Linux is device drivers, so most of the Linux power
  2. management code is also driver-specific. Most drivers will do very little;
  3. others, especially for platforms with small batteries (like cell phones),
  4. will do a lot.
  5. This writeup gives an overview of how drivers interact with system-wide
  6. power management goals, emphasizing the models and interfaces that are
  7. shared by everything that hooks up to the driver model core. Read it as
  8. background for the domain-specific work you'd do with any specific driver.
  9. Two Models for Device Power Management
  10. ======================================
  11. Drivers will use one or both of these models to put devices into low-power
  12. states:
  13. System Sleep model:
  14. Drivers can enter low power states as part of entering system-wide
  15. low-power states like "suspend-to-ram", or (mostly for systems with
  16. disks) "hibernate" (suspend-to-disk).
  17. This is something that device, bus, and class drivers collaborate on
  18. by implementing various role-specific suspend and resume methods to
  19. cleanly power down hardware and software subsystems, then reactivate
  20. them without loss of data.
  21. Some drivers can manage hardware wakeup events, which make the system
  22. leave that low-power state. This feature may be disabled using the
  23. relevant /sys/devices/.../power/wakeup file; enabling it may cost some
  24. power usage, but let the whole system enter low power states more often.
  25. Runtime Power Management model:
  26. Drivers may also enter low power states while the system is running,
  27. independently of other power management activity. Upstream drivers
  28. will normally not know (or care) if the device is in some low power
  29. state when issuing requests; the driver will auto-resume anything
  30. that's needed when it gets a request.
  31. This doesn't have, or need much infrastructure; it's just something you
  32. should do when writing your drivers. For example, clk_disable() unused
  33. clocks as part of minimizing power drain for currently-unused hardware.
  34. Of course, sometimes clusters of drivers will collaborate with each
  35. other, which could involve task-specific power management.
  36. There's not a lot to be said about those low power states except that they
  37. are very system-specific, and often device-specific. Also, that if enough
  38. drivers put themselves into low power states (at "runtime"), the effect may be
  39. the same as entering some system-wide low-power state (system sleep) ... and
  40. that synergies exist, so that several drivers using runtime pm might put the
  41. system into a state where even deeper power saving options are available.
  42. Most suspended devices will have quiesced all I/O: no more DMA or irqs, no
  43. more data read or written, and requests from upstream drivers are no longer
  44. accepted. A given bus or platform may have different requirements though.
  45. Examples of hardware wakeup events include an alarm from a real time clock,
  46. network wake-on-LAN packets, keyboard or mouse activity, and media insertion
  47. or removal (for PCMCIA, MMC/SD, USB, and so on).
  48. Interfaces for Entering System Sleep States
  49. ===========================================
  50. Most of the programming interfaces a device driver needs to know about
  51. relate to that first model: entering a system-wide low power state,
  52. rather than just minimizing power consumption by one device.
  53. Bus Driver Methods
  54. ------------------
  55. The core methods to suspend and resume devices reside in struct bus_type.
  56. These are mostly of interest to people writing infrastructure for busses
  57. like PCI or USB, or because they define the primitives that device drivers
  58. may need to apply in domain-specific ways to their devices:
  59. struct bus_type {
  60. ...
  61. int (*suspend)(struct device *dev, pm_message_t state);
  62. int (*suspend_late)(struct device *dev, pm_message_t state);
  63. int (*resume_early)(struct device *dev);
  64. int (*resume)(struct device *dev);
  65. };
  66. Bus drivers implement those methods as appropriate for the hardware and
  67. the drivers using it; PCI works differently from USB, and so on. Not many
  68. people write bus drivers; most driver code is a "device driver" that
  69. builds on top of bus-specific framework code.
  70. For more information on these driver calls, see the description later;
  71. they are called in phases for every device, respecting the parent-child
  72. sequencing in the driver model tree. Note that as this is being written,
  73. only the suspend() and resume() are widely available; not many bus drivers
  74. leverage all of those phases, or pass them down to lower driver levels.
  75. /sys/devices/.../power/wakeup files
  76. -----------------------------------
  77. All devices in the driver model have two flags to control handling of
  78. wakeup events, which are hardware signals that can force the device and/or
  79. system out of a low power state. These are initialized by bus or device
  80. driver code using device_init_wakeup(dev,can_wakeup).
  81. The "can_wakeup" flag just records whether the device (and its driver) can
  82. physically support wakeup events. When that flag is clear, the sysfs
  83. "wakeup" file is empty, and device_may_wakeup() returns false.
  84. For devices that can issue wakeup events, a separate flag controls whether
  85. that device should try to use its wakeup mechanism. The initial value of
  86. device_may_wakeup() will be true, so that the device's "wakeup" file holds
  87. the value "enabled". Userspace can change that to "disabled" so that
  88. device_may_wakeup() returns false; or change it back to "enabled" (so that
  89. it returns true again).
  90. EXAMPLE: PCI Device Driver Methods
  91. -----------------------------------
  92. PCI framework software calls these methods when the PCI device driver bound
  93. to a device device has provided them:
  94. struct pci_driver {
  95. ...
  96. int (*suspend)(struct pci_device *pdev, pm_message_t state);
  97. int (*suspend_late)(struct pci_device *pdev, pm_message_t state);
  98. int (*resume_early)(struct pci_device *pdev);
  99. int (*resume)(struct pci_device *pdev);
  100. };
  101. Drivers will implement those methods, and call PCI-specific procedures
  102. like pci_set_power_state(), pci_enable_wake(), pci_save_state(), and
  103. pci_restore_state() to manage PCI-specific mechanisms. (PCI config space
  104. could be saved during driver probe, if it weren't for the fact that some
  105. systems rely on userspace tweaking using setpci.) Devices are suspended
  106. before their bridges enter low power states, and likewise bridges resume
  107. before their devices.
  108. Upper Layers of Driver Stacks
  109. -----------------------------
  110. Device drivers generally have at least two interfaces, and the methods
  111. sketched above are the ones which apply to the lower level (nearer PCI, USB,
  112. or other bus hardware). The network and block layers are examples of upper
  113. level interfaces, as is a character device talking to userspace.
  114. Power management requests normally need to flow through those upper levels,
  115. which often use domain-oriented requests like "blank that screen". In
  116. some cases those upper levels will have power management intelligence that
  117. relates to end-user activity, or other devices that work in cooperation.
  118. When those interfaces are structured using class interfaces, there is a
  119. standard way to have the upper layer stop issuing requests to a given
  120. class device (and restart later):
  121. struct class {
  122. ...
  123. int (*suspend)(struct device *dev, pm_message_t state);
  124. int (*resume)(struct device *dev);
  125. };
  126. Those calls are issued in specific phases of the process by which the
  127. system enters a low power "suspend" state, or resumes from it.
  128. Calling Drivers to Enter System Sleep States
  129. ============================================
  130. When the system enters a low power state, each device's driver is asked
  131. to suspend the device by putting it into state compatible with the target
  132. system state. That's usually some version of "off", but the details are
  133. system-specific. Also, wakeup-enabled devices will usually stay partly
  134. functional in order to wake the system.
  135. When the system leaves that low power state, the device's driver is asked
  136. to resume it. The suspend and resume operations always go together, and
  137. both are multi-phase operations.
  138. For simple drivers, suspend might quiesce the device using the class code
  139. and then turn its hardware as "off" as possible with late_suspend. The
  140. matching resume calls would then completely reinitialize the hardware
  141. before reactivating its class I/O queues.
  142. More power-aware drivers drivers will use more than one device low power
  143. state, either at runtime or during system sleep states, and might trigger
  144. system wakeup events.
  145. Call Sequence Guarantees
  146. ------------------------
  147. To ensure that bridges and similar links needed to talk to a device are
  148. available when the device is suspended or resumed, the device tree is
  149. walked in a bottom-up order to suspend devices. A top-down order is
  150. used to resume those devices.
  151. The ordering of the device tree is defined by the order in which devices
  152. get registered: a child can never be registered, probed or resumed before
  153. its parent; and can't be removed or suspended after that parent.
  154. The policy is that the device tree should match hardware bus topology.
  155. (Or at least the control bus, for devices which use multiple busses.)
  156. Suspending Devices
  157. ------------------
  158. Suspending a given device is done in several phases. Suspending the
  159. system always includes every phase, executing calls for every device
  160. before the next phase begins. Not all busses or classes support all
  161. these callbacks; and not all drivers use all the callbacks.
  162. The phases are seen by driver notifications issued in this order:
  163. 1 class.suspend(dev, message) is called after tasks are frozen, for
  164. devices associated with a class that has such a method. This
  165. method may sleep.
  166. Since I/O activity usually comes from such higher layers, this is
  167. a good place to quiesce all drivers of a given type (and keep such
  168. code out of those drivers).
  169. 2 bus.suspend(dev, message) is called next. This method may sleep,
  170. and is often morphed into a device driver call with bus-specific
  171. parameters and/or rules.
  172. This call should handle parts of device suspend logic that require
  173. sleeping. It probably does work to quiesce the device which hasn't
  174. been abstracted into class.suspend() or bus.suspend_late().
  175. 3 bus.suspend_late(dev, message) is called with IRQs disabled, and
  176. with only one CPU active. Until the bus.resume_early() phase
  177. completes (see later), IRQs are not enabled again. This method
  178. won't be exposed by all busses; for message based busses like USB,
  179. I2C, or SPI, device interactions normally require IRQs. This bus
  180. call may be morphed into a driver call with bus-specific parameters.
  181. This call might save low level hardware state that might otherwise
  182. be lost in the upcoming low power state, and actually put the
  183. device into a low power state ... so that in some cases the device
  184. may stay partly usable until this late. This "late" call may also
  185. help when coping with hardware that behaves badly.
  186. The pm_message_t parameter is currently used to refine those semantics
  187. (described later).
  188. At the end of those phases, drivers should normally have stopped all I/O
  189. transactions (DMA, IRQs), saved enough state that they can re-initialize
  190. or restore previous state (as needed by the hardware), and placed the
  191. device into a low-power state. On many platforms they will also use
  192. clk_disable() to gate off one or more clock sources; sometimes they will
  193. also switch off power supplies, or reduce voltages. Drivers which have
  194. runtime PM support may already have performed some or all of the steps
  195. needed to prepare for the upcoming system sleep state.
  196. When any driver sees that its device_can_wakeup(dev), it should make sure
  197. to use the relevant hardware signals to trigger a system wakeup event.
  198. For example, enable_irq_wake() might identify GPIO signals hooked up to
  199. a switch or other external hardware, and pci_enable_wake() does something
  200. similar for PCI's PME# signal.
  201. If a driver (or bus, or class) fails it suspend method, the system won't
  202. enter the desired low power state; it will resume all the devices it's
  203. suspended so far.
  204. Note that drivers may need to perform different actions based on the target
  205. system lowpower/sleep state. At this writing, there are only platform
  206. specific APIs through which drivers could determine those target states.
  207. Device Low Power (suspend) States
  208. ---------------------------------
  209. Device low-power states aren't very standard. One device might only handle
  210. "on" and "off, while another might support a dozen different versions of
  211. "on" (how many engines are active?), plus a state that gets back to "on"
  212. faster than from a full "off".
  213. Some busses define rules about what different suspend states mean. PCI
  214. gives one example: after the suspend sequence completes, a non-legacy
  215. PCI device may not perform DMA or issue IRQs, and any wakeup events it
  216. issues would be issued through the PME# bus signal. Plus, there are
  217. several PCI-standard device states, some of which are optional.
  218. In contrast, integrated system-on-chip processors often use irqs as the
  219. wakeup event sources (so drivers would call enable_irq_wake) and might
  220. be able to treat DMA completion as a wakeup event (sometimes DMA can stay
  221. active too, it'd only be the CPU and some peripherals that sleep).
  222. Some details here may be platform-specific. Systems may have devices that
  223. can be fully active in certain sleep states, such as an LCD display that's
  224. refreshed using DMA while most of the system is sleeping lightly ... and
  225. its frame buffer might even be updated by a DSP or other non-Linux CPU while
  226. the Linux control processor stays idle.
  227. Moreover, the specific actions taken may depend on the target system state.
  228. One target system state might allow a given device to be very operational;
  229. another might require a hard shut down with re-initialization on resume.
  230. And two different target systems might use the same device in different
  231. ways; the aforementioned LCD might be active in one product's "standby",
  232. but a different product using the same SOC might work differently.
  233. Meaning of pm_message_t.event
  234. -----------------------------
  235. Parameters to suspend calls include the device affected and a message of
  236. type pm_message_t, which has one field: the event. If driver does not
  237. recognize the event code, suspend calls may abort the request and return
  238. a negative errno. However, most drivers will be fine if they implement
  239. PM_EVENT_SUSPEND semantics for all messages.
  240. The event codes are used to refine the goal of suspending the device, and
  241. mostly matter when creating or resuming system memory image snapshots, as
  242. used with suspend-to-disk:
  243. PM_EVENT_SUSPEND -- quiesce the driver and put hardware into a low-power
  244. state. When used with system sleep states like "suspend-to-RAM" or
  245. "standby", the upcoming resume() call will often be able to rely on
  246. state kept in hardware, or issue system wakeup events. When used
  247. instead with suspend-to-disk, few devices support this capability;
  248. most are completely powered off.
  249. PM_EVENT_FREEZE -- quiesce the driver, but don't necessarily change into
  250. any low power mode. A system snapshot is about to be taken, often
  251. followed by a call to the driver's resume() method. Neither wakeup
  252. events nor DMA are allowed.
  253. PM_EVENT_PRETHAW -- quiesce the driver, knowing that the upcoming resume()
  254. will restore a suspend-to-disk snapshot from a different kernel image.
  255. Drivers that are smart enough to look at their hardware state during
  256. resume() processing need that state to be correct ... a PRETHAW could
  257. be used to invalidate that state (by resetting the device), like a
  258. shutdown() invocation would before a kexec() or system halt. Other
  259. drivers might handle this the same way as PM_EVENT_FREEZE. Neither
  260. wakeup events nor DMA are allowed.
  261. To enter "standby" (ACPI S1) or "Suspend to RAM" (STR, ACPI S3) states, or
  262. the similarly named APM states, only PM_EVENT_SUSPEND is used; for "Suspend
  263. to Disk" (STD, hibernate, ACPI S4), all of those event codes are used.
  264. There's also PM_EVENT_ON, a value which never appears as a suspend event
  265. but is sometimes used to record the "not suspended" device state.
  266. Resuming Devices
  267. ----------------
  268. Resuming is done in multiple phases, much like suspending, with all
  269. devices processing each phase's calls before the next phase begins.
  270. The phases are seen by driver notifications issued in this order:
  271. 1 bus.resume_early(dev) is called with IRQs disabled, and with
  272. only one CPU active. As with bus.suspend_late(), this method
  273. won't be supported on busses that require IRQs in order to
  274. interact with devices.
  275. This reverses the effects of bus.suspend_late().
  276. 2 bus.resume(dev) is called next. This may be morphed into a device
  277. driver call with bus-specific parameters; implementations may sleep.
  278. This reverses the effects of bus.suspend().
  279. 3 class.resume(dev) is called for devices associated with a class
  280. that has such a method. Implementations may sleep.
  281. This reverses the effects of class.suspend(), and would usually
  282. reactivate the device's I/O queue.
  283. At the end of those phases, drivers should normally be as functional as
  284. they were before suspending: I/O can be performed using DMA and IRQs, and
  285. the relevant clocks are gated on. The device need not be "fully on"; it
  286. might be in a runtime lowpower/suspend state that acts as if it were.
  287. However, the details here may again be platform-specific. For example,
  288. some systems support multiple "run" states, and the mode in effect at
  289. the end of resume() might not be the one which preceded suspension.
  290. That means availability of certain clocks or power supplies changed,
  291. which could easily affect how a driver works.
  292. Drivers need to be able to handle hardware which has been reset since the
  293. suspend methods were called, for example by complete reinitialization.
  294. This may be the hardest part, and the one most protected by NDA'd documents
  295. and chip errata. It's simplest if the hardware state hasn't changed since
  296. the suspend() was called, but that can't always be guaranteed.
  297. Drivers must also be prepared to notice that the device has been removed
  298. while the system was powered off, whenever that's physically possible.
  299. PCMCIA, MMC, USB, Firewire, SCSI, and even IDE are common examples of busses
  300. where common Linux platforms will see such removal. Details of how drivers
  301. will notice and handle such removals are currently bus-specific, and often
  302. involve a separate thread.
  303. Note that the bus-specific runtime PM wakeup mechanism can exist, and might
  304. be defined to share some of the same driver code as for system wakeup. For
  305. example, a bus-specific device driver's resume() method might be used there,
  306. so it wouldn't only be called from bus.resume() during system-wide wakeup.
  307. See bus-specific information about how runtime wakeup events are handled.
  308. System Devices
  309. --------------
  310. System devices follow a slightly different API, which can be found in
  311. include/linux/sysdev.h
  312. drivers/base/sys.c
  313. System devices will only be suspended with interrupts disabled, and after
  314. all other devices have been suspended. On resume, they will be resumed
  315. before any other devices, and also with interrupts disabled.
  316. That is, IRQs are disabled, the suspend_late() phase begins, then the
  317. sysdev_driver.suspend() phase, and the system enters a sleep state. Then
  318. the sysdev_driver.resume() phase begins, followed by the resume_early()
  319. phase, after which IRQs are enabled.
  320. Code to actually enter and exit the system-wide low power state sometimes
  321. involves hardware details that are only known to the boot firmware, and
  322. may leave a CPU running software (from SRAM or flash memory) that monitors
  323. the system and manages its wakeup sequence.
  324. Runtime Power Management
  325. ========================
  326. Many devices are able to dynamically power down while the system is still
  327. running. This feature is useful for devices that are not being used, and
  328. can offer significant power savings on a running system. These devices
  329. often support a range of runtime power states, which might use names such
  330. as "off", "sleep", "idle", "active", and so on. Those states will in some
  331. cases (like PCI) be partially constrained by a bus the device uses, and will
  332. usually include hardware states that are also used in system sleep states.
  333. However, note that if a driver puts a device into a runtime low power state
  334. and the system then goes into a system-wide sleep state, it normally ought
  335. to resume into that runtime low power state rather than "full on". Such
  336. distinctions would be part of the driver-internal state machine for that
  337. hardware; the whole point of runtime power management is to be sure that
  338. drivers are decoupled in that way from the state machine governing phases
  339. of the system-wide power/sleep state transitions.
  340. Power Saving Techniques
  341. -----------------------
  342. Normally runtime power management is handled by the drivers without specific
  343. userspace or kernel intervention, by device-aware use of techniques like:
  344. Using information provided by other system layers
  345. - stay deeply "off" except between open() and close()
  346. - if transceiver/PHY indicates "nobody connected", stay "off"
  347. - application protocols may include power commands or hints
  348. Using fewer CPU cycles
  349. - using DMA instead of PIO
  350. - removing timers, or making them lower frequency
  351. - shortening "hot" code paths
  352. - eliminating cache misses
  353. - (sometimes) offloading work to device firmware
  354. Reducing other resource costs
  355. - gating off unused clocks in software (or hardware)
  356. - switching off unused power supplies
  357. - eliminating (or delaying/merging) IRQs
  358. - tuning DMA to use word and/or burst modes
  359. Using device-specific low power states
  360. - using lower voltages
  361. - avoiding needless DMA transfers
  362. Read your hardware documentation carefully to see the opportunities that
  363. may be available. If you can, measure the actual power usage and check
  364. it against the budget established for your project.
  365. Examples: USB hosts, system timer, system CPU
  366. ----------------------------------------------
  367. USB host controllers make interesting, if complex, examples. In many cases
  368. these have no work to do: no USB devices are connected, or all of them are
  369. in the USB "suspend" state. Linux host controller drivers can then disable
  370. periodic DMA transfers that would otherwise be a constant power drain on the
  371. memory subsystem, and enter a suspend state. In power-aware controllers,
  372. entering that suspend state may disable the clock used with USB signaling,
  373. saving a certain amount of power.
  374. The controller will be woken from that state (with an IRQ) by changes to the
  375. signal state on the data lines of a given port, for example by an existing
  376. peripheral requesting "remote wakeup" or by plugging a new peripheral. The
  377. same wakeup mechanism usually works from "standby" sleep states, and on some
  378. systems also from "suspend to RAM" (or even "suspend to disk") states.
  379. (Except that ACPI may be involved instead of normal IRQs, on some hardware.)
  380. System devices like timers and CPUs may have special roles in the platform
  381. power management scheme. For example, system timers using a "dynamic tick"
  382. approach don't just save CPU cycles (by eliminating needless timer IRQs),
  383. but they may also open the door to using lower power CPU "idle" states that
  384. cost more than a jiffie to enter and exit. On x86 systems these are states
  385. like "C3"; note that periodic DMA transfers from a USB host controller will
  386. also prevent entry to a C3 state, much like a periodic timer IRQ.
  387. That kind of runtime mechanism interaction is common. "System On Chip" (SOC)
  388. processors often have low power idle modes that can't be entered unless
  389. certain medium-speed clocks (often 12 or 48 MHz) are gated off. When the
  390. drivers gate those clocks effectively, then the system idle task may be able
  391. to use the lower power idle modes and thereby increase battery life.
  392. If the CPU can have a "cpufreq" driver, there also may be opportunities
  393. to shift to lower voltage settings and reduce the power cost of executing
  394. a given number of instructions. (Without voltage adjustment, it's rare
  395. for cpufreq to save much power; the cost-per-instruction must go down.)
  396. /sys/devices/.../power/state files
  397. ==================================
  398. For now you can also test some of this functionality using sysfs.
  399. DEPRECATED: USE "power/state" ONLY FOR DRIVER TESTING, AND
  400. AVOID USING dev->power.power_state IN DRIVERS.
  401. THESE WILL BE REMOVED. IF THE "power/state" FILE GETS REPLACED,
  402. IT WILL BECOME SOMETHING COUPLED TO THE BUS OR DRIVER.
  403. In each device's directory, there is a 'power' directory, which contains
  404. at least a 'state' file. The value of this field is effectively boolean,
  405. PM_EVENT_ON or PM_EVENT_SUSPEND.
  406. * Reading from this file displays a value corresponding to
  407. the power.power_state.event field. All nonzero values are
  408. displayed as "2", corresponding to a low power state; zero
  409. is displayed as "0", corresponding to normal operation.
  410. * Writing to this file initiates a transition using the
  411. specified event code number; only '0', '2', and '3' are
  412. accepted (without a newline); '2' and '3' are both
  413. mapped to PM_EVENT_SUSPEND.
  414. On writes, the PM core relies on that recorded event code and the device/bus
  415. capabilities to determine whether it uses a partial suspend() or resume()
  416. sequence to change things so that the recorded event corresponds to the
  417. numeric parameter.
  418. - If the bus requires the irqs-disabled suspend_late()/resume_early()
  419. phases, writes fail because those operations are not supported here.
  420. - If the recorded value is the expected value, nothing is done.
  421. - If the recorded value is nonzero, the device is partially resumed,
  422. using the bus.resume() and/or class.resume() methods.
  423. - If the target value is nonzero, the device is partially suspended,
  424. using the class.suspend() and/or bus.suspend() methods and the
  425. PM_EVENT_SUSPEND message.
  426. Drivers have no way to tell whether their suspend() and resume() calls
  427. have come through the sysfs power/state file or as part of entering a
  428. system sleep state, except that when accessed through sysfs the normal
  429. parent/child sequencing rules are ignored. Drivers (such as bus, bridge,
  430. or hub drivers) which expose child devices may need to enforce those rules
  431. on their own.