pm.h revision e7ecb331e11d1f7aa66aeef9170fc20781c9bb55
1/*
2 *  pm.h - Power management interface
3 *
4 *  Copyright (C) 2000 Andrew Henroid
5 *
6 *  This program is free software; you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License as published by
8 *  the Free Software Foundation; either version 2 of the License, or
9 *  (at your option) any later version.
10 *
11 *  This program is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with this program; if not, write to the Free Software
18 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21#ifndef _LINUX_PM_H
22#define _LINUX_PM_H
23
24#include <linux/list.h>
25#include <asm/atomic.h>
26#include <asm/errno.h>
27
28/*
29 * Callbacks for platform drivers to implement.
30 */
31extern void (*pm_idle)(void);
32extern void (*pm_power_off)(void);
33extern void (*pm_power_off_prepare)(void);
34
35/*
36 * Device power management
37 */
38
39struct device;
40
41typedef struct pm_message {
42	int event;
43} pm_message_t;
44
45/**
46 * struct pm_ops - device PM callbacks
47 *
48 * Several driver power state transitions are externally visible, affecting
49 * the state of pending I/O queues and (for drivers that touch hardware)
50 * interrupts, wakeups, DMA, and other hardware state.  There may also be
51 * internal transitions to various low power modes, which are transparent
52 * to the rest of the driver stack (such as a driver that's ON gating off
53 * clocks which are not in active use).
54 *
55 * The externally visible transitions are handled with the help of the following
56 * callbacks included in this structure:
57 *
58 * @prepare: Prepare the device for the upcoming transition, but do NOT change
59 *	its hardware state.  Prevent new children of the device from being
60 *	registered after @prepare() returns (the driver's subsystem and
61 *	generally the rest of the kernel is supposed to prevent new calls to the
62 *	probe method from being made too once @prepare() has succeeded).  If
63 *	@prepare() detects a situation it cannot handle (e.g. registration of a
64 *	child already in progress), it may return -EAGAIN, so that the PM core
65 *	can execute it once again (e.g. after the new child has been registered)
66 *	to recover from the race condition.  This method is executed for all
67 *	kinds of suspend transitions and is followed by one of the suspend
68 *	callbacks: @suspend(), @freeze(), or @poweroff().
69 *	The PM core executes @prepare() for all devices before starting to
70 *	execute suspend callbacks for any of them, so drivers may assume all of
71 *	the other devices to be present and functional while @prepare() is being
72 *	executed.  In particular, it is safe to make GFP_KERNEL memory
73 *	allocations from within @prepare().  However, drivers may NOT assume
74 *	anything about the availability of the user space at that time and it
75 *	is not correct to request firmware from within @prepare() (it's too
76 *	late to do that).  [To work around this limitation, drivers may
77 *	register suspend and hibernation notifiers that are executed before the
78 *	freezing of tasks.]
79 *
80 * @complete: Undo the changes made by @prepare().  This method is executed for
81 *	all kinds of resume transitions, following one of the resume callbacks:
82 *	@resume(), @thaw(), @restore().  Also called if the state transition
83 *	fails before the driver's suspend callback (@suspend(), @freeze(),
84 *	@poweroff()) can be executed (e.g. if the suspend callback fails for one
85 *	of the other devices that the PM core has unsuccessfully attempted to
86 *	suspend earlier).
87 *	The PM core executes @complete() after it has executed the appropriate
88 *	resume callback for all devices.
89 *
90 * @suspend: Executed before putting the system into a sleep state in which the
91 *	contents of main memory are preserved.  Quiesce the device, put it into
92 *	a low power state appropriate for the upcoming system state (such as
93 *	PCI_D3hot), and enable wakeup events as appropriate.
94 *
95 * @resume: Executed after waking the system up from a sleep state in which the
96 *	contents of main memory were preserved.  Put the device into the
97 *	appropriate state, according to the information saved in memory by the
98 *	preceding @suspend().  The driver starts working again, responding to
99 *	hardware events and software requests.  The hardware may have gone
100 *	through a power-off reset, or it may have maintained state from the
101 *	previous suspend() which the driver may rely on while resuming.  On most
102 *	platforms, there are no restrictions on availability of resources like
103 *	clocks during @resume().
104 *
105 * @freeze: Hibernation-specific, executed before creating a hibernation image.
106 *	Quiesce operations so that a consistent image can be created, but do NOT
107 *	otherwise put the device into a low power device state and do NOT emit
108 *	system wakeup events.  Save in main memory the device settings to be
109 *	used by @restore() during the subsequent resume from hibernation or by
110 *	the subsequent @thaw(), if the creation of the image or the restoration
111 *	of main memory contents from it fails.
112 *
113 * @thaw: Hibernation-specific, executed after creating a hibernation image OR
114 *	if the creation of the image fails.  Also executed after a failing
115 *	attempt to restore the contents of main memory from such an image.
116 *	Undo the changes made by the preceding @freeze(), so the device can be
117 *	operated in the same way as immediately before the call to @freeze().
118 *
119 * @poweroff: Hibernation-specific, executed after saving a hibernation image.
120 *	Quiesce the device, put it into a low power state appropriate for the
121 *	upcoming system state (such as PCI_D3hot), and enable wakeup events as
122 *	appropriate.
123 *
124 * @restore: Hibernation-specific, executed after restoring the contents of main
125 *	memory from a hibernation image.  Driver starts working again,
126 *	responding to hardware events and software requests.  Drivers may NOT
127 *	make ANY assumptions about the hardware state right prior to @restore().
128 *	On most platforms, there are no restrictions on availability of
129 *	resources like clocks during @restore().
130 *
131 * All of the above callbacks, except for @complete(), return error codes.
132 * However, the error codes returned by the resume operations, @resume(),
133 * @thaw(), and @restore(), do not cause the PM core to abort the resume
134 * transition during which they are returned.  The error codes returned in
135 * that cases are only printed by the PM core to the system logs for debugging
136 * purposes.  Still, it is recommended that drivers only return error codes
137 * from their resume methods in case of an unrecoverable failure (i.e. when the
138 * device being handled refuses to resume and becomes unusable) to allow us to
139 * modify the PM core in the future, so that it can avoid attempting to handle
140 * devices that failed to resume and their children.
141 *
142 * It is allowed to unregister devices while the above callbacks are being
143 * executed.  However, it is not allowed to unregister a device from within any
144 * of its own callbacks.
145 */
146
147struct pm_ops {
148	int (*prepare)(struct device *dev);
149	void (*complete)(struct device *dev);
150	int (*suspend)(struct device *dev);
151	int (*resume)(struct device *dev);
152	int (*freeze)(struct device *dev);
153	int (*thaw)(struct device *dev);
154	int (*poweroff)(struct device *dev);
155	int (*restore)(struct device *dev);
156};
157
158/**
159 * struct pm_ext_ops - extended device PM callbacks
160 *
161 * Some devices require certain operations related to suspend and hibernation
162 * to be carried out with interrupts disabled.  Thus, 'struct pm_ext_ops' below
163 * is defined, adding callbacks to be executed with interrupts disabled to
164 * 'struct pm_ops'.
165 *
166 * The following callbacks included in 'struct pm_ext_ops' are executed with
167 * the nonboot CPUs switched off and with interrupts disabled on the only
168 * functional CPU.  They also are executed with the PM core list of devices
169 * locked, so they must NOT unregister any devices.
170 *
171 * @suspend_noirq: Complete the operations of ->suspend() by carrying out any
172 *	actions required for suspending the device that need interrupts to be
173 *	disabled
174 *
175 * @resume_noirq: Prepare for the execution of ->resume() by carrying out any
176 *	actions required for resuming the device that need interrupts to be
177 *	disabled
178 *
179 * @freeze_noirq: Complete the operations of ->freeze() by carrying out any
180 *	actions required for freezing the device that need interrupts to be
181 *	disabled
182 *
183 * @thaw_noirq: Prepare for the execution of ->thaw() by carrying out any
184 *	actions required for thawing the device that need interrupts to be
185 *	disabled
186 *
187 * @poweroff_noirq: Complete the operations of ->poweroff() by carrying out any
188 *	actions required for handling the device that need interrupts to be
189 *	disabled
190 *
191 * @restore_noirq: Prepare for the execution of ->restore() by carrying out any
192 *	actions required for restoring the operations of the device that need
193 *	interrupts to be disabled
194 *
195 * All of the above callbacks return error codes, but the error codes returned
196 * by the resume operations, @resume_noirq(), @thaw_noirq(), and
197 * @restore_noirq(), do not cause the PM core to abort the resume transition
198 * during which they are returned.  The error codes returned in that cases are
199 * only printed by the PM core to the system logs for debugging purposes.
200 * Still, as stated above, it is recommended that drivers only return error
201 * codes from their resume methods if the device being handled fails to resume
202 * and is not usable any more.
203 */
204
205struct pm_ext_ops {
206	struct pm_ops base;
207	int (*suspend_noirq)(struct device *dev);
208	int (*resume_noirq)(struct device *dev);
209	int (*freeze_noirq)(struct device *dev);
210	int (*thaw_noirq)(struct device *dev);
211	int (*poweroff_noirq)(struct device *dev);
212	int (*restore_noirq)(struct device *dev);
213};
214
215/**
216 * PM_EVENT_ messages
217 *
218 * The following PM_EVENT_ messages are defined for the internal use of the PM
219 * core, in order to provide a mechanism allowing the high level suspend and
220 * hibernation code to convey the necessary information to the device PM core
221 * code:
222 *
223 * ON		No transition.
224 *
225 * FREEZE 	System is going to hibernate, call ->prepare() and ->freeze()
226 *		for all devices.
227 *
228 * SUSPEND	System is going to suspend, call ->prepare() and ->suspend()
229 *		for all devices.
230 *
231 * HIBERNATE	Hibernation image has been saved, call ->prepare() and
232 *		->poweroff() for all devices.
233 *
234 * QUIESCE	Contents of main memory are going to be restored from a (loaded)
235 *		hibernation image, call ->prepare() and ->freeze() for all
236 *		devices.
237 *
238 * RESUME	System is resuming, call ->resume() and ->complete() for all
239 *		devices.
240 *
241 * THAW		Hibernation image has been created, call ->thaw() and
242 *		->complete() for all devices.
243 *
244 * RESTORE	Contents of main memory have been restored from a hibernation
245 *		image, call ->restore() and ->complete() for all devices.
246 *
247 * RECOVER	Creation of a hibernation image or restoration of the main
248 *		memory contents from a hibernation image has failed, call
249 *		->thaw() and ->complete() for all devices.
250 */
251
252#define PM_EVENT_ON		0x0000
253#define PM_EVENT_FREEZE 	0x0001
254#define PM_EVENT_SUSPEND	0x0002
255#define PM_EVENT_HIBERNATE	0x0004
256#define PM_EVENT_QUIESCE	0x0008
257#define PM_EVENT_RESUME		0x0010
258#define PM_EVENT_THAW		0x0020
259#define PM_EVENT_RESTORE	0x0040
260#define PM_EVENT_RECOVER	0x0080
261
262#define PM_EVENT_SLEEP	(PM_EVENT_SUSPEND | PM_EVENT_HIBERNATE)
263
264#define PMSG_FREEZE	((struct pm_message){ .event = PM_EVENT_FREEZE, })
265#define PMSG_QUIESCE	((struct pm_message){ .event = PM_EVENT_QUIESCE, })
266#define PMSG_SUSPEND	((struct pm_message){ .event = PM_EVENT_SUSPEND, })
267#define PMSG_HIBERNATE	((struct pm_message){ .event = PM_EVENT_HIBERNATE, })
268#define PMSG_RESUME	((struct pm_message){ .event = PM_EVENT_RESUME, })
269#define PMSG_THAW	((struct pm_message){ .event = PM_EVENT_THAW, })
270#define PMSG_RESTORE	((struct pm_message){ .event = PM_EVENT_RESTORE, })
271#define PMSG_RECOVER	((struct pm_message){ .event = PM_EVENT_RECOVER, })
272#define PMSG_ON		((struct pm_message){ .event = PM_EVENT_ON, })
273
274/**
275 * Device power management states
276 *
277 * These state labels are used internally by the PM core to indicate the current
278 * status of a device with respect to the PM core operations.
279 *
280 * DPM_ON		Device is regarded as operational.  Set this way
281 *			initially and when ->complete() is about to be called.
282 *			Also set when ->prepare() fails.
283 *
284 * DPM_PREPARING	Device is going to be prepared for a PM transition.  Set
285 *			when ->prepare() is about to be called.
286 *
287 * DPM_RESUMING		Device is going to be resumed.  Set when ->resume(),
288 *			->thaw(), or ->restore() is about to be called.
289 *
290 * DPM_SUSPENDING	Device has been prepared for a power transition.  Set
291 *			when ->prepare() has just succeeded.
292 *
293 * DPM_OFF		Device is regarded as inactive.  Set immediately after
294 *			->suspend(), ->freeze(), or ->poweroff() has succeeded.
295 *			Also set when ->resume()_noirq, ->thaw_noirq(), or
296 *			->restore_noirq() is about to be called.
297 *
298 * DPM_OFF_IRQ		Device is in a "deep sleep".  Set immediately after
299 *			->suspend_noirq(), ->freeze_noirq(), or
300 *			->poweroff_noirq() has just succeeded.
301 */
302
303enum dpm_state {
304	DPM_INVALID,
305	DPM_ON,
306	DPM_PREPARING,
307	DPM_RESUMING,
308	DPM_SUSPENDING,
309	DPM_OFF,
310	DPM_OFF_IRQ,
311};
312
313struct dev_pm_info {
314	pm_message_t		power_state;
315	unsigned		can_wakeup:1;
316	unsigned		should_wakeup:1;
317	enum dpm_state		status;		/* Owned by the PM core */
318#ifdef	CONFIG_PM_SLEEP
319	struct list_head	entry;
320#endif
321};
322
323/*
324 * The PM_EVENT_ messages are also used by drivers implementing the legacy
325 * suspend framework, based on the ->suspend() and ->resume() callbacks common
326 * for suspend and hibernation transitions, according to the rules below.
327 */
328
329/* Necessary, because several drivers use PM_EVENT_PRETHAW */
330#define PM_EVENT_PRETHAW PM_EVENT_QUIESCE
331
332/*
333 * One transition is triggered by resume(), after a suspend() call; the
334 * message is implicit:
335 *
336 * ON		Driver starts working again, responding to hardware events
337 * 		and software requests.  The hardware may have gone through
338 * 		a power-off reset, or it may have maintained state from the
339 * 		previous suspend() which the driver will rely on while
340 * 		resuming.  On most platforms, there are no restrictions on
341 * 		availability of resources like clocks during resume().
342 *
343 * Other transitions are triggered by messages sent using suspend().  All
344 * these transitions quiesce the driver, so that I/O queues are inactive.
345 * That commonly entails turning off IRQs and DMA; there may be rules
346 * about how to quiesce that are specific to the bus or the device's type.
347 * (For example, network drivers mark the link state.)  Other details may
348 * differ according to the message:
349 *
350 * SUSPEND	Quiesce, enter a low power device state appropriate for
351 * 		the upcoming system state (such as PCI_D3hot), and enable
352 * 		wakeup events as appropriate.
353 *
354 * HIBERNATE	Enter a low power device state appropriate for the hibernation
355 * 		state (eg. ACPI S4) and enable wakeup events as appropriate.
356 *
357 * FREEZE	Quiesce operations so that a consistent image can be saved;
358 * 		but do NOT otherwise enter a low power device state, and do
359 * 		NOT emit system wakeup events.
360 *
361 * PRETHAW	Quiesce as if for FREEZE; additionally, prepare for restoring
362 * 		the system from a snapshot taken after an earlier FREEZE.
363 * 		Some drivers will need to reset their hardware state instead
364 * 		of preserving it, to ensure that it's never mistaken for the
365 * 		state which that earlier snapshot had set up.
366 *
367 * A minimally power-aware driver treats all messages as SUSPEND, fully
368 * reinitializes its device during resume() -- whether or not it was reset
369 * during the suspend/resume cycle -- and can't issue wakeup events.
370 *
371 * More power-aware drivers may also use low power states at runtime as
372 * well as during system sleep states like PM_SUSPEND_STANDBY.  They may
373 * be able to use wakeup events to exit from runtime low-power states,
374 * or from system low-power states such as standby or suspend-to-RAM.
375 */
376
377#ifdef CONFIG_PM_SLEEP
378extern void device_pm_lock(void);
379extern void device_power_up(pm_message_t state);
380extern void device_resume(pm_message_t state);
381
382extern void device_pm_unlock(void);
383extern int device_power_down(pm_message_t state);
384extern int device_suspend(pm_message_t state);
385extern int device_prepare_suspend(pm_message_t state);
386
387extern void __suspend_report_result(const char *function, void *fn, int ret);
388
389#define suspend_report_result(fn, ret)					\
390	do {								\
391		__suspend_report_result(__FUNCTION__, fn, ret);		\
392	} while (0)
393
394#else /* !CONFIG_PM_SLEEP */
395
396static inline int device_suspend(pm_message_t state)
397{
398	return 0;
399}
400
401#define suspend_report_result(fn, ret)		do {} while (0)
402
403#endif /* !CONFIG_PM_SLEEP */
404
405/*
406 * Global Power Management flags
407 * Used to keep APM and ACPI from both being active
408 */
409extern unsigned int	pm_flags;
410
411#define PM_APM	1
412#define PM_ACPI	2
413
414#endif /* _LINUX_PM_H */
415