1 #ifndef _LINUX_SUSPEND_H 2 #define _LINUX_SUSPEND_H 3 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> 11 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) 16 { 17 } 18 #endif 19 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) 25 { 26 return 0; 27 } 28 29 static inline void pm_restore_console(void) 30 { 31 } 32 #endif 33 34 typedef 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 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 48 }; 49 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]; 66 }; 67 68 extern struct suspend_stats suspend_stats; 69 70 static 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 79 static 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 86 static 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 93 /** 94 * struct platform_suspend_ops - Callbacks for managing platform dependent 95 * system sleep states. 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. 102 * There is the %suspend_valid_only_mem function available that can be 103 * assigned to this if the platform only supports mem sleep. 104 * 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. 112 * 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). 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 127 * system cannot enter the desired sleep state (@enter() will not be 128 * executed). 129 * 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. 135 * 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. 142 * 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. 150 * 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 * 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(). 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. 170 */ 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); 182 }; 183 184 #ifdef CONFIG_SUSPEND 185 /** 186 * suspend_set_ops - set platform dependent suspend operations 187 * @ops: The new suspend operations to set. 188 */ 189 extern void suspend_set_ops(const struct platform_suspend_ops *ops); 190 extern int suspend_valid_only_mem(suspend_state_t state); 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 */ 199 extern 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 */ 208 extern void arch_suspend_enable_irqs(void); 209 210 extern int pm_suspend(suspend_state_t state); 211 #else /* !CONFIG_SUSPEND */ 212 #define suspend_valid_only_mem NULL 213 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 */ 217 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 */ 222 struct pbe { 223 void *address; /* address of the copy */ 224 void *orig_address; /* original address of a page */ 225 struct pbe *next; 226 }; 227 228 /* mm/page_alloc.c */ 229 extern void mark_free_pages(struct zone *zone); 230 231 /** 232 * struct platform_hibernation_ops - hibernation platform support 233 * 234 * The methods in this structure allow a platform to carry out special 235 * operations required by it during a hibernation transition. 236 * 237 * All the methods below, except for @recover(), must be implemented. 238 * 239 * @begin: Tell the platform driver that we're starting hibernation. 240 * Called right after shrinking memory and before freezing devices. 241 * 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 * 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 * 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 * 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). 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. 282 */ 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); 294 }; 295 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) 300 { 301 __register_nosave_region(b, e, 0); 302 } 303 static inline void __init register_nosave_region_late(unsigned long b, unsigned long e) 304 { 305 __register_nosave_region(b, e, 1); 306 } 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); 311 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) {} 321 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 */ 326 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 335 extern struct mutex pm_mutex; 336 337 #ifdef CONFIG_PM_SLEEP 338 void save_processor_state(void); 339 void restore_processor_state(void); 340 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); 344 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); \ 349 } 350 351 /* drivers/base/power/wakeup.c */ 352 extern bool events_check_enabled; 353 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); 357 358 static inline void lock_system_sleep(void) 359 { 360 freezer_do_not_count(); 361 mutex_lock(&pm_mutex); 362 } 363 364 static inline void unlock_system_sleep(void) 365 { 366 mutex_unlock(&pm_mutex); 367 freezer_count(); 368 } 369 370 #else /* !CONFIG_PM_SLEEP */ 371 372 static inline int register_pm_notifier(struct notifier_block *nb) 373 { 374 return 0; 375 } 376 377 static inline int unregister_pm_notifier(struct notifier_block *nb) 378 { 379 return 0; 380 } 381 382 #define pm_notifier(fn, pri) do { (void)(fn); } while (0) 383 384 static inline bool pm_wakeup_pending(void) { return false; } 385 386 static inline void lock_system_sleep(void) {} 387 static inline void unlock_system_sleep(void) {} 388 389 #endif /* !CONFIG_PM_SLEEP */ 390 391 #ifdef CONFIG_ARCH_SAVE_PAGE_KEYS 392 /* 393 * The ARCH_SAVE_PAGE_KEYS functions can be used by an architecture 394 * to save/restore additional information to/from the array of page 395 * frame numbers in the hibernation image. For s390 this is used to 396 * save and restore the storage key for each page that is included 397 * in the hibernation image. 398 */ 399 unsigned long page_key_additional_pages(unsigned long pages); 400 int page_key_alloc(unsigned long pages); 401 void page_key_free(void); 402 void page_key_read(unsigned long *pfn); 403 void page_key_memorize(unsigned long *pfn); 404 void page_key_write(void *address); 405 406 #else /* !CONFIG_ARCH_SAVE_PAGE_KEYS */ 407 408 static inline unsigned long page_key_additional_pages(unsigned long pages) 409 { 410 return 0; 411 } 412 413 static inline int page_key_alloc(unsigned long pages) 414 { 415 return 0; 416 } 417 418 static inline void page_key_free(void) {} 419 static inline void page_key_read(unsigned long *pfn) {} 420 static inline void page_key_memorize(unsigned long *pfn) {} 421 static inline void page_key_write(void *address) {} 422 423 #endif /* !CONFIG_ARCH_SAVE_PAGE_KEYS */ 424 425 #endif /* _LINUX_SUSPEND_H */ 426