xref: /linux/include/linux/pm.h (revision 9b1b3dcd28c271fc8c4a87e81860f3a34b6d29b7)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *  pm.h - Power management interface
4  *
5  *  Copyright (C) 2000 Andrew Henroid
6  */
7 
8 #ifndef _LINUX_PM_H
9 #define _LINUX_PM_H
10 
11 #include <linux/completion.h>
12 #include <linux/export.h>
13 #include <linux/hrtimer_types.h>
14 #include <linux/mutex.h>
15 #include <linux/spinlock.h>
16 #include <linux/types.h>
17 #include <linux/util_macros.h>
18 #include <linux/wait.h>
19 #include <linux/workqueue_types.h>
20 
21 /*
22  * Callbacks for platform drivers to implement.
23  */
24 extern void (*pm_power_off)(void);
25 
26 struct device; /* we have a circular dep with device.h */
27 #ifdef CONFIG_VT_CONSOLE_SLEEP
28 extern int pm_vt_switch_required(struct device *dev, bool required);
29 extern void pm_vt_switch_unregister(struct device *dev);
30 #else
pm_vt_switch_required(struct device * dev,bool required)31 static inline int pm_vt_switch_required(struct device *dev, bool required)
32 {
33 	return 0;
34 }
pm_vt_switch_unregister(struct device * dev)35 static inline void pm_vt_switch_unregister(struct device *dev)
36 {
37 }
38 #endif /* CONFIG_VT_CONSOLE_SLEEP */
39 
40 #ifdef CONFIG_CXL_SUSPEND
41 bool cxl_mem_active(void);
42 #else
cxl_mem_active(void)43 static inline bool cxl_mem_active(void)
44 {
45 	return false;
46 }
47 #endif
48 
49 /*
50  * Device power management
51  */
52 
53 
54 #ifdef CONFIG_PM
55 extern const char power_group_name[];		/* = "power" */
56 #else
57 #define power_group_name	NULL
58 #endif
59 
60 typedef struct pm_message {
61 	int event;
62 } pm_message_t;
63 
64 /**
65  * struct dev_pm_ops - device PM callbacks.
66  *
67  * @prepare: The principal role of this callback is to prevent new children of
68  *	the device from being registered after it has returned (the driver's
69  *	subsystem and generally the rest of the kernel is supposed to prevent
70  *	new calls to the probe method from being made too once @prepare() has
71  *	succeeded).  If @prepare() detects a situation it cannot handle (e.g.
72  *	registration of a child already in progress), it may return -EAGAIN, so
73  *	that the PM core can execute it once again (e.g. after a new child has
74  *	been registered) to recover from the race condition.
75  *	This method is executed for all kinds of suspend transitions and is
76  *	followed by one of the suspend callbacks: @suspend(), @freeze(), or
77  *	@poweroff().  If the transition is a suspend to memory or standby (that
78  *	is, not related to hibernation), the return value of @prepare() may be
79  *	used to indicate to the PM core to leave the device in runtime suspend
80  *	if applicable.  Namely, if @prepare() returns a positive number, the PM
81  *	core will understand that as a declaration that the device appears to be
82  *	runtime-suspended and it may be left in that state during the entire
83  *	transition and during the subsequent resume if all of its descendants
84  *	are left in runtime suspend too.  If that happens, @complete() will be
85  *	executed directly after @prepare() and it must ensure the proper
86  *	functioning of the device after the system resume.
87  *	The PM core executes subsystem-level @prepare() for all devices before
88  *	starting to invoke suspend callbacks for any of them, so generally
89  *	devices may be assumed to be functional or to respond to runtime resume
90  *	requests while @prepare() is being executed.  However, device drivers
91  *	may NOT assume anything about the availability of user space at that
92  *	time and it is NOT valid to request firmware from within @prepare()
93  *	(it's too late to do that).  It also is NOT valid to allocate
94  *	substantial amounts of memory from @prepare() in the GFP_KERNEL mode.
95  *	[To work around these limitations, drivers may register suspend and
96  *	hibernation notifiers to be executed before the freezing of tasks.]
97  *
98  * @complete: Undo the changes made by @prepare().  This method is executed for
99  *	all kinds of resume transitions, following one of the resume callbacks:
100  *	@resume(), @thaw(), @restore().  Also called if the state transition
101  *	fails before the driver's suspend callback: @suspend(), @freeze() or
102  *	@poweroff(), can be executed (e.g. if the suspend callback fails for one
103  *	of the other devices that the PM core has unsuccessfully attempted to
104  *	suspend earlier).
105  *	The PM core executes subsystem-level @complete() after it has executed
106  *	the appropriate resume callbacks for all devices.  If the corresponding
107  *	@prepare() at the beginning of the suspend transition returned a
108  *	positive number and the device was left in runtime suspend (without
109  *	executing any suspend and resume callbacks for it), @complete() will be
110  *	the only callback executed for the device during resume.  In that case,
111  *	@complete() must be prepared to do whatever is necessary to ensure the
112  *	proper functioning of the device after the system resume.  To this end,
113  *	@complete() can check the power.direct_complete flag of the device to
114  *	learn whether (unset) or not (set) the previous suspend and resume
115  *	callbacks have been executed for it.
116  *
117  * @suspend: Executed before putting the system into a sleep state in which the
118  *	contents of main memory are preserved.  The exact action to perform
119  *	depends on the device's subsystem (PM domain, device type, class or bus
120  *	type), but generally the device must be quiescent after subsystem-level
121  *	@suspend() has returned, so that it doesn't do any I/O or DMA.
122  *	Subsystem-level @suspend() is executed for all devices after invoking
123  *	subsystem-level @prepare() for all of them.
124  *
125  * @suspend_late: Continue operations started by @suspend().  For a number of
126  *	devices @suspend_late() may point to the same callback routine as the
127  *	runtime suspend callback.
128  *
129  * @resume: Executed after waking the system up from a sleep state in which the
130  *	contents of main memory were preserved.  The exact action to perform
131  *	depends on the device's subsystem, but generally the driver is expected
132  *	to start working again, responding to hardware events and software
133  *	requests (the device itself may be left in a low-power state, waiting
134  *	for a runtime resume to occur).  The state of the device at the time its
135  *	driver's @resume() callback is run depends on the platform and subsystem
136  *	the device belongs to.  On most platforms, there are no restrictions on
137  *	availability of resources like clocks during @resume().
138  *	Subsystem-level @resume() is executed for all devices after invoking
139  *	subsystem-level @resume_noirq() for all of them.
140  *
141  * @resume_early: Prepare to execute @resume().  For a number of devices
142  *	@resume_early() may point to the same callback routine as the runtime
143  *	resume callback.
144  *
145  * @freeze: Hibernation-specific, executed before creating a hibernation image.
146  *	Analogous to @suspend(), but it should not enable the device to signal
147  *	wakeup events or change its power state.  The majority of subsystems
148  *	(with the notable exception of the PCI bus type) expect the driver-level
149  *	@freeze() to save the device settings in memory to be used by @restore()
150  *	during the subsequent resume from hibernation.
151  *	Subsystem-level @freeze() is executed for all devices after invoking
152  *	subsystem-level @prepare() for all of them.
153  *
154  * @freeze_late: Continue operations started by @freeze().  Analogous to
155  *	@suspend_late(), but it should not enable the device to signal wakeup
156  *	events or change its power state.
157  *
158  * @thaw: Hibernation-specific, executed after creating a hibernation image OR
159  *	if the creation of an image has failed.  Also executed after a failing
160  *	attempt to restore the contents of main memory from such an image.
161  *	Undo the changes made by the preceding @freeze(), so the device can be
162  *	operated in the same way as immediately before the call to @freeze().
163  *	Subsystem-level @thaw() is executed for all devices after invoking
164  *	subsystem-level @thaw_noirq() for all of them.  It also may be executed
165  *	directly after @freeze() in case of a transition error.
166  *
167  * @thaw_early: Prepare to execute @thaw().  Undo the changes made by the
168  *	preceding @freeze_late().
169  *
170  * @poweroff: Hibernation-specific, executed after saving a hibernation image.
171  *	Analogous to @suspend(), but it need not save the device's settings in
172  *	memory.
173  *	Subsystem-level @poweroff() is executed for all devices after invoking
174  *	subsystem-level @prepare() for all of them.
175  *
176  * @poweroff_late: Continue operations started by @poweroff().  Analogous to
177  *	@suspend_late(), but it need not save the device's settings in memory.
178  *
179  * @restore: Hibernation-specific, executed after restoring the contents of main
180  *	memory from a hibernation image, analogous to @resume().
181  *
182  * @restore_early: Prepare to execute @restore(), analogous to @resume_early().
183  *
184  * @suspend_noirq: Complete the actions started by @suspend().  Carry out any
185  *	additional operations required for suspending the device that might be
186  *	racing with its driver's interrupt handler, which is guaranteed not to
187  *	run while @suspend_noirq() is being executed.
188  *	It generally is expected that the device will be in a low-power state
189  *	(appropriate for the target system sleep state) after subsystem-level
190  *	@suspend_noirq() has returned successfully.  If the device can generate
191  *	system wakeup signals and is enabled to wake up the system, it should be
192  *	configured to do so at that time.  However, depending on the platform
193  *	and device's subsystem, @suspend() or @suspend_late() may be allowed to
194  *	put the device into the low-power state and configure it to generate
195  *	wakeup signals, in which case it generally is not necessary to define
196  *	@suspend_noirq().
197  *
198  * @resume_noirq: Prepare for the execution of @resume() by carrying out any
199  *	operations required for resuming the device that might be racing with
200  *	its driver's interrupt handler, which is guaranteed not to run while
201  *	@resume_noirq() is being executed.
202  *
203  * @freeze_noirq: Complete the actions started by @freeze().  Carry out any
204  *	additional operations required for freezing the device that might be
205  *	racing with its driver's interrupt handler, which is guaranteed not to
206  *	run while @freeze_noirq() is being executed.
207  *	The power state of the device should not be changed by either @freeze(),
208  *	or @freeze_late(), or @freeze_noirq() and it should not be configured to
209  *	signal system wakeup by any of these callbacks.
210  *
211  * @thaw_noirq: Prepare for the execution of @thaw() by carrying out any
212  *	operations required for thawing the device that might be racing with its
213  *	driver's interrupt handler, which is guaranteed not to run while
214  *	@thaw_noirq() is being executed.
215  *
216  * @poweroff_noirq: Complete the actions started by @poweroff().  Analogous to
217  *	@suspend_noirq(), but it need not save the device's settings in memory.
218  *
219  * @restore_noirq: Prepare for the execution of @restore() by carrying out any
220  *	operations required for thawing the device that might be racing with its
221  *	driver's interrupt handler, which is guaranteed not to run while
222  *	@restore_noirq() is being executed.  Analogous to @resume_noirq().
223  *
224  * @runtime_suspend: Prepare the device for a condition in which it won't be
225  *	able to communicate with the CPU(s) and RAM due to power management.
226  *	This need not mean that the device should be put into a low-power state.
227  *	For example, if the device is behind a link which is about to be turned
228  *	off, the device may remain at full power.  If the device does go to low
229  *	power and is capable of generating runtime wakeup events, remote wakeup
230  *	(i.e., a hardware mechanism allowing the device to request a change of
231  *	its power state via an interrupt) should be enabled for it.
232  *
233  * @runtime_resume: Put the device into the fully active state in response to a
234  *	wakeup event generated by hardware or at the request of software.  If
235  *	necessary, put the device into the full-power state and restore its
236  *	registers, so that it is fully operational.
237  *
238  * @runtime_idle: Device appears to be inactive and it might be put into a
239  *	low-power state if all of the necessary conditions are satisfied.
240  *	Check these conditions, and return 0 if it's appropriate to let the PM
241  *	core queue a suspend request for the device.
242  *
243  * Several device power state transitions are externally visible, affecting
244  * the state of pending I/O queues and (for drivers that touch hardware)
245  * interrupts, wakeups, DMA, and other hardware state.  There may also be
246  * internal transitions to various low-power modes which are transparent
247  * to the rest of the driver stack (such as a driver that's ON gating off
248  * clocks which are not in active use).
249  *
250  * The externally visible transitions are handled with the help of callbacks
251  * included in this structure in such a way that, typically, two levels of
252  * callbacks are involved.  First, the PM core executes callbacks provided by PM
253  * domains, device types, classes and bus types.  They are the subsystem-level
254  * callbacks expected to execute callbacks provided by device drivers, although
255  * they may choose not to do that.  If the driver callbacks are executed, they
256  * have to collaborate with the subsystem-level callbacks to achieve the goals
257  * appropriate for the given system transition, given transition phase and the
258  * subsystem the device belongs to.
259  *
260  * All of the above callbacks, except for @complete(), return error codes.
261  * However, the error codes returned by @resume(), @thaw(), @restore(),
262  * @resume_noirq(), @thaw_noirq(), and @restore_noirq(), do not cause the PM
263  * core to abort the resume transition during which they are returned.  The
264  * error codes returned in those cases are only printed to the system logs for
265  * debugging purposes.  Still, it is recommended that drivers only return error
266  * codes from their resume methods in case of an unrecoverable failure (i.e.
267  * when the device being handled refuses to resume and becomes unusable) to
268  * allow the PM core to be modified in the future, so that it can avoid
269  * attempting to handle devices that failed to resume and their children.
270  *
271  * It is allowed to unregister devices while the above callbacks are being
272  * executed.  However, a callback routine MUST NOT try to unregister the device
273  * it was called for, although it may unregister children of that device (for
274  * example, if it detects that a child was unplugged while the system was
275  * asleep).
276  *
277  * There also are callbacks related to runtime power management of devices.
278  * Again, as a rule these callbacks are executed by the PM core for subsystems
279  * (PM domains, device types, classes and bus types) and the subsystem-level
280  * callbacks are expected to invoke the driver callbacks.  Moreover, the exact
281  * actions to be performed by a device driver's callbacks generally depend on
282  * the platform and subsystem the device belongs to.
283  *
284  * Refer to Documentation/power/runtime_pm.rst for more information about the
285  * role of the @runtime_suspend(), @runtime_resume() and @runtime_idle()
286  * callbacks in device runtime power management.
287  */
288 struct dev_pm_ops {
289 	int (*prepare)(struct device *dev);
290 	void (*complete)(struct device *dev);
291 	int (*suspend)(struct device *dev);
292 	int (*resume)(struct device *dev);
293 	int (*freeze)(struct device *dev);
294 	int (*thaw)(struct device *dev);
295 	int (*poweroff)(struct device *dev);
296 	int (*restore)(struct device *dev);
297 	int (*suspend_late)(struct device *dev);
298 	int (*resume_early)(struct device *dev);
299 	int (*freeze_late)(struct device *dev);
300 	int (*thaw_early)(struct device *dev);
301 	int (*poweroff_late)(struct device *dev);
302 	int (*restore_early)(struct device *dev);
303 	int (*suspend_noirq)(struct device *dev);
304 	int (*resume_noirq)(struct device *dev);
305 	int (*freeze_noirq)(struct device *dev);
306 	int (*thaw_noirq)(struct device *dev);
307 	int (*poweroff_noirq)(struct device *dev);
308 	int (*restore_noirq)(struct device *dev);
309 	int (*runtime_suspend)(struct device *dev);
310 	int (*runtime_resume)(struct device *dev);
311 	int (*runtime_idle)(struct device *dev);
312 };
313 
314 #define SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
315 	.suspend = pm_sleep_ptr(suspend_fn), \
316 	.resume = pm_sleep_ptr(resume_fn), \
317 	.freeze = pm_sleep_ptr(suspend_fn), \
318 	.thaw = pm_sleep_ptr(resume_fn), \
319 	.poweroff = pm_sleep_ptr(suspend_fn), \
320 	.restore = pm_sleep_ptr(resume_fn),
321 
322 #define LATE_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
323 	.suspend_late = pm_sleep_ptr(suspend_fn), \
324 	.resume_early = pm_sleep_ptr(resume_fn), \
325 	.freeze_late = pm_sleep_ptr(suspend_fn), \
326 	.thaw_early = pm_sleep_ptr(resume_fn), \
327 	.poweroff_late = pm_sleep_ptr(suspend_fn), \
328 	.restore_early = pm_sleep_ptr(resume_fn),
329 
330 #define NOIRQ_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
331 	.suspend_noirq = pm_sleep_ptr(suspend_fn), \
332 	.resume_noirq = pm_sleep_ptr(resume_fn), \
333 	.freeze_noirq = pm_sleep_ptr(suspend_fn), \
334 	.thaw_noirq = pm_sleep_ptr(resume_fn), \
335 	.poweroff_noirq = pm_sleep_ptr(suspend_fn), \
336 	.restore_noirq = pm_sleep_ptr(resume_fn),
337 
338 #define RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn) \
339 	.runtime_suspend = suspend_fn, \
340 	.runtime_resume = resume_fn, \
341 	.runtime_idle = idle_fn,
342 
343 #ifdef CONFIG_PM_SLEEP
344 #define SET_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
345 	SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
346 #else
347 #define SET_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
348 #endif
349 
350 #ifdef CONFIG_PM_SLEEP
351 #define SET_LATE_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
352 	LATE_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
353 #else
354 #define SET_LATE_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
355 #endif
356 
357 #ifdef CONFIG_PM_SLEEP
358 #define SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
359 	NOIRQ_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
360 #else
361 #define SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn)
362 #endif
363 
364 #ifdef CONFIG_PM
365 #define SET_RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn) \
366 	RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn)
367 #else
368 #define SET_RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn)
369 #endif
370 
371 #define _DEFINE_DEV_PM_OPS(name, \
372 			   suspend_fn, resume_fn, \
373 			   runtime_suspend_fn, runtime_resume_fn, idle_fn) \
374 const struct dev_pm_ops name = { \
375 	SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
376 	RUNTIME_PM_OPS(runtime_suspend_fn, runtime_resume_fn, idle_fn) \
377 }
378 
379 #define _EXPORT_PM_OPS(name, license, ns)				\
380 	const struct dev_pm_ops name;					\
381 	__EXPORT_SYMBOL(name, license, ns);				\
382 	const struct dev_pm_ops name
383 
384 #define _DISCARD_PM_OPS(name, license, ns)				\
385 	static __maybe_unused const struct dev_pm_ops __static_##name
386 
387 #ifdef CONFIG_PM
388 #define _EXPORT_DEV_PM_OPS(name, license, ns)		_EXPORT_PM_OPS(name, license, ns)
389 #else
390 #define _EXPORT_DEV_PM_OPS(name, license, ns)		_DISCARD_PM_OPS(name, license, ns)
391 #endif
392 
393 #ifdef CONFIG_PM_SLEEP
394 #define _EXPORT_DEV_SLEEP_PM_OPS(name, license, ns)	_EXPORT_PM_OPS(name, license, ns)
395 #else
396 #define _EXPORT_DEV_SLEEP_PM_OPS(name, license, ns)	_DISCARD_PM_OPS(name, license, ns)
397 #endif
398 
399 #define EXPORT_DEV_PM_OPS(name)				_EXPORT_DEV_PM_OPS(name, "", "")
400 #define EXPORT_GPL_DEV_PM_OPS(name)			_EXPORT_DEV_PM_OPS(name, "GPL", "")
401 #define EXPORT_NS_DEV_PM_OPS(name, ns)			_EXPORT_DEV_PM_OPS(name, "", #ns)
402 #define EXPORT_NS_GPL_DEV_PM_OPS(name, ns)		_EXPORT_DEV_PM_OPS(name, "GPL", #ns)
403 
404 #define EXPORT_DEV_SLEEP_PM_OPS(name)			_EXPORT_DEV_SLEEP_PM_OPS(name, "", "")
405 #define EXPORT_GPL_DEV_SLEEP_PM_OPS(name)		_EXPORT_DEV_SLEEP_PM_OPS(name, "GPL", "")
406 #define EXPORT_NS_DEV_SLEEP_PM_OPS(name, ns)		_EXPORT_DEV_SLEEP_PM_OPS(name, "", #ns)
407 #define EXPORT_NS_GPL_DEV_SLEEP_PM_OPS(name, ns)	_EXPORT_DEV_SLEEP_PM_OPS(name, "GPL", #ns)
408 
409 /*
410  * Use this if you want to use the same suspend and resume callbacks for suspend
411  * to RAM and hibernation.
412  *
413  * If the underlying dev_pm_ops struct symbol has to be exported, use
414  * EXPORT_SIMPLE_DEV_PM_OPS() or EXPORT_GPL_SIMPLE_DEV_PM_OPS() instead.
415  */
416 #define DEFINE_SIMPLE_DEV_PM_OPS(name, suspend_fn, resume_fn) \
417 	_DEFINE_DEV_PM_OPS(name, suspend_fn, resume_fn, NULL, NULL, NULL)
418 
419 #define EXPORT_SIMPLE_DEV_PM_OPS(name, suspend_fn, resume_fn) \
420 	EXPORT_DEV_SLEEP_PM_OPS(name) = { \
421 		SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
422 	}
423 #define EXPORT_GPL_SIMPLE_DEV_PM_OPS(name, suspend_fn, resume_fn) \
424 	EXPORT_GPL_DEV_SLEEP_PM_OPS(name) = { \
425 		SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
426 	}
427 #define EXPORT_NS_SIMPLE_DEV_PM_OPS(name, suspend_fn, resume_fn, ns)	\
428 	EXPORT_NS_DEV_SLEEP_PM_OPS(name, ns) = { \
429 		SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
430 	}
431 #define EXPORT_NS_GPL_SIMPLE_DEV_PM_OPS(name, suspend_fn, resume_fn, ns)	\
432 	EXPORT_NS_GPL_DEV_SLEEP_PM_OPS(name, ns) = { \
433 		SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
434 	}
435 
436 /* Deprecated. Use DEFINE_SIMPLE_DEV_PM_OPS() instead. */
437 #define SIMPLE_DEV_PM_OPS(name, suspend_fn, resume_fn) \
438 const struct dev_pm_ops __maybe_unused name = { \
439 	SET_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
440 }
441 
442 /*
443  * Use this for defining a set of PM operations to be used in all situations
444  * (system suspend, hibernation or runtime PM).
445  * NOTE: In general, system suspend callbacks, .suspend() and .resume(), should
446  * be different from the corresponding runtime PM callbacks, .runtime_suspend(),
447  * and .runtime_resume(), because .runtime_suspend() always works on an already
448  * quiescent device, while .suspend() should assume that the device may be doing
449  * something when it is called (it should ensure that the device will be
450  * quiescent after it has returned).  Therefore it's better to point the "late"
451  * suspend and "early" resume callback pointers, .suspend_late() and
452  * .resume_early(), to the same routines as .runtime_suspend() and
453  * .runtime_resume(), respectively (and analogously for hibernation).
454  *
455  * Deprecated. You most likely don't want this macro. Use
456  * DEFINE_RUNTIME_DEV_PM_OPS() instead.
457  */
458 #define UNIVERSAL_DEV_PM_OPS(name, suspend_fn, resume_fn, idle_fn) \
459 const struct dev_pm_ops __maybe_unused name = { \
460 	SET_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
461 	SET_RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn) \
462 }
463 
464 /*
465  * Use this if you want to have the suspend and resume callbacks be called
466  * with IRQs disabled.
467  */
468 #define DEFINE_NOIRQ_DEV_PM_OPS(name, suspend_fn, resume_fn) \
469 const struct dev_pm_ops name = { \
470 	NOIRQ_SYSTEM_SLEEP_PM_OPS(suspend_fn, resume_fn) \
471 }
472 
473 #define pm_ptr(_ptr) PTR_IF(IS_ENABLED(CONFIG_PM), (_ptr))
474 #define pm_sleep_ptr(_ptr) PTR_IF(IS_ENABLED(CONFIG_PM_SLEEP), (_ptr))
475 
476 /*
477  * PM_EVENT_ messages
478  *
479  * The following PM_EVENT_ messages are defined for the internal use of the PM
480  * core, in order to provide a mechanism allowing the high level suspend and
481  * hibernation code to convey the necessary information to the device PM core
482  * code:
483  *
484  * ON		No transition.
485  *
486  * FREEZE	System is going to hibernate, call ->prepare() and ->freeze()
487  *		for all devices.
488  *
489  * SUSPEND	System is going to suspend, call ->prepare() and ->suspend()
490  *		for all devices.
491  *
492  * HIBERNATE	Hibernation image has been saved, call ->prepare() and
493  *		->poweroff() for all devices.
494  *
495  * QUIESCE	Contents of main memory are going to be restored from a (loaded)
496  *		hibernation image, call ->prepare() and ->freeze() for all
497  *		devices.
498  *
499  * RESUME	System is resuming, call ->resume() and ->complete() for all
500  *		devices.
501  *
502  * THAW		Hibernation image has been created, call ->thaw() and
503  *		->complete() for all devices.
504  *
505  * RESTORE	Contents of main memory have been restored from a hibernation
506  *		image, call ->restore() and ->complete() for all devices.
507  *
508  * RECOVER	Creation of a hibernation image or restoration of the main
509  *		memory contents from a hibernation image has failed, call
510  *		->thaw() and ->complete() for all devices.
511  * POWEROFF	System will poweroff, call ->poweroff() for all devices.
512  *
513  * The following PM_EVENT_ messages are defined for internal use by
514  * kernel subsystems.  They are never issued by the PM core.
515  *
516  * USER_SUSPEND		Manual selective suspend was issued by userspace.
517  *
518  * USER_RESUME		Manual selective resume was issued by userspace.
519  *
520  * REMOTE_WAKEUP	Remote-wakeup request was received from the device.
521  *
522  * AUTO_SUSPEND		Automatic (device idle) runtime suspend was
523  *			initiated by the subsystem.
524  *
525  * AUTO_RESUME		Automatic (device needed) runtime resume was
526  *			requested by a driver.
527  */
528 
529 #define PM_EVENT_INVALID	(-1)
530 #define PM_EVENT_ON		0x0000
531 #define PM_EVENT_FREEZE		0x0001
532 #define PM_EVENT_SUSPEND	0x0002
533 #define PM_EVENT_HIBERNATE	0x0004
534 #define PM_EVENT_QUIESCE	0x0008
535 #define PM_EVENT_RESUME		0x0010
536 #define PM_EVENT_THAW		0x0020
537 #define PM_EVENT_RESTORE	0x0040
538 #define PM_EVENT_RECOVER	0x0080
539 #define PM_EVENT_USER		0x0100
540 #define PM_EVENT_REMOTE		0x0200
541 #define PM_EVENT_AUTO		0x0400
542 #define PM_EVENT_POWEROFF	0x0800
543 
544 #define PM_EVENT_SLEEP		(PM_EVENT_SUSPEND | PM_EVENT_HIBERNATE)
545 #define PM_EVENT_USER_SUSPEND	(PM_EVENT_USER | PM_EVENT_SUSPEND)
546 #define PM_EVENT_USER_RESUME	(PM_EVENT_USER | PM_EVENT_RESUME)
547 #define PM_EVENT_REMOTE_RESUME	(PM_EVENT_REMOTE | PM_EVENT_RESUME)
548 #define PM_EVENT_AUTO_SUSPEND	(PM_EVENT_AUTO | PM_EVENT_SUSPEND)
549 #define PM_EVENT_AUTO_RESUME	(PM_EVENT_AUTO | PM_EVENT_RESUME)
550 
551 #define PMSG_INVALID	((struct pm_message){ .event = PM_EVENT_INVALID, })
552 #define PMSG_ON		((struct pm_message){ .event = PM_EVENT_ON, })
553 #define PMSG_FREEZE	((struct pm_message){ .event = PM_EVENT_FREEZE, })
554 #define PMSG_QUIESCE	((struct pm_message){ .event = PM_EVENT_QUIESCE, })
555 #define PMSG_SUSPEND	((struct pm_message){ .event = PM_EVENT_SUSPEND, })
556 #define PMSG_HIBERNATE	((struct pm_message){ .event = PM_EVENT_HIBERNATE, })
557 #define PMSG_POWEROFF	((struct pm_message){ .event = PM_EVENT_POWEROFF, })
558 #define PMSG_RESUME	((struct pm_message){ .event = PM_EVENT_RESUME, })
559 #define PMSG_THAW	((struct pm_message){ .event = PM_EVENT_THAW, })
560 #define PMSG_RESTORE	((struct pm_message){ .event = PM_EVENT_RESTORE, })
561 #define PMSG_RECOVER	((struct pm_message){ .event = PM_EVENT_RECOVER, })
562 #define PMSG_USER_SUSPEND	((struct pm_message) \
563 					{ .event = PM_EVENT_USER_SUSPEND, })
564 #define PMSG_USER_RESUME	((struct pm_message) \
565 					{ .event = PM_EVENT_USER_RESUME, })
566 #define PMSG_REMOTE_RESUME	((struct pm_message) \
567 					{ .event = PM_EVENT_REMOTE_RESUME, })
568 #define PMSG_AUTO_SUSPEND	((struct pm_message) \
569 					{ .event = PM_EVENT_AUTO_SUSPEND, })
570 #define PMSG_AUTO_RESUME	((struct pm_message) \
571 					{ .event = PM_EVENT_AUTO_RESUME, })
572 
573 #define PMSG_IS_AUTO(msg)	(((msg).event & PM_EVENT_AUTO) != 0)
574 #define PMSG_NO_WAKEUP(msg)	(((msg).event & \
575 				(PM_EVENT_FREEZE | PM_EVENT_QUIESCE)) != 0)
576 /*
577  * Device run-time power management status.
578  *
579  * These status labels are used internally by the PM core to indicate the
580  * current status of a device with respect to the PM core operations.  They do
581  * not reflect the actual power state of the device or its status as seen by the
582  * driver.
583  *
584  * RPM_ACTIVE		Device is fully operational.  Indicates that the device
585  *			bus type's ->runtime_resume() callback has completed
586  *			successfully.
587  *
588  * RPM_SUSPENDED	Device bus type's ->runtime_suspend() callback has
589  *			completed successfully.  The device is regarded as
590  *			suspended.
591  *
592  * RPM_RESUMING		Device bus type's ->runtime_resume() callback is being
593  *			executed.
594  *
595  * RPM_SUSPENDING	Device bus type's ->runtime_suspend() callback is being
596  *			executed.
597  */
598 
599 enum rpm_status {
600 	RPM_INVALID = -1,
601 	RPM_ACTIVE = 0,
602 	RPM_RESUMING,
603 	RPM_SUSPENDED,
604 	RPM_SUSPENDING,
605 	RPM_BLOCKED,
606 };
607 
608 /*
609  * Device run-time power management request types.
610  *
611  * RPM_REQ_NONE		Do nothing.
612  *
613  * RPM_REQ_IDLE		Run the device bus type's ->runtime_idle() callback
614  *
615  * RPM_REQ_SUSPEND	Run the device bus type's ->runtime_suspend() callback
616  *
617  * RPM_REQ_AUTOSUSPEND	Same as RPM_REQ_SUSPEND, but not until the device has
618  *			been inactive for as long as power.autosuspend_delay
619  *
620  * RPM_REQ_RESUME	Run the device bus type's ->runtime_resume() callback
621  */
622 
623 enum rpm_request {
624 	RPM_REQ_NONE = 0,
625 	RPM_REQ_IDLE,
626 	RPM_REQ_SUSPEND,
627 	RPM_REQ_AUTOSUSPEND,
628 	RPM_REQ_RESUME,
629 };
630 
631 struct wakeup_source;
632 struct wake_irq;
633 struct pm_domain_data;
634 
635 struct pm_subsys_data {
636 	spinlock_t lock;
637 	unsigned int refcount;
638 #ifdef CONFIG_PM_CLK
639 	unsigned int clock_op_might_sleep;
640 	struct mutex clock_mutex;
641 	struct list_head clock_list;
642 #endif
643 #ifdef CONFIG_PM_GENERIC_DOMAINS
644 	struct pm_domain_data *domain_data;
645 #endif
646 };
647 
648 /*
649  * Driver flags to control system suspend/resume behavior.
650  *
651  * These flags can be set by device drivers at the probe time.  They need not be
652  * cleared by the drivers as the driver core will take care of that.
653  *
654  * NO_DIRECT_COMPLETE: Do not apply direct-complete optimization to the device.
655  * SMART_PREPARE: Take the driver ->prepare callback return value into account.
656  * SMART_SUSPEND: Avoid resuming the device from runtime suspend.
657  * MAY_SKIP_RESUME: Allow driver "noirq" and "early" callbacks to be skipped.
658  *
659  * See Documentation/driver-api/pm/devices.rst for details.
660  */
661 #define DPM_FLAG_NO_DIRECT_COMPLETE	BIT(0)
662 #define DPM_FLAG_SMART_PREPARE		BIT(1)
663 #define DPM_FLAG_SMART_SUSPEND		BIT(2)
664 #define DPM_FLAG_MAY_SKIP_RESUME	BIT(3)
665 
666 struct dev_pm_info {
667 	pm_message_t		power_state;
668 	bool			can_wakeup:1;
669 	bool			async_suspend:1;
670 	bool			in_dpm_list:1;	/* Owned by the PM core */
671 	bool			is_prepared:1;	/* Owned by the PM core */
672 	bool			is_suspended:1;	/* Ditto */
673 	bool			is_noirq_suspended:1;
674 	bool			is_late_suspended:1;
675 	bool			no_pm:1;
676 	bool			early_init:1;	/* Owned by the PM core */
677 	bool			direct_complete:1;	/* Owned by the PM core */
678 	u32			driver_flags;
679 	spinlock_t		lock;
680 #ifdef CONFIG_PM_SLEEP
681 	struct list_head	entry;
682 	struct completion	completion;
683 	struct wakeup_source	*wakeup;
684 	bool			work_in_progress;	/* Owned by the PM core */
685 	bool			wakeup_path:1;
686 	bool			syscore:1;
687 	bool			no_pm_callbacks:1;	/* Owned by the PM core */
688 	bool			smart_suspend:1;	/* Owned by the PM core */
689 	bool			must_resume:1;		/* Owned by the PM core */
690 	bool			may_skip_resume:1;	/* Set by subsystems */
691 	bool			out_band_wakeup:1;
692 	bool			strict_midlayer:1;
693 #else
694 	bool			should_wakeup:1;
695 #endif
696 #ifdef CONFIG_PM
697 	struct hrtimer		suspend_timer;
698 	u64			timer_expires;
699 	struct work_struct	work;
700 	wait_queue_head_t	wait_queue;
701 	struct wake_irq		*wakeirq;
702 	atomic_t		usage_count;
703 	atomic_t		child_count;
704 	unsigned int		disable_depth:3;
705 	bool			idle_notification:1;
706 	bool			request_pending:1;
707 	bool			deferred_resume:1;
708 	bool			needs_force_resume:1;
709 	bool			runtime_auto:1;
710 	bool			ignore_children:1;
711 	bool			no_callbacks:1;
712 	bool			irq_safe:1;
713 	bool			use_autosuspend:1;
714 	bool			timer_autosuspends:1;
715 	bool			memalloc_noio:1;
716 	unsigned int		links_count;
717 	enum rpm_request	request;
718 	enum rpm_status		runtime_status;
719 	enum rpm_status		last_status;
720 	int			runtime_error;
721 	int			autosuspend_delay;
722 	u64			last_busy;
723 	u64			active_time;
724 	u64			suspended_time;
725 	u64			accounting_timestamp;
726 #endif
727 	struct pm_subsys_data	*subsys_data;  /* Owned by the subsystem. */
728 	void (*set_latency_tolerance)(struct device *, s32);
729 	struct dev_pm_qos	*qos;
730 	bool			detach_power_off:1;	/* Owned by the driver core */
731 };
732 
733 extern int dev_pm_get_subsys_data(struct device *dev);
734 extern void dev_pm_put_subsys_data(struct device *dev);
735 
736 /**
737  * struct dev_pm_domain - power management domain representation.
738  *
739  * @ops: Power management operations associated with this domain.
740  * @start: Called when a user needs to start the device via the domain.
741  * @detach: Called when removing a device from the domain.
742  * @activate: Called before executing probe routines for bus types and drivers.
743  * @sync: Called after successful driver probe.
744  * @dismiss: Called after unsuccessful driver probe and after driver removal.
745  * @set_performance_state: Called to request a new performance state.
746  *
747  * Power domains provide callbacks that are executed during system suspend,
748  * hibernation, system resume and during runtime PM transitions instead of
749  * subsystem-level and driver-level callbacks.
750  */
751 struct dev_pm_domain {
752 	struct dev_pm_ops	ops;
753 	int (*start)(struct device *dev);
754 	void (*detach)(struct device *dev, bool power_off);
755 	int (*activate)(struct device *dev);
756 	void (*sync)(struct device *dev);
757 	void (*dismiss)(struct device *dev);
758 	int (*set_performance_state)(struct device *dev, unsigned int state);
759 };
760 
761 /*
762  * The PM_EVENT_ messages are also used by drivers implementing the legacy
763  * suspend framework, based on the ->suspend() and ->resume() callbacks common
764  * for suspend and hibernation transitions, according to the rules below.
765  */
766 
767 /* Necessary, because several drivers use PM_EVENT_PRETHAW */
768 #define PM_EVENT_PRETHAW PM_EVENT_QUIESCE
769 
770 /*
771  * One transition is triggered by resume(), after a suspend() call; the
772  * message is implicit:
773  *
774  * ON		Driver starts working again, responding to hardware events
775  *		and software requests.  The hardware may have gone through
776  *		a power-off reset, or it may have maintained state from the
777  *		previous suspend() which the driver will rely on while
778  *		resuming.  On most platforms, there are no restrictions on
779  *		availability of resources like clocks during resume().
780  *
781  * Other transitions are triggered by messages sent using suspend().  All
782  * these transitions quiesce the driver, so that I/O queues are inactive.
783  * That commonly entails turning off IRQs and DMA; there may be rules
784  * about how to quiesce that are specific to the bus or the device's type.
785  * (For example, network drivers mark the link state.)  Other details may
786  * differ according to the message:
787  *
788  * SUSPEND	Quiesce, enter a low power device state appropriate for
789  *		the upcoming system state (such as PCI_D3hot), and enable
790  *		wakeup events as appropriate.
791  *
792  * HIBERNATE	Enter a low power device state appropriate for the hibernation
793  *		state (eg. ACPI S4) and enable wakeup events as appropriate.
794  *
795  * FREEZE	Quiesce operations so that a consistent image can be saved;
796  *		but do NOT otherwise enter a low power device state, and do
797  *		NOT emit system wakeup events.
798  *
799  * PRETHAW	Quiesce as if for FREEZE; additionally, prepare for restoring
800  *		the system from a snapshot taken after an earlier FREEZE.
801  *		Some drivers will need to reset their hardware state instead
802  *		of preserving it, to ensure that it's never mistaken for the
803  *		state which that earlier snapshot had set up.
804  *
805  * A minimally power-aware driver treats all messages as SUSPEND, fully
806  * reinitializes its device during resume() -- whether or not it was reset
807  * during the suspend/resume cycle -- and can't issue wakeup events.
808  *
809  * More power-aware drivers may also use low power states at runtime as
810  * well as during system sleep states like PM_SUSPEND_STANDBY.  They may
811  * be able to use wakeup events to exit from runtime low-power states,
812  * or from system low-power states such as standby or suspend-to-RAM.
813  */
814 
815 #ifdef CONFIG_PM_SLEEP
816 extern void device_pm_lock(void);
817 extern void dpm_resume_start(pm_message_t state);
818 extern void dpm_resume_end(pm_message_t state);
819 extern void dpm_resume_noirq(pm_message_t state);
820 extern void dpm_resume_early(pm_message_t state);
821 extern void dpm_resume(pm_message_t state);
822 extern void dpm_complete(pm_message_t state);
823 
824 extern void device_pm_unlock(void);
825 extern int dpm_suspend_end(pm_message_t state);
826 extern int dpm_suspend_start(pm_message_t state);
827 extern int dpm_suspend_noirq(pm_message_t state);
828 extern int dpm_suspend_late(pm_message_t state);
829 extern int dpm_suspend(pm_message_t state);
830 extern int dpm_prepare(pm_message_t state);
831 
832 extern void __suspend_report_result(const char *function, struct device *dev, void *fn, int ret);
833 
834 #define suspend_report_result(dev, fn, ret)				\
835 	do {								\
836 		__suspend_report_result(__func__, dev, fn, ret);	\
837 	} while (0)
838 
839 extern int device_pm_wait_for_dev(struct device *sub, struct device *dev);
840 extern void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *));
841 
842 extern int pm_generic_prepare(struct device *dev);
843 extern int pm_generic_suspend_late(struct device *dev);
844 extern int pm_generic_suspend_noirq(struct device *dev);
845 extern int pm_generic_suspend(struct device *dev);
846 extern int pm_generic_resume_early(struct device *dev);
847 extern int pm_generic_resume_noirq(struct device *dev);
848 extern int pm_generic_resume(struct device *dev);
849 extern int pm_generic_freeze_noirq(struct device *dev);
850 extern int pm_generic_freeze(struct device *dev);
851 extern int pm_generic_thaw_noirq(struct device *dev);
852 extern int pm_generic_thaw(struct device *dev);
853 extern int pm_generic_restore_noirq(struct device *dev);
854 extern int pm_generic_restore_early(struct device *dev);
855 extern int pm_generic_restore(struct device *dev);
856 extern int pm_generic_poweroff_noirq(struct device *dev);
857 extern int pm_generic_poweroff_late(struct device *dev);
858 extern int pm_generic_poweroff(struct device *dev);
859 extern void pm_generic_complete(struct device *dev);
860 
861 extern bool dev_pm_skip_resume(struct device *dev);
862 extern bool dev_pm_skip_suspend(struct device *dev);
863 
864 #else /* !CONFIG_PM_SLEEP */
865 
866 #define device_pm_lock() do {} while (0)
867 #define device_pm_unlock() do {} while (0)
868 
dpm_suspend_start(pm_message_t state)869 static inline int dpm_suspend_start(pm_message_t state)
870 {
871 	return 0;
872 }
873 
874 #define suspend_report_result(dev, fn, ret)	do {} while (0)
875 
device_pm_wait_for_dev(struct device * a,struct device * b)876 static inline int device_pm_wait_for_dev(struct device *a, struct device *b)
877 {
878 	return 0;
879 }
880 
dpm_for_each_dev(void * data,void (* fn)(struct device *,void *))881 static inline void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
882 {
883 }
884 
885 #define pm_generic_prepare		NULL
886 #define pm_generic_suspend_late		NULL
887 #define pm_generic_suspend_noirq	NULL
888 #define pm_generic_suspend		NULL
889 #define pm_generic_resume_early		NULL
890 #define pm_generic_resume_noirq		NULL
891 #define pm_generic_resume		NULL
892 #define pm_generic_freeze_noirq		NULL
893 #define pm_generic_freeze		NULL
894 #define pm_generic_thaw_noirq		NULL
895 #define pm_generic_thaw			NULL
896 #define pm_generic_restore_noirq	NULL
897 #define pm_generic_restore_early	NULL
898 #define pm_generic_restore		NULL
899 #define pm_generic_poweroff_noirq	NULL
900 #define pm_generic_poweroff_late	NULL
901 #define pm_generic_poweroff		NULL
902 #define pm_generic_complete		NULL
903 #endif /* !CONFIG_PM_SLEEP */
904 
905 /* How to reorder dpm_list after device_move() */
906 enum dpm_order {
907 	DPM_ORDER_NONE,
908 	DPM_ORDER_DEV_AFTER_PARENT,
909 	DPM_ORDER_PARENT_BEFORE_DEV,
910 	DPM_ORDER_DEV_LAST,
911 };
912 
913 #endif /* _LINUX_PM_H */
914