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