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