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