PM / Sleep: Use the freezer_count() functions in [un]lock_system_sleep() APIs
[deliverable/linux.git] / include / linux / suspend.h
CommitLineData
95d9ffbe
RW
1#ifndef _LINUX_SUSPEND_H
2#define _LINUX_SUSPEND_H
1da177e4 3
1da177e4
LT
4#include <linux/swap.h>
5#include <linux/notifier.h>
1da177e4
LT
6#include <linux/init.h>
7#include <linux/pm.h>
7be98234 8#include <linux/mm.h>
33e638b9 9#include <linux/freezer.h>
95d9ffbe
RW
10#include <asm/errno.h>
11
37cce26b 12#ifdef CONFIG_VT
b6f448e9 13extern void pm_set_vt_switch(int);
95d9ffbe 14#else
b6f448e9
AS
15static inline void pm_set_vt_switch(int do_switch)
16{
17}
37cce26b 18#endif
b6f448e9 19
37cce26b
HS
20#ifdef CONFIG_VT_CONSOLE_SLEEP
21extern int pm_prepare_console(void);
22extern void pm_restore_console(void);
23#else
b6f448e9
AS
24static inline int pm_prepare_console(void)
25{
26 return 0;
27}
28
29static inline void pm_restore_console(void)
30{
31}
95d9ffbe
RW
32#endif
33
34typedef int __bitwise suspend_state_t;
35
36#define PM_SUSPEND_ON ((__force suspend_state_t) 0)
37#define PM_SUSPEND_STANDBY ((__force suspend_state_t) 1)
38#define PM_SUSPEND_MEM ((__force suspend_state_t) 3)
39#define PM_SUSPEND_MAX ((__force suspend_state_t) 4)
40
2a77c46d
SL
41enum suspend_stat_step {
42 SUSPEND_FREEZE = 1,
43 SUSPEND_PREPARE,
44 SUSPEND_SUSPEND,
45 SUSPEND_SUSPEND_NOIRQ,
46 SUSPEND_RESUME_NOIRQ,
47 SUSPEND_RESUME
48};
49
50struct suspend_stats {
51 int success;
52 int fail;
53 int failed_freeze;
54 int failed_prepare;
55 int failed_suspend;
56 int failed_suspend_noirq;
57 int failed_resume;
58 int failed_resume_noirq;
59#define REC_FAILED_NUM 2
60 int last_failed_dev;
61 char failed_devs[REC_FAILED_NUM][40];
62 int last_failed_errno;
63 int errno[REC_FAILED_NUM];
64 int last_failed_step;
65 enum suspend_stat_step failed_steps[REC_FAILED_NUM];
66};
67
68extern struct suspend_stats suspend_stats;
69
70static inline void dpm_save_failed_dev(const char *name)
71{
72 strlcpy(suspend_stats.failed_devs[suspend_stats.last_failed_dev],
73 name,
74 sizeof(suspend_stats.failed_devs[0]));
75 suspend_stats.last_failed_dev++;
76 suspend_stats.last_failed_dev %= REC_FAILED_NUM;
77}
78
79static inline void dpm_save_failed_errno(int err)
80{
81 suspend_stats.errno[suspend_stats.last_failed_errno] = err;
82 suspend_stats.last_failed_errno++;
83 suspend_stats.last_failed_errno %= REC_FAILED_NUM;
84}
85
86static inline void dpm_save_failed_step(enum suspend_stat_step step)
87{
88 suspend_stats.failed_steps[suspend_stats.last_failed_step] = step;
89 suspend_stats.last_failed_step++;
90 suspend_stats.last_failed_step %= REC_FAILED_NUM;
91}
92
95d9ffbe 93/**
26398a70
RW
94 * struct platform_suspend_ops - Callbacks for managing platform dependent
95 * system sleep states.
95d9ffbe
RW
96 *
97 * @valid: Callback to determine if given system sleep state is supported by
98 * the platform.
99 * Valid (ie. supported) states are advertised in /sys/power/state. Note
100 * that it still may be impossible to enter given system sleep state if the
101 * conditions aren't right.
26398a70
RW
102 * There is the %suspend_valid_only_mem function available that can be
103 * assigned to this if the platform only supports mem sleep.
95d9ffbe 104 *
c697eece
RW
105 * @begin: Initialise a transition to given system sleep state.
106 * @begin() is executed right prior to suspending devices. The information
107 * conveyed to the platform code by @begin() should be disregarded by it as
108 * soon as @end() is executed. If @begin() fails (ie. returns nonzero),
95d9ffbe
RW
109 * @prepare(), @enter() and @finish() will not be called by the PM core.
110 * This callback is optional. However, if it is implemented, the argument
c697eece 111 * passed to @enter() is redundant and should be ignored.
95d9ffbe
RW
112 *
113 * @prepare: Prepare the platform for entering the system sleep state indicated
c697eece 114 * by @begin().
95d9ffbe
RW
115 * @prepare() is called right after devices have been suspended (ie. the
116 * appropriate .suspend() method has been executed for each device) and
6a7c7eaf
RW
117 * before device drivers' late suspend callbacks are executed. It returns
118 * 0 on success or a negative error code otherwise, in which case the
119 * system cannot enter the desired sleep state (@prepare_late(), @enter(),
ce441011 120 * and @wake() will not be called in that case).
6a7c7eaf
RW
121 *
122 * @prepare_late: Finish preparing the platform for entering the system sleep
123 * state indicated by @begin().
124 * @prepare_late is called before disabling nonboot CPUs and after
125 * device drivers' late suspend callbacks have been executed. It returns
126 * 0 on success or a negative error code otherwise, in which case the
ce441011
RW
127 * system cannot enter the desired sleep state (@enter() will not be
128 * executed).
95d9ffbe 129 *
c697eece
RW
130 * @enter: Enter the system sleep state indicated by @begin() or represented by
131 * the argument if @begin() is not implemented.
95d9ffbe
RW
132 * This callback is mandatory. It returns 0 on success or a negative
133 * error code otherwise, in which case the system cannot enter the desired
134 * sleep state.
135 *
6a7c7eaf
RW
136 * @wake: Called when the system has just left a sleep state, right after
137 * the nonboot CPUs have been enabled and before device drivers' early
138 * resume callbacks are executed.
139 * This callback is optional, but should be implemented by the platforms
140 * that implement @prepare_late(). If implemented, it is always called
ce441011 141 * after @prepare_late and @enter(), even if one of them fails.
6a7c7eaf
RW
142 *
143 * @finish: Finish wake-up of the platform.
144 * @finish is called right prior to calling device drivers' regular suspend
145 * callbacks.
95d9ffbe
RW
146 * This callback is optional, but should be implemented by the platforms
147 * that implement @prepare(). If implemented, it is always called after
ce441011
RW
148 * @enter() and @wake(), even if any of them fails. It is executed after
149 * a failing @prepare.
c697eece 150 *
3b5fe852
MH
151 * @suspend_again: Returns whether the system should suspend again (true) or
152 * not (false). If the platform wants to poll sensors or execute some
153 * code during suspended without invoking userspace and most of devices,
154 * suspend_again callback is the place assuming that periodic-wakeup or
155 * alarm-wakeup is already setup. This allows to execute some codes while
156 * being kept suspended in the view of userland and devices.
157 *
c697eece
RW
158 * @end: Called by the PM core right after resuming devices, to indicate to
159 * the platform that the system has returned to the working state or
160 * the transition to the sleep state has been aborted.
161 * This callback is optional, but should be implemented by the platforms
6a7c7eaf
RW
162 * that implement @begin(). Accordingly, platforms implementing @begin()
163 * should also provide a @end() which cleans up transitions aborted before
c697eece 164 * @enter().
d8f3de0d
RW
165 *
166 * @recover: Recover the platform from a suspend failure.
167 * Called by the PM core if the suspending of devices fails.
168 * This callback is optional and should only be implemented by platforms
169 * which require special recovery actions in that situation.
95d9ffbe 170 */
26398a70 171struct platform_suspend_ops {
95d9ffbe 172 int (*valid)(suspend_state_t state);
c697eece 173 int (*begin)(suspend_state_t state);
e6c5eb95 174 int (*prepare)(void);
6a7c7eaf 175 int (*prepare_late)(void);
95d9ffbe 176 int (*enter)(suspend_state_t state);
6a7c7eaf 177 void (*wake)(void);
e6c5eb95 178 void (*finish)(void);
3b5fe852 179 bool (*suspend_again)(void);
c697eece 180 void (*end)(void);
d8f3de0d 181 void (*recover)(void);
95d9ffbe
RW
182};
183
184#ifdef CONFIG_SUSPEND
95d9ffbe 185/**
26398a70
RW
186 * suspend_set_ops - set platform dependent suspend operations
187 * @ops: The new suspend operations to set.
95d9ffbe 188 */
2f55ac07 189extern void suspend_set_ops(const struct platform_suspend_ops *ops);
26398a70 190extern int suspend_valid_only_mem(suspend_state_t state);
95d9ffbe
RW
191
192/**
193 * arch_suspend_disable_irqs - disable IRQs for suspend
194 *
195 * Disables IRQs (in the default case). This is a weak symbol in the common
196 * code and thus allows architectures to override it if more needs to be
197 * done. Not called for suspend to disk.
198 */
199extern void arch_suspend_disable_irqs(void);
200
201/**
202 * arch_suspend_enable_irqs - enable IRQs after suspend
203 *
204 * Enables IRQs (in the default case). This is a weak symbol in the common
205 * code and thus allows architectures to override it if more needs to be
206 * done. Not called for suspend to disk.
207 */
208extern void arch_suspend_enable_irqs(void);
209
210extern int pm_suspend(suspend_state_t state);
211#else /* !CONFIG_SUSPEND */
212#define suspend_valid_only_mem NULL
213
2f55ac07 214static inline void suspend_set_ops(const struct platform_suspend_ops *ops) {}
95d9ffbe
RW
215static inline int pm_suspend(suspend_state_t state) { return -ENOSYS; }
216#endif /* !CONFIG_SUSPEND */
1da177e4 217
8357376d
RW
218/* struct pbe is used for creating lists of pages that should be restored
219 * atomically during the resume from disk, because the page frames they have
220 * occupied before the suspend are in use.
221 */
dcbb5a54 222struct pbe {
8357376d
RW
223 void *address; /* address of the copy */
224 void *orig_address; /* original address of a page */
7088a5c0 225 struct pbe *next;
dcbb5a54 226};
1da177e4 227
1da177e4 228/* mm/page_alloc.c */
1da177e4
LT
229extern void mark_free_pages(struct zone *zone);
230
a3d25c27 231/**
b3dac3b3 232 * struct platform_hibernation_ops - hibernation platform support
a3d25c27 233 *
caea99ef
RW
234 * The methods in this structure allow a platform to carry out special
235 * operations required by it during a hibernation transition.
a3d25c27 236 *
d8f3de0d 237 * All the methods below, except for @recover(), must be implemented.
a3d25c27 238 *
caea99ef 239 * @begin: Tell the platform driver that we're starting hibernation.
74f270af
RW
240 * Called right after shrinking memory and before freezing devices.
241 *
caea99ef
RW
242 * @end: Called by the PM core right after resuming devices, to indicate to
243 * the platform that the system has returned to the working state.
244 *
74f270af
RW
245 * @pre_snapshot: Prepare the platform for creating the hibernation image.
246 * Called right after devices have been frozen and before the nonboot
247 * CPUs are disabled (runs with IRQs on).
248 *
249 * @finish: Restore the previous state of the platform after the hibernation
250 * image has been created *or* put the platform into the normal operation
251 * mode after the hibernation (the same method is executed in both cases).
252 * Called right after the nonboot CPUs have been enabled and before
253 * thawing devices (runs with IRQs on).
254 *
255 * @prepare: Prepare the platform for entering the low power state.
256 * Called right after the hibernation image has been saved and before
257 * devices are prepared for entering the low power state.
258 *
259 * @enter: Put the system into the low power state after the hibernation image
260 * has been saved to disk.
261 * Called after the nonboot CPUs have been disabled and all of the low
262 * level devices have been shut down (runs with IRQs off).
263 *
c7e0831d
RW
264 * @leave: Perform the first stage of the cleanup after the system sleep state
265 * indicated by @set_target() has been left.
266 * Called right after the control has been passed from the boot kernel to
267 * the image kernel, before the nonboot CPUs are enabled and before devices
268 * are resumed. Executed with interrupts disabled.
269 *
74f270af
RW
270 * @pre_restore: Prepare system for the restoration from a hibernation image.
271 * Called right after devices have been frozen and before the nonboot
272 * CPUs are disabled (runs with IRQs on).
273 *
274 * @restore_cleanup: Clean up after a failing image restoration.
275 * Called right after the nonboot CPUs have been enabled and before
276 * thawing devices (runs with IRQs on).
d8f3de0d
RW
277 *
278 * @recover: Recover the platform from a failure to suspend devices.
279 * Called by the PM core if the suspending of devices during hibernation
280 * fails. This callback is optional and should only be implemented by
281 * platforms which require special recovery actions in that situation.
a3d25c27 282 */
b3dac3b3 283struct platform_hibernation_ops {
caea99ef
RW
284 int (*begin)(void);
285 void (*end)(void);
74f270af
RW
286 int (*pre_snapshot)(void);
287 void (*finish)(void);
a3d25c27
RW
288 int (*prepare)(void);
289 int (*enter)(void);
c7e0831d 290 void (*leave)(void);
a634cc10
RW
291 int (*pre_restore)(void);
292 void (*restore_cleanup)(void);
d8f3de0d 293 void (*recover)(void);
a3d25c27
RW
294};
295
b0cb1a19 296#ifdef CONFIG_HIBERNATION
74dfd666 297/* kernel/power/snapshot.c */
940d67f6 298extern void __register_nosave_region(unsigned long b, unsigned long e, int km);
ce289e89 299static inline void __init register_nosave_region(unsigned long b, unsigned long e)
940d67f6
JB
300{
301 __register_nosave_region(b, e, 0);
302}
ce289e89 303static inline void __init register_nosave_region_late(unsigned long b, unsigned long e)
940d67f6
JB
304{
305 __register_nosave_region(b, e, 1);
306}
74dfd666
RW
307extern int swsusp_page_is_forbidden(struct page *);
308extern void swsusp_set_page_free(struct page *);
309extern void swsusp_unset_page_free(struct page *);
310extern unsigned long get_safe_page(gfp_t gfp_mask);
a3d25c27 311
073ef1f6 312extern void hibernation_set_ops(const struct platform_hibernation_ops *ops);
a3d25c27 313extern int hibernate(void);
abfe2d7b 314extern bool system_entering_hibernation(void);
b0cb1a19 315#else /* CONFIG_HIBERNATION */
1f112cee
RW
316static inline void register_nosave_region(unsigned long b, unsigned long e) {}
317static inline void register_nosave_region_late(unsigned long b, unsigned long e) {}
74dfd666
RW
318static inline int swsusp_page_is_forbidden(struct page *p) { return 0; }
319static inline void swsusp_set_page_free(struct page *p) {}
320static inline void swsusp_unset_page_free(struct page *p) {}
a3d25c27 321
073ef1f6 322static inline void hibernation_set_ops(const struct platform_hibernation_ops *ops) {}
a3d25c27 323static inline int hibernate(void) { return -ENOSYS; }
fce2b111
CH
324static inline bool system_entering_hibernation(void) { return false; }
325#endif /* CONFIG_HIBERNATION */
326
35eb6db1
AW
327/* Hibernation and suspend events */
328#define PM_HIBERNATION_PREPARE 0x0001 /* Going to hibernate */
329#define PM_POST_HIBERNATION 0x0002 /* Hibernation finished */
330#define PM_SUSPEND_PREPARE 0x0003 /* Going to suspend the system */
331#define PM_POST_SUSPEND 0x0004 /* Suspend finished */
332#define PM_RESTORE_PREPARE 0x0005 /* Going to restore a saved image */
333#define PM_POST_RESTORE 0x0006 /* Restore failed */
334
296699de 335#ifdef CONFIG_PM_SLEEP
1da177e4
LT
336void save_processor_state(void);
337void restore_processor_state(void);
25761b6e 338
b10d9117 339/* kernel/power/main.c */
82525756
AS
340extern int register_pm_notifier(struct notifier_block *nb);
341extern int unregister_pm_notifier(struct notifier_block *nb);
b10d9117
RW
342
343#define pm_notifier(fn, pri) { \
344 static struct notifier_block fn##_nb = \
345 { .notifier_call = fn, .priority = pri }; \
346 register_pm_notifier(&fn##_nb); \
347}
c125e96f
RW
348
349/* drivers/base/power/wakeup.c */
350extern bool events_check_enabled;
351
a2867e08 352extern bool pm_wakeup_pending(void);
074037ec
RW
353extern bool pm_get_wakeup_count(unsigned int *count);
354extern bool pm_save_wakeup_count(unsigned int count);
296699de 355#else /* !CONFIG_PM_SLEEP */
b10d9117
RW
356
357static inline int register_pm_notifier(struct notifier_block *nb)
358{
359 return 0;
360}
361
362static inline int unregister_pm_notifier(struct notifier_block *nb)
363{
364 return 0;
365}
366
367#define pm_notifier(fn, pri) do { (void)(fn); } while (0)
dbeeec5f 368
a2867e08 369static inline bool pm_wakeup_pending(void) { return false; }
296699de 370#endif /* !CONFIG_PM_SLEEP */
b10d9117 371
6ad696d2
AK
372extern struct mutex pm_mutex;
373
1f112cee 374#ifndef CONFIG_HIBERNATE_CALLBACKS
6ad696d2
AK
375static inline void lock_system_sleep(void) {}
376static inline void unlock_system_sleep(void) {}
377
378#else
379
380/* Let some subsystems like memory hotadd exclude hibernation */
381
382static inline void lock_system_sleep(void)
383{
33e638b9 384 freezer_do_not_count();
6ad696d2
AK
385 mutex_lock(&pm_mutex);
386}
387
388static inline void unlock_system_sleep(void)
389{
390 mutex_unlock(&pm_mutex);
33e638b9 391 freezer_count();
6ad696d2
AK
392}
393#endif
89081d17 394
85055dd8
MS
395#ifdef CONFIG_ARCH_SAVE_PAGE_KEYS
396/*
397 * The ARCH_SAVE_PAGE_KEYS functions can be used by an architecture
398 * to save/restore additional information to/from the array of page
399 * frame numbers in the hibernation image. For s390 this is used to
400 * save and restore the storage key for each page that is included
401 * in the hibernation image.
402 */
403unsigned long page_key_additional_pages(unsigned long pages);
404int page_key_alloc(unsigned long pages);
405void page_key_free(void);
406void page_key_read(unsigned long *pfn);
407void page_key_memorize(unsigned long *pfn);
408void page_key_write(void *address);
409
410#else /* !CONFIG_ARCH_SAVE_PAGE_KEYS */
411
412static inline unsigned long page_key_additional_pages(unsigned long pages)
413{
414 return 0;
415}
416
417static inline int page_key_alloc(unsigned long pages)
418{
419 return 0;
420}
421
422static inline void page_key_free(void) {}
423static inline void page_key_read(unsigned long *pfn) {}
424static inline void page_key_memorize(unsigned long *pfn) {}
425static inline void page_key_write(void *address) {}
426
427#endif /* !CONFIG_ARCH_SAVE_PAGE_KEYS */
428
95d9ffbe 429#endif /* _LINUX_SUSPEND_H */
This page took 1.012266 seconds and 5 git commands to generate.