xref: /linux-6.15/include/linux/pm.h (revision aeb3f462)
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 #ifdef __KERNEL__
25 
26 #include <linux/list.h>
27 #include <asm/atomic.h>
28 #include <asm/errno.h>
29 
30 /*
31  * Power management requests... these are passed to pm_send_all() and friends.
32  *
33  * these functions are old and deprecated, see below.
34  */
35 typedef int __bitwise pm_request_t;
36 
37 #define PM_SUSPEND	((__force pm_request_t) 1)	/* enter D1-D3 */
38 #define PM_RESUME	((__force pm_request_t) 2)	/* enter D0 */
39 
40 
41 /*
42  * Device types... these are passed to pm_register
43  */
44 typedef int __bitwise pm_dev_t;
45 
46 #define PM_UNKNOWN_DEV	((__force pm_dev_t) 0)	/* generic */
47 #define PM_SYS_DEV	((__force pm_dev_t) 1)	/* system device (fan, KB controller, ...) */
48 #define PM_PCI_DEV	((__force pm_dev_t) 2)	/* PCI device */
49 #define PM_USB_DEV	((__force pm_dev_t) 3)	/* USB device */
50 #define PM_SCSI_DEV	((__force pm_dev_t) 4)	/* SCSI device */
51 #define PM_ISA_DEV	((__force pm_dev_t) 5)	/* ISA device */
52 #define	PM_MTD_DEV	((__force pm_dev_t) 6)	/* Memory Technology Device */
53 
54 /*
55  * System device hardware ID (PnP) values
56  */
57 enum
58 {
59 	PM_SYS_UNKNOWN = 0x00000000, /* generic */
60 	PM_SYS_KBC =	 0x41d00303, /* keyboard controller */
61 	PM_SYS_COM =	 0x41d00500, /* serial port */
62 	PM_SYS_IRDA =	 0x41d00510, /* IRDA controller */
63 	PM_SYS_FDC =	 0x41d00700, /* floppy controller */
64 	PM_SYS_VGA =	 0x41d00900, /* VGA controller */
65 	PM_SYS_PCMCIA =	 0x41d00e00, /* PCMCIA controller */
66 };
67 
68 /*
69  * Device identifier
70  */
71 #define PM_PCI_ID(dev) ((dev)->bus->number << 16 | (dev)->devfn)
72 
73 /*
74  * Request handler callback
75  */
76 struct pm_dev;
77 
78 typedef int (*pm_callback)(struct pm_dev *dev, pm_request_t rqst, void *data);
79 
80 /*
81  * Dynamic device information
82  */
83 struct pm_dev
84 {
85 	pm_dev_t	 type;
86 	unsigned long	 id;
87 	pm_callback	 callback;
88 	void		*data;
89 
90 	unsigned long	 flags;
91 	unsigned long	 state;
92 	unsigned long	 prev_state;
93 
94 	struct list_head entry;
95 };
96 
97 /* Functions above this comment are list-based old-style power
98  * managment. Please avoid using them.  */
99 
100 /*
101  * Callbacks for platform drivers to implement.
102  */
103 extern void (*pm_idle)(void);
104 extern void (*pm_power_off)(void);
105 extern void (*pm_power_off_prepare)(void);
106 
107 typedef int __bitwise suspend_state_t;
108 
109 #define PM_SUSPEND_ON		((__force suspend_state_t) 0)
110 #define PM_SUSPEND_STANDBY	((__force suspend_state_t) 1)
111 #define PM_SUSPEND_MEM		((__force suspend_state_t) 3)
112 #define PM_SUSPEND_MAX		((__force suspend_state_t) 4)
113 
114 /**
115  * struct pm_ops - Callbacks for managing platform dependent system sleep
116  *	states.
117  *
118  * @valid: Callback to determine if given system sleep state is supported by
119  *	the platform.
120  *	Valid (ie. supported) states are advertised in /sys/power/state.  Note
121  *	that it still may be impossible to enter given system sleep state if the
122  *	conditions aren't right.
123  *	There is the %pm_valid_only_mem function available that can be assigned
124  *	to this if the platform only supports mem sleep.
125  *
126  * @set_target: Tell the platform which system sleep state is going to be
127  *	entered.
128  *	@set_target() is executed right prior to suspending devices.  The
129  *	information conveyed to the platform code by @set_target() should be
130  *	disregarded by the platform as soon as @finish() is executed and if
131  *	@prepare() fails.  If @set_target() fails (ie. returns nonzero),
132  *	@prepare(), @enter() and @finish() will not be called by the PM core.
133  *	This callback is optional.  However, if it is implemented, the argument
134  *	passed to @prepare(), @enter() and @finish() is meaningless and should
135  *	be ignored.
136  *
137  * @prepare: Prepare the platform for entering the system sleep state indicated
138  *	by @set_target() or represented by the argument if @set_target() is not
139  *	implemented.
140  *	@prepare() is called right after devices have been suspended (ie. the
141  *	appropriate .suspend() method has been executed for each device) and
142  *	before the nonboot CPUs are disabled (it is executed with IRQs enabled).
143  *	This callback is optional.  It returns 0 on success or a negative
144  *	error code otherwise, in which case the system cannot enter the desired
145  *	sleep state (@enter() and @finish() will not be called in that case).
146  *
147  * @enter: Enter the system sleep state indicated by @set_target() or
148  *	represented by the argument if @set_target() is not implemented.
149  *	This callback is mandatory.  It returns 0 on success or a negative
150  *	error code otherwise, in which case the system cannot enter the desired
151  *	sleep state.
152  *
153  * @finish: Called when the system has just left a sleep state, right after
154  *	the nonboot CPUs have been enabled and before devices are resumed (it is
155  *	executed with IRQs enabled).  If @set_target() is not implemented, the
156  *	argument represents the sleep state being left.
157  *	This callback is optional, but should be implemented by the platforms
158  *	that implement @prepare().  If implemented, it is always called after
159  *	@enter() (even if @enter() fails).
160  */
161 struct pm_ops {
162 	int (*valid)(suspend_state_t state);
163 	int (*set_target)(suspend_state_t state);
164 	int (*prepare)(suspend_state_t state);
165 	int (*enter)(suspend_state_t state);
166 	int (*finish)(suspend_state_t state);
167 };
168 
169 #ifdef CONFIG_SUSPEND
170 extern struct pm_ops *pm_ops;
171 
172 /**
173  * pm_set_ops - set platform dependent power management ops
174  * @pm_ops: The new power management operations to set.
175  */
176 extern void pm_set_ops(struct pm_ops *pm_ops);
177 extern int pm_valid_only_mem(suspend_state_t state);
178 
179 /**
180  * arch_suspend_disable_irqs - disable IRQs for suspend
181  *
182  * Disables IRQs (in the default case). This is a weak symbol in the common
183  * code and thus allows architectures to override it if more needs to be
184  * done. Not called for suspend to disk.
185  */
186 extern void arch_suspend_disable_irqs(void);
187 
188 /**
189  * arch_suspend_enable_irqs - enable IRQs after suspend
190  *
191  * Enables IRQs (in the default case). This is a weak symbol in the common
192  * code and thus allows architectures to override it if more needs to be
193  * done. Not called for suspend to disk.
194  */
195 extern void arch_suspend_enable_irqs(void);
196 
197 extern int pm_suspend(suspend_state_t state);
198 #else /* !CONFIG_SUSPEND */
199 #define suspend_valid_only_mem	NULL
200 
201 static inline void pm_set_ops(struct pm_ops *pm_ops) {}
202 static inline int pm_suspend(suspend_state_t state) { return -ENOSYS; }
203 #endif /* !CONFIG_SUSPEND */
204 
205 /*
206  * Device power management
207  */
208 
209 struct device;
210 
211 typedef struct pm_message {
212 	int event;
213 } pm_message_t;
214 
215 /*
216  * Several driver power state transitions are externally visible, affecting
217  * the state of pending I/O queues and (for drivers that touch hardware)
218  * interrupts, wakeups, DMA, and other hardware state.  There may also be
219  * internal transitions to various low power modes, which are transparent
220  * to the rest of the driver stack (such as a driver that's ON gating off
221  * clocks which are not in active use).
222  *
223  * One transition is triggered by resume(), after a suspend() call; the
224  * message is implicit:
225  *
226  * ON		Driver starts working again, responding to hardware events
227  * 		and software requests.  The hardware may have gone through
228  * 		a power-off reset, or it may have maintained state from the
229  * 		previous suspend() which the driver will rely on while
230  * 		resuming.  On most platforms, there are no restrictions on
231  * 		availability of resources like clocks during resume().
232  *
233  * Other transitions are triggered by messages sent using suspend().  All
234  * these transitions quiesce the driver, so that I/O queues are inactive.
235  * That commonly entails turning off IRQs and DMA; there may be rules
236  * about how to quiesce that are specific to the bus or the device's type.
237  * (For example, network drivers mark the link state.)  Other details may
238  * differ according to the message:
239  *
240  * SUSPEND	Quiesce, enter a low power device state appropriate for
241  * 		the upcoming system state (such as PCI_D3hot), and enable
242  * 		wakeup events as appropriate.
243  *
244  * FREEZE	Quiesce operations so that a consistent image can be saved;
245  * 		but do NOT otherwise enter a low power device state, and do
246  * 		NOT emit system wakeup events.
247  *
248  * PRETHAW	Quiesce as if for FREEZE; additionally, prepare for restoring
249  * 		the system from a snapshot taken after an earlier FREEZE.
250  * 		Some drivers will need to reset their hardware state instead
251  * 		of preserving it, to ensure that it's never mistaken for the
252  * 		state which that earlier snapshot had set up.
253  *
254  * A minimally power-aware driver treats all messages as SUSPEND, fully
255  * reinitializes its device during resume() -- whether or not it was reset
256  * during the suspend/resume cycle -- and can't issue wakeup events.
257  *
258  * More power-aware drivers may also use low power states at runtime as
259  * well as during system sleep states like PM_SUSPEND_STANDBY.  They may
260  * be able to use wakeup events to exit from runtime low-power states,
261  * or from system low-power states such as standby or suspend-to-RAM.
262  */
263 
264 #define PM_EVENT_ON 0
265 #define PM_EVENT_FREEZE 1
266 #define PM_EVENT_SUSPEND 2
267 #define PM_EVENT_PRETHAW 3
268 
269 #define PMSG_FREEZE	((struct pm_message){ .event = PM_EVENT_FREEZE, })
270 #define PMSG_PRETHAW	((struct pm_message){ .event = PM_EVENT_PRETHAW, })
271 #define PMSG_SUSPEND	((struct pm_message){ .event = PM_EVENT_SUSPEND, })
272 #define PMSG_ON		((struct pm_message){ .event = PM_EVENT_ON, })
273 
274 struct dev_pm_info {
275 	pm_message_t		power_state;
276 	unsigned		can_wakeup:1;
277 #ifdef	CONFIG_PM_SLEEP
278 	unsigned		should_wakeup:1;
279 	struct list_head	entry;
280 #endif
281 };
282 
283 extern int device_power_down(pm_message_t state);
284 extern void device_power_up(void);
285 extern void device_resume(void);
286 
287 #ifdef CONFIG_PM_SLEEP
288 extern int device_suspend(pm_message_t state);
289 extern int device_prepare_suspend(pm_message_t state);
290 
291 #define device_set_wakeup_enable(dev,val) \
292 	((dev)->power.should_wakeup = !!(val))
293 #define device_may_wakeup(dev) \
294 	(device_can_wakeup(dev) && (dev)->power.should_wakeup)
295 
296 extern void __suspend_report_result(const char *function, void *fn, int ret);
297 
298 #define suspend_report_result(fn, ret)					\
299 	do {								\
300 		__suspend_report_result(__FUNCTION__, fn, ret);		\
301 	} while (0)
302 
303 /*
304  * Platform hook to activate device wakeup capability, if that's not already
305  * handled by enable_irq_wake() etc.
306  * Returns zero on success, else negative errno
307  */
308 extern int (*platform_enable_wakeup)(struct device *dev, int is_on);
309 
310 static inline int call_platform_enable_wakeup(struct device *dev, int is_on)
311 {
312 	if (platform_enable_wakeup)
313 		return (*platform_enable_wakeup)(dev, is_on);
314 	return 0;
315 }
316 
317 #else /* !CONFIG_PM_SLEEP */
318 
319 static inline int device_suspend(pm_message_t state)
320 {
321 	return 0;
322 }
323 
324 #define device_set_wakeup_enable(dev,val)	do{}while(0)
325 #define device_may_wakeup(dev)			(0)
326 
327 #define suspend_report_result(fn, ret) do { } while (0)
328 
329 static inline int call_platform_enable_wakeup(struct device *dev, int is_on)
330 {
331 	return 0;
332 }
333 
334 #endif /* !CONFIG_PM_SLEEP */
335 
336 /* changes to device_may_wakeup take effect on the next pm state change.
337  * by default, devices should wakeup if they can.
338  */
339 #define device_can_wakeup(dev) \
340 	((dev)->power.can_wakeup)
341 #define device_init_wakeup(dev,val) \
342 	do { \
343 		device_can_wakeup(dev) = !!(val); \
344 		device_set_wakeup_enable(dev,val); \
345 	} while(0)
346 
347 #endif /* __KERNEL__ */
348 
349 #endif /* _LINUX_PM_H */
350