1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #include <linux/suspend.h> 3 #include <linux/suspend_ioctls.h> 4 #include <linux/utsname.h> 5 #include <linux/freezer.h> 6 #include <linux/compiler.h> 7 #include <linux/cpu.h> 8 #include <linux/cpuidle.h> 9 #include <linux/crypto.h> 10 11 struct swsusp_info { 12 struct new_utsname uts; 13 u32 version_code; 14 unsigned long num_physpages; 15 int cpus; 16 unsigned long image_pages; 17 unsigned long pages; 18 unsigned long size; 19 } __aligned(PAGE_SIZE); 20 21 #if defined(CONFIG_SUSPEND) || defined(CONFIG_HIBERNATION) 22 extern bool filesystem_freeze_enabled; 23 #endif 24 25 #ifdef CONFIG_HIBERNATION 26 /* kernel/power/snapshot.c */ 27 extern void __init hibernate_reserved_size_init(void); 28 extern void __init hibernate_image_size_init(void); 29 30 #ifdef CONFIG_ARCH_HIBERNATION_HEADER 31 /* Maximum size of architecture specific data in a hibernation header */ 32 #define MAX_ARCH_HEADER_SIZE (sizeof(struct new_utsname) + 4) 33 34 static inline int init_header_complete(struct swsusp_info *info) 35 { 36 return arch_hibernation_header_save(info, MAX_ARCH_HEADER_SIZE); 37 } 38 39 static inline const char *check_image_kernel(struct swsusp_info *info) 40 { 41 return arch_hibernation_header_restore(info) ? 42 "architecture specific data" : NULL; 43 } 44 #endif /* CONFIG_ARCH_HIBERNATION_HEADER */ 45 46 /* 47 * Keep some memory free so that I/O operations can succeed without paging 48 * [Might this be more than 4 MB?] 49 */ 50 #define PAGES_FOR_IO ((4096 * 1024) >> PAGE_SHIFT) 51 52 /* 53 * Keep 1 MB of memory free so that device drivers can allocate some pages in 54 * their .suspend() routines without breaking the suspend to disk. 55 */ 56 #define SPARE_PAGES ((1024 * 1024) >> PAGE_SHIFT) 57 58 asmlinkage int swsusp_save(void); 59 60 /* kernel/power/hibernate.c */ 61 extern bool freezer_test_done; 62 extern char hib_comp_algo[CRYPTO_MAX_ALG_NAME]; 63 64 /* kernel/power/swap.c */ 65 extern unsigned int swsusp_header_flags; 66 67 extern int hibernation_snapshot(int platform_mode); 68 extern int hibernation_restore(int platform_mode); 69 extern int hibernation_platform_enter(void); 70 71 #ifdef CONFIG_STRICT_KERNEL_RWX 72 /* kernel/power/snapshot.c */ 73 extern void enable_restore_image_protection(void); 74 #else 75 static inline void enable_restore_image_protection(void) {} 76 #endif /* CONFIG_STRICT_KERNEL_RWX */ 77 78 extern bool hibernation_in_progress(void); 79 80 #else /* !CONFIG_HIBERNATION */ 81 82 static inline void hibernate_reserved_size_init(void) {} 83 static inline void hibernate_image_size_init(void) {} 84 85 static inline bool hibernation_in_progress(void) { return false; } 86 #endif /* !CONFIG_HIBERNATION */ 87 88 #define power_attr(_name) \ 89 static struct kobj_attribute _name##_attr = { \ 90 .attr = { \ 91 .name = __stringify(_name), \ 92 .mode = 0644, \ 93 }, \ 94 .show = _name##_show, \ 95 .store = _name##_store, \ 96 } 97 98 #define power_attr_ro(_name) \ 99 static struct kobj_attribute _name##_attr = { \ 100 .attr = { \ 101 .name = __stringify(_name), \ 102 .mode = S_IRUGO, \ 103 }, \ 104 .show = _name##_show, \ 105 } 106 107 /* Preferred image size in bytes (default 500 MB) */ 108 extern unsigned long image_size; 109 /* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */ 110 extern unsigned long reserved_size; 111 extern int in_suspend; 112 extern dev_t swsusp_resume_device; 113 extern sector_t swsusp_resume_block; 114 115 extern int create_basic_memory_bitmaps(void); 116 extern void free_basic_memory_bitmaps(void); 117 extern int hibernate_preallocate_memory(void); 118 119 extern void clear_or_poison_free_pages(void); 120 121 /* 122 * Auxiliary structure used for reading the snapshot image data and 123 * metadata from and writing them to the list of page backup entries 124 * (PBEs) which is the main data structure of swsusp. 125 * 126 * Using struct snapshot_handle we can transfer the image, including its 127 * metadata, as a continuous sequence of bytes with the help of 128 * snapshot_read_next() and snapshot_write_next(). 129 * 130 * The code that writes the image to a storage or transfers it to 131 * the user land is required to use snapshot_read_next() for this 132 * purpose and it should not make any assumptions regarding the internal 133 * structure of the image. Similarly, the code that reads the image from 134 * a storage or transfers it from the user land is required to use 135 * snapshot_write_next(). 136 * 137 * This may allow us to change the internal structure of the image 138 * in the future with considerably less effort. 139 */ 140 141 struct snapshot_handle { 142 unsigned int cur; /* number of the block of PAGE_SIZE bytes the 143 * next operation will refer to (ie. current) 144 */ 145 void *buffer; /* address of the block to read from 146 * or write to 147 */ 148 int sync_read; /* Set to one to notify the caller of 149 * snapshot_write_next() that it may 150 * need to call wait_on_bio_chain() 151 */ 152 }; 153 154 /* This macro returns the address from/to which the caller of 155 * snapshot_read_next()/snapshot_write_next() is allowed to 156 * read/write data after the function returns 157 */ 158 #define data_of(handle) ((handle).buffer) 159 160 extern unsigned int snapshot_additional_pages(struct zone *zone); 161 extern unsigned long snapshot_get_image_size(void); 162 extern int snapshot_read_next(struct snapshot_handle *handle); 163 extern int snapshot_write_next(struct snapshot_handle *handle); 164 int snapshot_write_finalize(struct snapshot_handle *handle); 165 extern int snapshot_image_loaded(struct snapshot_handle *handle); 166 167 extern bool hibernate_acquire(void); 168 extern void hibernate_release(void); 169 170 extern sector_t alloc_swapdev_block(int swap); 171 extern void free_all_swap_pages(int swap); 172 extern int swsusp_swap_in_use(void); 173 174 /* 175 * Flags that can be passed from the hibernatig hernel to the "boot" kernel in 176 * the image header. 177 */ 178 #define SF_COMPRESSION_ALG_LZO 0 /* dummy, details given below */ 179 #define SF_PLATFORM_MODE 1 180 #define SF_NOCOMPRESS_MODE 2 181 #define SF_CRC32_MODE 4 182 #define SF_HW_SIG 8 183 184 /* 185 * Bit to indicate the compression algorithm to be used(for LZ4). The same 186 * could be checked while saving/loading image to/from disk to use the 187 * corresponding algorithms. 188 * 189 * By default, LZO compression is enabled if SF_CRC32_MODE is set. Use 190 * SF_COMPRESSION_ALG_LZ4 to override this behaviour and use LZ4. 191 * 192 * SF_CRC32_MODE, SF_COMPRESSION_ALG_LZO(dummy) -> Compression, LZO 193 * SF_CRC32_MODE, SF_COMPRESSION_ALG_LZ4 -> Compression, LZ4 194 */ 195 #define SF_COMPRESSION_ALG_LZ4 16 196 197 /* kernel/power/hibernate.c */ 198 int swsusp_check(bool exclusive); 199 extern void swsusp_free(void); 200 extern int swsusp_read(unsigned int *flags_p); 201 extern int swsusp_write(unsigned int flags); 202 void swsusp_close(void); 203 #ifdef CONFIG_SUSPEND 204 extern int swsusp_unmark(void); 205 #else 206 static inline int swsusp_unmark(void) { return 0; } 207 #endif 208 209 struct __kernel_old_timeval; 210 /* kernel/power/swsusp.c */ 211 extern void swsusp_show_speed(ktime_t, ktime_t, unsigned int, char *); 212 213 #ifdef CONFIG_SUSPEND 214 /* kernel/power/suspend.c */ 215 extern const char * const pm_labels[]; 216 extern const char *pm_states[]; 217 extern const char *mem_sleep_states[]; 218 219 extern int suspend_devices_and_enter(suspend_state_t state); 220 #else /* !CONFIG_SUSPEND */ 221 #define mem_sleep_current PM_SUSPEND_ON 222 223 static inline int suspend_devices_and_enter(suspend_state_t state) 224 { 225 return -ENOSYS; 226 } 227 #endif /* !CONFIG_SUSPEND */ 228 229 #ifdef CONFIG_PM_TEST_SUSPEND 230 /* kernel/power/suspend_test.c */ 231 extern void suspend_test_start(void); 232 extern void suspend_test_finish(const char *label); 233 #else /* !CONFIG_PM_TEST_SUSPEND */ 234 static inline void suspend_test_start(void) {} 235 static inline void suspend_test_finish(const char *label) {} 236 #endif /* !CONFIG_PM_TEST_SUSPEND */ 237 238 #ifdef CONFIG_PM_SLEEP 239 /* kernel/power/main.c */ 240 extern int pm_notifier_call_chain_robust(unsigned long val_up, unsigned long val_down); 241 extern int pm_notifier_call_chain(unsigned long val); 242 void pm_restrict_gfp_mask(void); 243 void pm_restore_gfp_mask(void); 244 #else 245 static inline void pm_restrict_gfp_mask(void) {} 246 static inline void pm_restore_gfp_mask(void) {} 247 #endif 248 249 #ifdef CONFIG_HIGHMEM 250 int restore_highmem(void); 251 #else 252 static inline unsigned int count_highmem_pages(void) { return 0; } 253 static inline int restore_highmem(void) { return 0; } 254 #endif 255 256 /* 257 * Suspend test levels 258 */ 259 enum { 260 /* keep first */ 261 TEST_NONE, 262 TEST_CORE, 263 TEST_CPUS, 264 TEST_PLATFORM, 265 TEST_DEVICES, 266 TEST_FREEZER, 267 /* keep last */ 268 __TEST_AFTER_LAST 269 }; 270 271 #define TEST_FIRST TEST_NONE 272 #define TEST_MAX (__TEST_AFTER_LAST - 1) 273 274 #ifdef CONFIG_PM_SLEEP_DEBUG 275 extern int pm_test_level; 276 #else 277 #define pm_test_level (TEST_NONE) 278 #endif 279 280 #ifdef CONFIG_SUSPEND_FREEZER 281 static inline int suspend_freeze_processes(void) 282 { 283 int error; 284 285 error = freeze_processes(); 286 /* 287 * freeze_processes() automatically thaws every task if freezing 288 * fails. So we need not do anything extra upon error. 289 */ 290 if (error) 291 return error; 292 293 error = freeze_kernel_threads(); 294 /* 295 * freeze_kernel_threads() thaws only kernel threads upon freezing 296 * failure. So we have to thaw the userspace tasks ourselves. 297 */ 298 if (error) 299 thaw_processes(); 300 301 return error; 302 } 303 304 static inline void suspend_thaw_processes(void) 305 { 306 thaw_processes(); 307 } 308 #else 309 static inline int suspend_freeze_processes(void) 310 { 311 return 0; 312 } 313 314 static inline void suspend_thaw_processes(void) 315 { 316 } 317 #endif 318 319 #ifdef CONFIG_PM_AUTOSLEEP 320 321 /* kernel/power/autosleep.c */ 322 extern int pm_autosleep_init(void); 323 extern int pm_autosleep_lock(void); 324 extern void pm_autosleep_unlock(void); 325 extern suspend_state_t pm_autosleep_state(void); 326 extern int pm_autosleep_set_state(suspend_state_t state); 327 328 #else /* !CONFIG_PM_AUTOSLEEP */ 329 330 static inline int pm_autosleep_init(void) { return 0; } 331 static inline int pm_autosleep_lock(void) { return 0; } 332 static inline void pm_autosleep_unlock(void) {} 333 static inline suspend_state_t pm_autosleep_state(void) { return PM_SUSPEND_ON; } 334 335 #endif /* !CONFIG_PM_AUTOSLEEP */ 336 337 #ifdef CONFIG_PM_WAKELOCKS 338 339 /* kernel/power/wakelock.c */ 340 extern ssize_t pm_show_wakelocks(char *buf, bool show_active); 341 extern int pm_wake_lock(const char *buf); 342 extern int pm_wake_unlock(const char *buf); 343 344 #endif /* !CONFIG_PM_WAKELOCKS */ 345 346 static inline int pm_sleep_disable_secondary_cpus(void) 347 { 348 cpuidle_pause(); 349 return suspend_disable_secondary_cpus(); 350 } 351 352 static inline void pm_sleep_enable_secondary_cpus(void) 353 { 354 suspend_enable_secondary_cpus(); 355 cpuidle_resume(); 356 } 357 358 void dpm_save_errno(int err); 359