Commit | Line | Data |
---|---|---|
55716d26 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 | 2 | /* |
8b759b84 | 3 | * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support. |
1da177e4 LT |
4 | * |
5 | * Copyright (c) 2003 Patrick Mochel | |
6 | * Copyright (c) 2003 Open Source Development Lab | |
a2531293 | 7 | * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz> |
8b759b84 | 8 | * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc. |
62c552cc | 9 | * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com> |
1da177e4 LT |
10 | */ |
11 | ||
7a7b99bf | 12 | #define pr_fmt(fmt) "PM: hibernation: " fmt |
2872de13 | 13 | |
b03d542c | 14 | #include <crypto/acompress.h> |
cf056a43 | 15 | #include <linux/blkdev.h> |
6e5fdeed | 16 | #include <linux/export.h> |
1da177e4 | 17 | #include <linux/suspend.h> |
1da177e4 LT |
18 | #include <linux/reboot.h> |
19 | #include <linux/string.h> | |
20 | #include <linux/device.h> | |
6f8d7022 | 21 | #include <linux/async.h> |
1da177e4 LT |
22 | #include <linux/delay.h> |
23 | #include <linux/fs.h> | |
d53d9f16 | 24 | #include <linux/mount.h> |
88d10bba | 25 | #include <linux/pm.h> |
38b8d208 | 26 | #include <linux/nmi.h> |
97c7801c | 27 | #include <linux/console.h> |
e3920fb4 | 28 | #include <linux/cpu.h> |
7dfb7103 | 29 | #include <linux/freezer.h> |
5a0e3ad6 | 30 | #include <linux/gfp.h> |
40dc166c | 31 | #include <linux/syscore_ops.h> |
2df83fa4 | 32 | #include <linux/ctype.h> |
db597605 | 33 | #include <linux/ktime.h> |
38bd94b8 | 34 | #include <linux/security.h> |
9a436f8f | 35 | #include <linux/secretmem.h> |
bb3632c6 | 36 | #include <trace/events/power.h> |
d53d9f16 | 37 | |
1da177e4 LT |
38 | #include "power.h" |
39 | ||
40 | ||
d231ff1a BS |
41 | static int nocompress; |
42 | static int noresume; | |
a6e15a39 | 43 | static int nohibernate; |
d231ff1a | 44 | static int resume_wait; |
f6514be5 | 45 | static unsigned int resume_delay; |
47a460d5 | 46 | static char resume_file[256] = CONFIG_PM_STD_PARTITION; |
1da177e4 | 47 | dev_t swsusp_resume_device; |
9a154d9d | 48 | sector_t swsusp_resume_block; |
d6efc2f7 | 49 | __visible int in_suspend __nosavedata; |
1da177e4 | 50 | |
3fec6e59 | 51 | static char hibernate_compressor[CRYPTO_MAX_ALG_NAME] = CONFIG_HIBERNATION_DEF_COMP; |
a06c6f5d N |
52 | |
53 | /* | |
54 | * Compression/decompression algorithm to be used while saving/loading | |
55 | * image to/from disk. This would later be used in 'kernel/power/swap.c' | |
56 | * to allocate comp streams. | |
57 | */ | |
58 | char hib_comp_algo[CRYPTO_MAX_ALG_NAME]; | |
59 | ||
a3d25c27 RW |
60 | enum { |
61 | HIBERNATION_INVALID, | |
62 | HIBERNATION_PLATFORM, | |
a3d25c27 RW |
63 | HIBERNATION_SHUTDOWN, |
64 | HIBERNATION_REBOOT, | |
62c552cc BS |
65 | #ifdef CONFIG_SUSPEND |
66 | HIBERNATION_SUSPEND, | |
67 | #endif | |
fe12c00d | 68 | HIBERNATION_TEST_RESUME, |
a3d25c27 RW |
69 | /* keep last */ |
70 | __HIBERNATION_AFTER_LAST | |
71 | }; | |
72 | #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1) | |
73 | #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1) | |
74 | ||
75 | static int hibernation_mode = HIBERNATION_SHUTDOWN; | |
76 | ||
97819a26 | 77 | bool freezer_test_done; |
aa9a7b11 | 78 | |
073ef1f6 | 79 | static const struct platform_hibernation_ops *hibernation_ops; |
a3d25c27 | 80 | |
ab7e9b06 DA |
81 | static atomic_t hibernate_atomic = ATOMIC_INIT(1); |
82 | ||
83 | bool hibernate_acquire(void) | |
84 | { | |
85 | return atomic_add_unless(&hibernate_atomic, -1, 0); | |
86 | } | |
87 | ||
88 | void hibernate_release(void) | |
89 | { | |
90 | atomic_inc(&hibernate_atomic); | |
91 | } | |
92 | ||
1b17d452 RW |
93 | bool hibernation_in_progress(void) |
94 | { | |
95 | return !atomic_read(&hibernate_atomic); | |
96 | } | |
97 | ||
a6e15a39 KC |
98 | bool hibernation_available(void) |
99 | { | |
9a436f8f MR |
100 | return nohibernate == 0 && |
101 | !security_locked_down(LOCKDOWN_HIBERNATION) && | |
9ea4dcf4 | 102 | !secretmem_active() && !cxl_mem_active(); |
a6e15a39 KC |
103 | } |
104 | ||
a3d25c27 | 105 | /** |
f42a9813 RW |
106 | * hibernation_set_ops - Set the global hibernate operations. |
107 | * @ops: Hibernation operations to use in subsequent hibernation transitions. | |
a3d25c27 | 108 | */ |
073ef1f6 | 109 | void hibernation_set_ops(const struct platform_hibernation_ops *ops) |
a3d25c27 | 110 | { |
5950e5d5 PZ |
111 | unsigned int sleep_flags; |
112 | ||
caea99ef RW |
113 | if (ops && !(ops->begin && ops->end && ops->pre_snapshot |
114 | && ops->prepare && ops->finish && ops->enter && ops->pre_restore | |
5729c63a | 115 | && ops->restore_cleanup && ops->leave)) { |
a3d25c27 RW |
116 | WARN_ON(1); |
117 | return; | |
118 | } | |
5950e5d5 PZ |
119 | |
120 | sleep_flags = lock_system_sleep(); | |
121 | ||
a3d25c27 RW |
122 | hibernation_ops = ops; |
123 | if (ops) | |
124 | hibernation_mode = HIBERNATION_PLATFORM; | |
125 | else if (hibernation_mode == HIBERNATION_PLATFORM) | |
126 | hibernation_mode = HIBERNATION_SHUTDOWN; | |
127 | ||
5950e5d5 | 128 | unlock_system_sleep(sleep_flags); |
a3d25c27 | 129 | } |
e0c7855e | 130 | EXPORT_SYMBOL_GPL(hibernation_set_ops); |
a3d25c27 | 131 | |
abfe2d7b RW |
132 | static bool entering_platform_hibernation; |
133 | ||
134 | bool system_entering_hibernation(void) | |
135 | { | |
136 | return entering_platform_hibernation; | |
137 | } | |
138 | EXPORT_SYMBOL(system_entering_hibernation); | |
139 | ||
4cc79776 | 140 | #ifdef CONFIG_PM_DEBUG |
50c9bb30 ZZ |
141 | static unsigned int pm_test_delay = 5; |
142 | module_param(pm_test_delay, uint, 0644); | |
143 | MODULE_PARM_DESC(pm_test_delay, | |
144 | "Number of seconds to wait before resuming from hibernation test"); | |
4cc79776 RW |
145 | static void hibernation_debug_sleep(void) |
146 | { | |
50c9bb30 ZZ |
147 | pr_info("hibernation debug: Waiting for %d second(s).\n", |
148 | pm_test_delay); | |
149 | mdelay(pm_test_delay * 1000); | |
4cc79776 RW |
150 | } |
151 | ||
4cc79776 RW |
152 | static int hibernation_test(int level) |
153 | { | |
154 | if (pm_test_level == level) { | |
155 | hibernation_debug_sleep(); | |
156 | return 1; | |
157 | } | |
158 | return 0; | |
159 | } | |
160 | #else /* !CONFIG_PM_DEBUG */ | |
4cc79776 RW |
161 | static int hibernation_test(int level) { return 0; } |
162 | #endif /* !CONFIG_PM_DEBUG */ | |
163 | ||
74f270af | 164 | /** |
f42a9813 RW |
165 | * platform_begin - Call platform to start hibernation. |
166 | * @platform_mode: Whether or not to use the platform driver. | |
74f270af | 167 | */ |
caea99ef | 168 | static int platform_begin(int platform_mode) |
74f270af RW |
169 | { |
170 | return (platform_mode && hibernation_ops) ? | |
bb186901 | 171 | hibernation_ops->begin(PMSG_FREEZE) : 0; |
caea99ef RW |
172 | } |
173 | ||
174 | /** | |
f42a9813 RW |
175 | * platform_end - Call platform to finish transition to the working state. |
176 | * @platform_mode: Whether or not to use the platform driver. | |
caea99ef | 177 | */ |
caea99ef RW |
178 | static void platform_end(int platform_mode) |
179 | { | |
180 | if (platform_mode && hibernation_ops) | |
181 | hibernation_ops->end(); | |
74f270af | 182 | } |
a3d25c27 | 183 | |
8a05aac2 | 184 | /** |
f42a9813 RW |
185 | * platform_pre_snapshot - Call platform to prepare the machine for hibernation. |
186 | * @platform_mode: Whether or not to use the platform driver. | |
187 | * | |
188 | * Use the platform driver to prepare the system for creating a hibernate image, | |
189 | * if so configured, and return an error code if that fails. | |
8a05aac2 SS |
190 | */ |
191 | ||
74f270af | 192 | static int platform_pre_snapshot(int platform_mode) |
8a05aac2 | 193 | { |
7777fab9 | 194 | return (platform_mode && hibernation_ops) ? |
74f270af | 195 | hibernation_ops->pre_snapshot() : 0; |
a3d25c27 | 196 | } |
8a05aac2 | 197 | |
c7e0831d | 198 | /** |
f42a9813 RW |
199 | * platform_leave - Call platform to prepare a transition to the working state. |
200 | * @platform_mode: Whether or not to use the platform driver. | |
201 | * | |
202 | * Use the platform driver prepare to prepare the machine for switching to the | |
203 | * normal mode of operation. | |
204 | * | |
205 | * This routine is called on one CPU with interrupts disabled. | |
c7e0831d | 206 | */ |
c7e0831d RW |
207 | static void platform_leave(int platform_mode) |
208 | { | |
209 | if (platform_mode && hibernation_ops) | |
210 | hibernation_ops->leave(); | |
211 | } | |
212 | ||
a3d25c27 | 213 | /** |
f42a9813 RW |
214 | * platform_finish - Call platform to switch the system to the working state. |
215 | * @platform_mode: Whether or not to use the platform driver. | |
216 | * | |
217 | * Use the platform driver to switch the machine to the normal mode of | |
218 | * operation. | |
219 | * | |
220 | * This routine must be called after platform_prepare(). | |
a3d25c27 | 221 | */ |
7777fab9 | 222 | static void platform_finish(int platform_mode) |
a3d25c27 | 223 | { |
7777fab9 | 224 | if (platform_mode && hibernation_ops) |
a3d25c27 | 225 | hibernation_ops->finish(); |
8a05aac2 SS |
226 | } |
227 | ||
a634cc10 | 228 | /** |
f42a9813 RW |
229 | * platform_pre_restore - Prepare for hibernate image restoration. |
230 | * @platform_mode: Whether or not to use the platform driver. | |
231 | * | |
232 | * Use the platform driver to prepare the system for resume from a hibernation | |
233 | * image. | |
234 | * | |
235 | * If the restore fails after this function has been called, | |
236 | * platform_restore_cleanup() must be called. | |
a634cc10 | 237 | */ |
a634cc10 RW |
238 | static int platform_pre_restore(int platform_mode) |
239 | { | |
240 | return (platform_mode && hibernation_ops) ? | |
241 | hibernation_ops->pre_restore() : 0; | |
242 | } | |
243 | ||
244 | /** | |
f42a9813 RW |
245 | * platform_restore_cleanup - Switch to the working state after failing restore. |
246 | * @platform_mode: Whether or not to use the platform driver. | |
247 | * | |
248 | * Use the platform driver to switch the system to the normal mode of operation | |
249 | * after a failing restore. | |
250 | * | |
251 | * If platform_pre_restore() has been called before the failing restore, this | |
252 | * function must be called too, regardless of the result of | |
253 | * platform_pre_restore(). | |
a634cc10 | 254 | */ |
a634cc10 RW |
255 | static void platform_restore_cleanup(int platform_mode) |
256 | { | |
257 | if (platform_mode && hibernation_ops) | |
258 | hibernation_ops->restore_cleanup(); | |
259 | } | |
260 | ||
d8f3de0d | 261 | /** |
f42a9813 RW |
262 | * platform_recover - Recover from a failure to suspend devices. |
263 | * @platform_mode: Whether or not to use the platform driver. | |
d8f3de0d | 264 | */ |
d8f3de0d RW |
265 | static void platform_recover(int platform_mode) |
266 | { | |
267 | if (platform_mode && hibernation_ops && hibernation_ops->recover) | |
268 | hibernation_ops->recover(); | |
269 | } | |
270 | ||
8e60c6a1 | 271 | /** |
f42a9813 RW |
272 | * swsusp_show_speed - Print time elapsed between two events during hibernation. |
273 | * @start: Starting event. | |
274 | * @stop: Final event. | |
275 | * @nr_pages: Number of memory pages processed between @start and @stop. | |
276 | * @msg: Additional diagnostic message to print. | |
8e60c6a1 | 277 | */ |
db597605 TR |
278 | void swsusp_show_speed(ktime_t start, ktime_t stop, |
279 | unsigned nr_pages, char *msg) | |
8e60c6a1 | 280 | { |
db597605 | 281 | ktime_t diff; |
4881f603 CG |
282 | u64 elapsed_centisecs64; |
283 | unsigned int centisecs; | |
284 | unsigned int k; | |
285 | unsigned int kps; | |
8e60c6a1 | 286 | |
db597605 TR |
287 | diff = ktime_sub(stop, start); |
288 | elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC); | |
8e60c6a1 NC |
289 | centisecs = elapsed_centisecs64; |
290 | if (centisecs == 0) | |
291 | centisecs = 1; /* avoid div-by-zero */ | |
292 | k = nr_pages * (PAGE_SIZE / 1024); | |
293 | kps = (k * 100) / centisecs; | |
2872de13 RW |
294 | pr_info("%s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n", |
295 | msg, k, centisecs / 100, centisecs % 100, kps / 1000, | |
296 | (kps % 1000) / 10); | |
8e60c6a1 NC |
297 | } |
298 | ||
ec527c31 JK |
299 | __weak int arch_resume_nosmt(void) |
300 | { | |
301 | return 0; | |
302 | } | |
303 | ||
c7e0831d | 304 | /** |
f42a9813 RW |
305 | * create_image - Create a hibernation image. |
306 | * @platform_mode: Whether or not to use the platform driver. | |
307 | * | |
cf579dfb RW |
308 | * Execute device drivers' "late" and "noirq" freeze callbacks, create a |
309 | * hibernation image and run the drivers' "noirq" and "early" thaw callbacks. | |
f42a9813 RW |
310 | * |
311 | * Control reappears in this routine after the subsequent restore. | |
c7e0831d | 312 | */ |
47a460d5 | 313 | static int create_image(int platform_mode) |
c7e0831d RW |
314 | { |
315 | int error; | |
316 | ||
cf579dfb | 317 | error = dpm_suspend_end(PMSG_FREEZE); |
c7e0831d | 318 | if (error) { |
7a7b99bf | 319 | pr_err("Some devices failed to power down, aborting\n"); |
32bdfac5 | 320 | return error; |
c7e0831d | 321 | } |
2ed8d2b3 | 322 | |
4aecd671 RW |
323 | error = platform_pre_snapshot(platform_mode); |
324 | if (error || hibernation_test(TEST_PLATFORM)) | |
325 | goto Platform_finish; | |
326 | ||
23f62d7a | 327 | error = pm_sleep_disable_secondary_cpus(); |
48580ab8 | 328 | if (error || hibernation_test(TEST_CPUS)) |
4aecd671 RW |
329 | goto Enable_cpus; |
330 | ||
2ed8d2b3 RW |
331 | local_irq_disable(); |
332 | ||
c1a957d1 TG |
333 | system_state = SYSTEM_SUSPEND; |
334 | ||
2e711c04 | 335 | error = syscore_suspend(); |
770824bd | 336 | if (error) { |
7a7b99bf | 337 | pr_err("Some system devices failed to power down, aborting\n"); |
4aecd671 | 338 | goto Enable_irqs; |
770824bd | 339 | } |
c7e0831d | 340 | |
a2867e08 | 341 | if (hibernation_test(TEST_CORE) || pm_wakeup_pending()) |
4cc79776 RW |
342 | goto Power_up; |
343 | ||
344 | in_suspend = 1; | |
c7e0831d | 345 | save_processor_state(); |
bb3632c6 | 346 | trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true); |
c7e0831d | 347 | error = swsusp_arch_suspend(); |
62822e2e TG |
348 | /* Restore control flow magically appears here */ |
349 | restore_processor_state(); | |
bb3632c6 | 350 | trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false); |
c7e0831d | 351 | if (error) |
7a7b99bf | 352 | pr_err("Error %d creating image\n", error); |
2872de13 | 353 | |
1ad1410f | 354 | if (!in_suspend) { |
c125e96f | 355 | events_check_enabled = false; |
03b6c9a3 | 356 | clear_or_poison_free_pages(); |
1ad1410f | 357 | } |
362e77d1 BM |
358 | |
359 | platform_leave(platform_mode); | |
4aecd671 | 360 | |
4cc79776 | 361 | Power_up: |
40dc166c | 362 | syscore_resume(); |
2ed8d2b3 | 363 | |
4aecd671 | 364 | Enable_irqs: |
c1a957d1 | 365 | system_state = SYSTEM_RUNNING; |
2ed8d2b3 RW |
366 | local_irq_enable(); |
367 | ||
4aecd671 | 368 | Enable_cpus: |
23f62d7a | 369 | pm_sleep_enable_secondary_cpus(); |
4aecd671 | 370 | |
ec527c31 JK |
371 | /* Allow architectures to do nosmt-specific post-resume dances */ |
372 | if (!in_suspend) | |
373 | error = arch_resume_nosmt(); | |
374 | ||
4aecd671 RW |
375 | Platform_finish: |
376 | platform_finish(platform_mode); | |
377 | ||
cf579dfb | 378 | dpm_resume_start(in_suspend ? |
1eede070 | 379 | (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE); |
2ed8d2b3 | 380 | |
c7e0831d RW |
381 | return error; |
382 | } | |
383 | ||
7777fab9 | 384 | /** |
f42a9813 RW |
385 | * hibernation_snapshot - Quiesce devices and create a hibernation image. |
386 | * @platform_mode: If set, use platform driver to prepare for the transition. | |
7777fab9 | 387 | * |
55f2503c | 388 | * This routine must be called with system_transition_mutex held. |
7777fab9 | 389 | */ |
7777fab9 RW |
390 | int hibernation_snapshot(int platform_mode) |
391 | { | |
953a2063 | 392 | pm_message_t msg; |
cbe2f5a6 | 393 | int error; |
7777fab9 | 394 | |
27614273 | 395 | pm_suspend_clear_flags(); |
3fe0313e | 396 | error = platform_begin(platform_mode); |
7777fab9 | 397 | if (error) |
d074ee02 | 398 | goto Close; |
7777fab9 | 399 | |
64a473cb RW |
400 | /* Preallocate image memory before shutting down devices. */ |
401 | error = hibernate_preallocate_memory(); | |
2aede851 RW |
402 | if (error) |
403 | goto Close; | |
404 | ||
405 | error = freeze_kernel_threads(); | |
406 | if (error) | |
bb58dd5d | 407 | goto Cleanup; |
2aede851 | 408 | |
48580ab8 | 409 | if (hibernation_test(TEST_FREEZER)) { |
aa9a7b11 SB |
410 | |
411 | /* | |
412 | * Indicate to the caller that we are returning due to a | |
413 | * successful freezer test. | |
414 | */ | |
415 | freezer_test_done = true; | |
51d6ff7a | 416 | goto Thaw; |
aa9a7b11 SB |
417 | } |
418 | ||
2aede851 | 419 | error = dpm_prepare(PMSG_FREEZE); |
bb58dd5d | 420 | if (error) { |
953a2063 | 421 | dpm_complete(PMSG_RECOVER); |
51d6ff7a | 422 | goto Thaw; |
bb58dd5d | 423 | } |
74f270af | 424 | |
e9cec448 | 425 | console_suspend_all(); |
953a2063 | 426 | |
91e7c75b | 427 | error = dpm_suspend(PMSG_FREEZE); |
10a1803d | 428 | |
953a2063 SB |
429 | if (error || hibernation_test(TEST_DEVICES)) |
430 | platform_recover(platform_mode); | |
431 | else | |
432 | error = create_image(platform_mode); | |
7777fab9 | 433 | |
c9e664f1 | 434 | /* |
953a2063 SB |
435 | * In the case that we call create_image() above, the control |
436 | * returns here (1) after the image has been created or the | |
c9e664f1 RW |
437 | * image creation has failed and (2) after a successful restore. |
438 | */ | |
4cc79776 | 439 | |
64a473cb RW |
440 | /* We may need to release the preallocated image pages here. */ |
441 | if (error || !in_suspend) | |
442 | swsusp_free(); | |
443 | ||
91e7c75b RW |
444 | msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE; |
445 | dpm_resume(msg); | |
c9e664f1 RW |
446 | |
447 | if (error || !in_suspend) | |
448 | pm_restore_gfp_mask(); | |
449 | ||
63830aef | 450 | console_resume_all(); |
91e7c75b RW |
451 | dpm_complete(msg); |
452 | ||
caea99ef RW |
453 | Close: |
454 | platform_end(platform_mode); | |
7777fab9 | 455 | return error; |
d8f3de0d | 456 | |
51d6ff7a SB |
457 | Thaw: |
458 | thaw_kernel_threads(); | |
bb58dd5d RW |
459 | Cleanup: |
460 | swsusp_free(); | |
461 | goto Close; | |
7777fab9 RW |
462 | } |
463 | ||
406f992e RW |
464 | int __weak hibernate_resume_nonboot_cpu_disable(void) |
465 | { | |
2f1a6fbb | 466 | return suspend_disable_secondary_cpus(); |
406f992e RW |
467 | } |
468 | ||
72df68ca | 469 | /** |
f42a9813 RW |
470 | * resume_target_kernel - Restore system state from a hibernation image. |
471 | * @platform_mode: Whether or not to use the platform driver. | |
472 | * | |
cf579dfb RW |
473 | * Execute device drivers' "noirq" and "late" freeze callbacks, restore the |
474 | * contents of highmem that have not been restored yet from the image and run | |
475 | * the low-level code that will restore the remaining contents of memory and | |
476 | * switch to the just restored target kernel. | |
72df68ca | 477 | */ |
4aecd671 | 478 | static int resume_target_kernel(bool platform_mode) |
72df68ca RW |
479 | { |
480 | int error; | |
481 | ||
cf579dfb | 482 | error = dpm_suspend_end(PMSG_QUIESCE); |
72df68ca | 483 | if (error) { |
2872de13 | 484 | pr_err("Some devices failed to power down, aborting resume\n"); |
32bdfac5 | 485 | return error; |
72df68ca | 486 | } |
2ed8d2b3 | 487 | |
4aecd671 RW |
488 | error = platform_pre_restore(platform_mode); |
489 | if (error) | |
490 | goto Cleanup; | |
491 | ||
23f62d7a RW |
492 | cpuidle_pause(); |
493 | ||
406f992e | 494 | error = hibernate_resume_nonboot_cpu_disable(); |
4aecd671 RW |
495 | if (error) |
496 | goto Enable_cpus; | |
497 | ||
2ed8d2b3 | 498 | local_irq_disable(); |
c1a957d1 | 499 | system_state = SYSTEM_SUSPEND; |
2ed8d2b3 | 500 | |
2e711c04 | 501 | error = syscore_suspend(); |
4aecd671 RW |
502 | if (error) |
503 | goto Enable_irqs; | |
504 | ||
72df68ca RW |
505 | save_processor_state(); |
506 | error = restore_highmem(); | |
507 | if (!error) { | |
508 | error = swsusp_arch_resume(); | |
509 | /* | |
510 | * The code below is only ever reached in case of a failure. | |
4e2d9491 RW |
511 | * Otherwise, execution continues at the place where |
512 | * swsusp_arch_suspend() was called. | |
72df68ca RW |
513 | */ |
514 | BUG_ON(!error); | |
4e2d9491 RW |
515 | /* |
516 | * This call to restore_highmem() reverts the changes made by | |
517 | * the previous one. | |
518 | */ | |
72df68ca RW |
519 | restore_highmem(); |
520 | } | |
521 | /* | |
522 | * The only reason why swsusp_arch_resume() can fail is memory being | |
523 | * very tight, so we have to free it as soon as we can to avoid | |
4e2d9491 | 524 | * subsequent failures. |
72df68ca RW |
525 | */ |
526 | swsusp_free(); | |
527 | restore_processor_state(); | |
528 | touch_softlockup_watchdog(); | |
2ed8d2b3 | 529 | |
40dc166c | 530 | syscore_resume(); |
2ed8d2b3 | 531 | |
4aecd671 | 532 | Enable_irqs: |
c1a957d1 | 533 | system_state = SYSTEM_RUNNING; |
72df68ca | 534 | local_irq_enable(); |
2ed8d2b3 | 535 | |
4aecd671 | 536 | Enable_cpus: |
23f62d7a | 537 | pm_sleep_enable_secondary_cpus(); |
4aecd671 RW |
538 | |
539 | Cleanup: | |
540 | platform_restore_cleanup(platform_mode); | |
541 | ||
cf579dfb | 542 | dpm_resume_start(PMSG_RECOVER); |
2ed8d2b3 | 543 | |
72df68ca RW |
544 | return error; |
545 | } | |
546 | ||
7777fab9 | 547 | /** |
f42a9813 RW |
548 | * hibernation_restore - Quiesce devices and restore from a hibernation image. |
549 | * @platform_mode: If set, use platform driver to prepare for the transition. | |
7777fab9 | 550 | * |
55f2503c PL |
551 | * This routine must be called with system_transition_mutex held. If it is |
552 | * successful, control reappears in the restored target kernel in | |
553 | * hibernation_snapshot(). | |
7777fab9 | 554 | */ |
a634cc10 | 555 | int hibernation_restore(int platform_mode) |
7777fab9 | 556 | { |
cbe2f5a6 | 557 | int error; |
7777fab9 RW |
558 | |
559 | pm_prepare_console(); | |
e9cec448 | 560 | console_suspend_all(); |
d1616302 | 561 | error = dpm_suspend_start(PMSG_QUIESCE); |
a634cc10 | 562 | if (!error) { |
4aecd671 | 563 | error = resume_target_kernel(platform_mode); |
94fb823f ID |
564 | /* |
565 | * The above should either succeed and jump to the new kernel, | |
566 | * or return with an error. Otherwise things are just | |
567 | * undefined, so let's be paranoid. | |
568 | */ | |
569 | BUG_ON(!error); | |
a634cc10 | 570 | } |
94fb823f | 571 | dpm_resume_end(PMSG_RECOVER); |
63830aef | 572 | console_resume_all(); |
7777fab9 RW |
573 | pm_restore_console(); |
574 | return error; | |
575 | } | |
576 | ||
577 | /** | |
f42a9813 | 578 | * hibernation_platform_enter - Power off the system using the platform driver. |
7777fab9 | 579 | */ |
7777fab9 RW |
580 | int hibernation_platform_enter(void) |
581 | { | |
cbe2f5a6 | 582 | int error; |
b1457bcc | 583 | |
9cd9a005 RW |
584 | if (!hibernation_ops) |
585 | return -ENOSYS; | |
586 | ||
587 | /* | |
588 | * We have cancelled the power transition by running | |
589 | * hibernation_ops->finish() before saving the image, so we should let | |
590 | * the firmware know that we're going to enter the sleep state after all | |
591 | */ | |
bb186901 | 592 | error = hibernation_ops->begin(PMSG_HIBERNATE); |
9cd9a005 | 593 | if (error) |
caea99ef | 594 | goto Close; |
9cd9a005 | 595 | |
abfe2d7b | 596 | entering_platform_hibernation = true; |
e9cec448 | 597 | console_suspend_all(); |
d1616302 | 598 | error = dpm_suspend_start(PMSG_HIBERNATE); |
d8f3de0d RW |
599 | if (error) { |
600 | if (hibernation_ops->recover) | |
601 | hibernation_ops->recover(); | |
602 | goto Resume_devices; | |
603 | } | |
9cd9a005 | 604 | |
cf579dfb | 605 | error = dpm_suspend_end(PMSG_HIBERNATE); |
4aecd671 | 606 | if (error) |
32bdfac5 | 607 | goto Resume_devices; |
4aecd671 | 608 | |
9cd9a005 RW |
609 | error = hibernation_ops->prepare(); |
610 | if (error) | |
e681c9dd | 611 | goto Platform_finish; |
9cd9a005 | 612 | |
23f62d7a | 613 | error = pm_sleep_disable_secondary_cpus(); |
9cd9a005 | 614 | if (error) |
8c506608 | 615 | goto Enable_cpus; |
2ed8d2b3 | 616 | |
4aecd671 | 617 | local_irq_disable(); |
c1a957d1 | 618 | system_state = SYSTEM_SUSPEND; |
e20a70c5 WL |
619 | |
620 | error = syscore_suspend(); | |
621 | if (error) | |
622 | goto Enable_irqs; | |
623 | ||
a2867e08 | 624 | if (pm_wakeup_pending()) { |
c125e96f RW |
625 | error = -EAGAIN; |
626 | goto Power_up; | |
627 | } | |
628 | ||
4aecd671 RW |
629 | hibernation_ops->enter(); |
630 | /* We should never get here */ | |
631 | while (1); | |
9cd9a005 | 632 | |
c125e96f | 633 | Power_up: |
40dc166c | 634 | syscore_resume(); |
e20a70c5 | 635 | Enable_irqs: |
c1a957d1 | 636 | system_state = SYSTEM_RUNNING; |
c125e96f | 637 | local_irq_enable(); |
8c506608 VK |
638 | |
639 | Enable_cpus: | |
23f62d7a | 640 | pm_sleep_enable_secondary_cpus(); |
c125e96f | 641 | |
e681c9dd | 642 | Platform_finish: |
9cd9a005 | 643 | hibernation_ops->finish(); |
2ed8d2b3 | 644 | |
cf579dfb | 645 | dpm_resume_start(PMSG_RESTORE); |
4aecd671 | 646 | |
9cd9a005 | 647 | Resume_devices: |
abfe2d7b | 648 | entering_platform_hibernation = false; |
d1616302 | 649 | dpm_resume_end(PMSG_RESTORE); |
63830aef | 650 | console_resume_all(); |
2ed8d2b3 | 651 | |
caea99ef RW |
652 | Close: |
653 | hibernation_ops->end(); | |
2ed8d2b3 | 654 | |
b1457bcc | 655 | return error; |
7777fab9 RW |
656 | } |
657 | ||
1da177e4 | 658 | /** |
f42a9813 | 659 | * power_down - Shut the machine down for hibernation. |
1da177e4 | 660 | * |
f42a9813 RW |
661 | * Use the platform driver, if configured, to put the system into the sleep |
662 | * state corresponding to hibernation, or try to power it off or reboot, | |
663 | * depending on the value of hibernation_mode. | |
1da177e4 | 664 | */ |
fe0c935a | 665 | static void power_down(void) |
1da177e4 | 666 | { |
62c552cc | 667 | int error; |
81d45bdf | 668 | |
0c4cae1b | 669 | #ifdef CONFIG_SUSPEND |
81d45bdf | 670 | if (hibernation_mode == HIBERNATION_SUSPEND) { |
85850af4 | 671 | error = suspend_devices_and_enter(mem_sleep_current); |
81d45bdf RW |
672 | if (error) { |
673 | hibernation_mode = hibernation_ops ? | |
674 | HIBERNATION_PLATFORM : | |
675 | HIBERNATION_SHUTDOWN; | |
676 | } else { | |
677 | /* Restore swap signature. */ | |
678 | error = swsusp_unmark(); | |
679 | if (error) | |
2872de13 | 680 | pr_err("Swap will be unusable! Try swapon -a.\n"); |
81d45bdf RW |
681 | |
682 | return; | |
683 | } | |
684 | } | |
62c552cc BS |
685 | #endif |
686 | ||
a3d25c27 | 687 | switch (hibernation_mode) { |
a3d25c27 | 688 | case HIBERNATION_REBOOT: |
fdde86ac | 689 | kernel_restart(NULL); |
1da177e4 | 690 | break; |
a3d25c27 | 691 | case HIBERNATION_PLATFORM: |
0c4cae1b CF |
692 | error = hibernation_platform_enter(); |
693 | if (error == -EAGAIN || error == -EBUSY) { | |
694 | swsusp_unmark(); | |
695 | events_check_enabled = false; | |
696 | pr_info("Wakeup event detected during hibernation, rolling back.\n"); | |
697 | return; | |
698 | } | |
df561f66 | 699 | fallthrough; |
9cd9a005 | 700 | case HIBERNATION_SHUTDOWN: |
3e251afa DW |
701 | if (kernel_can_power_off()) { |
702 | entering_platform_hibernation = true; | |
2c730785 | 703 | kernel_power_off(); |
3e251afa DW |
704 | entering_platform_hibernation = false; |
705 | } | |
9cd9a005 | 706 | break; |
1da177e4 | 707 | } |
fdde86ac | 708 | kernel_halt(); |
fe0c935a JB |
709 | /* |
710 | * Valid image is on the disk, if we continue we risk serious data | |
711 | * corruption after resume. | |
712 | */ | |
2872de13 | 713 | pr_crit("Power down manually\n"); |
2c730785 SC |
714 | while (1) |
715 | cpu_relax(); | |
1da177e4 LT |
716 | } |
717 | ||
93745df1 | 718 | static int load_image_and_restore(void) |
fe12c00d CY |
719 | { |
720 | int error; | |
721 | unsigned int flags; | |
722 | ||
8d8b2441 | 723 | pm_pr_dbg("Loading hibernation image.\n"); |
fe12c00d CY |
724 | |
725 | lock_device_hotplug(); | |
726 | error = create_basic_memory_bitmaps(); | |
3f51aa9e | 727 | if (error) { |
93745df1 | 728 | swsusp_close(); |
fe12c00d | 729 | goto Unlock; |
3f51aa9e | 730 | } |
fe12c00d CY |
731 | |
732 | error = swsusp_read(&flags); | |
93745df1 | 733 | swsusp_close(); |
fe12c00d | 734 | if (!error) |
3704a6a4 | 735 | error = hibernation_restore(flags & SF_PLATFORM_MODE); |
fe12c00d | 736 | |
7a7b99bf | 737 | pr_err("Failed to load image, recovering.\n"); |
fe12c00d CY |
738 | swsusp_free(); |
739 | free_basic_memory_bitmaps(); | |
740 | Unlock: | |
741 | unlock_device_hotplug(); | |
742 | ||
743 | return error; | |
744 | } | |
745 | ||
8bc29736 N |
746 | #define COMPRESSION_ALGO_LZO "lzo" |
747 | #define COMPRESSION_ALGO_LZ4 "lz4" | |
748 | ||
1da177e4 | 749 | /** |
f42a9813 | 750 | * hibernate - Carry out system hibernation, including saving the image. |
1da177e4 | 751 | */ |
a3d25c27 | 752 | int hibernate(void) |
1da177e4 | 753 | { |
d6545e68 | 754 | bool snapshot_test = false; |
5950e5d5 | 755 | unsigned int sleep_flags; |
70d93298 | 756 | int error; |
1da177e4 | 757 | |
a6e15a39 | 758 | if (!hibernation_available()) { |
8d8b2441 | 759 | pm_pr_dbg("Hibernation not available.\n"); |
a6e15a39 KC |
760 | return -EPERM; |
761 | } | |
762 | ||
a06c6f5d N |
763 | /* |
764 | * Query for the compression algorithm support if compression is enabled. | |
765 | */ | |
766 | if (!nocompress) { | |
cfdb7520 | 767 | strscpy(hib_comp_algo, hibernate_compressor); |
b03d542c | 768 | if (!crypto_has_acomp(hib_comp_algo, 0, CRYPTO_ALG_ASYNC)) { |
a06c6f5d N |
769 | pr_err("%s compression is not available\n", hib_comp_algo); |
770 | return -EOPNOTSUPP; | |
771 | } | |
772 | } | |
773 | ||
5950e5d5 | 774 | sleep_flags = lock_system_sleep(); |
0709db60 | 775 | /* The snapshot device should not be opened while we're running */ |
ab7e9b06 | 776 | if (!hibernate_acquire()) { |
b10d9117 RW |
777 | error = -EBUSY; |
778 | goto Unlock; | |
779 | } | |
780 | ||
8915aa20 | 781 | pr_info("hibernation entry\n"); |
5a0a2f30 | 782 | pm_prepare_console(); |
70d93298 PZ |
783 | error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION); |
784 | if (error) | |
785 | goto Restore; | |
0709db60 | 786 | |
b5dee313 | 787 | ksys_sync_helper(); |
eacfbf74 CB |
788 | if (filesystem_freeze_enabled) |
789 | filesystems_freeze(); | |
232b1432 | 790 | |
03afed8b | 791 | error = freeze_processes(); |
5a72e04d | 792 | if (error) |
8fd37a4c RW |
793 | goto Exit; |
794 | ||
942f4015 | 795 | lock_device_hotplug(); |
8fd37a4c RW |
796 | /* Allocate memory management structures */ |
797 | error = create_basic_memory_bitmaps(); | |
798 | if (error) | |
799 | goto Thaw; | |
1da177e4 | 800 | |
7777fab9 | 801 | error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM); |
a556d5b5 | 802 | if (error || freezer_test_done) |
8fd37a4c | 803 | goto Free_bitmaps; |
64a473cb RW |
804 | |
805 | if (in_suspend) { | |
a634cc10 RW |
806 | unsigned int flags = 0; |
807 | ||
808 | if (hibernation_mode == HIBERNATION_PLATFORM) | |
809 | flags |= SF_PLATFORM_MODE; | |
8bc29736 | 810 | if (nocompress) { |
f996fc96 | 811 | flags |= SF_NOCOMPRESS_MODE; |
8bc29736 | 812 | } else { |
081a9d04 BS |
813 | flags |= SF_CRC32_MODE; |
814 | ||
8bc29736 N |
815 | /* |
816 | * By default, LZO compression is enabled. Use SF_COMPRESSION_ALG_LZ4 | |
817 | * to override this behaviour and use LZ4. | |
818 | * | |
819 | * Refer kernel/power/power.h for more details | |
820 | */ | |
821 | ||
822 | if (!strcmp(hib_comp_algo, COMPRESSION_ALGO_LZ4)) | |
823 | flags |= SF_COMPRESSION_ALG_LZ4; | |
824 | else | |
825 | flags |= SF_COMPRESSION_ALG_LZO; | |
826 | } | |
827 | ||
7a7b99bf | 828 | pm_pr_dbg("Writing hibernation image.\n"); |
a634cc10 | 829 | error = swsusp_write(flags); |
7777fab9 | 830 | swsusp_free(); |
fe12c00d CY |
831 | if (!error) { |
832 | if (hibernation_mode == HIBERNATION_TEST_RESUME) | |
833 | snapshot_test = true; | |
834 | else | |
835 | power_down(); | |
836 | } | |
5262a475 | 837 | in_suspend = 0; |
c9e664f1 | 838 | pm_restore_gfp_mask(); |
b918f6e6 | 839 | } else { |
7a7b99bf | 840 | pm_pr_dbg("Hibernation image restored successfully.\n"); |
b918f6e6 | 841 | } |
64a473cb | 842 | |
8fd37a4c RW |
843 | Free_bitmaps: |
844 | free_basic_memory_bitmaps(); | |
b918f6e6 | 845 | Thaw: |
942f4015 | 846 | unlock_device_hotplug(); |
fe12c00d | 847 | if (snapshot_test) { |
8d8b2441 | 848 | pm_pr_dbg("Checking hibernation image\n"); |
148b6f4c | 849 | error = swsusp_check(false); |
fe12c00d | 850 | if (!error) |
93745df1 | 851 | error = load_image_and_restore(); |
fe12c00d | 852 | } |
5a0a2f30 | 853 | thaw_processes(); |
a556d5b5 SB |
854 | |
855 | /* Don't bother checking whether freezer_test_done is true */ | |
856 | freezer_test_done = false; | |
0709db60 | 857 | Exit: |
eacfbf74 | 858 | filesystems_thaw(); |
70d93298 PZ |
859 | pm_notifier_call_chain(PM_POST_HIBERNATION); |
860 | Restore: | |
5a0a2f30 | 861 | pm_restore_console(); |
ab7e9b06 | 862 | hibernate_release(); |
b10d9117 | 863 | Unlock: |
5950e5d5 | 864 | unlock_system_sleep(sleep_flags); |
8915aa20 RW |
865 | pr_info("hibernation exit\n"); |
866 | ||
1da177e4 LT |
867 | return error; |
868 | } | |
869 | ||
48001ea5 DW |
870 | /** |
871 | * hibernate_quiet_exec - Execute a function with all devices frozen. | |
872 | * @func: Function to execute. | |
873 | * @data: Data pointer to pass to @func. | |
874 | * | |
875 | * Return the @func return value or an error code if it cannot be executed. | |
876 | */ | |
877 | int hibernate_quiet_exec(int (*func)(void *data), void *data) | |
878 | { | |
5950e5d5 | 879 | unsigned int sleep_flags; |
70d93298 | 880 | int error; |
48001ea5 | 881 | |
5950e5d5 | 882 | sleep_flags = lock_system_sleep(); |
48001ea5 DW |
883 | |
884 | if (!hibernate_acquire()) { | |
885 | error = -EBUSY; | |
886 | goto unlock; | |
887 | } | |
888 | ||
889 | pm_prepare_console(); | |
890 | ||
70d93298 PZ |
891 | error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION); |
892 | if (error) | |
893 | goto restore; | |
48001ea5 | 894 | |
eacfbf74 CB |
895 | if (filesystem_freeze_enabled) |
896 | filesystems_freeze(); | |
897 | ||
48001ea5 DW |
898 | error = freeze_processes(); |
899 | if (error) | |
900 | goto exit; | |
901 | ||
902 | lock_device_hotplug(); | |
903 | ||
904 | pm_suspend_clear_flags(); | |
905 | ||
906 | error = platform_begin(true); | |
907 | if (error) | |
908 | goto thaw; | |
909 | ||
910 | error = freeze_kernel_threads(); | |
911 | if (error) | |
912 | goto thaw; | |
913 | ||
914 | error = dpm_prepare(PMSG_FREEZE); | |
915 | if (error) | |
916 | goto dpm_complete; | |
917 | ||
e9cec448 | 918 | console_suspend_all(); |
48001ea5 DW |
919 | |
920 | error = dpm_suspend(PMSG_FREEZE); | |
921 | if (error) | |
922 | goto dpm_resume; | |
923 | ||
924 | error = dpm_suspend_end(PMSG_FREEZE); | |
925 | if (error) | |
926 | goto dpm_resume; | |
927 | ||
928 | error = platform_pre_snapshot(true); | |
929 | if (error) | |
930 | goto skip; | |
931 | ||
932 | error = func(data); | |
933 | ||
934 | skip: | |
935 | platform_finish(true); | |
936 | ||
937 | dpm_resume_start(PMSG_THAW); | |
938 | ||
939 | dpm_resume: | |
940 | dpm_resume(PMSG_THAW); | |
941 | ||
63830aef | 942 | console_resume_all(); |
48001ea5 DW |
943 | |
944 | dpm_complete: | |
945 | dpm_complete(PMSG_THAW); | |
946 | ||
947 | thaw_kernel_threads(); | |
948 | ||
949 | thaw: | |
950 | platform_end(true); | |
951 | ||
952 | unlock_device_hotplug(); | |
953 | ||
954 | thaw_processes(); | |
955 | ||
956 | exit: | |
eacfbf74 | 957 | filesystems_thaw(); |
70d93298 | 958 | pm_notifier_call_chain(PM_POST_HIBERNATION); |
48001ea5 | 959 | |
70d93298 | 960 | restore: |
48001ea5 DW |
961 | pm_restore_console(); |
962 | ||
963 | hibernate_release(); | |
964 | ||
965 | unlock: | |
5950e5d5 | 966 | unlock_system_sleep(sleep_flags); |
48001ea5 DW |
967 | |
968 | return error; | |
969 | } | |
970 | EXPORT_SYMBOL_GPL(hibernate_quiet_exec); | |
1da177e4 | 971 | |
cc89c63e | 972 | static int __init find_resume_device(void) |
02b42d58 CH |
973 | { |
974 | if (!strlen(resume_file)) | |
975 | return -ENOENT; | |
976 | ||
977 | pm_pr_dbg("Checking hibernation image partition %s\n", resume_file); | |
978 | ||
979 | if (resume_delay) { | |
980 | pr_info("Waiting %dsec before reading resume device ...\n", | |
981 | resume_delay); | |
982 | ssleep(resume_delay); | |
983 | } | |
984 | ||
985 | /* Check if the device is there */ | |
cf056a43 | 986 | if (!early_lookup_bdev(resume_file, &swsusp_resume_device)) |
02b42d58 CH |
987 | return 0; |
988 | ||
989 | /* | |
990 | * Some device discovery might still be in progress; we need to wait for | |
991 | * this to finish. | |
992 | */ | |
993 | wait_for_device_probe(); | |
994 | if (resume_wait) { | |
cf056a43 | 995 | while (early_lookup_bdev(resume_file, &swsusp_resume_device)) |
02b42d58 CH |
996 | msleep(10); |
997 | async_synchronize_full(); | |
998 | } | |
999 | ||
cf056a43 | 1000 | return early_lookup_bdev(resume_file, &swsusp_resume_device); |
02b42d58 CH |
1001 | } |
1002 | ||
1da177e4 LT |
1003 | static int software_resume(void) |
1004 | { | |
70d93298 | 1005 | int error; |
1da177e4 | 1006 | |
8d8b2441 | 1007 | pm_pr_dbg("Hibernation image partition %d:%d present\n", |
0c8454f5 | 1008 | MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device)); |
1da177e4 | 1009 | |
8d8b2441 | 1010 | pm_pr_dbg("Looking for hibernation image.\n"); |
cc89c63e CH |
1011 | |
1012 | mutex_lock(&system_transition_mutex); | |
148b6f4c | 1013 | error = swsusp_check(true); |
ed746e3b | 1014 | if (error) |
74dfd666 | 1015 | goto Unlock; |
1da177e4 | 1016 | |
a06c6f5d N |
1017 | /* |
1018 | * Check if the hibernation image is compressed. If so, query for | |
1019 | * the algorithm support. | |
1020 | */ | |
1021 | if (!(swsusp_header_flags & SF_NOCOMPRESS_MODE)) { | |
8bc29736 | 1022 | if (swsusp_header_flags & SF_COMPRESSION_ALG_LZ4) |
cfdb7520 | 1023 | strscpy(hib_comp_algo, COMPRESSION_ALGO_LZ4); |
8bc29736 | 1024 | else |
cfdb7520 | 1025 | strscpy(hib_comp_algo, COMPRESSION_ALGO_LZO); |
b03d542c | 1026 | if (!crypto_has_acomp(hib_comp_algo, 0, CRYPTO_ALG_ASYNC)) { |
a06c6f5d N |
1027 | pr_err("%s compression is not available\n", hib_comp_algo); |
1028 | error = -EOPNOTSUPP; | |
1029 | goto Unlock; | |
1030 | } | |
1031 | } | |
1032 | ||
0709db60 | 1033 | /* The snapshot device should not be opened while we're running */ |
ab7e9b06 | 1034 | if (!hibernate_acquire()) { |
0709db60 | 1035 | error = -EBUSY; |
93745df1 | 1036 | swsusp_close(); |
0709db60 RW |
1037 | goto Unlock; |
1038 | } | |
1039 | ||
8915aa20 | 1040 | pr_info("resume from hibernation\n"); |
5a0a2f30 | 1041 | pm_prepare_console(); |
70d93298 PZ |
1042 | error = pm_notifier_call_chain_robust(PM_RESTORE_PREPARE, PM_POST_RESTORE); |
1043 | if (error) | |
1044 | goto Restore; | |
1bfcf130 | 1045 | |
eacfbf74 CB |
1046 | if (filesystem_freeze_enabled) |
1047 | filesystems_freeze(); | |
1048 | ||
7a7b99bf | 1049 | pm_pr_dbg("Preparing processes for hibernation restore.\n"); |
03afed8b | 1050 | error = freeze_processes(); |
eacfbf74 CB |
1051 | if (error) { |
1052 | filesystems_thaw(); | |
8fd37a4c | 1053 | goto Close_Finish; |
eacfbf74 | 1054 | } |
2351f8d2 DC |
1055 | |
1056 | error = freeze_kernel_threads(); | |
1057 | if (error) { | |
1058 | thaw_processes(); | |
eacfbf74 | 1059 | filesystems_thaw(); |
2351f8d2 DC |
1060 | goto Close_Finish; |
1061 | } | |
1062 | ||
93745df1 | 1063 | error = load_image_and_restore(); |
8fd37a4c | 1064 | thaw_processes(); |
eacfbf74 | 1065 | filesystems_thaw(); |
0709db60 | 1066 | Finish: |
70d93298 PZ |
1067 | pm_notifier_call_chain(PM_POST_RESTORE); |
1068 | Restore: | |
5a0a2f30 | 1069 | pm_restore_console(); |
7a7b99bf | 1070 | pr_info("resume failed (%d)\n", error); |
ab7e9b06 | 1071 | hibernate_release(); |
dd5d666b | 1072 | /* For success case, the suspend path will release the lock */ |
74dfd666 | 1073 | Unlock: |
55f2503c | 1074 | mutex_unlock(&system_transition_mutex); |
8d8b2441 | 1075 | pm_pr_dbg("Hibernation image not present or could not be loaded.\n"); |
7777fab9 | 1076 | return error; |
8fd37a4c | 1077 | Close_Finish: |
93745df1 | 1078 | swsusp_close(); |
76b57e61 | 1079 | goto Finish; |
1da177e4 LT |
1080 | } |
1081 | ||
cc89c63e CH |
1082 | /** |
1083 | * software_resume_initcall - Resume from a saved hibernation image. | |
1084 | * | |
1085 | * This routine is called as a late initcall, when all devices have been | |
1086 | * discovered and initialized already. | |
1087 | * | |
1088 | * The image reading code is called to see if there is a hibernation image | |
1089 | * available for reading. If that is the case, devices are quiesced and the | |
1090 | * contents of memory is restored from the saved image. | |
1091 | * | |
1092 | * If this is successful, control reappears in the restored target kernel in | |
1093 | * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine | |
1094 | * attempts to recover gracefully and make the kernel return to the normal mode | |
1095 | * of operation. | |
1096 | */ | |
1097 | static int __init software_resume_initcall(void) | |
1098 | { | |
1099 | /* | |
1100 | * If the user said "noresume".. bail out early. | |
1101 | */ | |
1102 | if (noresume || !hibernation_available()) | |
1103 | return 0; | |
1104 | ||
1105 | if (!swsusp_resume_device) { | |
1106 | int error = find_resume_device(); | |
1107 | ||
1108 | if (error) | |
1109 | return error; | |
1110 | } | |
1111 | ||
1112 | return software_resume(); | |
1113 | } | |
1114 | late_initcall_sync(software_resume_initcall); | |
1da177e4 LT |
1115 | |
1116 | ||
a3d25c27 RW |
1117 | static const char * const hibernation_modes[] = { |
1118 | [HIBERNATION_PLATFORM] = "platform", | |
1119 | [HIBERNATION_SHUTDOWN] = "shutdown", | |
1120 | [HIBERNATION_REBOOT] = "reboot", | |
62c552cc BS |
1121 | #ifdef CONFIG_SUSPEND |
1122 | [HIBERNATION_SUSPEND] = "suspend", | |
1123 | #endif | |
fe12c00d | 1124 | [HIBERNATION_TEST_RESUME] = "test_resume", |
1da177e4 LT |
1125 | }; |
1126 | ||
f42a9813 RW |
1127 | /* |
1128 | * /sys/power/disk - Control hibernation mode. | |
1da177e4 | 1129 | * |
f42a9813 RW |
1130 | * Hibernation can be handled in several ways. There are a few different ways |
1131 | * to put the system into the sleep state: using the platform driver (e.g. ACPI | |
1132 | * or other hibernation_ops), powering it off or rebooting it (for testing | |
48580ab8 | 1133 | * mostly). |
1da177e4 | 1134 | * |
f42a9813 RW |
1135 | * The sysfs file /sys/power/disk provides an interface for selecting the |
1136 | * hibernation mode to use. Reading from this file causes the available modes | |
48580ab8 | 1137 | * to be printed. There are 3 modes that can be supported: |
1da177e4 | 1138 | * |
1da177e4 LT |
1139 | * 'platform' |
1140 | * 'shutdown' | |
1141 | * 'reboot' | |
1142 | * | |
f42a9813 RW |
1143 | * If a platform hibernation driver is in use, 'platform' will be supported |
1144 | * and will be used by default. Otherwise, 'shutdown' will be used by default. | |
1145 | * The selected option (i.e. the one corresponding to the current value of | |
1146 | * hibernation_mode) is enclosed by a square bracket. | |
1147 | * | |
1148 | * To select a given hibernation mode it is necessary to write the mode's | |
1149 | * string representation (as returned by reading from /sys/power/disk) back | |
1150 | * into /sys/power/disk. | |
1da177e4 LT |
1151 | */ |
1152 | ||
386f275f KS |
1153 | static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr, |
1154 | char *buf) | |
1da177e4 | 1155 | { |
6306653c | 1156 | ssize_t count = 0; |
f0ced9b2 | 1157 | int i; |
f0ced9b2 | 1158 | |
a6e15a39 | 1159 | if (!hibernation_available()) |
6306653c | 1160 | return sysfs_emit(buf, "[disabled]\n"); |
a6e15a39 | 1161 | |
a3d25c27 RW |
1162 | for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { |
1163 | if (!hibernation_modes[i]) | |
f0ced9b2 JB |
1164 | continue; |
1165 | switch (i) { | |
a3d25c27 RW |
1166 | case HIBERNATION_SHUTDOWN: |
1167 | case HIBERNATION_REBOOT: | |
62c552cc BS |
1168 | #ifdef CONFIG_SUSPEND |
1169 | case HIBERNATION_SUSPEND: | |
1170 | #endif | |
fe12c00d | 1171 | case HIBERNATION_TEST_RESUME: |
f0ced9b2 | 1172 | break; |
a3d25c27 RW |
1173 | case HIBERNATION_PLATFORM: |
1174 | if (hibernation_ops) | |
f0ced9b2 JB |
1175 | break; |
1176 | /* not a valid mode, continue with loop */ | |
1177 | continue; | |
1178 | } | |
a3d25c27 | 1179 | if (i == hibernation_mode) |
6306653c | 1180 | count += sysfs_emit_at(buf, count, "[%s] ", hibernation_modes[i]); |
f0ced9b2 | 1181 | else |
6306653c | 1182 | count += sysfs_emit_at(buf, count, "%s ", hibernation_modes[i]); |
f0ced9b2 | 1183 | } |
6306653c XL |
1184 | |
1185 | /* Convert the last space to a newline if needed. */ | |
1186 | if (count > 0) | |
1187 | buf[count - 1] = '\n'; | |
1188 | ||
1189 | return count; | |
1da177e4 LT |
1190 | } |
1191 | ||
386f275f KS |
1192 | static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, |
1193 | const char *buf, size_t n) | |
1da177e4 | 1194 | { |
5950e5d5 PZ |
1195 | int mode = HIBERNATION_INVALID; |
1196 | unsigned int sleep_flags; | |
1da177e4 | 1197 | int error = 0; |
1da177e4 LT |
1198 | int len; |
1199 | char *p; | |
5950e5d5 | 1200 | int i; |
1da177e4 | 1201 | |
a6e15a39 KC |
1202 | if (!hibernation_available()) |
1203 | return -EPERM; | |
1204 | ||
1da177e4 LT |
1205 | p = memchr(buf, '\n', n); |
1206 | len = p ? p - buf : n; | |
1207 | ||
5950e5d5 | 1208 | sleep_flags = lock_system_sleep(); |
a3d25c27 | 1209 | for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { |
8d98a690 RW |
1210 | if (len == strlen(hibernation_modes[i]) |
1211 | && !strncmp(buf, hibernation_modes[i], len)) { | |
1da177e4 LT |
1212 | mode = i; |
1213 | break; | |
1214 | } | |
1215 | } | |
a3d25c27 | 1216 | if (mode != HIBERNATION_INVALID) { |
fe0c935a | 1217 | switch (mode) { |
a3d25c27 RW |
1218 | case HIBERNATION_SHUTDOWN: |
1219 | case HIBERNATION_REBOOT: | |
62c552cc BS |
1220 | #ifdef CONFIG_SUSPEND |
1221 | case HIBERNATION_SUSPEND: | |
1222 | #endif | |
fe12c00d | 1223 | case HIBERNATION_TEST_RESUME: |
a3d25c27 | 1224 | hibernation_mode = mode; |
fe0c935a | 1225 | break; |
a3d25c27 RW |
1226 | case HIBERNATION_PLATFORM: |
1227 | if (hibernation_ops) | |
1228 | hibernation_mode = mode; | |
1da177e4 LT |
1229 | else |
1230 | error = -EINVAL; | |
1231 | } | |
a3d25c27 | 1232 | } else |
1da177e4 LT |
1233 | error = -EINVAL; |
1234 | ||
a3d25c27 | 1235 | if (!error) |
8d8b2441 RW |
1236 | pm_pr_dbg("Hibernation mode set to '%s'\n", |
1237 | hibernation_modes[mode]); | |
5950e5d5 | 1238 | unlock_system_sleep(sleep_flags); |
1da177e4 LT |
1239 | return error ? error : n; |
1240 | } | |
1241 | ||
1242 | power_attr(disk); | |
1243 | ||
386f275f KS |
1244 | static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr, |
1245 | char *buf) | |
1da177e4 | 1246 | { |
6306653c XL |
1247 | return sysfs_emit(buf, "%d:%d\n", MAJOR(swsusp_resume_device), |
1248 | MINOR(swsusp_resume_device)); | |
1da177e4 LT |
1249 | } |
1250 | ||
386f275f KS |
1251 | static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr, |
1252 | const char *buf, size_t n) | |
1da177e4 | 1253 | { |
5950e5d5 | 1254 | unsigned int sleep_flags; |
421a5fa1 SC |
1255 | int len = n; |
1256 | char *name; | |
cf056a43 CH |
1257 | dev_t dev; |
1258 | int error; | |
1da177e4 | 1259 | |
cc89c63e | 1260 | if (!hibernation_available()) |
df2f7cde | 1261 | return n; |
cc89c63e | 1262 | |
421a5fa1 SC |
1263 | if (len && buf[len-1] == '\n') |
1264 | len--; | |
1265 | name = kstrndup(buf, len, GFP_KERNEL); | |
1266 | if (!name) | |
1267 | return -ENOMEM; | |
1da177e4 | 1268 | |
1e8c813b CH |
1269 | error = lookup_bdev(name, &dev); |
1270 | if (error) { | |
1271 | unsigned maj, min, offset; | |
1272 | char *p, dummy; | |
1273 | ||
c9e4bf60 | 1274 | error = 0; |
1e8c813b CH |
1275 | if (sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2 || |
1276 | sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset, | |
1277 | &dummy) == 3) { | |
1278 | dev = MKDEV(maj, min); | |
1279 | if (maj != MAJOR(dev) || min != MINOR(dev)) | |
1280 | error = -EINVAL; | |
1281 | } else { | |
1282 | dev = new_decode_dev(simple_strtoul(name, &p, 16)); | |
1283 | if (*p) | |
1284 | error = -EINVAL; | |
1285 | } | |
1286 | } | |
421a5fa1 | 1287 | kfree(name); |
cf056a43 CH |
1288 | if (error) |
1289 | return error; | |
1da177e4 | 1290 | |
5950e5d5 | 1291 | sleep_flags = lock_system_sleep(); |
cf056a43 | 1292 | swsusp_resume_device = dev; |
5950e5d5 PZ |
1293 | unlock_system_sleep(sleep_flags); |
1294 | ||
7a7b99bf LS |
1295 | pm_pr_dbg("Configured hibernation resume from disk to %u\n", |
1296 | swsusp_resume_device); | |
a576219a AM |
1297 | noresume = 0; |
1298 | software_resume(); | |
421a5fa1 | 1299 | return n; |
1da177e4 LT |
1300 | } |
1301 | ||
1302 | power_attr(resume); | |
1303 | ||
35506467 ML |
1304 | static ssize_t resume_offset_show(struct kobject *kobj, |
1305 | struct kobj_attribute *attr, char *buf) | |
1306 | { | |
6306653c | 1307 | return sysfs_emit(buf, "%llu\n", (unsigned long long)swsusp_resume_block); |
35506467 ML |
1308 | } |
1309 | ||
1310 | static ssize_t resume_offset_store(struct kobject *kobj, | |
1311 | struct kobj_attribute *attr, const char *buf, | |
1312 | size_t n) | |
1313 | { | |
1314 | unsigned long long offset; | |
1315 | int rc; | |
1316 | ||
1317 | rc = kstrtoull(buf, 0, &offset); | |
1318 | if (rc) | |
1319 | return rc; | |
1320 | swsusp_resume_block = offset; | |
1321 | ||
1322 | return n; | |
1323 | } | |
1324 | ||
1325 | power_attr(resume_offset); | |
1326 | ||
386f275f KS |
1327 | static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr, |
1328 | char *buf) | |
ca0aec0f | 1329 | { |
6306653c | 1330 | return sysfs_emit(buf, "%lu\n", image_size); |
ca0aec0f RW |
1331 | } |
1332 | ||
386f275f KS |
1333 | static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr, |
1334 | const char *buf, size_t n) | |
ca0aec0f | 1335 | { |
853609b6 | 1336 | unsigned long size; |
ca0aec0f | 1337 | |
853609b6 | 1338 | if (sscanf(buf, "%lu", &size) == 1) { |
ca0aec0f RW |
1339 | image_size = size; |
1340 | return n; | |
1341 | } | |
1342 | ||
1343 | return -EINVAL; | |
1344 | } | |
1345 | ||
1346 | power_attr(image_size); | |
1347 | ||
ddeb6487 RW |
1348 | static ssize_t reserved_size_show(struct kobject *kobj, |
1349 | struct kobj_attribute *attr, char *buf) | |
1350 | { | |
6306653c | 1351 | return sysfs_emit(buf, "%lu\n", reserved_size); |
ddeb6487 RW |
1352 | } |
1353 | ||
1354 | static ssize_t reserved_size_store(struct kobject *kobj, | |
1355 | struct kobj_attribute *attr, | |
1356 | const char *buf, size_t n) | |
1357 | { | |
1358 | unsigned long size; | |
1359 | ||
1360 | if (sscanf(buf, "%lu", &size) == 1) { | |
1361 | reserved_size = size; | |
1362 | return n; | |
1363 | } | |
1364 | ||
1365 | return -EINVAL; | |
1366 | } | |
1367 | ||
1368 | power_attr(reserved_size); | |
1369 | ||
6ada7ba2 | 1370 | static struct attribute *g[] = { |
1da177e4 | 1371 | &disk_attr.attr, |
35506467 | 1372 | &resume_offset_attr.attr, |
1da177e4 | 1373 | &resume_attr.attr, |
ca0aec0f | 1374 | &image_size_attr.attr, |
ddeb6487 | 1375 | &reserved_size_attr.attr, |
1da177e4 LT |
1376 | NULL, |
1377 | }; | |
1378 | ||
1379 | ||
59494fe2 | 1380 | static const struct attribute_group attr_group = { |
1da177e4 LT |
1381 | .attrs = g, |
1382 | }; | |
1383 | ||
1384 | ||
1385 | static int __init pm_disk_init(void) | |
1386 | { | |
d76e15fb | 1387 | return sysfs_create_group(power_kobj, &attr_group); |
1da177e4 LT |
1388 | } |
1389 | ||
1390 | core_initcall(pm_disk_init); | |
1391 | ||
1392 | ||
1393 | static int __init resume_setup(char *str) | |
1394 | { | |
1395 | if (noresume) | |
1396 | return 1; | |
1397 | ||
7b831bd3 | 1398 | strscpy(resume_file, str); |
1da177e4 LT |
1399 | return 1; |
1400 | } | |
1401 | ||
9a154d9d RW |
1402 | static int __init resume_offset_setup(char *str) |
1403 | { | |
1404 | unsigned long long offset; | |
1405 | ||
1406 | if (noresume) | |
1407 | return 1; | |
1408 | ||
1409 | if (sscanf(str, "%llu", &offset) == 1) | |
1410 | swsusp_resume_block = offset; | |
1411 | ||
1412 | return 1; | |
1413 | } | |
1414 | ||
f996fc96 BS |
1415 | static int __init hibernate_setup(char *str) |
1416 | { | |
2f88e41a | 1417 | if (!strncmp(str, "noresume", 8)) { |
f996fc96 | 1418 | noresume = 1; |
2f88e41a | 1419 | } else if (!strncmp(str, "nocompress", 10)) { |
f996fc96 | 1420 | nocompress = 1; |
2f88e41a | 1421 | } else if (!strncmp(str, "no", 2)) { |
a6e15a39 KC |
1422 | noresume = 1; |
1423 | nohibernate = 1; | |
0f5bf6d0 | 1424 | } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) |
4c0b6c10 RW |
1425 | && !strncmp(str, "protect_image", 13)) { |
1426 | enable_restore_image_protection(); | |
a6e15a39 | 1427 | } |
f996fc96 BS |
1428 | return 1; |
1429 | } | |
1430 | ||
1da177e4 LT |
1431 | static int __init noresume_setup(char *str) |
1432 | { | |
1433 | noresume = 1; | |
1434 | return 1; | |
1435 | } | |
1436 | ||
6f8d7022 BS |
1437 | static int __init resumewait_setup(char *str) |
1438 | { | |
1439 | resume_wait = 1; | |
1440 | return 1; | |
1441 | } | |
1442 | ||
f126f733 BS |
1443 | static int __init resumedelay_setup(char *str) |
1444 | { | |
f6514be5 | 1445 | int rc = kstrtouint(str, 0, &resume_delay); |
317cf7e5 FF |
1446 | |
1447 | if (rc) | |
ba7ffcd4 | 1448 | pr_warn("resumedelay: bad option string '%s'\n", str); |
f126f733 BS |
1449 | return 1; |
1450 | } | |
1451 | ||
a6e15a39 KC |
1452 | static int __init nohibernate_setup(char *str) |
1453 | { | |
1454 | noresume = 1; | |
1455 | nohibernate = 1; | |
1456 | return 1; | |
1457 | } | |
1458 | ||
3fec6e59 N |
1459 | static const char * const comp_alg_enabled[] = { |
1460 | #if IS_ENABLED(CONFIG_CRYPTO_LZO) | |
1461 | COMPRESSION_ALGO_LZO, | |
1462 | #endif | |
1463 | #if IS_ENABLED(CONFIG_CRYPTO_LZ4) | |
1464 | COMPRESSION_ALGO_LZ4, | |
1465 | #endif | |
1466 | }; | |
1467 | ||
1468 | static int hibernate_compressor_param_set(const char *compressor, | |
1469 | const struct kernel_param *kp) | |
1470 | { | |
3fec6e59 N |
1471 | int index, ret; |
1472 | ||
52323ed1 LX |
1473 | if (!mutex_trylock(&system_transition_mutex)) |
1474 | return -EBUSY; | |
3fec6e59 N |
1475 | |
1476 | index = sysfs_match_string(comp_alg_enabled, compressor); | |
1477 | if (index >= 0) { | |
1478 | ret = param_set_copystring(comp_alg_enabled[index], kp); | |
1479 | if (!ret) | |
cfdb7520 | 1480 | strscpy(hib_comp_algo, comp_alg_enabled[index]); |
3fec6e59 N |
1481 | } else { |
1482 | ret = index; | |
1483 | } | |
1484 | ||
52323ed1 | 1485 | mutex_unlock(&system_transition_mutex); |
3fec6e59 N |
1486 | |
1487 | if (ret) | |
1488 | pr_debug("Cannot set specified compressor %s\n", | |
1489 | compressor); | |
1490 | ||
1491 | return ret; | |
1492 | } | |
1493 | ||
1494 | static const struct kernel_param_ops hibernate_compressor_param_ops = { | |
1495 | .set = hibernate_compressor_param_set, | |
1496 | .get = param_get_string, | |
1497 | }; | |
1498 | ||
1499 | static struct kparam_string hibernate_compressor_param_string = { | |
1500 | .maxlen = sizeof(hibernate_compressor), | |
1501 | .string = hibernate_compressor, | |
1502 | }; | |
1503 | ||
1504 | module_param_cb(compressor, &hibernate_compressor_param_ops, | |
1505 | &hibernate_compressor_param_string, 0644); | |
1506 | MODULE_PARM_DESC(compressor, | |
1507 | "Compression algorithm to be used with hibernation"); | |
1508 | ||
1da177e4 | 1509 | __setup("noresume", noresume_setup); |
9a154d9d | 1510 | __setup("resume_offset=", resume_offset_setup); |
1da177e4 | 1511 | __setup("resume=", resume_setup); |
f996fc96 | 1512 | __setup("hibernate=", hibernate_setup); |
6f8d7022 | 1513 | __setup("resumewait", resumewait_setup); |
f126f733 | 1514 | __setup("resumedelay=", resumedelay_setup); |
a6e15a39 | 1515 | __setup("nohibernate", nohibernate_setup); |