Blame view

Documentation/power/devices.txt 35.6 KB
624f6ec87   Rafael J. Wysocki   PM: Update device...
1
  Device Power Management
7538e3db6   Rafael J. Wysocki   PM: Add support f...
2
  Copyright (c) 2010-2011 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
d6f9cda1f   Alan Stern   PM: Improve devic...
3
  Copyright (c) 2010 Alan Stern <stern@rowland.harvard.edu>
f71495f3f   Rafael J. Wysocki   PM / sleep: Updat...
4
  Copyright (c) 2014 Intel Corp., Rafael J. Wysocki <rafael.j.wysocki@intel.com>
d6f9cda1f   Alan Stern   PM: Improve devic...
5

624f6ec87   Rafael J. Wysocki   PM: Update device...
6

4fc084000   David Brownell   updated Documenta...
7
  Most of the code in Linux is device drivers, so most of the Linux power
d6f9cda1f   Alan Stern   PM: Improve devic...
8
9
10
  management (PM) code is also driver-specific.  Most drivers will do very
  little; others, especially for platforms with small batteries (like cell
  phones), will do a lot.
4fc084000   David Brownell   updated Documenta...
11
12
13
14
15
16
17
18
19
20
21
22
23
  
  This writeup gives an overview of how drivers interact with system-wide
  power management goals, emphasizing the models and interfaces that are
  shared by everything that hooks up to the driver model core.  Read it as
  background for the domain-specific work you'd do with any specific driver.
  
  
  Two Models for Device Power Management
  ======================================
  Drivers will use one or both of these models to put devices into low-power
  states:
  
      System Sleep model:
d6f9cda1f   Alan Stern   PM: Improve devic...
24
25
26
27
  	Drivers can enter low-power states as part of entering system-wide
  	low-power states like "suspend" (also known as "suspend-to-RAM"), or
  	(mostly for systems with disks) "hibernation" (also known as
  	"suspend-to-disk").
4fc084000   David Brownell   updated Documenta...
28
29
30
31
32
33
34
  
  	This is something that device, bus, and class drivers collaborate on
  	by implementing various role-specific suspend and resume methods to
  	cleanly power down hardware and software subsystems, then reactivate
  	them without loss of data.
  
  	Some drivers can manage hardware wakeup events, which make the system
d6f9cda1f   Alan Stern   PM: Improve devic...
35
  	leave the low-power state.  This feature may be enabled or disabled
624f6ec87   Rafael J. Wysocki   PM: Update device...
36
37
38
  	using the relevant /sys/devices/.../power/wakeup file (for Ethernet
  	drivers the ioctl interface used by ethtool may also be used for this
  	purpose); enabling it may cost some power usage, but let the whole
d6f9cda1f   Alan Stern   PM: Improve devic...
39
  	system enter low-power states more often.
4fc084000   David Brownell   updated Documenta...
40
41
  
      Runtime Power Management model:
d6f9cda1f   Alan Stern   PM: Improve devic...
42
  	Devices may also be put into low-power states while the system is
624f6ec87   Rafael J. Wysocki   PM: Update device...
43
44
  	running, independently of other power management activity in principle.
  	However, devices are not generally independent of each other (for
d6f9cda1f   Alan Stern   PM: Improve devic...
45
46
  	example, a parent device cannot be suspended unless all of its child
  	devices have been suspended).  Moreover, depending on the bus type the
624f6ec87   Rafael J. Wysocki   PM: Update device...
47
  	device is on, it may be necessary to carry out some bus-specific
d6f9cda1f   Alan Stern   PM: Improve devic...
48
49
50
  	operations on the device for this purpose.  Devices put into low power
  	states at run time may require special handling during system-wide power
  	transitions (suspend or hibernation).
624f6ec87   Rafael J. Wysocki   PM: Update device...
51
52
  
  	For these reasons not only the device driver itself, but also the
d6f9cda1f   Alan Stern   PM: Improve devic...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  	appropriate subsystem (bus type, device type or device class) driver and
  	the PM core are involved in runtime power management.  As in the system
  	sleep power management case, they need to collaborate by implementing
  	various role-specific suspend and resume methods, so that the hardware
  	is cleanly powered down and reactivated without data or service loss.
  
  There's not a lot to be said about those low-power states except that they are
  very system-specific, and often device-specific.  Also, that if enough devices
  have been put into low-power states (at runtime), the effect may be very similar
  to entering some system-wide low-power state (system sleep) ... and that
  synergies exist, so that several drivers using runtime PM might put the system
  into a state where even deeper power saving options are available.
  
  Most suspended devices will have quiesced all I/O: no more DMA or IRQs (except
  for wakeup events), no more data read or written, and requests from upstream
  drivers are no longer accepted.  A given bus or platform may have different
  requirements though.
4fc084000   David Brownell   updated Documenta...
70
71
72
73
74
75
76
77
  
  Examples of hardware wakeup events include an alarm from a real time clock,
  network wake-on-LAN packets, keyboard or mouse activity, and media insertion
  or removal (for PCMCIA, MMC/SD, USB, and so on).
  
  
  Interfaces for Entering System Sleep States
  ===========================================
d6f9cda1f   Alan Stern   PM: Improve devic...
78
79
80
81
  There are programming interfaces provided for subsystems (bus type, device type,
  device class) and device drivers to allow them to participate in the power
  management of devices they are concerned with.  These interfaces cover both
  system sleep and runtime power management.
624f6ec87   Rafael J. Wysocki   PM: Update device...
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  
  
  Device Power Management Operations
  ----------------------------------
  Device power management operations, at the subsystem level as well as at the
  device driver level, are implemented by defining and populating objects of type
  struct dev_pm_ops:
  
  struct dev_pm_ops {
  	int (*prepare)(struct device *dev);
  	void (*complete)(struct device *dev);
  	int (*suspend)(struct device *dev);
  	int (*resume)(struct device *dev);
  	int (*freeze)(struct device *dev);
  	int (*thaw)(struct device *dev);
  	int (*poweroff)(struct device *dev);
  	int (*restore)(struct device *dev);
cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
99
100
101
102
103
104
  	int (*suspend_late)(struct device *dev);
  	int (*resume_early)(struct device *dev);
  	int (*freeze_late)(struct device *dev);
  	int (*thaw_early)(struct device *dev);
  	int (*poweroff_late)(struct device *dev);
  	int (*restore_early)(struct device *dev);
624f6ec87   Rafael J. Wysocki   PM: Update device...
105
106
107
108
109
110
111
112
113
114
  	int (*suspend_noirq)(struct device *dev);
  	int (*resume_noirq)(struct device *dev);
  	int (*freeze_noirq)(struct device *dev);
  	int (*thaw_noirq)(struct device *dev);
  	int (*poweroff_noirq)(struct device *dev);
  	int (*restore_noirq)(struct device *dev);
  	int (*runtime_suspend)(struct device *dev);
  	int (*runtime_resume)(struct device *dev);
  	int (*runtime_idle)(struct device *dev);
  };
4fc084000   David Brownell   updated Documenta...
115

624f6ec87   Rafael J. Wysocki   PM: Update device...
116
117
  This structure is defined in include/linux/pm.h and the methods included in it
  are also described in that file.  Their roles will be explained in what follows.
d6f9cda1f   Alan Stern   PM: Improve devic...
118
119
  For now, it should be sufficient to remember that the last three methods are
  specific to runtime power management while the remaining ones are used during
624f6ec87   Rafael J. Wysocki   PM: Update device...
120
  system-wide power transitions.
4fc084000   David Brownell   updated Documenta...
121

d6f9cda1f   Alan Stern   PM: Improve devic...
122
123
124
125
126
  There also is a deprecated "old" or "legacy" interface for power management
  operations available at least for some subsystems.  This approach does not use
  struct dev_pm_ops objects and it is suitable only for implementing system sleep
  power management methods.  Therefore it is not described in this document, so
  please refer directly to the source code for more information about it.
624f6ec87   Rafael J. Wysocki   PM: Update device...
127
128
129
130
131
  
  
  Subsystem-Level Methods
  -----------------------
  The core methods to suspend and resume devices reside in struct dev_pm_ops
5841eb640   Rafael J. Wysocki   PM / Domains: Doc...
132
133
134
  pointed to by the ops member of struct dev_pm_domain, or by the pm member of
  struct bus_type, struct device_type and struct class.  They are mostly of
  interest to the people writing infrastructure for platforms and buses, like PCI
35cd133c6   Rafael J. Wysocki   PM: Run the drive...
135
136
137
  or USB, or device type and device class drivers.  They also are relevant to the
  writers of device drivers whose subsystems (PM domains, device types, device
  classes and bus types) don't provide all power management methods.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138

d6f9cda1f   Alan Stern   PM: Improve devic...
139
140
141
142
  Bus drivers implement these methods as appropriate for the hardware and the
  drivers using it; PCI works differently from USB, and so on.  Not many people
  write subsystem-level drivers; most driver code is a "device driver" that builds
  on top of bus-specific framework code.
4fc084000   David Brownell   updated Documenta...
143
144
145
  
  For more information on these driver calls, see the description later;
  they are called in phases for every device, respecting the parent-child
624f6ec87   Rafael J. Wysocki   PM: Update device...
146
  sequencing in the driver model tree.
4fc084000   David Brownell   updated Documenta...
147
148
149
150
  
  
  /sys/devices/.../power/wakeup files
  -----------------------------------
fafba48d4   Rafael J. Wysocki   PM / Sleep: Updat...
151
152
153
  All device objects in the driver model contain fields that control the handling
  of system wakeup events (hardware signals that can force the system out of a
  sleep state).  These fields are initialized by bus or device driver code using
d6f9cda1f   Alan Stern   PM: Improve devic...
154
155
  device_set_wakeup_capable() and device_set_wakeup_enable(), defined in
  include/linux/pm_wakeup.h.
4fc084000   David Brownell   updated Documenta...
156

fafba48d4   Rafael J. Wysocki   PM / Sleep: Updat...
157
  The "power.can_wakeup" flag just records whether the device (and its driver) can
d6f9cda1f   Alan Stern   PM: Improve devic...
158
  physically support wakeup events.  The device_set_wakeup_capable() routine
fafba48d4   Rafael J. Wysocki   PM / Sleep: Updat...
159
160
161
162
163
164
  affects this flag.  The "power.wakeup" field is a pointer to an object of type
  struct wakeup_source used for controlling whether or not the device should use
  its system wakeup mechanism and for notifying the PM core of system wakeup
  events signaled by the device.  This object is only present for wakeup-capable
  devices (i.e. devices whose "can_wakeup" flags are set) and is created (or
  removed) by device_set_wakeup_capable().
d6f9cda1f   Alan Stern   PM: Improve devic...
165
166
167
168
169
  
  Whether or not a device is capable of issuing wakeup events is a hardware
  matter, and the kernel is responsible for keeping track of it.  By contrast,
  whether or not a wakeup-capable device should issue wakeup events is a policy
  decision, and it is managed by user space through a sysfs attribute: the
fafba48d4   Rafael J. Wysocki   PM / Sleep: Updat...
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  "power/wakeup" file.  User space can write the strings "enabled" or "disabled"
  to it to indicate whether or not, respectively, the device is supposed to signal
  system wakeup.  This file is only present if the "power.wakeup" object exists
  for the given device and is created (or removed) along with that object, by
  device_set_wakeup_capable().  Reads from the file will return the corresponding
  string.
  
  The "power/wakeup" file is supposed to contain the "disabled" string initially
  for the majority of devices; the major exceptions are power buttons, keyboards,
  and Ethernet adapters whose WoL (wake-on-LAN) feature has been set up with
  ethtool.  It should also default to "enabled" for devices that don't generate
  wakeup requests on their own but merely forward wakeup requests from one bus to
  another (like PCI Express ports).
  
  The device_may_wakeup() routine returns true only if the "power.wakeup" object
  exists and the corresponding "power/wakeup" file contains the string "enabled".
cb8f51bda   Rafael J. Wysocki   PM: Do not create...
186
187
188
189
  This information is used by subsystems, like the PCI bus type code, to see
  whether or not to enable the devices' wakeup mechanisms.  If device wakeup
  mechanisms are enabled or disabled directly by drivers, they also should use
  device_may_wakeup() to decide what to do during a system sleep transition.
fafba48d4   Rafael J. Wysocki   PM / Sleep: Updat...
190
191
192
193
194
195
196
197
198
199
200
201
  Device drivers, however, are not supposed to call device_set_wakeup_enable()
  directly in any case.
  
  It ought to be noted that system wakeup is conceptually different from "remote
  wakeup" used by runtime power management, although it may be supported by the
  same physical mechanism.  Remote wakeup is a feature allowing devices in
  low-power states to trigger specific interrupts to signal conditions in which
  they should be put into the full-power state.  Those interrupts may or may not
  be used to signal system wakeup events, depending on the hardware design.  On
  some systems it is impossible to trigger them from system sleep states.  In any
  case, remote wakeup should always be enabled for runtime power management for
  all devices and drivers that support it.
624f6ec87   Rafael J. Wysocki   PM: Update device...
202
203
204
  
  /sys/devices/.../power/control files
  ------------------------------------
d6f9cda1f   Alan Stern   PM: Improve devic...
205
206
207
208
209
210
211
212
213
214
215
216
  Each device in the driver model has a flag to control whether it is subject to
  runtime power management.  This flag, called runtime_auto, is initialized by the
  bus type (or generally subsystem) code using pm_runtime_allow() or
  pm_runtime_forbid(); the default is to allow runtime power management.
  
  The setting can be adjusted by user space by writing either "on" or "auto" to
  the device's power/control sysfs file.  Writing "auto" calls pm_runtime_allow(),
  setting the flag and allowing the device to be runtime power-managed by its
  driver.  Writing "on" calls pm_runtime_forbid(), clearing the flag, returning
  the device to full power if it was in a low-power state, and preventing the
  device from being runtime power-managed.  User space can check the current value
  of the runtime_auto flag by reading the file.
624f6ec87   Rafael J. Wysocki   PM: Update device...
217
218
  
  The device's runtime_auto flag has no effect on the handling of system-wide
d6f9cda1f   Alan Stern   PM: Improve devic...
219
220
221
  power transitions.  In particular, the device can (and in the majority of cases
  should and will) be put into a low-power state during a system-wide transition
  to a sleep state even though its runtime_auto flag is clear.
624f6ec87   Rafael J. Wysocki   PM: Update device...
222

d6f9cda1f   Alan Stern   PM: Improve devic...
223
224
  For more information about the runtime power management framework, refer to
  Documentation/power/runtime_pm.txt.
4fc084000   David Brownell   updated Documenta...
225

d6f9cda1f   Alan Stern   PM: Improve devic...
226
227
228
229
  Calling Drivers to Enter and Leave System Sleep States
  ======================================================
  When the system goes into a sleep state, each device's driver is asked to
  suspend the device by putting it into a state compatible with the target
4fc084000   David Brownell   updated Documenta...
230
231
232
  system state.  That's usually some version of "off", but the details are
  system-specific.  Also, wakeup-enabled devices will usually stay partly
  functional in order to wake the system.
d6f9cda1f   Alan Stern   PM: Improve devic...
233
234
235
  When the system leaves that low-power state, the device's driver is asked to
  resume it by returning it to full power.  The suspend and resume operations
  always go together, and both are multi-phase operations.
4fc084000   David Brownell   updated Documenta...
236

d6f9cda1f   Alan Stern   PM: Improve devic...
237
238
  For simple drivers, suspend might quiesce the device using class code
  and then turn its hardware as "off" as possible during suspend_noirq.  The
4fc084000   David Brownell   updated Documenta...
239
240
  matching resume calls would then completely reinitialize the hardware
  before reactivating its class I/O queues.
624f6ec87   Rafael J. Wysocki   PM: Update device...
241
242
  More power-aware drivers might prepare the devices for triggering system wakeup
  events.
4fc084000   David Brownell   updated Documenta...
243
244
245
246
  
  
  Call Sequence Guarantees
  ------------------------
624f6ec87   Rafael J. Wysocki   PM: Update device...
247
  To ensure that bridges and similar links needing to talk to a device are
4fc084000   David Brownell   updated Documenta...
248
249
250
251
252
253
254
255
256
257
  available when the device is suspended or resumed, the device tree is
  walked in a bottom-up order to suspend devices.  A top-down order is
  used to resume those devices.
  
  The ordering of the device tree is defined by the order in which devices
  get registered:  a child can never be registered, probed or resumed before
  its parent; and can't be removed or suspended after that parent.
  
  The policy is that the device tree should match hardware bus topology.
  (Or at least the control bus, for devices which use multiple busses.)
58aca2322   Rafael J. Wysocki   PM: Handle device...
258
  In particular, this means that a device registration may fail if the parent of
624f6ec87   Rafael J. Wysocki   PM: Update device...
259
  the device is suspending (i.e. has been chosen by the PM core as the next
58aca2322   Rafael J. Wysocki   PM: Handle device...
260
261
262
  device to suspend) or has already suspended, as well as after all of the other
  devices have been suspended.  Device drivers must be prepared to cope with such
  situations.
4fc084000   David Brownell   updated Documenta...
263

d6f9cda1f   Alan Stern   PM: Improve devic...
264
265
266
  System Power Management Phases
  ------------------------------
  Suspending or resuming the system is done in several phases.  Different phases
dc5aeae4f   Zhang Rui   PM: Documentation...
267
  are used for freeze, standby, and memory sleep states ("suspend-to-RAM") and the
d6f9cda1f   Alan Stern   PM: Improve devic...
268
269
270
271
272
  hibernation state ("suspend-to-disk").  Each phase involves executing callbacks
  for every device before the next phase begins.  Not all busses or classes
  support all these callbacks and not all drivers use all the callbacks.  The
  various phases always run after tasks have been frozen and before they are
  unfrozen.  Furthermore, the *_noirq phases run at a time when IRQ handlers have
fa8ce7239   Rafael J. Wysocki   PM / Sleep: Corre...
273
  been disabled (except for those marked with the IRQF_NO_SUSPEND flag).
624f6ec87   Rafael J. Wysocki   PM: Update device...
274

35cd133c6   Rafael J. Wysocki   PM: Run the drive...
275
276
277
278
279
280
281
  All phases use PM domain, bus, type, class or driver callbacks (that is, methods
  defined in dev->pm_domain->ops, dev->bus->pm, dev->type->pm, dev->class->pm or
  dev->driver->pm).  These callbacks are regarded by the PM core as mutually
  exclusive.  Moreover, PM domain callbacks always take precedence over all of the
  other callbacks and, for example, type callbacks take precedence over bus, class
  and driver callbacks.  To be precise, the following rules are used to determine
  which callback to execute in the given phase:
5841eb640   Rafael J. Wysocki   PM / Domains: Doc...
282

35cd133c6   Rafael J. Wysocki   PM: Run the drive...
283
284
      1.	If dev->pm_domain is present, the PM core will choose the callback
  	included in dev->pm_domain->ops for execution
5841eb640   Rafael J. Wysocki   PM / Domains: Doc...
285
286
  
      2.	Otherwise, if both dev->type and dev->type->pm are present, the callback
35cd133c6   Rafael J. Wysocki   PM: Run the drive...
287
  	included in dev->type->pm will be chosen for execution.
5841eb640   Rafael J. Wysocki   PM / Domains: Doc...
288
289
  
      3.	Otherwise, if both dev->class and dev->class->pm are present, the
35cd133c6   Rafael J. Wysocki   PM: Run the drive...
290
  	callback included in dev->class->pm will be chosen for execution.
5841eb640   Rafael J. Wysocki   PM / Domains: Doc...
291
292
  
      4.	Otherwise, if both dev->bus and dev->bus->pm are present, the callback
35cd133c6   Rafael J. Wysocki   PM: Run the drive...
293
  	included in dev->bus->pm will be chosen for execution.
5841eb640   Rafael J. Wysocki   PM / Domains: Doc...
294
295
296
  
  This allows PM domains and device types to override callbacks provided by bus
  types or device classes if necessary.
4fc084000   David Brownell   updated Documenta...
297

35cd133c6   Rafael J. Wysocki   PM: Run the drive...
298
299
300
301
302
303
  The PM domain, type, class and bus callbacks may in turn invoke device- or
  driver-specific methods stored in dev->driver->pm, but they don't have to do
  that.
  
  If the subsystem callback chosen for execution is not present, the PM core will
  execute the corresponding method from dev->driver->pm instead if there is one.
4fc084000   David Brownell   updated Documenta...
304

4fc084000   David Brownell   updated Documenta...
305

d6f9cda1f   Alan Stern   PM: Improve devic...
306
307
  Entering System Suspend
  -----------------------
dc5aeae4f   Zhang Rui   PM: Documentation...
308
309
  When the system goes into the freeze, standby or memory sleep state,
  the phases are:
d6f9cda1f   Alan Stern   PM: Improve devic...
310

cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
311
  		prepare, suspend, suspend_late, suspend_noirq.
d6f9cda1f   Alan Stern   PM: Improve devic...
312
313
314
315
316
317
318
  
      1.	The prepare phase is meant to prevent races by preventing new devices
  	from being registered; the PM core would never know that all the
  	children of a device had been suspended if new children could be
  	registered at will.  (By contrast, devices may be unregistered at any
  	time.)  Unlike the other suspend-related phases, during the prepare
  	phase the device tree is traversed top-down.
91e7c75ba   Rafael J. Wysocki   PM: Allow drivers...
319
320
  	After the prepare callback method returns, no new children may be
  	registered below the device.  The method may also prepare the device or
fa8ce7239   Rafael J. Wysocki   PM / Sleep: Corre...
321
322
  	driver in some way for the upcoming system power transition, but it
  	should not put the device into a low-power state.
d6f9cda1f   Alan Stern   PM: Improve devic...
323

f71495f3f   Rafael J. Wysocki   PM / sleep: Updat...
324
325
326
327
328
329
330
331
332
333
334
335
336
  	For devices supporting runtime power management, the return value of the
  	prepare callback can be used to indicate to the PM core that it may
  	safely leave the device in runtime suspend (if runtime-suspended
  	already), provided that all of the device's descendants are also left in
  	runtime suspend.  Namely, if the prepare callback returns a positive
  	number and that happens for all of the descendants of the device too,
  	and all of them (including the device itself) are runtime-suspended, the
  	PM core will skip the suspend, suspend_late and	suspend_noirq suspend
  	phases as well as the resume_noirq, resume_early and resume phases of
  	the following system resume for all of these devices.	In that case,
  	the complete callback will be called directly after the prepare callback
  	and is entirely responsible for bringing the device back to the
  	functional state as appropriate.
d6f9cda1f   Alan Stern   PM: Improve devic...
337
338
339
340
      2.	The suspend methods should quiesce the device to stop it from performing
  	I/O.  They also may save the device registers and put it into the
  	appropriate low-power state, depending on the bus type the device is on,
  	and they may enable wakeup events.
cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
341
342
343
344
345
346
      3	For a number of devices it is convenient to split suspend into the
  	"quiesce device" and "save device state" phases, in which cases
  	suspend_late is meant to do the latter.  It is always executed after
  	runtime power management has been disabled for all devices.
  
      4.	The suspend_noirq phase occurs after IRQ handlers have been disabled,
d6f9cda1f   Alan Stern   PM: Improve devic...
347
348
349
350
  	which means that the driver's interrupt handler will not be called while
  	the callback method is running.  The methods should save the values of
  	the device's registers that weren't saved previously and finally put the
  	device into the appropriate low-power state.
624f6ec87   Rafael J. Wysocki   PM: Update device...
351
352
  
  	The majority of subsystems and device drivers need not implement this
d6f9cda1f   Alan Stern   PM: Improve devic...
353
354
355
356
357
358
359
360
361
362
363
364
  	callback.  However, bus types allowing devices to share interrupt
  	vectors, like PCI, generally need it; otherwise a driver might encounter
  	an error during the suspend phase by fielding a shared interrupt
  	generated by some other device after its own device had been set to low
  	power.
  
  At the end of these phases, drivers should have stopped all I/O transactions
  (DMA, IRQs), saved enough state that they can re-initialize or restore previous
  state (as needed by the hardware), and placed the device into a low-power state.
  On many platforms they will gate off one or more clock sources; sometimes they
  will also switch off power supplies or reduce voltages.  (Drivers supporting
  runtime PM may already have performed some or all of these steps.)
624f6ec87   Rafael J. Wysocki   PM: Update device...
365
366
  
  If device_may_wakeup(dev) returns true, the device should be prepared for
d6f9cda1f   Alan Stern   PM: Improve devic...
367
368
  generating hardware wakeup signals to trigger a system wakeup event when the
  system is in the sleep state.  For example, enable_irq_wake() might identify
624f6ec87   Rafael J. Wysocki   PM: Update device...
369
370
  GPIO signals hooked up to a switch or other external hardware, and
  pci_enable_wake() does something similar for the PCI PME signal.
d6f9cda1f   Alan Stern   PM: Improve devic...
371
372
373
  If any of these callbacks returns an error, the system won't enter the desired
  low-power state.  Instead the PM core will unwind its actions by resuming all
  the devices that were suspended.
4fc084000   David Brownell   updated Documenta...
374

d6f9cda1f   Alan Stern   PM: Improve devic...
375
376
  Leaving System Suspend
  ----------------------
dc5aeae4f   Zhang Rui   PM: Documentation...
377
  When resuming from freeze, standby or memory sleep, the phases are:
4fc084000   David Brownell   updated Documenta...
378

cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
379
  		resume_noirq, resume_early, resume, complete.
4fc084000   David Brownell   updated Documenta...
380

d6f9cda1f   Alan Stern   PM: Improve devic...
381
382
383
384
385
386
387
      1.	The resume_noirq callback methods should perform any actions needed
  	before the driver's interrupt handlers are invoked.  This generally
  	means undoing the actions of the suspend_noirq phase.  If the bus type
  	permits devices to share interrupt vectors, like PCI, the method should
  	bring the device and its driver into a state in which the driver can
  	recognize if the device is the source of incoming interrupts, if any,
  	and handle them correctly.
4fc084000   David Brownell   updated Documenta...
388

624f6ec87   Rafael J. Wysocki   PM: Update device...
389
  	For example, the PCI bus type's ->pm.resume_noirq() puts the device into
d6f9cda1f   Alan Stern   PM: Improve devic...
390
391
  	the full-power state (D0 in the PCI terminology) and restores the
  	standard configuration registers of the device.  Then it calls the
624f6ec87   Rafael J. Wysocki   PM: Update device...
392
  	device driver's ->pm.resume_noirq() method to perform device-specific
d6f9cda1f   Alan Stern   PM: Improve devic...
393
  	actions.
4fc084000   David Brownell   updated Documenta...
394

cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
395
396
397
      2.	The resume_early methods should prepare devices for the execution of
  	the resume methods.  This generally involves undoing the actions of the
  	preceding suspend_late phase.
df5cbb278   Masanari Iida   doc: fix double w...
398
      3	The resume methods should bring the device back to its operating
d6f9cda1f   Alan Stern   PM: Improve devic...
399
400
  	state, so that it can perform normal I/O.  This generally involves
  	undoing the actions of the suspend phase.
4fc084000   David Brownell   updated Documenta...
401

cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
402
403
404
405
      4.	The complete phase should undo the actions of the prepare phase.  Note,
  	however, that new children may be registered below the device as soon as
  	the resume callbacks occur; it's not necessary to wait until the
  	complete phase.
4fc084000   David Brownell   updated Documenta...
406

f71495f3f   Rafael J. Wysocki   PM / sleep: Updat...
407
408
409
410
411
412
413
414
415
416
417
418
419
  	Moreover, if the preceding prepare callback returned a positive number,
  	the device may have been left in runtime suspend throughout the whole
  	system suspend and resume (the suspend, suspend_late, suspend_noirq
  	phases of system suspend and the resume_noirq, resume_early, resume
  	phases of system resume may have been skipped for it).  In that case,
  	the complete callback is entirely responsible for bringing the device
  	back to the functional state after system suspend if necessary.  [For
  	example, it may need to queue up a runtime resume request for the device
  	for this purpose.]  To check if that is the case, the complete callback
  	can consult the device's power.direct_complete flag.  Namely, if that
  	flag is set when the complete callback is being run, it has been called
  	directly after the preceding prepare and special action may be required
  	to make the device work correctly afterward.
d6f9cda1f   Alan Stern   PM: Improve devic...
420
421
  At the end of these phases, drivers should be as functional as they were before
  suspending: I/O can be performed using DMA and IRQs, and the relevant clocks are
f71495f3f   Rafael J. Wysocki   PM / sleep: Updat...
422
  gated on.
4fc084000   David Brownell   updated Documenta...
423
424
425
  
  However, the details here may again be platform-specific.  For example,
  some systems support multiple "run" states, and the mode in effect at
624f6ec87   Rafael J. Wysocki   PM: Update device...
426
  the end of resume might not be the one which preceded suspension.
4fc084000   David Brownell   updated Documenta...
427
428
  That means availability of certain clocks or power supplies changed,
  which could easily affect how a driver works.
4fc084000   David Brownell   updated Documenta...
429
430
431
432
  Drivers need to be able to handle hardware which has been reset since the
  suspend methods were called, for example by complete reinitialization.
  This may be the hardest part, and the one most protected by NDA'd documents
  and chip errata.  It's simplest if the hardware state hasn't changed since
25985edce   Lucas De Marchi   Fix common misspe...
433
  the suspend was carried out, but that can't be guaranteed (in fact, it usually
624f6ec87   Rafael J. Wysocki   PM: Update device...
434
  is not the case).
4fc084000   David Brownell   updated Documenta...
435
436
  
  Drivers must also be prepared to notice that the device has been removed
d6f9cda1f   Alan Stern   PM: Improve devic...
437
  while the system was powered down, whenever that's physically possible.
4fc084000   David Brownell   updated Documenta...
438
439
440
441
  PCMCIA, MMC, USB, Firewire, SCSI, and even IDE are common examples of busses
  where common Linux platforms will see such removal.  Details of how drivers
  will notice and handle such removals are currently bus-specific, and often
  involve a separate thread.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
442

d6f9cda1f   Alan Stern   PM: Improve devic...
443
444
445
  These callbacks may return an error value, but the PM core will ignore such
  errors since there's nothing it can do about them other than printing them in
  the system log.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
446

d6f9cda1f   Alan Stern   PM: Improve devic...
447
448
449
  
  Entering Hibernation
  --------------------
dc5aeae4f   Zhang Rui   PM: Documentation...
450
451
  Hibernating the system is more complicated than putting it into the other
  sleep states, because it involves creating and saving a system image.
d6f9cda1f   Alan Stern   PM: Improve devic...
452
453
454
455
456
457
458
459
  Therefore there are more phases for hibernation, with a different set of
  callbacks.  These phases always run after tasks have been frozen and memory has
  been freed.
  
  The general procedure for hibernation is to quiesce all devices (freeze), create
  an image of the system memory while everything is stable, reactivate all
  devices (thaw), write the image to permanent storage, and finally shut down the
  system (poweroff).  The phases used to accomplish this are:
cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
460
461
  	prepare, freeze, freeze_late, freeze_noirq, thaw_noirq, thaw_early,
  	thaw, complete, prepare, poweroff, poweroff_late, poweroff_noirq
d6f9cda1f   Alan Stern   PM: Improve devic...
462
463
464
465
466
467
468
469
470
  
      1.	The prepare phase is discussed in the "Entering System Suspend" section
  	above.
  
      2.	The freeze methods should quiesce the device so that it doesn't generate
  	IRQs or DMA, and they may need to save the values of device registers.
  	However the device does not have to be put in a low-power state, and to
  	save time it's best not to do so.  Also, the device should not be
  	prepared to generate wakeup events.
cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
471
472
473
474
475
      3.	The freeze_late phase is analogous to the suspend_late phase described
  	above, except that the device should not be put in a low-power state and
  	should not be allowed to generate wakeup events by it.
  
      4.	The freeze_noirq phase is analogous to the suspend_noirq phase discussed
d6f9cda1f   Alan Stern   PM: Improve devic...
476
477
478
479
480
481
  	above, except again that the device should not be put in a low-power
  	state and should not be allowed to generate wakeup events.
  
  At this point the system image is created.  All devices should be inactive and
  the contents of memory should remain undisturbed while this happens, so that the
  image forms an atomic snapshot of the system state.
cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
482
      5.	The thaw_noirq phase is analogous to the resume_noirq phase discussed
d6f9cda1f   Alan Stern   PM: Improve devic...
483
484
  	above.  The main difference is that its methods can assume the device is
  	in the same state as at the end of the freeze_noirq phase.
cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
485
486
487
488
489
      6.	The thaw_early phase is analogous to the resume_early phase described
  	above.  Its methods should undo the actions of the preceding
  	freeze_late, if necessary.
  
      7.	The thaw phase is analogous to the resume phase discussed above.  Its
d6f9cda1f   Alan Stern   PM: Improve devic...
490
491
  	methods should bring the device back to an operating state, so that it
  	can be used for saving the image if necessary.
cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
492
      8.	The complete phase is discussed in the "Leaving System Suspend" section
d6f9cda1f   Alan Stern   PM: Improve devic...
493
494
495
496
  	above.
  
  At this point the system image is saved, and the devices then need to be
  prepared for the upcoming system shutdown.  This is much like suspending them
dc5aeae4f   Zhang Rui   PM: Documentation...
497
498
  before putting the system into the freeze, standby or memory sleep state,
  and the phases are similar.
d6f9cda1f   Alan Stern   PM: Improve devic...
499

cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
500
501
502
      9.	The prepare phase is discussed above.
  
      10.	The poweroff phase is analogous to the suspend phase.
d6f9cda1f   Alan Stern   PM: Improve devic...
503

cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
504
      11.	The poweroff_late phase is analogous to the suspend_late phase.
d6f9cda1f   Alan Stern   PM: Improve devic...
505

cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
506
      12.	The poweroff_noirq phase is analogous to the suspend_noirq phase.
d6f9cda1f   Alan Stern   PM: Improve devic...
507

cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
508
509
510
511
512
  The poweroff, poweroff_late and poweroff_noirq callbacks should do essentially
  the same things as the suspend, suspend_late and suspend_noirq callbacks,
  respectively.  The only notable difference is that they need not store the
  device register values, because the registers should already have been stored
  during the freeze, freeze_late or freeze_noirq phases.
d6f9cda1f   Alan Stern   PM: Improve devic...
513
514
515
516
  
  
  Leaving Hibernation
  -------------------
624f6ec87   Rafael J. Wysocki   PM: Update device...
517
518
519
520
  Resuming from hibernation is, again, more complicated than resuming from a sleep
  state in which the contents of main memory are preserved, because it requires
  a system image to be loaded into memory and the pre-hibernation memory contents
  to be restored before control can be passed back to the image kernel.
d6f9cda1f   Alan Stern   PM: Improve devic...
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
  Although in principle, the image might be loaded into memory and the
  pre-hibernation memory contents restored by the boot loader, in practice this
  can't be done because boot loaders aren't smart enough and there is no
  established protocol for passing the necessary information.  So instead, the
  boot loader loads a fresh instance of the kernel, called the boot kernel, into
  memory and passes control to it in the usual way.  Then the boot kernel reads
  the system image, restores the pre-hibernation memory contents, and passes
  control to the image kernel.  Thus two different kernels are involved in
  resuming from hibernation.  In fact, the boot kernel may be completely different
  from the image kernel: a different configuration and even a different version.
  This has important consequences for device drivers and their subsystems.
  
  To be able to load the system image into memory, the boot kernel needs to
  include at least a subset of device drivers allowing it to access the storage
  medium containing the image, although it doesn't need to include all of the
  drivers present in the image kernel.  After the image has been loaded, the
  devices managed by the boot kernel need to be prepared for passing control back
  to the image kernel.  This is very similar to the initial steps involved in
  creating a system image, and it is accomplished in the same way, using prepare,
  freeze, and freeze_noirq phases.  However the devices affected by these phases
  are only those having drivers in the boot kernel; other devices will still be in
  whatever state the boot loader left them.
624f6ec87   Rafael J. Wysocki   PM: Update device...
543
544
  
  Should the restoration of the pre-hibernation memory contents fail, the boot
d6f9cda1f   Alan Stern   PM: Improve devic...
545
546
547
548
549
  kernel would go through the "thawing" procedure described above, using the
  thaw_noirq, thaw, and complete phases, and then continue running normally.  This
  happens only rarely.  Most often the pre-hibernation memory contents are
  restored successfully and control is passed to the image kernel, which then
  becomes responsible for bringing the system back to the working state.
624f6ec87   Rafael J. Wysocki   PM: Update device...
550

d6f9cda1f   Alan Stern   PM: Improve devic...
551
552
553
  To achieve this, the image kernel must restore the devices' pre-hibernation
  functionality.  The operation is much like waking up from the memory sleep
  state, although it involves different phases:
624f6ec87   Rafael J. Wysocki   PM: Update device...
554

cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
555
  	restore_noirq, restore_early, restore, complete
624f6ec87   Rafael J. Wysocki   PM: Update device...
556

d6f9cda1f   Alan Stern   PM: Improve devic...
557
      1.	The restore_noirq phase is analogous to the resume_noirq phase.
624f6ec87   Rafael J. Wysocki   PM: Update device...
558

cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
559
560
561
      2.	The restore_early phase is analogous to the resume_early phase.
  
      3.	The restore phase is analogous to the resume phase.
624f6ec87   Rafael J. Wysocki   PM: Update device...
562

cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
563
      4.	The complete phase is discussed above.
624f6ec87   Rafael J. Wysocki   PM: Update device...
564

cf579dfb8   Rafael J. Wysocki   PM / Sleep: Intro...
565
566
567
568
569
570
571
572
573
  The main difference from resume[_early|_noirq] is that restore[_early|_noirq]
  must assume the device has been accessed and reconfigured by the boot loader or
  the boot kernel.  Consequently the state of the device may be different from the
  state remembered from the freeze, freeze_late and freeze_noirq phases.  The
  device may even need to be reset and completely re-initialized.  In many cases
  this difference doesn't matter, so the resume[_early|_noirq] and
  restore[_early|_norq] method pointers can be set to the same routines.
  Nevertheless, different callback pointers are used in case there is a situation
  where it actually does matter.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
574

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
575

564b905ab   Rafael J. Wysocki   PM / Domains: Ren...
576
577
  Device Power Management Domains
  -------------------------------
7538e3db6   Rafael J. Wysocki   PM: Add support f...
578
579
580
581
582
583
584
  Sometimes devices share reference clocks or other power resources.  In those
  cases it generally is not possible to put devices into low-power states
  individually.  Instead, a set of devices sharing a power resource can be put
  into a low-power state together at the same time by turning off the shared
  power resource.  Of course, they also need to be put into the full-power state
  together, by turning the shared power resource on.  A set of devices with this
  property is often referred to as a power domain.
564b905ab   Rafael J. Wysocki   PM / Domains: Ren...
585
586
  Support for power domains is provided through the pm_domain field of struct
  device.  This field is a pointer to an object of type struct dev_pm_domain,
7538e3db6   Rafael J. Wysocki   PM: Add support f...
587
588
  defined in include/linux/pm.h, providing a set of power management callbacks
  analogous to the subsystem-level and device driver callbacks that are executed
ca9c6890b   Rafael J. Wysocki   PM / Domains: Upd...
589
590
591
592
  for the given device during all power transitions, instead of the respective
  subsystem-level callbacks.  Specifically, if a device's pm_domain pointer is
  not NULL, the ->suspend() callback from the object pointed to by it will be
  executed instead of its subsystem's (e.g. bus type's) ->suspend() callback and
8d2c79410   Oskar Schirmer   PM / Documentatio...
593
594
595
596
  analogously for all of the remaining callbacks.  In other words, power
  management domain callbacks, if defined for the given device, always take
  precedence over the callbacks provided by the device's subsystem (e.g. bus
  type).
ca9c6890b   Rafael J. Wysocki   PM / Domains: Upd...
597
598
599
600
601
602
603
  
  The support for device power management domains is only relevant to platforms
  needing to use the same device driver power management callbacks in many
  different power domain configurations and wanting to avoid incorporating the
  support for power domains into subsystem-level callbacks, for example by
  modifying the platform bus type.  Other platforms need not implement it or take
  it into account in any way.
7538e3db6   Rafael J. Wysocki   PM: Add support f...
604

d6f9cda1f   Alan Stern   PM: Improve devic...
605
606
607
  Device Low Power (suspend) States
  ---------------------------------
  Device low-power states aren't standard.  One device might only handle
8d2c79410   Oskar Schirmer   PM / Documentatio...
608
  "on" and "off", while another might support a dozen different versions of
d6f9cda1f   Alan Stern   PM: Improve devic...
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
  "on" (how many engines are active?), plus a state that gets back to "on"
  faster than from a full "off".
  
  Some busses define rules about what different suspend states mean.  PCI
  gives one example:  after the suspend sequence completes, a non-legacy
  PCI device may not perform DMA or issue IRQs, and any wakeup events it
  issues would be issued through the PME# bus signal.  Plus, there are
  several PCI-standard device states, some of which are optional.
  
  In contrast, integrated system-on-chip processors often use IRQs as the
  wakeup event sources (so drivers would call enable_irq_wake) and might
  be able to treat DMA completion as a wakeup event (sometimes DMA can stay
  active too, it'd only be the CPU and some peripherals that sleep).
  
  Some details here may be platform-specific.  Systems may have devices that
  can be fully active in certain sleep states, such as an LCD display that's
  refreshed using DMA while most of the system is sleeping lightly ... and
  its frame buffer might even be updated by a DSP or other non-Linux CPU while
  the Linux control processor stays idle.
  
  Moreover, the specific actions taken may depend on the target system state.
  One target system state might allow a given device to be very operational;
  another might require a hard shut down with re-initialization on resume.
  And two different target systems might use the same device in different
  ways; the aforementioned LCD might be active in one product's "standby",
  but a different product using the same SOC might work differently.
624f6ec87   Rafael J. Wysocki   PM: Update device...
635
636
  Power Management Notifiers
  --------------------------
d6f9cda1f   Alan Stern   PM: Improve devic...
637
638
639
640
641
642
643
  There are some operations that cannot be carried out by the power management
  callbacks discussed above, because the callbacks occur too late or too early.
  To handle these cases, subsystems and device drivers may register power
  management notifiers that are called before tasks are frozen and after they have
  been thawed.  Generally speaking, the PM notifiers are suitable for performing
  actions that either require user space to be available, or at least won't
  interfere with user space.
624f6ec87   Rafael J. Wysocki   PM: Update device...
644
645
  
  For details refer to Documentation/power/notifiers.txt.
4fc084000   David Brownell   updated Documenta...
646
647
648
649
650
651
652
  Runtime Power Management
  ========================
  Many devices are able to dynamically power down while the system is still
  running. This feature is useful for devices that are not being used, and
  can offer significant power savings on a running system.  These devices
  often support a range of runtime power states, which might use names such
  as "off", "sleep", "idle", "active", and so on.  Those states will in some
d6f9cda1f   Alan Stern   PM: Improve devic...
653
  cases (like PCI) be partially constrained by the bus the device uses, and will
4fc084000   David Brownell   updated Documenta...
654
  usually include hardware states that are also used in system sleep states.
d6f9cda1f   Alan Stern   PM: Improve devic...
655
656
657
658
659
660
661
662
663
664
665
666
  A system-wide power transition can be started while some devices are in low
  power states due to runtime power management.  The system sleep PM callbacks
  should recognize such situations and react to them appropriately, but the
  necessary actions are subsystem-specific.
  
  In some cases the decision may be made at the subsystem level while in other
  cases the device driver may be left to decide.  In some cases it may be
  desirable to leave a suspended device in that state during a system-wide power
  transition, but in other cases the device must be put back into the full-power
  state temporarily, for example so that its system wakeup capability can be
  disabled.  This all depends on the hardware and the design of the subsystem and
  device driver in question.
455716e9b   Rafael J. Wysocki   PM / Runtime: Upd...
667
668
669
  During system-wide resume from a sleep state it's easiest to put devices into
  the full-power state, as explained in Documentation/power/runtime_pm.txt.  Refer
  to that document for more information regarding this particular issue as well as
624f6ec87   Rafael J. Wysocki   PM: Update device...
670
  for information on the device runtime power management framework in general.