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(); |
c9e664f1 | 426 | pm_restrict_gfp_mask(); |
953a2063 | 427 | |
91e7c75b | 428 | error = dpm_suspend(PMSG_FREEZE); |
10a1803d | 429 | |
953a2063 SB |
430 | if (error || hibernation_test(TEST_DEVICES)) |
431 | platform_recover(platform_mode); | |
432 | else | |
433 | error = create_image(platform_mode); | |
7777fab9 | 434 | |
c9e664f1 | 435 | /* |
953a2063 SB |
436 | * In the case that we call create_image() above, the control |
437 | * returns here (1) after the image has been created or the | |
c9e664f1 RW |
438 | * image creation has failed and (2) after a successful restore. |
439 | */ | |
4cc79776 | 440 | |
64a473cb RW |
441 | /* We may need to release the preallocated image pages here. */ |
442 | if (error || !in_suspend) | |
443 | swsusp_free(); | |
444 | ||
91e7c75b RW |
445 | msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE; |
446 | dpm_resume(msg); | |
c9e664f1 RW |
447 | |
448 | if (error || !in_suspend) | |
449 | pm_restore_gfp_mask(); | |
450 | ||
63830aef | 451 | console_resume_all(); |
91e7c75b RW |
452 | dpm_complete(msg); |
453 | ||
caea99ef RW |
454 | Close: |
455 | platform_end(platform_mode); | |
7777fab9 | 456 | return error; |
d8f3de0d | 457 | |
51d6ff7a SB |
458 | Thaw: |
459 | thaw_kernel_threads(); | |
bb58dd5d RW |
460 | Cleanup: |
461 | swsusp_free(); | |
462 | goto Close; | |
7777fab9 RW |
463 | } |
464 | ||
406f992e RW |
465 | int __weak hibernate_resume_nonboot_cpu_disable(void) |
466 | { | |
2f1a6fbb | 467 | return suspend_disable_secondary_cpus(); |
406f992e RW |
468 | } |
469 | ||
72df68ca | 470 | /** |
f42a9813 RW |
471 | * resume_target_kernel - Restore system state from a hibernation image. |
472 | * @platform_mode: Whether or not to use the platform driver. | |
473 | * | |
cf579dfb RW |
474 | * Execute device drivers' "noirq" and "late" freeze callbacks, restore the |
475 | * contents of highmem that have not been restored yet from the image and run | |
476 | * the low-level code that will restore the remaining contents of memory and | |
477 | * switch to the just restored target kernel. | |
72df68ca | 478 | */ |
4aecd671 | 479 | static int resume_target_kernel(bool platform_mode) |
72df68ca RW |
480 | { |
481 | int error; | |
482 | ||
cf579dfb | 483 | error = dpm_suspend_end(PMSG_QUIESCE); |
72df68ca | 484 | if (error) { |
2872de13 | 485 | pr_err("Some devices failed to power down, aborting resume\n"); |
32bdfac5 | 486 | return error; |
72df68ca | 487 | } |
2ed8d2b3 | 488 | |
4aecd671 RW |
489 | error = platform_pre_restore(platform_mode); |
490 | if (error) | |
491 | goto Cleanup; | |
492 | ||
23f62d7a RW |
493 | cpuidle_pause(); |
494 | ||
406f992e | 495 | error = hibernate_resume_nonboot_cpu_disable(); |
4aecd671 RW |
496 | if (error) |
497 | goto Enable_cpus; | |
498 | ||
2ed8d2b3 | 499 | local_irq_disable(); |
c1a957d1 | 500 | system_state = SYSTEM_SUSPEND; |
2ed8d2b3 | 501 | |
2e711c04 | 502 | error = syscore_suspend(); |
4aecd671 RW |
503 | if (error) |
504 | goto Enable_irqs; | |
505 | ||
72df68ca RW |
506 | save_processor_state(); |
507 | error = restore_highmem(); | |
508 | if (!error) { | |
509 | error = swsusp_arch_resume(); | |
510 | /* | |
511 | * The code below is only ever reached in case of a failure. | |
4e2d9491 RW |
512 | * Otherwise, execution continues at the place where |
513 | * swsusp_arch_suspend() was called. | |
72df68ca RW |
514 | */ |
515 | BUG_ON(!error); | |
4e2d9491 RW |
516 | /* |
517 | * This call to restore_highmem() reverts the changes made by | |
518 | * the previous one. | |
519 | */ | |
72df68ca RW |
520 | restore_highmem(); |
521 | } | |
522 | /* | |
523 | * The only reason why swsusp_arch_resume() can fail is memory being | |
524 | * very tight, so we have to free it as soon as we can to avoid | |
4e2d9491 | 525 | * subsequent failures. |
72df68ca RW |
526 | */ |
527 | swsusp_free(); | |
528 | restore_processor_state(); | |
529 | touch_softlockup_watchdog(); | |
2ed8d2b3 | 530 | |
40dc166c | 531 | syscore_resume(); |
2ed8d2b3 | 532 | |
4aecd671 | 533 | Enable_irqs: |
c1a957d1 | 534 | system_state = SYSTEM_RUNNING; |
72df68ca | 535 | local_irq_enable(); |
2ed8d2b3 | 536 | |
4aecd671 | 537 | Enable_cpus: |
23f62d7a | 538 | pm_sleep_enable_secondary_cpus(); |
4aecd671 RW |
539 | |
540 | Cleanup: | |
541 | platform_restore_cleanup(platform_mode); | |
542 | ||
cf579dfb | 543 | dpm_resume_start(PMSG_RECOVER); |
2ed8d2b3 | 544 | |
72df68ca RW |
545 | return error; |
546 | } | |
547 | ||
7777fab9 | 548 | /** |
f42a9813 RW |
549 | * hibernation_restore - Quiesce devices and restore from a hibernation image. |
550 | * @platform_mode: If set, use platform driver to prepare for the transition. | |
7777fab9 | 551 | * |
55f2503c PL |
552 | * This routine must be called with system_transition_mutex held. If it is |
553 | * successful, control reappears in the restored target kernel in | |
554 | * hibernation_snapshot(). | |
7777fab9 | 555 | */ |
a634cc10 | 556 | int hibernation_restore(int platform_mode) |
7777fab9 | 557 | { |
cbe2f5a6 | 558 | int error; |
7777fab9 RW |
559 | |
560 | pm_prepare_console(); | |
e9cec448 | 561 | console_suspend_all(); |
c9e664f1 | 562 | pm_restrict_gfp_mask(); |
d1616302 | 563 | error = dpm_suspend_start(PMSG_QUIESCE); |
a634cc10 | 564 | if (!error) { |
4aecd671 | 565 | error = resume_target_kernel(platform_mode); |
94fb823f ID |
566 | /* |
567 | * The above should either succeed and jump to the new kernel, | |
568 | * or return with an error. Otherwise things are just | |
569 | * undefined, so let's be paranoid. | |
570 | */ | |
571 | BUG_ON(!error); | |
a634cc10 | 572 | } |
94fb823f | 573 | dpm_resume_end(PMSG_RECOVER); |
c9e664f1 | 574 | pm_restore_gfp_mask(); |
63830aef | 575 | console_resume_all(); |
7777fab9 RW |
576 | pm_restore_console(); |
577 | return error; | |
578 | } | |
579 | ||
580 | /** | |
f42a9813 | 581 | * hibernation_platform_enter - Power off the system using the platform driver. |
7777fab9 | 582 | */ |
7777fab9 RW |
583 | int hibernation_platform_enter(void) |
584 | { | |
cbe2f5a6 | 585 | int error; |
b1457bcc | 586 | |
9cd9a005 RW |
587 | if (!hibernation_ops) |
588 | return -ENOSYS; | |
589 | ||
590 | /* | |
591 | * We have cancelled the power transition by running | |
592 | * hibernation_ops->finish() before saving the image, so we should let | |
593 | * the firmware know that we're going to enter the sleep state after all | |
594 | */ | |
bb186901 | 595 | error = hibernation_ops->begin(PMSG_HIBERNATE); |
9cd9a005 | 596 | if (error) |
caea99ef | 597 | goto Close; |
9cd9a005 | 598 | |
abfe2d7b | 599 | entering_platform_hibernation = true; |
e9cec448 | 600 | console_suspend_all(); |
d1616302 | 601 | error = dpm_suspend_start(PMSG_HIBERNATE); |
d8f3de0d RW |
602 | if (error) { |
603 | if (hibernation_ops->recover) | |
604 | hibernation_ops->recover(); | |
605 | goto Resume_devices; | |
606 | } | |
9cd9a005 | 607 | |
cf579dfb | 608 | error = dpm_suspend_end(PMSG_HIBERNATE); |
4aecd671 | 609 | if (error) |
32bdfac5 | 610 | goto Resume_devices; |
4aecd671 | 611 | |
9cd9a005 RW |
612 | error = hibernation_ops->prepare(); |
613 | if (error) | |
e681c9dd | 614 | goto Platform_finish; |
9cd9a005 | 615 | |
23f62d7a | 616 | error = pm_sleep_disable_secondary_cpus(); |
9cd9a005 | 617 | if (error) |
8c506608 | 618 | goto Enable_cpus; |
2ed8d2b3 | 619 | |
4aecd671 | 620 | local_irq_disable(); |
c1a957d1 | 621 | system_state = SYSTEM_SUSPEND; |
e20a70c5 WL |
622 | |
623 | error = syscore_suspend(); | |
624 | if (error) | |
625 | goto Enable_irqs; | |
626 | ||
a2867e08 | 627 | if (pm_wakeup_pending()) { |
c125e96f RW |
628 | error = -EAGAIN; |
629 | goto Power_up; | |
630 | } | |
631 | ||
4aecd671 RW |
632 | hibernation_ops->enter(); |
633 | /* We should never get here */ | |
634 | while (1); | |
9cd9a005 | 635 | |
c125e96f | 636 | Power_up: |
40dc166c | 637 | syscore_resume(); |
e20a70c5 | 638 | Enable_irqs: |
c1a957d1 | 639 | system_state = SYSTEM_RUNNING; |
c125e96f | 640 | local_irq_enable(); |
8c506608 VK |
641 | |
642 | Enable_cpus: | |
23f62d7a | 643 | pm_sleep_enable_secondary_cpus(); |
c125e96f | 644 | |
e681c9dd | 645 | Platform_finish: |
9cd9a005 | 646 | hibernation_ops->finish(); |
2ed8d2b3 | 647 | |
cf579dfb | 648 | dpm_resume_start(PMSG_RESTORE); |
4aecd671 | 649 | |
9cd9a005 | 650 | Resume_devices: |
abfe2d7b | 651 | entering_platform_hibernation = false; |
d1616302 | 652 | dpm_resume_end(PMSG_RESTORE); |
63830aef | 653 | console_resume_all(); |
2ed8d2b3 | 654 | |
caea99ef RW |
655 | Close: |
656 | hibernation_ops->end(); | |
2ed8d2b3 | 657 | |
b1457bcc | 658 | return error; |
7777fab9 RW |
659 | } |
660 | ||
1da177e4 | 661 | /** |
f42a9813 | 662 | * power_down - Shut the machine down for hibernation. |
1da177e4 | 663 | * |
f42a9813 RW |
664 | * Use the platform driver, if configured, to put the system into the sleep |
665 | * state corresponding to hibernation, or try to power it off or reboot, | |
666 | * depending on the value of hibernation_mode. | |
1da177e4 | 667 | */ |
fe0c935a | 668 | static void power_down(void) |
1da177e4 | 669 | { |
62c552cc | 670 | int error; |
81d45bdf | 671 | |
0c4cae1b | 672 | #ifdef CONFIG_SUSPEND |
81d45bdf | 673 | if (hibernation_mode == HIBERNATION_SUSPEND) { |
85850af4 | 674 | error = suspend_devices_and_enter(mem_sleep_current); |
81d45bdf RW |
675 | if (error) { |
676 | hibernation_mode = hibernation_ops ? | |
677 | HIBERNATION_PLATFORM : | |
678 | HIBERNATION_SHUTDOWN; | |
679 | } else { | |
680 | /* Restore swap signature. */ | |
681 | error = swsusp_unmark(); | |
682 | if (error) | |
2872de13 | 683 | pr_err("Swap will be unusable! Try swapon -a.\n"); |
81d45bdf RW |
684 | |
685 | return; | |
686 | } | |
687 | } | |
62c552cc BS |
688 | #endif |
689 | ||
a3d25c27 | 690 | switch (hibernation_mode) { |
a3d25c27 | 691 | case HIBERNATION_REBOOT: |
fdde86ac | 692 | kernel_restart(NULL); |
1da177e4 | 693 | break; |
a3d25c27 | 694 | case HIBERNATION_PLATFORM: |
0c4cae1b CF |
695 | error = hibernation_platform_enter(); |
696 | if (error == -EAGAIN || error == -EBUSY) { | |
697 | swsusp_unmark(); | |
698 | events_check_enabled = false; | |
699 | pr_info("Wakeup event detected during hibernation, rolling back.\n"); | |
700 | return; | |
701 | } | |
df561f66 | 702 | fallthrough; |
9cd9a005 | 703 | case HIBERNATION_SHUTDOWN: |
3e251afa DW |
704 | if (kernel_can_power_off()) { |
705 | entering_platform_hibernation = true; | |
2c730785 | 706 | kernel_power_off(); |
3e251afa DW |
707 | entering_platform_hibernation = false; |
708 | } | |
9cd9a005 | 709 | break; |
1da177e4 | 710 | } |
fdde86ac | 711 | kernel_halt(); |
fe0c935a JB |
712 | /* |
713 | * Valid image is on the disk, if we continue we risk serious data | |
714 | * corruption after resume. | |
715 | */ | |
2872de13 | 716 | pr_crit("Power down manually\n"); |
2c730785 SC |
717 | while (1) |
718 | cpu_relax(); | |
1da177e4 LT |
719 | } |
720 | ||
93745df1 | 721 | static int load_image_and_restore(void) |
fe12c00d CY |
722 | { |
723 | int error; | |
724 | unsigned int flags; | |
725 | ||
8d8b2441 | 726 | pm_pr_dbg("Loading hibernation image.\n"); |
fe12c00d CY |
727 | |
728 | lock_device_hotplug(); | |
729 | error = create_basic_memory_bitmaps(); | |
3f51aa9e | 730 | if (error) { |
93745df1 | 731 | swsusp_close(); |
fe12c00d | 732 | goto Unlock; |
3f51aa9e | 733 | } |
fe12c00d CY |
734 | |
735 | error = swsusp_read(&flags); | |
93745df1 | 736 | swsusp_close(); |
fe12c00d | 737 | if (!error) |
3704a6a4 | 738 | error = hibernation_restore(flags & SF_PLATFORM_MODE); |
fe12c00d | 739 | |
7a7b99bf | 740 | pr_err("Failed to load image, recovering.\n"); |
fe12c00d CY |
741 | swsusp_free(); |
742 | free_basic_memory_bitmaps(); | |
743 | Unlock: | |
744 | unlock_device_hotplug(); | |
745 | ||
746 | return error; | |
747 | } | |
748 | ||
8bc29736 N |
749 | #define COMPRESSION_ALGO_LZO "lzo" |
750 | #define COMPRESSION_ALGO_LZ4 "lz4" | |
751 | ||
1da177e4 | 752 | /** |
f42a9813 | 753 | * hibernate - Carry out system hibernation, including saving the image. |
1da177e4 | 754 | */ |
a3d25c27 | 755 | int hibernate(void) |
1da177e4 | 756 | { |
d6545e68 | 757 | bool snapshot_test = false; |
5950e5d5 | 758 | unsigned int sleep_flags; |
70d93298 | 759 | int error; |
1da177e4 | 760 | |
a6e15a39 | 761 | if (!hibernation_available()) { |
8d8b2441 | 762 | pm_pr_dbg("Hibernation not available.\n"); |
a6e15a39 KC |
763 | return -EPERM; |
764 | } | |
765 | ||
a06c6f5d N |
766 | /* |
767 | * Query for the compression algorithm support if compression is enabled. | |
768 | */ | |
769 | if (!nocompress) { | |
cfdb7520 | 770 | strscpy(hib_comp_algo, hibernate_compressor); |
b03d542c | 771 | if (!crypto_has_acomp(hib_comp_algo, 0, CRYPTO_ALG_ASYNC)) { |
a06c6f5d N |
772 | pr_err("%s compression is not available\n", hib_comp_algo); |
773 | return -EOPNOTSUPP; | |
774 | } | |
775 | } | |
776 | ||
5950e5d5 | 777 | sleep_flags = lock_system_sleep(); |
0709db60 | 778 | /* The snapshot device should not be opened while we're running */ |
ab7e9b06 | 779 | if (!hibernate_acquire()) { |
b10d9117 RW |
780 | error = -EBUSY; |
781 | goto Unlock; | |
782 | } | |
783 | ||
8915aa20 | 784 | pr_info("hibernation entry\n"); |
5a0a2f30 | 785 | pm_prepare_console(); |
70d93298 PZ |
786 | error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION); |
787 | if (error) | |
788 | goto Restore; | |
0709db60 | 789 | |
b5dee313 | 790 | ksys_sync_helper(); |
eacfbf74 CB |
791 | if (filesystem_freeze_enabled) |
792 | filesystems_freeze(); | |
232b1432 | 793 | |
03afed8b | 794 | error = freeze_processes(); |
5a72e04d | 795 | if (error) |
8fd37a4c RW |
796 | goto Exit; |
797 | ||
942f4015 | 798 | lock_device_hotplug(); |
8fd37a4c RW |
799 | /* Allocate memory management structures */ |
800 | error = create_basic_memory_bitmaps(); | |
801 | if (error) | |
802 | goto Thaw; | |
1da177e4 | 803 | |
7777fab9 | 804 | error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM); |
a556d5b5 | 805 | if (error || freezer_test_done) |
8fd37a4c | 806 | goto Free_bitmaps; |
64a473cb RW |
807 | |
808 | if (in_suspend) { | |
a634cc10 RW |
809 | unsigned int flags = 0; |
810 | ||
811 | if (hibernation_mode == HIBERNATION_PLATFORM) | |
812 | flags |= SF_PLATFORM_MODE; | |
8bc29736 | 813 | if (nocompress) { |
f996fc96 | 814 | flags |= SF_NOCOMPRESS_MODE; |
8bc29736 | 815 | } else { |
081a9d04 BS |
816 | flags |= SF_CRC32_MODE; |
817 | ||
8bc29736 N |
818 | /* |
819 | * By default, LZO compression is enabled. Use SF_COMPRESSION_ALG_LZ4 | |
820 | * to override this behaviour and use LZ4. | |
821 | * | |
822 | * Refer kernel/power/power.h for more details | |
823 | */ | |
824 | ||
825 | if (!strcmp(hib_comp_algo, COMPRESSION_ALGO_LZ4)) | |
826 | flags |= SF_COMPRESSION_ALG_LZ4; | |
827 | else | |
828 | flags |= SF_COMPRESSION_ALG_LZO; | |
829 | } | |
830 | ||
7a7b99bf | 831 | pm_pr_dbg("Writing hibernation image.\n"); |
a634cc10 | 832 | error = swsusp_write(flags); |
7777fab9 | 833 | swsusp_free(); |
fe12c00d CY |
834 | if (!error) { |
835 | if (hibernation_mode == HIBERNATION_TEST_RESUME) | |
836 | snapshot_test = true; | |
837 | else | |
838 | power_down(); | |
839 | } | |
5262a475 | 840 | in_suspend = 0; |
c9e664f1 | 841 | pm_restore_gfp_mask(); |
b918f6e6 | 842 | } else { |
7a7b99bf | 843 | pm_pr_dbg("Hibernation image restored successfully.\n"); |
b918f6e6 | 844 | } |
64a473cb | 845 | |
8fd37a4c RW |
846 | Free_bitmaps: |
847 | free_basic_memory_bitmaps(); | |
b918f6e6 | 848 | Thaw: |
942f4015 | 849 | unlock_device_hotplug(); |
fe12c00d | 850 | if (snapshot_test) { |
8d8b2441 | 851 | pm_pr_dbg("Checking hibernation image\n"); |
148b6f4c | 852 | error = swsusp_check(false); |
fe12c00d | 853 | if (!error) |
93745df1 | 854 | error = load_image_and_restore(); |
fe12c00d | 855 | } |
5a0a2f30 | 856 | thaw_processes(); |
a556d5b5 SB |
857 | |
858 | /* Don't bother checking whether freezer_test_done is true */ | |
859 | freezer_test_done = false; | |
0709db60 | 860 | Exit: |
eacfbf74 | 861 | filesystems_thaw(); |
70d93298 PZ |
862 | pm_notifier_call_chain(PM_POST_HIBERNATION); |
863 | Restore: | |
5a0a2f30 | 864 | pm_restore_console(); |
ab7e9b06 | 865 | hibernate_release(); |
b10d9117 | 866 | Unlock: |
5950e5d5 | 867 | unlock_system_sleep(sleep_flags); |
8915aa20 RW |
868 | pr_info("hibernation exit\n"); |
869 | ||
1da177e4 LT |
870 | return error; |
871 | } | |
872 | ||
48001ea5 DW |
873 | /** |
874 | * hibernate_quiet_exec - Execute a function with all devices frozen. | |
875 | * @func: Function to execute. | |
876 | * @data: Data pointer to pass to @func. | |
877 | * | |
878 | * Return the @func return value or an error code if it cannot be executed. | |
879 | */ | |
880 | int hibernate_quiet_exec(int (*func)(void *data), void *data) | |
881 | { | |
5950e5d5 | 882 | unsigned int sleep_flags; |
70d93298 | 883 | int error; |
48001ea5 | 884 | |
5950e5d5 | 885 | sleep_flags = lock_system_sleep(); |
48001ea5 DW |
886 | |
887 | if (!hibernate_acquire()) { | |
888 | error = -EBUSY; | |
889 | goto unlock; | |
890 | } | |
891 | ||
892 | pm_prepare_console(); | |
893 | ||
70d93298 PZ |
894 | error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION); |
895 | if (error) | |
896 | goto restore; | |
48001ea5 | 897 | |
eacfbf74 CB |
898 | if (filesystem_freeze_enabled) |
899 | filesystems_freeze(); | |
900 | ||
48001ea5 DW |
901 | error = freeze_processes(); |
902 | if (error) | |
903 | goto exit; | |
904 | ||
905 | lock_device_hotplug(); | |
906 | ||
907 | pm_suspend_clear_flags(); | |
908 | ||
909 | error = platform_begin(true); | |
910 | if (error) | |
911 | goto thaw; | |
912 | ||
913 | error = freeze_kernel_threads(); | |
914 | if (error) | |
915 | goto thaw; | |
916 | ||
917 | error = dpm_prepare(PMSG_FREEZE); | |
918 | if (error) | |
919 | goto dpm_complete; | |
920 | ||
e9cec448 | 921 | console_suspend_all(); |
48001ea5 DW |
922 | |
923 | error = dpm_suspend(PMSG_FREEZE); | |
924 | if (error) | |
925 | goto dpm_resume; | |
926 | ||
927 | error = dpm_suspend_end(PMSG_FREEZE); | |
928 | if (error) | |
929 | goto dpm_resume; | |
930 | ||
931 | error = platform_pre_snapshot(true); | |
932 | if (error) | |
933 | goto skip; | |
934 | ||
935 | error = func(data); | |
936 | ||
937 | skip: | |
938 | platform_finish(true); | |
939 | ||
940 | dpm_resume_start(PMSG_THAW); | |
941 | ||
942 | dpm_resume: | |
943 | dpm_resume(PMSG_THAW); | |
944 | ||
63830aef | 945 | console_resume_all(); |
48001ea5 DW |
946 | |
947 | dpm_complete: | |
948 | dpm_complete(PMSG_THAW); | |
949 | ||
950 | thaw_kernel_threads(); | |
951 | ||
952 | thaw: | |
953 | platform_end(true); | |
954 | ||
955 | unlock_device_hotplug(); | |
956 | ||
957 | thaw_processes(); | |
958 | ||
959 | exit: | |
eacfbf74 | 960 | filesystems_thaw(); |
70d93298 | 961 | pm_notifier_call_chain(PM_POST_HIBERNATION); |
48001ea5 | 962 | |
70d93298 | 963 | restore: |
48001ea5 DW |
964 | pm_restore_console(); |
965 | ||
966 | hibernate_release(); | |
967 | ||
968 | unlock: | |
5950e5d5 | 969 | unlock_system_sleep(sleep_flags); |
48001ea5 DW |
970 | |
971 | return error; | |
972 | } | |
973 | EXPORT_SYMBOL_GPL(hibernate_quiet_exec); | |
1da177e4 | 974 | |
cc89c63e | 975 | static int __init find_resume_device(void) |
02b42d58 CH |
976 | { |
977 | if (!strlen(resume_file)) | |
978 | return -ENOENT; | |
979 | ||
980 | pm_pr_dbg("Checking hibernation image partition %s\n", resume_file); | |
981 | ||
982 | if (resume_delay) { | |
983 | pr_info("Waiting %dsec before reading resume device ...\n", | |
984 | resume_delay); | |
985 | ssleep(resume_delay); | |
986 | } | |
987 | ||
988 | /* Check if the device is there */ | |
cf056a43 | 989 | if (!early_lookup_bdev(resume_file, &swsusp_resume_device)) |
02b42d58 CH |
990 | return 0; |
991 | ||
992 | /* | |
993 | * Some device discovery might still be in progress; we need to wait for | |
994 | * this to finish. | |
995 | */ | |
996 | wait_for_device_probe(); | |
997 | if (resume_wait) { | |
cf056a43 | 998 | while (early_lookup_bdev(resume_file, &swsusp_resume_device)) |
02b42d58 CH |
999 | msleep(10); |
1000 | async_synchronize_full(); | |
1001 | } | |
1002 | ||
cf056a43 | 1003 | return early_lookup_bdev(resume_file, &swsusp_resume_device); |
02b42d58 CH |
1004 | } |
1005 | ||
1da177e4 LT |
1006 | static int software_resume(void) |
1007 | { | |
70d93298 | 1008 | int error; |
1da177e4 | 1009 | |
8d8b2441 | 1010 | pm_pr_dbg("Hibernation image partition %d:%d present\n", |
0c8454f5 | 1011 | MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device)); |
1da177e4 | 1012 | |
8d8b2441 | 1013 | pm_pr_dbg("Looking for hibernation image.\n"); |
cc89c63e CH |
1014 | |
1015 | mutex_lock(&system_transition_mutex); | |
148b6f4c | 1016 | error = swsusp_check(true); |
ed746e3b | 1017 | if (error) |
74dfd666 | 1018 | goto Unlock; |
1da177e4 | 1019 | |
a06c6f5d N |
1020 | /* |
1021 | * Check if the hibernation image is compressed. If so, query for | |
1022 | * the algorithm support. | |
1023 | */ | |
1024 | if (!(swsusp_header_flags & SF_NOCOMPRESS_MODE)) { | |
8bc29736 | 1025 | if (swsusp_header_flags & SF_COMPRESSION_ALG_LZ4) |
cfdb7520 | 1026 | strscpy(hib_comp_algo, COMPRESSION_ALGO_LZ4); |
8bc29736 | 1027 | else |
cfdb7520 | 1028 | strscpy(hib_comp_algo, COMPRESSION_ALGO_LZO); |
b03d542c | 1029 | if (!crypto_has_acomp(hib_comp_algo, 0, CRYPTO_ALG_ASYNC)) { |
a06c6f5d N |
1030 | pr_err("%s compression is not available\n", hib_comp_algo); |
1031 | error = -EOPNOTSUPP; | |
1032 | goto Unlock; | |
1033 | } | |
1034 | } | |
1035 | ||
0709db60 | 1036 | /* The snapshot device should not be opened while we're running */ |
ab7e9b06 | 1037 | if (!hibernate_acquire()) { |
0709db60 | 1038 | error = -EBUSY; |
93745df1 | 1039 | swsusp_close(); |
0709db60 RW |
1040 | goto Unlock; |
1041 | } | |
1042 | ||
8915aa20 | 1043 | pr_info("resume from hibernation\n"); |
5a0a2f30 | 1044 | pm_prepare_console(); |
70d93298 PZ |
1045 | error = pm_notifier_call_chain_robust(PM_RESTORE_PREPARE, PM_POST_RESTORE); |
1046 | if (error) | |
1047 | goto Restore; | |
1bfcf130 | 1048 | |
eacfbf74 CB |
1049 | if (filesystem_freeze_enabled) |
1050 | filesystems_freeze(); | |
1051 | ||
7a7b99bf | 1052 | pm_pr_dbg("Preparing processes for hibernation restore.\n"); |
03afed8b | 1053 | error = freeze_processes(); |
eacfbf74 CB |
1054 | if (error) { |
1055 | filesystems_thaw(); | |
8fd37a4c | 1056 | goto Close_Finish; |
eacfbf74 | 1057 | } |
2351f8d2 DC |
1058 | |
1059 | error = freeze_kernel_threads(); | |
1060 | if (error) { | |
1061 | thaw_processes(); | |
eacfbf74 | 1062 | filesystems_thaw(); |
2351f8d2 DC |
1063 | goto Close_Finish; |
1064 | } | |
1065 | ||
93745df1 | 1066 | error = load_image_and_restore(); |
8fd37a4c | 1067 | thaw_processes(); |
eacfbf74 | 1068 | filesystems_thaw(); |
0709db60 | 1069 | Finish: |
70d93298 PZ |
1070 | pm_notifier_call_chain(PM_POST_RESTORE); |
1071 | Restore: | |
5a0a2f30 | 1072 | pm_restore_console(); |
7a7b99bf | 1073 | pr_info("resume failed (%d)\n", error); |
ab7e9b06 | 1074 | hibernate_release(); |
dd5d666b | 1075 | /* For success case, the suspend path will release the lock */ |
74dfd666 | 1076 | Unlock: |
55f2503c | 1077 | mutex_unlock(&system_transition_mutex); |
8d8b2441 | 1078 | pm_pr_dbg("Hibernation image not present or could not be loaded.\n"); |
7777fab9 | 1079 | return error; |
8fd37a4c | 1080 | Close_Finish: |
93745df1 | 1081 | swsusp_close(); |
76b57e61 | 1082 | goto Finish; |
1da177e4 LT |
1083 | } |
1084 | ||
cc89c63e CH |
1085 | /** |
1086 | * software_resume_initcall - Resume from a saved hibernation image. | |
1087 | * | |
1088 | * This routine is called as a late initcall, when all devices have been | |
1089 | * discovered and initialized already. | |
1090 | * | |
1091 | * The image reading code is called to see if there is a hibernation image | |
1092 | * available for reading. If that is the case, devices are quiesced and the | |
1093 | * contents of memory is restored from the saved image. | |
1094 | * | |
1095 | * If this is successful, control reappears in the restored target kernel in | |
1096 | * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine | |
1097 | * attempts to recover gracefully and make the kernel return to the normal mode | |
1098 | * of operation. | |
1099 | */ | |
1100 | static int __init software_resume_initcall(void) | |
1101 | { | |
1102 | /* | |
1103 | * If the user said "noresume".. bail out early. | |
1104 | */ | |
1105 | if (noresume || !hibernation_available()) | |
1106 | return 0; | |
1107 | ||
1108 | if (!swsusp_resume_device) { | |
1109 | int error = find_resume_device(); | |
1110 | ||
1111 | if (error) | |
1112 | return error; | |
1113 | } | |
1114 | ||
1115 | return software_resume(); | |
1116 | } | |
1117 | late_initcall_sync(software_resume_initcall); | |
1da177e4 LT |
1118 | |
1119 | ||
a3d25c27 RW |
1120 | static const char * const hibernation_modes[] = { |
1121 | [HIBERNATION_PLATFORM] = "platform", | |
1122 | [HIBERNATION_SHUTDOWN] = "shutdown", | |
1123 | [HIBERNATION_REBOOT] = "reboot", | |
62c552cc BS |
1124 | #ifdef CONFIG_SUSPEND |
1125 | [HIBERNATION_SUSPEND] = "suspend", | |
1126 | #endif | |
fe12c00d | 1127 | [HIBERNATION_TEST_RESUME] = "test_resume", |
1da177e4 LT |
1128 | }; |
1129 | ||
f42a9813 RW |
1130 | /* |
1131 | * /sys/power/disk - Control hibernation mode. | |
1da177e4 | 1132 | * |
f42a9813 RW |
1133 | * Hibernation can be handled in several ways. There are a few different ways |
1134 | * to put the system into the sleep state: using the platform driver (e.g. ACPI | |
1135 | * or other hibernation_ops), powering it off or rebooting it (for testing | |
48580ab8 | 1136 | * mostly). |
1da177e4 | 1137 | * |
f42a9813 RW |
1138 | * The sysfs file /sys/power/disk provides an interface for selecting the |
1139 | * hibernation mode to use. Reading from this file causes the available modes | |
48580ab8 | 1140 | * to be printed. There are 3 modes that can be supported: |
1da177e4 | 1141 | * |
1da177e4 LT |
1142 | * 'platform' |
1143 | * 'shutdown' | |
1144 | * 'reboot' | |
1145 | * | |
f42a9813 RW |
1146 | * If a platform hibernation driver is in use, 'platform' will be supported |
1147 | * and will be used by default. Otherwise, 'shutdown' will be used by default. | |
1148 | * The selected option (i.e. the one corresponding to the current value of | |
1149 | * hibernation_mode) is enclosed by a square bracket. | |
1150 | * | |
1151 | * To select a given hibernation mode it is necessary to write the mode's | |
1152 | * string representation (as returned by reading from /sys/power/disk) back | |
1153 | * into /sys/power/disk. | |
1da177e4 LT |
1154 | */ |
1155 | ||
386f275f KS |
1156 | static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr, |
1157 | char *buf) | |
1da177e4 | 1158 | { |
6306653c | 1159 | ssize_t count = 0; |
f0ced9b2 | 1160 | int i; |
f0ced9b2 | 1161 | |
a6e15a39 | 1162 | if (!hibernation_available()) |
6306653c | 1163 | return sysfs_emit(buf, "[disabled]\n"); |
a6e15a39 | 1164 | |
a3d25c27 RW |
1165 | for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { |
1166 | if (!hibernation_modes[i]) | |
f0ced9b2 JB |
1167 | continue; |
1168 | switch (i) { | |
a3d25c27 RW |
1169 | case HIBERNATION_SHUTDOWN: |
1170 | case HIBERNATION_REBOOT: | |
62c552cc BS |
1171 | #ifdef CONFIG_SUSPEND |
1172 | case HIBERNATION_SUSPEND: | |
1173 | #endif | |
fe12c00d | 1174 | case HIBERNATION_TEST_RESUME: |
f0ced9b2 | 1175 | break; |
a3d25c27 RW |
1176 | case HIBERNATION_PLATFORM: |
1177 | if (hibernation_ops) | |
f0ced9b2 JB |
1178 | break; |
1179 | /* not a valid mode, continue with loop */ | |
1180 | continue; | |
1181 | } | |
a3d25c27 | 1182 | if (i == hibernation_mode) |
6306653c | 1183 | count += sysfs_emit_at(buf, count, "[%s] ", hibernation_modes[i]); |
f0ced9b2 | 1184 | else |
6306653c | 1185 | count += sysfs_emit_at(buf, count, "%s ", hibernation_modes[i]); |
f0ced9b2 | 1186 | } |
6306653c XL |
1187 | |
1188 | /* Convert the last space to a newline if needed. */ | |
1189 | if (count > 0) | |
1190 | buf[count - 1] = '\n'; | |
1191 | ||
1192 | return count; | |
1da177e4 LT |
1193 | } |
1194 | ||
386f275f KS |
1195 | static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, |
1196 | const char *buf, size_t n) | |
1da177e4 | 1197 | { |
5950e5d5 PZ |
1198 | int mode = HIBERNATION_INVALID; |
1199 | unsigned int sleep_flags; | |
1da177e4 | 1200 | int error = 0; |
1da177e4 LT |
1201 | int len; |
1202 | char *p; | |
5950e5d5 | 1203 | int i; |
1da177e4 | 1204 | |
a6e15a39 KC |
1205 | if (!hibernation_available()) |
1206 | return -EPERM; | |
1207 | ||
1da177e4 LT |
1208 | p = memchr(buf, '\n', n); |
1209 | len = p ? p - buf : n; | |
1210 | ||
5950e5d5 | 1211 | sleep_flags = lock_system_sleep(); |
a3d25c27 | 1212 | for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { |
8d98a690 RW |
1213 | if (len == strlen(hibernation_modes[i]) |
1214 | && !strncmp(buf, hibernation_modes[i], len)) { | |
1da177e4 LT |
1215 | mode = i; |
1216 | break; | |
1217 | } | |
1218 | } | |
a3d25c27 | 1219 | if (mode != HIBERNATION_INVALID) { |
fe0c935a | 1220 | switch (mode) { |
a3d25c27 RW |
1221 | case HIBERNATION_SHUTDOWN: |
1222 | case HIBERNATION_REBOOT: | |
62c552cc BS |
1223 | #ifdef CONFIG_SUSPEND |
1224 | case HIBERNATION_SUSPEND: | |
1225 | #endif | |
fe12c00d | 1226 | case HIBERNATION_TEST_RESUME: |
a3d25c27 | 1227 | hibernation_mode = mode; |
fe0c935a | 1228 | break; |
a3d25c27 RW |
1229 | case HIBERNATION_PLATFORM: |
1230 | if (hibernation_ops) | |
1231 | hibernation_mode = mode; | |
1da177e4 LT |
1232 | else |
1233 | error = -EINVAL; | |
1234 | } | |
a3d25c27 | 1235 | } else |
1da177e4 LT |
1236 | error = -EINVAL; |
1237 | ||
a3d25c27 | 1238 | if (!error) |
8d8b2441 RW |
1239 | pm_pr_dbg("Hibernation mode set to '%s'\n", |
1240 | hibernation_modes[mode]); | |
5950e5d5 | 1241 | unlock_system_sleep(sleep_flags); |
1da177e4 LT |
1242 | return error ? error : n; |
1243 | } | |
1244 | ||
1245 | power_attr(disk); | |
1246 | ||
386f275f KS |
1247 | static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr, |
1248 | char *buf) | |
1da177e4 | 1249 | { |
6306653c XL |
1250 | return sysfs_emit(buf, "%d:%d\n", MAJOR(swsusp_resume_device), |
1251 | MINOR(swsusp_resume_device)); | |
1da177e4 LT |
1252 | } |
1253 | ||
386f275f KS |
1254 | static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr, |
1255 | const char *buf, size_t n) | |
1da177e4 | 1256 | { |
5950e5d5 | 1257 | unsigned int sleep_flags; |
421a5fa1 SC |
1258 | int len = n; |
1259 | char *name; | |
cf056a43 CH |
1260 | dev_t dev; |
1261 | int error; | |
1da177e4 | 1262 | |
cc89c63e | 1263 | if (!hibernation_available()) |
df2f7cde | 1264 | return n; |
cc89c63e | 1265 | |
421a5fa1 SC |
1266 | if (len && buf[len-1] == '\n') |
1267 | len--; | |
1268 | name = kstrndup(buf, len, GFP_KERNEL); | |
1269 | if (!name) | |
1270 | return -ENOMEM; | |
1da177e4 | 1271 | |
1e8c813b CH |
1272 | error = lookup_bdev(name, &dev); |
1273 | if (error) { | |
1274 | unsigned maj, min, offset; | |
1275 | char *p, dummy; | |
1276 | ||
c9e4bf60 | 1277 | error = 0; |
1e8c813b CH |
1278 | if (sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2 || |
1279 | sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset, | |
1280 | &dummy) == 3) { | |
1281 | dev = MKDEV(maj, min); | |
1282 | if (maj != MAJOR(dev) || min != MINOR(dev)) | |
1283 | error = -EINVAL; | |
1284 | } else { | |
1285 | dev = new_decode_dev(simple_strtoul(name, &p, 16)); | |
1286 | if (*p) | |
1287 | error = -EINVAL; | |
1288 | } | |
1289 | } | |
421a5fa1 | 1290 | kfree(name); |
cf056a43 CH |
1291 | if (error) |
1292 | return error; | |
1da177e4 | 1293 | |
5950e5d5 | 1294 | sleep_flags = lock_system_sleep(); |
cf056a43 | 1295 | swsusp_resume_device = dev; |
5950e5d5 PZ |
1296 | unlock_system_sleep(sleep_flags); |
1297 | ||
7a7b99bf LS |
1298 | pm_pr_dbg("Configured hibernation resume from disk to %u\n", |
1299 | swsusp_resume_device); | |
a576219a AM |
1300 | noresume = 0; |
1301 | software_resume(); | |
421a5fa1 | 1302 | return n; |
1da177e4 LT |
1303 | } |
1304 | ||
1305 | power_attr(resume); | |
1306 | ||
35506467 ML |
1307 | static ssize_t resume_offset_show(struct kobject *kobj, |
1308 | struct kobj_attribute *attr, char *buf) | |
1309 | { | |
6306653c | 1310 | return sysfs_emit(buf, "%llu\n", (unsigned long long)swsusp_resume_block); |
35506467 ML |
1311 | } |
1312 | ||
1313 | static ssize_t resume_offset_store(struct kobject *kobj, | |
1314 | struct kobj_attribute *attr, const char *buf, | |
1315 | size_t n) | |
1316 | { | |
1317 | unsigned long long offset; | |
1318 | int rc; | |
1319 | ||
1320 | rc = kstrtoull(buf, 0, &offset); | |
1321 | if (rc) | |
1322 | return rc; | |
1323 | swsusp_resume_block = offset; | |
1324 | ||
1325 | return n; | |
1326 | } | |
1327 | ||
1328 | power_attr(resume_offset); | |
1329 | ||
386f275f KS |
1330 | static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr, |
1331 | char *buf) | |
ca0aec0f | 1332 | { |
6306653c | 1333 | return sysfs_emit(buf, "%lu\n", image_size); |
ca0aec0f RW |
1334 | } |
1335 | ||
386f275f KS |
1336 | static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr, |
1337 | const char *buf, size_t n) | |
ca0aec0f | 1338 | { |
853609b6 | 1339 | unsigned long size; |
ca0aec0f | 1340 | |
853609b6 | 1341 | if (sscanf(buf, "%lu", &size) == 1) { |
ca0aec0f RW |
1342 | image_size = size; |
1343 | return n; | |
1344 | } | |
1345 | ||
1346 | return -EINVAL; | |
1347 | } | |
1348 | ||
1349 | power_attr(image_size); | |
1350 | ||
ddeb6487 RW |
1351 | static ssize_t reserved_size_show(struct kobject *kobj, |
1352 | struct kobj_attribute *attr, char *buf) | |
1353 | { | |
6306653c | 1354 | return sysfs_emit(buf, "%lu\n", reserved_size); |
ddeb6487 RW |
1355 | } |
1356 | ||
1357 | static ssize_t reserved_size_store(struct kobject *kobj, | |
1358 | struct kobj_attribute *attr, | |
1359 | const char *buf, size_t n) | |
1360 | { | |
1361 | unsigned long size; | |
1362 | ||
1363 | if (sscanf(buf, "%lu", &size) == 1) { | |
1364 | reserved_size = size; | |
1365 | return n; | |
1366 | } | |
1367 | ||
1368 | return -EINVAL; | |
1369 | } | |
1370 | ||
1371 | power_attr(reserved_size); | |
1372 | ||
6ada7ba2 | 1373 | static struct attribute *g[] = { |
1da177e4 | 1374 | &disk_attr.attr, |
35506467 | 1375 | &resume_offset_attr.attr, |
1da177e4 | 1376 | &resume_attr.attr, |
ca0aec0f | 1377 | &image_size_attr.attr, |
ddeb6487 | 1378 | &reserved_size_attr.attr, |
1da177e4 LT |
1379 | NULL, |
1380 | }; | |
1381 | ||
1382 | ||
59494fe2 | 1383 | static const struct attribute_group attr_group = { |
1da177e4 LT |
1384 | .attrs = g, |
1385 | }; | |
1386 | ||
1387 | ||
1388 | static int __init pm_disk_init(void) | |
1389 | { | |
d76e15fb | 1390 | return sysfs_create_group(power_kobj, &attr_group); |
1da177e4 LT |
1391 | } |
1392 | ||
1393 | core_initcall(pm_disk_init); | |
1394 | ||
1395 | ||
1396 | static int __init resume_setup(char *str) | |
1397 | { | |
1398 | if (noresume) | |
1399 | return 1; | |
1400 | ||
7b831bd3 | 1401 | strscpy(resume_file, str); |
1da177e4 LT |
1402 | return 1; |
1403 | } | |
1404 | ||
9a154d9d RW |
1405 | static int __init resume_offset_setup(char *str) |
1406 | { | |
1407 | unsigned long long offset; | |
1408 | ||
1409 | if (noresume) | |
1410 | return 1; | |
1411 | ||
1412 | if (sscanf(str, "%llu", &offset) == 1) | |
1413 | swsusp_resume_block = offset; | |
1414 | ||
1415 | return 1; | |
1416 | } | |
1417 | ||
f996fc96 BS |
1418 | static int __init hibernate_setup(char *str) |
1419 | { | |
2f88e41a | 1420 | if (!strncmp(str, "noresume", 8)) { |
f996fc96 | 1421 | noresume = 1; |
2f88e41a | 1422 | } else if (!strncmp(str, "nocompress", 10)) { |
f996fc96 | 1423 | nocompress = 1; |
2f88e41a | 1424 | } else if (!strncmp(str, "no", 2)) { |
a6e15a39 KC |
1425 | noresume = 1; |
1426 | nohibernate = 1; | |
0f5bf6d0 | 1427 | } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) |
4c0b6c10 RW |
1428 | && !strncmp(str, "protect_image", 13)) { |
1429 | enable_restore_image_protection(); | |
a6e15a39 | 1430 | } |
f996fc96 BS |
1431 | return 1; |
1432 | } | |
1433 | ||
1da177e4 LT |
1434 | static int __init noresume_setup(char *str) |
1435 | { | |
1436 | noresume = 1; | |
1437 | return 1; | |
1438 | } | |
1439 | ||
6f8d7022 BS |
1440 | static int __init resumewait_setup(char *str) |
1441 | { | |
1442 | resume_wait = 1; | |
1443 | return 1; | |
1444 | } | |
1445 | ||
f126f733 BS |
1446 | static int __init resumedelay_setup(char *str) |
1447 | { | |
f6514be5 | 1448 | int rc = kstrtouint(str, 0, &resume_delay); |
317cf7e5 FF |
1449 | |
1450 | if (rc) | |
ba7ffcd4 | 1451 | pr_warn("resumedelay: bad option string '%s'\n", str); |
f126f733 BS |
1452 | return 1; |
1453 | } | |
1454 | ||
a6e15a39 KC |
1455 | static int __init nohibernate_setup(char *str) |
1456 | { | |
1457 | noresume = 1; | |
1458 | nohibernate = 1; | |
1459 | return 1; | |
1460 | } | |
1461 | ||
3fec6e59 N |
1462 | static const char * const comp_alg_enabled[] = { |
1463 | #if IS_ENABLED(CONFIG_CRYPTO_LZO) | |
1464 | COMPRESSION_ALGO_LZO, | |
1465 | #endif | |
1466 | #if IS_ENABLED(CONFIG_CRYPTO_LZ4) | |
1467 | COMPRESSION_ALGO_LZ4, | |
1468 | #endif | |
1469 | }; | |
1470 | ||
1471 | static int hibernate_compressor_param_set(const char *compressor, | |
1472 | const struct kernel_param *kp) | |
1473 | { | |
3fec6e59 N |
1474 | int index, ret; |
1475 | ||
52323ed1 LX |
1476 | if (!mutex_trylock(&system_transition_mutex)) |
1477 | return -EBUSY; | |
3fec6e59 N |
1478 | |
1479 | index = sysfs_match_string(comp_alg_enabled, compressor); | |
1480 | if (index >= 0) { | |
1481 | ret = param_set_copystring(comp_alg_enabled[index], kp); | |
1482 | if (!ret) | |
cfdb7520 | 1483 | strscpy(hib_comp_algo, comp_alg_enabled[index]); |
3fec6e59 N |
1484 | } else { |
1485 | ret = index; | |
1486 | } | |
1487 | ||
52323ed1 | 1488 | mutex_unlock(&system_transition_mutex); |
3fec6e59 N |
1489 | |
1490 | if (ret) | |
1491 | pr_debug("Cannot set specified compressor %s\n", | |
1492 | compressor); | |
1493 | ||
1494 | return ret; | |
1495 | } | |
1496 | ||
1497 | static const struct kernel_param_ops hibernate_compressor_param_ops = { | |
1498 | .set = hibernate_compressor_param_set, | |
1499 | .get = param_get_string, | |
1500 | }; | |
1501 | ||
1502 | static struct kparam_string hibernate_compressor_param_string = { | |
1503 | .maxlen = sizeof(hibernate_compressor), | |
1504 | .string = hibernate_compressor, | |
1505 | }; | |
1506 | ||
1507 | module_param_cb(compressor, &hibernate_compressor_param_ops, | |
1508 | &hibernate_compressor_param_string, 0644); | |
1509 | MODULE_PARM_DESC(compressor, | |
1510 | "Compression algorithm to be used with hibernation"); | |
1511 | ||
1da177e4 | 1512 | __setup("noresume", noresume_setup); |
9a154d9d | 1513 | __setup("resume_offset=", resume_offset_setup); |
1da177e4 | 1514 | __setup("resume=", resume_setup); |
f996fc96 | 1515 | __setup("hibernate=", hibernate_setup); |
6f8d7022 | 1516 | __setup("resumewait", resumewait_setup); |
f126f733 | 1517 | __setup("resumedelay=", resumedelay_setup); |
a6e15a39 | 1518 | __setup("nohibernate", nohibernate_setup); |