spi-topcliff-pch: Fix issue for transmitting over 4KByte
[zen-stable.git] / include / linux / suspend.h
blob91784a4f860852d01f958ecce70439e381dcaabf
1 #ifndef _LINUX_SUSPEND_H
2 #define _LINUX_SUSPEND_H
4 #include <linux/swap.h>
5 #include <linux/notifier.h>
6 #include <linux/init.h>
7 #include <linux/pm.h>
8 #include <linux/mm.h>
9 #include <linux/freezer.h>
10 #include <asm/errno.h>
12 #ifdef CONFIG_VT
13 extern void pm_set_vt_switch(int);
14 #else
15 static inline void pm_set_vt_switch(int do_switch)
18 #endif
20 #ifdef CONFIG_VT_CONSOLE_SLEEP
21 extern int pm_prepare_console(void);
22 extern void pm_restore_console(void);
23 #else
24 static inline int pm_prepare_console(void)
26 return 0;
29 static inline void pm_restore_console(void)
32 #endif
34 typedef int __bitwise suspend_state_t;
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)
41 enum suspend_stat_step {
42 SUSPEND_FREEZE = 1,
43 SUSPEND_PREPARE,
44 SUSPEND_SUSPEND,
45 SUSPEND_SUSPEND_NOIRQ,
46 SUSPEND_RESUME_NOIRQ,
47 SUSPEND_RESUME
50 struct 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];
68 extern struct suspend_stats suspend_stats;
70 static inline void dpm_save_failed_dev(const char *name)
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;
79 static inline void dpm_save_failed_errno(int err)
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;
86 static inline void dpm_save_failed_step(enum suspend_stat_step step)
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;
93 /**
94 * struct platform_suspend_ops - Callbacks for managing platform dependent
95 * system sleep states.
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.
102 * There is the %suspend_valid_only_mem function available that can be
103 * assigned to this if the platform only supports mem sleep.
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),
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
111 * passed to @enter() is redundant and should be ignored.
113 * @prepare: Prepare the platform for entering the system sleep state indicated
114 * by @begin().
115 * @prepare() is called right after devices have been suspended (ie. the
116 * appropriate .suspend() method has been executed for each device) and
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(),
120 * and @wake() will not be called in that case).
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
127 * system cannot enter the desired sleep state (@enter() will not be
128 * executed).
130 * @enter: Enter the system sleep state indicated by @begin() or represented by
131 * the argument if @begin() is not implemented.
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.
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
141 * after @prepare_late and @enter(), even if one of them fails.
143 * @finish: Finish wake-up of the platform.
144 * @finish is called right prior to calling device drivers' regular suspend
145 * callbacks.
146 * This callback is optional, but should be implemented by the platforms
147 * that implement @prepare(). If implemented, it is always called after
148 * @enter() and @wake(), even if any of them fails. It is executed after
149 * a failing @prepare.
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.
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
162 * that implement @begin(). Accordingly, platforms implementing @begin()
163 * should also provide a @end() which cleans up transitions aborted before
164 * @enter().
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.
171 struct platform_suspend_ops {
172 int (*valid)(suspend_state_t state);
173 int (*begin)(suspend_state_t state);
174 int (*prepare)(void);
175 int (*prepare_late)(void);
176 int (*enter)(suspend_state_t state);
177 void (*wake)(void);
178 void (*finish)(void);
179 bool (*suspend_again)(void);
180 void (*end)(void);
181 void (*recover)(void);
184 #ifdef CONFIG_SUSPEND
186 * suspend_set_ops - set platform dependent suspend operations
187 * @ops: The new suspend operations to set.
189 extern void suspend_set_ops(const struct platform_suspend_ops *ops);
190 extern int suspend_valid_only_mem(suspend_state_t state);
193 * arch_suspend_disable_irqs - disable IRQs for suspend
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.
199 extern void arch_suspend_disable_irqs(void);
202 * arch_suspend_enable_irqs - enable IRQs after suspend
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.
208 extern void arch_suspend_enable_irqs(void);
210 extern int pm_suspend(suspend_state_t state);
211 #else /* !CONFIG_SUSPEND */
212 #define suspend_valid_only_mem NULL
214 static inline void suspend_set_ops(const struct platform_suspend_ops *ops) {}
215 static inline int pm_suspend(suspend_state_t state) { return -ENOSYS; }
216 #endif /* !CONFIG_SUSPEND */
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.
222 struct pbe {
223 void *address; /* address of the copy */
224 void *orig_address; /* original address of a page */
225 struct pbe *next;
228 /* mm/page_alloc.c */
229 extern void mark_free_pages(struct zone *zone);
232 * struct platform_hibernation_ops - hibernation platform support
234 * The methods in this structure allow a platform to carry out special
235 * operations required by it during a hibernation transition.
237 * All the methods below, except for @recover(), must be implemented.
239 * @begin: Tell the platform driver that we're starting hibernation.
240 * Called right after shrinking memory and before freezing devices.
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.
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).
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).
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.
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).
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.
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).
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).
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.
283 struct platform_hibernation_ops {
284 int (*begin)(void);
285 void (*end)(void);
286 int (*pre_snapshot)(void);
287 void (*finish)(void);
288 int (*prepare)(void);
289 int (*enter)(void);
290 void (*leave)(void);
291 int (*pre_restore)(void);
292 void (*restore_cleanup)(void);
293 void (*recover)(void);
296 #ifdef CONFIG_HIBERNATION
297 /* kernel/power/snapshot.c */
298 extern void __register_nosave_region(unsigned long b, unsigned long e, int km);
299 static inline void __init register_nosave_region(unsigned long b, unsigned long e)
301 __register_nosave_region(b, e, 0);
303 static inline void __init register_nosave_region_late(unsigned long b, unsigned long e)
305 __register_nosave_region(b, e, 1);
307 extern int swsusp_page_is_forbidden(struct page *);
308 extern void swsusp_set_page_free(struct page *);
309 extern void swsusp_unset_page_free(struct page *);
310 extern unsigned long get_safe_page(gfp_t gfp_mask);
312 extern void hibernation_set_ops(const struct platform_hibernation_ops *ops);
313 extern int hibernate(void);
314 extern bool system_entering_hibernation(void);
315 #else /* CONFIG_HIBERNATION */
316 static inline void register_nosave_region(unsigned long b, unsigned long e) {}
317 static inline void register_nosave_region_late(unsigned long b, unsigned long e) {}
318 static inline int swsusp_page_is_forbidden(struct page *p) { return 0; }
319 static inline void swsusp_set_page_free(struct page *p) {}
320 static inline void swsusp_unset_page_free(struct page *p) {}
322 static inline void hibernation_set_ops(const struct platform_hibernation_ops *ops) {}
323 static inline int hibernate(void) { return -ENOSYS; }
324 static inline bool system_entering_hibernation(void) { return false; }
325 #endif /* CONFIG_HIBERNATION */
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 */
335 extern struct mutex pm_mutex;
337 #ifdef CONFIG_PM_SLEEP
338 void save_processor_state(void);
339 void restore_processor_state(void);
341 /* kernel/power/main.c */
342 extern int register_pm_notifier(struct notifier_block *nb);
343 extern int unregister_pm_notifier(struct notifier_block *nb);
345 #define pm_notifier(fn, pri) { \
346 static struct notifier_block fn##_nb = \
347 { .notifier_call = fn, .priority = pri }; \
348 register_pm_notifier(&fn##_nb); \
351 /* drivers/base/power/wakeup.c */
352 extern bool events_check_enabled;
354 extern bool pm_wakeup_pending(void);
355 extern bool pm_get_wakeup_count(unsigned int *count);
356 extern bool pm_save_wakeup_count(unsigned int count);
358 static inline void lock_system_sleep(void)
360 current->flags |= PF_FREEZER_SKIP;
361 mutex_lock(&pm_mutex);
364 static inline void unlock_system_sleep(void)
367 * Don't use freezer_count() because we don't want the call to
368 * try_to_freeze() here.
370 * Reason:
371 * Fundamentally, we just don't need it, because freezing condition
372 * doesn't come into effect until we release the pm_mutex lock,
373 * since the freezer always works with pm_mutex held.
375 * More importantly, in the case of hibernation,
376 * unlock_system_sleep() gets called in snapshot_read() and
377 * snapshot_write() when the freezing condition is still in effect.
378 * Which means, if we use try_to_freeze() here, it would make them
379 * enter the refrigerator, thus causing hibernation to lockup.
381 current->flags &= ~PF_FREEZER_SKIP;
382 mutex_unlock(&pm_mutex);
385 #else /* !CONFIG_PM_SLEEP */
387 static inline int register_pm_notifier(struct notifier_block *nb)
389 return 0;
392 static inline int unregister_pm_notifier(struct notifier_block *nb)
394 return 0;
397 #define pm_notifier(fn, pri) do { (void)(fn); } while (0)
399 static inline bool pm_wakeup_pending(void) { return false; }
401 static inline void lock_system_sleep(void) {}
402 static inline void unlock_system_sleep(void) {}
404 #endif /* !CONFIG_PM_SLEEP */
406 #ifdef CONFIG_ARCH_SAVE_PAGE_KEYS
408 * The ARCH_SAVE_PAGE_KEYS functions can be used by an architecture
409 * to save/restore additional information to/from the array of page
410 * frame numbers in the hibernation image. For s390 this is used to
411 * save and restore the storage key for each page that is included
412 * in the hibernation image.
414 unsigned long page_key_additional_pages(unsigned long pages);
415 int page_key_alloc(unsigned long pages);
416 void page_key_free(void);
417 void page_key_read(unsigned long *pfn);
418 void page_key_memorize(unsigned long *pfn);
419 void page_key_write(void *address);
421 #else /* !CONFIG_ARCH_SAVE_PAGE_KEYS */
423 static inline unsigned long page_key_additional_pages(unsigned long pages)
425 return 0;
428 static inline int page_key_alloc(unsigned long pages)
430 return 0;
433 static inline void page_key_free(void) {}
434 static inline void page_key_read(unsigned long *pfn) {}
435 static inline void page_key_memorize(unsigned long *pfn) {}
436 static inline void page_key_write(void *address) {}
438 #endif /* !CONFIG_ARCH_SAVE_PAGE_KEYS */
440 #endif /* _LINUX_SUSPEND_H */