Fix: rseq: arm branch to failure
[deliverable/linux.git] / kernel / power / hibernate.c
CommitLineData
1da177e4 1/*
8b759b84 2 * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
1da177e4
LT
3 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
a2531293 6 * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz>
8b759b84 7 * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
62c552cc 8 * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com>
1da177e4
LT
9 *
10 * This file is released under the GPLv2.
1da177e4
LT
11 */
12
6e5fdeed 13#include <linux/export.h>
1da177e4
LT
14#include <linux/suspend.h>
15#include <linux/syscalls.h>
16#include <linux/reboot.h>
17#include <linux/string.h>
18#include <linux/device.h>
6f8d7022 19#include <linux/async.h>
1da177e4
LT
20#include <linux/delay.h>
21#include <linux/fs.h>
d53d9f16 22#include <linux/mount.h>
88d10bba 23#include <linux/pm.h>
97c7801c 24#include <linux/console.h>
e3920fb4 25#include <linux/cpu.h>
7dfb7103 26#include <linux/freezer.h>
5a0e3ad6 27#include <linux/gfp.h>
40dc166c 28#include <linux/syscore_ops.h>
2df83fa4
MB
29#include <linux/ctype.h>
30#include <linux/genhd.h>
db597605 31#include <linux/ktime.h>
bb3632c6 32#include <trace/events/power.h>
d53d9f16 33
1da177e4
LT
34#include "power.h"
35
36
d231ff1a
BS
37static int nocompress;
38static int noresume;
a6e15a39 39static int nohibernate;
d231ff1a 40static int resume_wait;
f6514be5 41static unsigned int resume_delay;
47a460d5 42static char resume_file[256] = CONFIG_PM_STD_PARTITION;
1da177e4 43dev_t swsusp_resume_device;
9a154d9d 44sector_t swsusp_resume_block;
d6efc2f7 45__visible int in_suspend __nosavedata;
1da177e4 46
a3d25c27
RW
47enum {
48 HIBERNATION_INVALID,
49 HIBERNATION_PLATFORM,
a3d25c27
RW
50 HIBERNATION_SHUTDOWN,
51 HIBERNATION_REBOOT,
62c552cc
BS
52#ifdef CONFIG_SUSPEND
53 HIBERNATION_SUSPEND,
54#endif
fe12c00d 55 HIBERNATION_TEST_RESUME,
a3d25c27
RW
56 /* keep last */
57 __HIBERNATION_AFTER_LAST
58};
59#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
60#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
61
62static int hibernation_mode = HIBERNATION_SHUTDOWN;
63
97819a26 64bool freezer_test_done;
aa9a7b11 65
073ef1f6 66static const struct platform_hibernation_ops *hibernation_ops;
a3d25c27 67
a6e15a39
KC
68bool hibernation_available(void)
69{
70 return (nohibernate == 0);
71}
72
a3d25c27 73/**
f42a9813
RW
74 * hibernation_set_ops - Set the global hibernate operations.
75 * @ops: Hibernation operations to use in subsequent hibernation transitions.
a3d25c27 76 */
073ef1f6 77void hibernation_set_ops(const struct platform_hibernation_ops *ops)
a3d25c27 78{
caea99ef
RW
79 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
80 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
5729c63a 81 && ops->restore_cleanup && ops->leave)) {
a3d25c27
RW
82 WARN_ON(1);
83 return;
84 }
bcda53fa 85 lock_system_sleep();
a3d25c27
RW
86 hibernation_ops = ops;
87 if (ops)
88 hibernation_mode = HIBERNATION_PLATFORM;
89 else if (hibernation_mode == HIBERNATION_PLATFORM)
90 hibernation_mode = HIBERNATION_SHUTDOWN;
91
bcda53fa 92 unlock_system_sleep();
a3d25c27 93}
e0c7855e 94EXPORT_SYMBOL_GPL(hibernation_set_ops);
a3d25c27 95
abfe2d7b
RW
96static bool entering_platform_hibernation;
97
98bool system_entering_hibernation(void)
99{
100 return entering_platform_hibernation;
101}
102EXPORT_SYMBOL(system_entering_hibernation);
103
4cc79776
RW
104#ifdef CONFIG_PM_DEBUG
105static void hibernation_debug_sleep(void)
106{
107 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
108 mdelay(5000);
109}
110
4cc79776
RW
111static int hibernation_test(int level)
112{
113 if (pm_test_level == level) {
114 hibernation_debug_sleep();
115 return 1;
116 }
117 return 0;
118}
119#else /* !CONFIG_PM_DEBUG */
4cc79776
RW
120static int hibernation_test(int level) { return 0; }
121#endif /* !CONFIG_PM_DEBUG */
122
74f270af 123/**
f42a9813
RW
124 * platform_begin - Call platform to start hibernation.
125 * @platform_mode: Whether or not to use the platform driver.
74f270af 126 */
caea99ef 127static int platform_begin(int platform_mode)
74f270af
RW
128{
129 return (platform_mode && hibernation_ops) ?
caea99ef
RW
130 hibernation_ops->begin() : 0;
131}
132
133/**
f42a9813
RW
134 * platform_end - Call platform to finish transition to the working state.
135 * @platform_mode: Whether or not to use the platform driver.
caea99ef 136 */
caea99ef
RW
137static void platform_end(int platform_mode)
138{
139 if (platform_mode && hibernation_ops)
140 hibernation_ops->end();
74f270af 141}
a3d25c27 142
8a05aac2 143/**
f42a9813
RW
144 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
145 * @platform_mode: Whether or not to use the platform driver.
146 *
147 * Use the platform driver to prepare the system for creating a hibernate image,
148 * if so configured, and return an error code if that fails.
8a05aac2
SS
149 */
150
74f270af 151static int platform_pre_snapshot(int platform_mode)
8a05aac2 152{
7777fab9 153 return (platform_mode && hibernation_ops) ?
74f270af 154 hibernation_ops->pre_snapshot() : 0;
a3d25c27 155}
8a05aac2 156
c7e0831d 157/**
f42a9813
RW
158 * platform_leave - Call platform to prepare a transition to the working state.
159 * @platform_mode: Whether or not to use the platform driver.
160 *
161 * Use the platform driver prepare to prepare the machine for switching to the
162 * normal mode of operation.
163 *
164 * This routine is called on one CPU with interrupts disabled.
c7e0831d 165 */
c7e0831d
RW
166static void platform_leave(int platform_mode)
167{
168 if (platform_mode && hibernation_ops)
169 hibernation_ops->leave();
170}
171
a3d25c27 172/**
f42a9813
RW
173 * platform_finish - Call platform to switch the system to the working state.
174 * @platform_mode: Whether or not to use the platform driver.
175 *
176 * Use the platform driver to switch the machine to the normal mode of
177 * operation.
178 *
179 * This routine must be called after platform_prepare().
a3d25c27 180 */
7777fab9 181static void platform_finish(int platform_mode)
a3d25c27 182{
7777fab9 183 if (platform_mode && hibernation_ops)
a3d25c27 184 hibernation_ops->finish();
8a05aac2
SS
185}
186
a634cc10 187/**
f42a9813
RW
188 * platform_pre_restore - Prepare for hibernate image restoration.
189 * @platform_mode: Whether or not to use the platform driver.
190 *
191 * Use the platform driver to prepare the system for resume from a hibernation
192 * image.
193 *
194 * If the restore fails after this function has been called,
195 * platform_restore_cleanup() must be called.
a634cc10 196 */
a634cc10
RW
197static int platform_pre_restore(int platform_mode)
198{
199 return (platform_mode && hibernation_ops) ?
200 hibernation_ops->pre_restore() : 0;
201}
202
203/**
f42a9813
RW
204 * platform_restore_cleanup - Switch to the working state after failing restore.
205 * @platform_mode: Whether or not to use the platform driver.
206 *
207 * Use the platform driver to switch the system to the normal mode of operation
208 * after a failing restore.
209 *
210 * If platform_pre_restore() has been called before the failing restore, this
211 * function must be called too, regardless of the result of
212 * platform_pre_restore().
a634cc10 213 */
a634cc10
RW
214static void platform_restore_cleanup(int platform_mode)
215{
216 if (platform_mode && hibernation_ops)
217 hibernation_ops->restore_cleanup();
218}
219
d8f3de0d 220/**
f42a9813
RW
221 * platform_recover - Recover from a failure to suspend devices.
222 * @platform_mode: Whether or not to use the platform driver.
d8f3de0d 223 */
d8f3de0d
RW
224static void platform_recover(int platform_mode)
225{
226 if (platform_mode && hibernation_ops && hibernation_ops->recover)
227 hibernation_ops->recover();
228}
229
8e60c6a1 230/**
f42a9813
RW
231 * swsusp_show_speed - Print time elapsed between two events during hibernation.
232 * @start: Starting event.
233 * @stop: Final event.
234 * @nr_pages: Number of memory pages processed between @start and @stop.
235 * @msg: Additional diagnostic message to print.
8e60c6a1 236 */
db597605
TR
237void swsusp_show_speed(ktime_t start, ktime_t stop,
238 unsigned nr_pages, char *msg)
8e60c6a1 239{
db597605 240 ktime_t diff;
4881f603
CG
241 u64 elapsed_centisecs64;
242 unsigned int centisecs;
243 unsigned int k;
244 unsigned int kps;
8e60c6a1 245
db597605
TR
246 diff = ktime_sub(stop, start);
247 elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC);
8e60c6a1
NC
248 centisecs = elapsed_centisecs64;
249 if (centisecs == 0)
250 centisecs = 1; /* avoid div-by-zero */
251 k = nr_pages * (PAGE_SIZE / 1024);
252 kps = (k * 100) / centisecs;
4881f603 253 printk(KERN_INFO "PM: %s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
8e60c6a1
NC
254 msg, k,
255 centisecs / 100, centisecs % 100,
256 kps / 1000, (kps % 1000) / 10);
257}
258
c7e0831d 259/**
f42a9813
RW
260 * create_image - Create a hibernation image.
261 * @platform_mode: Whether or not to use the platform driver.
262 *
cf579dfb
RW
263 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
264 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
f42a9813
RW
265 *
266 * Control reappears in this routine after the subsequent restore.
c7e0831d 267 */
47a460d5 268static int create_image(int platform_mode)
c7e0831d
RW
269{
270 int error;
271
cf579dfb 272 error = dpm_suspend_end(PMSG_FREEZE);
c7e0831d 273 if (error) {
23976728
RW
274 printk(KERN_ERR "PM: Some devices failed to power down, "
275 "aborting hibernation\n");
32bdfac5 276 return error;
c7e0831d 277 }
2ed8d2b3 278
4aecd671
RW
279 error = platform_pre_snapshot(platform_mode);
280 if (error || hibernation_test(TEST_PLATFORM))
281 goto Platform_finish;
282
283 error = disable_nonboot_cpus();
48580ab8 284 if (error || hibernation_test(TEST_CPUS))
4aecd671
RW
285 goto Enable_cpus;
286
2ed8d2b3
RW
287 local_irq_disable();
288
2e711c04 289 error = syscore_suspend();
770824bd 290 if (error) {
4484079d 291 printk(KERN_ERR "PM: Some system devices failed to power down, "
770824bd 292 "aborting hibernation\n");
4aecd671 293 goto Enable_irqs;
770824bd 294 }
c7e0831d 295
a2867e08 296 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
4cc79776
RW
297 goto Power_up;
298
299 in_suspend = 1;
c7e0831d 300 save_processor_state();
bb3632c6 301 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
c7e0831d 302 error = swsusp_arch_suspend();
62822e2e
TG
303 /* Restore control flow magically appears here */
304 restore_processor_state();
bb3632c6 305 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
c7e0831d 306 if (error)
23976728
RW
307 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
308 error);
362e77d1 309 if (!in_suspend)
c125e96f 310 events_check_enabled = false;
362e77d1
BM
311
312 platform_leave(platform_mode);
4aecd671 313
4cc79776 314 Power_up:
40dc166c 315 syscore_resume();
2ed8d2b3 316
4aecd671 317 Enable_irqs:
2ed8d2b3
RW
318 local_irq_enable();
319
4aecd671
RW
320 Enable_cpus:
321 enable_nonboot_cpus();
322
323 Platform_finish:
324 platform_finish(platform_mode);
325
cf579dfb 326 dpm_resume_start(in_suspend ?
1eede070 327 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
2ed8d2b3 328
c7e0831d
RW
329 return error;
330}
331
7777fab9 332/**
f42a9813
RW
333 * hibernation_snapshot - Quiesce devices and create a hibernation image.
334 * @platform_mode: If set, use platform driver to prepare for the transition.
7777fab9 335 *
f42a9813 336 * This routine must be called with pm_mutex held.
7777fab9 337 */
7777fab9
RW
338int hibernation_snapshot(int platform_mode)
339{
953a2063 340 pm_message_t msg;
cbe2f5a6 341 int error;
7777fab9 342
27614273 343 pm_suspend_clear_flags();
3fe0313e 344 error = platform_begin(platform_mode);
7777fab9 345 if (error)
d074ee02 346 goto Close;
7777fab9 347
64a473cb
RW
348 /* Preallocate image memory before shutting down devices. */
349 error = hibernate_preallocate_memory();
2aede851
RW
350 if (error)
351 goto Close;
352
353 error = freeze_kernel_threads();
354 if (error)
bb58dd5d 355 goto Cleanup;
2aede851 356
48580ab8 357 if (hibernation_test(TEST_FREEZER)) {
aa9a7b11
SB
358
359 /*
360 * Indicate to the caller that we are returning due to a
361 * successful freezer test.
362 */
363 freezer_test_done = true;
51d6ff7a 364 goto Thaw;
aa9a7b11
SB
365 }
366
2aede851 367 error = dpm_prepare(PMSG_FREEZE);
bb58dd5d 368 if (error) {
953a2063 369 dpm_complete(PMSG_RECOVER);
51d6ff7a 370 goto Thaw;
bb58dd5d 371 }
74f270af 372
7777fab9 373 suspend_console();
c9e664f1 374 pm_restrict_gfp_mask();
953a2063 375
91e7c75b 376 error = dpm_suspend(PMSG_FREEZE);
10a1803d 377
953a2063
SB
378 if (error || hibernation_test(TEST_DEVICES))
379 platform_recover(platform_mode);
380 else
381 error = create_image(platform_mode);
7777fab9 382
c9e664f1 383 /*
953a2063
SB
384 * In the case that we call create_image() above, the control
385 * returns here (1) after the image has been created or the
c9e664f1
RW
386 * image creation has failed and (2) after a successful restore.
387 */
4cc79776 388
64a473cb
RW
389 /* We may need to release the preallocated image pages here. */
390 if (error || !in_suspend)
391 swsusp_free();
392
91e7c75b
RW
393 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
394 dpm_resume(msg);
c9e664f1
RW
395
396 if (error || !in_suspend)
397 pm_restore_gfp_mask();
398
7777fab9 399 resume_console();
91e7c75b
RW
400 dpm_complete(msg);
401
caea99ef
RW
402 Close:
403 platform_end(platform_mode);
7777fab9 404 return error;
d8f3de0d 405
51d6ff7a
SB
406 Thaw:
407 thaw_kernel_threads();
bb58dd5d
RW
408 Cleanup:
409 swsusp_free();
410 goto Close;
7777fab9
RW
411}
412
406f992e
RW
413int __weak hibernate_resume_nonboot_cpu_disable(void)
414{
415 return disable_nonboot_cpus();
416}
417
72df68ca 418/**
f42a9813
RW
419 * resume_target_kernel - Restore system state from a hibernation image.
420 * @platform_mode: Whether or not to use the platform driver.
421 *
cf579dfb
RW
422 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
423 * contents of highmem that have not been restored yet from the image and run
424 * the low-level code that will restore the remaining contents of memory and
425 * switch to the just restored target kernel.
72df68ca 426 */
4aecd671 427static int resume_target_kernel(bool platform_mode)
72df68ca
RW
428{
429 int error;
430
cf579dfb 431 error = dpm_suspend_end(PMSG_QUIESCE);
72df68ca 432 if (error) {
23976728 433 printk(KERN_ERR "PM: Some devices failed to power down, "
72df68ca 434 "aborting resume\n");
32bdfac5 435 return error;
72df68ca 436 }
2ed8d2b3 437
4aecd671
RW
438 error = platform_pre_restore(platform_mode);
439 if (error)
440 goto Cleanup;
441
406f992e 442 error = hibernate_resume_nonboot_cpu_disable();
4aecd671
RW
443 if (error)
444 goto Enable_cpus;
445
2ed8d2b3
RW
446 local_irq_disable();
447
2e711c04 448 error = syscore_suspend();
4aecd671
RW
449 if (error)
450 goto Enable_irqs;
451
72df68ca
RW
452 save_processor_state();
453 error = restore_highmem();
454 if (!error) {
455 error = swsusp_arch_resume();
456 /*
457 * The code below is only ever reached in case of a failure.
4e2d9491
RW
458 * Otherwise, execution continues at the place where
459 * swsusp_arch_suspend() was called.
72df68ca
RW
460 */
461 BUG_ON(!error);
4e2d9491
RW
462 /*
463 * This call to restore_highmem() reverts the changes made by
464 * the previous one.
465 */
72df68ca
RW
466 restore_highmem();
467 }
468 /*
469 * The only reason why swsusp_arch_resume() can fail is memory being
470 * very tight, so we have to free it as soon as we can to avoid
4e2d9491 471 * subsequent failures.
72df68ca
RW
472 */
473 swsusp_free();
474 restore_processor_state();
475 touch_softlockup_watchdog();
2ed8d2b3 476
40dc166c 477 syscore_resume();
2ed8d2b3 478
4aecd671 479 Enable_irqs:
72df68ca 480 local_irq_enable();
2ed8d2b3 481
4aecd671
RW
482 Enable_cpus:
483 enable_nonboot_cpus();
484
485 Cleanup:
486 platform_restore_cleanup(platform_mode);
487
cf579dfb 488 dpm_resume_start(PMSG_RECOVER);
2ed8d2b3 489
72df68ca
RW
490 return error;
491}
492
7777fab9 493/**
f42a9813
RW
494 * hibernation_restore - Quiesce devices and restore from a hibernation image.
495 * @platform_mode: If set, use platform driver to prepare for the transition.
7777fab9 496 *
f42a9813 497 * This routine must be called with pm_mutex held. If it is successful, control
21e82808 498 * reappears in the restored target kernel in hibernation_snapshot().
7777fab9 499 */
a634cc10 500int hibernation_restore(int platform_mode)
7777fab9 501{
cbe2f5a6 502 int error;
7777fab9
RW
503
504 pm_prepare_console();
505 suspend_console();
c9e664f1 506 pm_restrict_gfp_mask();
d1616302 507 error = dpm_suspend_start(PMSG_QUIESCE);
a634cc10 508 if (!error) {
4aecd671 509 error = resume_target_kernel(platform_mode);
94fb823f
ID
510 /*
511 * The above should either succeed and jump to the new kernel,
512 * or return with an error. Otherwise things are just
513 * undefined, so let's be paranoid.
514 */
515 BUG_ON(!error);
a634cc10 516 }
94fb823f 517 dpm_resume_end(PMSG_RECOVER);
c9e664f1 518 pm_restore_gfp_mask();
7777fab9
RW
519 resume_console();
520 pm_restore_console();
521 return error;
522}
523
524/**
f42a9813 525 * hibernation_platform_enter - Power off the system using the platform driver.
7777fab9 526 */
7777fab9
RW
527int hibernation_platform_enter(void)
528{
cbe2f5a6 529 int error;
b1457bcc 530
9cd9a005
RW
531 if (!hibernation_ops)
532 return -ENOSYS;
533
534 /*
535 * We have cancelled the power transition by running
536 * hibernation_ops->finish() before saving the image, so we should let
537 * the firmware know that we're going to enter the sleep state after all
538 */
caea99ef 539 error = hibernation_ops->begin();
9cd9a005 540 if (error)
caea99ef 541 goto Close;
9cd9a005 542
abfe2d7b 543 entering_platform_hibernation = true;
9cd9a005 544 suspend_console();
d1616302 545 error = dpm_suspend_start(PMSG_HIBERNATE);
d8f3de0d
RW
546 if (error) {
547 if (hibernation_ops->recover)
548 hibernation_ops->recover();
549 goto Resume_devices;
550 }
9cd9a005 551
cf579dfb 552 error = dpm_suspend_end(PMSG_HIBERNATE);
4aecd671 553 if (error)
32bdfac5 554 goto Resume_devices;
4aecd671 555
9cd9a005
RW
556 error = hibernation_ops->prepare();
557 if (error)
e681c9dd 558 goto Platform_finish;
9cd9a005
RW
559
560 error = disable_nonboot_cpus();
561 if (error)
8c506608 562 goto Enable_cpus;
2ed8d2b3 563
4aecd671 564 local_irq_disable();
40dc166c 565 syscore_suspend();
a2867e08 566 if (pm_wakeup_pending()) {
c125e96f
RW
567 error = -EAGAIN;
568 goto Power_up;
569 }
570
4aecd671
RW
571 hibernation_ops->enter();
572 /* We should never get here */
573 while (1);
9cd9a005 574
c125e96f 575 Power_up:
40dc166c 576 syscore_resume();
c125e96f 577 local_irq_enable();
8c506608
VK
578
579 Enable_cpus:
c125e96f
RW
580 enable_nonboot_cpus();
581
e681c9dd 582 Platform_finish:
9cd9a005 583 hibernation_ops->finish();
2ed8d2b3 584
cf579dfb 585 dpm_resume_start(PMSG_RESTORE);
4aecd671 586
9cd9a005 587 Resume_devices:
abfe2d7b 588 entering_platform_hibernation = false;
d1616302 589 dpm_resume_end(PMSG_RESTORE);
9cd9a005 590 resume_console();
2ed8d2b3 591
caea99ef
RW
592 Close:
593 hibernation_ops->end();
2ed8d2b3 594
b1457bcc 595 return error;
7777fab9
RW
596}
597
1da177e4 598/**
f42a9813 599 * power_down - Shut the machine down for hibernation.
1da177e4 600 *
f42a9813
RW
601 * Use the platform driver, if configured, to put the system into the sleep
602 * state corresponding to hibernation, or try to power it off or reboot,
603 * depending on the value of hibernation_mode.
1da177e4 604 */
fe0c935a 605static void power_down(void)
1da177e4 606{
62c552cc
BS
607#ifdef CONFIG_SUSPEND
608 int error;
609#endif
610
a3d25c27 611 switch (hibernation_mode) {
a3d25c27 612 case HIBERNATION_REBOOT:
fdde86ac 613 kernel_restart(NULL);
1da177e4 614 break;
a3d25c27 615 case HIBERNATION_PLATFORM:
7777fab9 616 hibernation_platform_enter();
9cd9a005 617 case HIBERNATION_SHUTDOWN:
2c730785
SC
618 if (pm_power_off)
619 kernel_power_off();
9cd9a005 620 break;
62c552cc
BS
621#ifdef CONFIG_SUSPEND
622 case HIBERNATION_SUSPEND:
623 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
624 if (error) {
625 if (hibernation_ops)
626 hibernation_mode = HIBERNATION_PLATFORM;
627 else
628 hibernation_mode = HIBERNATION_SHUTDOWN;
629 power_down();
630 }
631 /*
632 * Restore swap signature.
633 */
634 error = swsusp_unmark();
635 if (error)
636 printk(KERN_ERR "PM: Swap will be unusable! "
637 "Try swapon -a.\n");
638 return;
639#endif
1da177e4 640 }
fdde86ac 641 kernel_halt();
fe0c935a
JB
642 /*
643 * Valid image is on the disk, if we continue we risk serious data
644 * corruption after resume.
645 */
23976728 646 printk(KERN_CRIT "PM: Please power down manually\n");
2c730785
SC
647 while (1)
648 cpu_relax();
1da177e4
LT
649}
650
fe12c00d
CY
651static int load_image_and_restore(void)
652{
653 int error;
654 unsigned int flags;
655
656 pr_debug("PM: Loading hibernation image.\n");
657
658 lock_device_hotplug();
659 error = create_basic_memory_bitmaps();
660 if (error)
661 goto Unlock;
662
663 error = swsusp_read(&flags);
664 swsusp_close(FMODE_READ);
665 if (!error)
666 hibernation_restore(flags & SF_PLATFORM_MODE);
667
668 printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n");
669 swsusp_free();
670 free_basic_memory_bitmaps();
671 Unlock:
672 unlock_device_hotplug();
673
674 return error;
675}
676
1da177e4 677/**
f42a9813 678 * hibernate - Carry out system hibernation, including saving the image.
1da177e4 679 */
a3d25c27 680int hibernate(void)
1da177e4 681{
ea00f4f4 682 int error, nr_calls = 0;
fe12c00d 683 bool snapshot_test = false;
1da177e4 684
a6e15a39
KC
685 if (!hibernation_available()) {
686 pr_debug("PM: Hibernation not available.\n");
687 return -EPERM;
688 }
689
bcda53fa 690 lock_system_sleep();
0709db60 691 /* The snapshot device should not be opened while we're running */
b10d9117
RW
692 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
693 error = -EBUSY;
694 goto Unlock;
695 }
696
5a0a2f30 697 pm_prepare_console();
ea00f4f4
LW
698 error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
699 if (error) {
700 nr_calls--;
b10d9117 701 goto Exit;
ea00f4f4 702 }
0709db60 703
23976728 704 printk(KERN_INFO "PM: Syncing filesystems ... ");
232b1432
RW
705 sys_sync();
706 printk("done.\n");
707
03afed8b 708 error = freeze_processes();
5a72e04d 709 if (error)
8fd37a4c
RW
710 goto Exit;
711
942f4015 712 lock_device_hotplug();
8fd37a4c
RW
713 /* Allocate memory management structures */
714 error = create_basic_memory_bitmaps();
715 if (error)
716 goto Thaw;
1da177e4 717
7777fab9 718 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
a556d5b5 719 if (error || freezer_test_done)
8fd37a4c 720 goto Free_bitmaps;
64a473cb
RW
721
722 if (in_suspend) {
a634cc10
RW
723 unsigned int flags = 0;
724
725 if (hibernation_mode == HIBERNATION_PLATFORM)
726 flags |= SF_PLATFORM_MODE;
f996fc96
BS
727 if (nocompress)
728 flags |= SF_NOCOMPRESS_MODE;
081a9d04
BS
729 else
730 flags |= SF_CRC32_MODE;
731
1da177e4 732 pr_debug("PM: writing image.\n");
a634cc10 733 error = swsusp_write(flags);
7777fab9 734 swsusp_free();
fe12c00d
CY
735 if (!error) {
736 if (hibernation_mode == HIBERNATION_TEST_RESUME)
737 snapshot_test = true;
738 else
739 power_down();
740 }
5262a475 741 in_suspend = 0;
c9e664f1 742 pm_restore_gfp_mask();
b918f6e6 743 } else {
1da177e4 744 pr_debug("PM: Image restored successfully.\n");
b918f6e6 745 }
64a473cb 746
8fd37a4c
RW
747 Free_bitmaps:
748 free_basic_memory_bitmaps();
b918f6e6 749 Thaw:
942f4015 750 unlock_device_hotplug();
fe12c00d
CY
751 if (snapshot_test) {
752 pr_debug("PM: Checking hibernation image\n");
753 error = swsusp_check();
754 if (!error)
755 error = load_image_and_restore();
756 }
5a0a2f30 757 thaw_processes();
a556d5b5
SB
758
759 /* Don't bother checking whether freezer_test_done is true */
760 freezer_test_done = false;
0709db60 761 Exit:
ea00f4f4 762 __pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL);
5a0a2f30 763 pm_restore_console();
0709db60 764 atomic_inc(&snapshot_device_available);
b10d9117 765 Unlock:
bcda53fa 766 unlock_system_sleep();
1da177e4
LT
767 return error;
768}
769
770
771/**
f42a9813
RW
772 * software_resume - Resume from a saved hibernation image.
773 *
774 * This routine is called as a late initcall, when all devices have been
775 * discovered and initialized already.
1da177e4 776 *
f42a9813
RW
777 * The image reading code is called to see if there is a hibernation image
778 * available for reading. If that is the case, devices are quiesced and the
779 * contents of memory is restored from the saved image.
1da177e4 780 *
f42a9813 781 * If this is successful, control reappears in the restored target kernel in
d439e64f 782 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
f42a9813
RW
783 * attempts to recover gracefully and make the kernel return to the normal mode
784 * of operation.
1da177e4 785 */
1da177e4
LT
786static int software_resume(void)
787{
ea00f4f4 788 int error, nr_calls = 0;
1da177e4 789
eed3ee08
AV
790 /*
791 * If the user said "noresume".. bail out early.
792 */
a6e15a39 793 if (noresume || !hibernation_available())
eed3ee08
AV
794 return 0;
795
60a0d233
JB
796 /*
797 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
798 * is configured into the kernel. Since the regular hibernate
799 * trigger path is via sysfs which takes a buffer mutex before
800 * calling hibernate functions (which take pm_mutex) this can
801 * cause lockdep to complain about a possible ABBA deadlock
802 * which cannot happen since we're in the boot code here and
803 * sysfs can't be invoked yet. Therefore, we use a subclass
804 * here to avoid lockdep complaining.
805 */
806 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
0c8454f5
RW
807
808 if (swsusp_resume_device)
809 goto Check_image;
810
811 if (!strlen(resume_file)) {
812 error = -ENOENT;
813 goto Unlock;
814 }
815
d0941ead 816 pr_debug("PM: Checking hibernation image partition %s\n", resume_file);
0c8454f5 817
f126f733
BS
818 if (resume_delay) {
819 printk(KERN_INFO "Waiting %dsec before reading resume device...\n",
820 resume_delay);
821 ssleep(resume_delay);
822 }
823
0c8454f5
RW
824 /* Check if the device is there */
825 swsusp_resume_device = name_to_dev_t(resume_file);
2df83fa4
MB
826
827 /*
828 * name_to_dev_t is ineffective to verify parition if resume_file is in
829 * integer format. (e.g. major:minor)
830 */
831 if (isdigit(resume_file[0]) && resume_wait) {
832 int partno;
833 while (!get_gendisk(swsusp_resume_device, &partno))
834 msleep(10);
835 }
836
3efa147a 837 if (!swsusp_resume_device) {
eed3ee08
AV
838 /*
839 * Some device discovery might still be in progress; we need
840 * to wait for this to finish.
841 */
842 wait_for_device_probe();
6f8d7022
BS
843
844 if (resume_wait) {
845 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
846 msleep(10);
847 async_synchronize_full();
848 }
849
3efa147a 850 swsusp_resume_device = name_to_dev_t(resume_file);
0c8454f5
RW
851 if (!swsusp_resume_device) {
852 error = -ENODEV;
853 goto Unlock;
854 }
3efa147a
PM
855 }
856
0c8454f5 857 Check_image:
d0941ead 858 pr_debug("PM: Hibernation image partition %d:%d present\n",
0c8454f5 859 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
1da177e4 860
d0941ead 861 pr_debug("PM: Looking for hibernation image.\n");
ed746e3b
RW
862 error = swsusp_check();
863 if (error)
74dfd666 864 goto Unlock;
1da177e4 865
0709db60
RW
866 /* The snapshot device should not be opened while we're running */
867 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
868 error = -EBUSY;
76b57e61 869 swsusp_close(FMODE_READ);
0709db60
RW
870 goto Unlock;
871 }
872
5a0a2f30 873 pm_prepare_console();
ea00f4f4
LW
874 error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
875 if (error) {
876 nr_calls--;
8fd37a4c 877 goto Close_Finish;
ea00f4f4 878 }
1bfcf130 879
74dfd666 880 pr_debug("PM: Preparing processes for restore.\n");
03afed8b 881 error = freeze_processes();
8fd37a4c
RW
882 if (error)
883 goto Close_Finish;
fe12c00d 884 error = load_image_and_restore();
8fd37a4c 885 thaw_processes();
0709db60 886 Finish:
ea00f4f4 887 __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
5a0a2f30 888 pm_restore_console();
0709db60 889 atomic_inc(&snapshot_device_available);
dd5d666b 890 /* For success case, the suspend path will release the lock */
74dfd666 891 Unlock:
a6d70980 892 mutex_unlock(&pm_mutex);
d0941ead 893 pr_debug("PM: Hibernation image not present or could not be loaded.\n");
7777fab9 894 return error;
8fd37a4c 895 Close_Finish:
76b57e61
JS
896 swsusp_close(FMODE_READ);
897 goto Finish;
1da177e4
LT
898}
899
d3c345db 900late_initcall_sync(software_resume);
1da177e4
LT
901
902
a3d25c27
RW
903static const char * const hibernation_modes[] = {
904 [HIBERNATION_PLATFORM] = "platform",
905 [HIBERNATION_SHUTDOWN] = "shutdown",
906 [HIBERNATION_REBOOT] = "reboot",
62c552cc
BS
907#ifdef CONFIG_SUSPEND
908 [HIBERNATION_SUSPEND] = "suspend",
909#endif
fe12c00d 910 [HIBERNATION_TEST_RESUME] = "test_resume",
1da177e4
LT
911};
912
f42a9813
RW
913/*
914 * /sys/power/disk - Control hibernation mode.
1da177e4 915 *
f42a9813
RW
916 * Hibernation can be handled in several ways. There are a few different ways
917 * to put the system into the sleep state: using the platform driver (e.g. ACPI
918 * or other hibernation_ops), powering it off or rebooting it (for testing
48580ab8 919 * mostly).
1da177e4 920 *
f42a9813
RW
921 * The sysfs file /sys/power/disk provides an interface for selecting the
922 * hibernation mode to use. Reading from this file causes the available modes
48580ab8 923 * to be printed. There are 3 modes that can be supported:
1da177e4 924 *
1da177e4
LT
925 * 'platform'
926 * 'shutdown'
927 * 'reboot'
928 *
f42a9813
RW
929 * If a platform hibernation driver is in use, 'platform' will be supported
930 * and will be used by default. Otherwise, 'shutdown' will be used by default.
931 * The selected option (i.e. the one corresponding to the current value of
932 * hibernation_mode) is enclosed by a square bracket.
933 *
934 * To select a given hibernation mode it is necessary to write the mode's
935 * string representation (as returned by reading from /sys/power/disk) back
936 * into /sys/power/disk.
1da177e4
LT
937 */
938
386f275f
KS
939static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
940 char *buf)
1da177e4 941{
f0ced9b2
JB
942 int i;
943 char *start = buf;
944
a6e15a39
KC
945 if (!hibernation_available())
946 return sprintf(buf, "[disabled]\n");
947
a3d25c27
RW
948 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
949 if (!hibernation_modes[i])
f0ced9b2
JB
950 continue;
951 switch (i) {
a3d25c27
RW
952 case HIBERNATION_SHUTDOWN:
953 case HIBERNATION_REBOOT:
62c552cc
BS
954#ifdef CONFIG_SUSPEND
955 case HIBERNATION_SUSPEND:
956#endif
fe12c00d 957 case HIBERNATION_TEST_RESUME:
f0ced9b2 958 break;
a3d25c27
RW
959 case HIBERNATION_PLATFORM:
960 if (hibernation_ops)
f0ced9b2
JB
961 break;
962 /* not a valid mode, continue with loop */
963 continue;
964 }
a3d25c27
RW
965 if (i == hibernation_mode)
966 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
f0ced9b2 967 else
a3d25c27 968 buf += sprintf(buf, "%s ", hibernation_modes[i]);
f0ced9b2
JB
969 }
970 buf += sprintf(buf, "\n");
971 return buf-start;
1da177e4
LT
972}
973
386f275f
KS
974static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
975 const char *buf, size_t n)
1da177e4
LT
976{
977 int error = 0;
978 int i;
979 int len;
980 char *p;
a3d25c27 981 int mode = HIBERNATION_INVALID;
1da177e4 982
a6e15a39
KC
983 if (!hibernation_available())
984 return -EPERM;
985
1da177e4
LT
986 p = memchr(buf, '\n', n);
987 len = p ? p - buf : n;
988
bcda53fa 989 lock_system_sleep();
a3d25c27 990 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
8d98a690
RW
991 if (len == strlen(hibernation_modes[i])
992 && !strncmp(buf, hibernation_modes[i], len)) {
1da177e4
LT
993 mode = i;
994 break;
995 }
996 }
a3d25c27 997 if (mode != HIBERNATION_INVALID) {
fe0c935a 998 switch (mode) {
a3d25c27
RW
999 case HIBERNATION_SHUTDOWN:
1000 case HIBERNATION_REBOOT:
62c552cc
BS
1001#ifdef CONFIG_SUSPEND
1002 case HIBERNATION_SUSPEND:
1003#endif
fe12c00d 1004 case HIBERNATION_TEST_RESUME:
a3d25c27 1005 hibernation_mode = mode;
fe0c935a 1006 break;
a3d25c27
RW
1007 case HIBERNATION_PLATFORM:
1008 if (hibernation_ops)
1009 hibernation_mode = mode;
1da177e4
LT
1010 else
1011 error = -EINVAL;
1012 }
a3d25c27 1013 } else
1da177e4
LT
1014 error = -EINVAL;
1015
a3d25c27 1016 if (!error)
23976728 1017 pr_debug("PM: Hibernation mode set to '%s'\n",
a3d25c27 1018 hibernation_modes[mode]);
bcda53fa 1019 unlock_system_sleep();
1da177e4
LT
1020 return error ? error : n;
1021}
1022
1023power_attr(disk);
1024
386f275f
KS
1025static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1026 char *buf)
1da177e4
LT
1027{
1028 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
1029 MINOR(swsusp_resume_device));
1030}
1031
386f275f
KS
1032static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1033 const char *buf, size_t n)
1da177e4 1034{
1da177e4 1035 dev_t res;
421a5fa1
SC
1036 int len = n;
1037 char *name;
1da177e4 1038
421a5fa1
SC
1039 if (len && buf[len-1] == '\n')
1040 len--;
1041 name = kstrndup(buf, len, GFP_KERNEL);
1042 if (!name)
1043 return -ENOMEM;
1da177e4 1044
421a5fa1
SC
1045 res = name_to_dev_t(name);
1046 kfree(name);
1047 if (!res)
1048 return -EINVAL;
1da177e4 1049
bcda53fa 1050 lock_system_sleep();
a576219a 1051 swsusp_resume_device = res;
bcda53fa 1052 unlock_system_sleep();
23976728 1053 printk(KERN_INFO "PM: Starting manual resume from disk\n");
a576219a
AM
1054 noresume = 0;
1055 software_resume();
421a5fa1 1056 return n;
1da177e4
LT
1057}
1058
1059power_attr(resume);
1060
386f275f
KS
1061static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1062 char *buf)
ca0aec0f 1063{
853609b6 1064 return sprintf(buf, "%lu\n", image_size);
ca0aec0f
RW
1065}
1066
386f275f
KS
1067static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1068 const char *buf, size_t n)
ca0aec0f 1069{
853609b6 1070 unsigned long size;
ca0aec0f 1071
853609b6 1072 if (sscanf(buf, "%lu", &size) == 1) {
ca0aec0f
RW
1073 image_size = size;
1074 return n;
1075 }
1076
1077 return -EINVAL;
1078}
1079
1080power_attr(image_size);
1081
ddeb6487
RW
1082static ssize_t reserved_size_show(struct kobject *kobj,
1083 struct kobj_attribute *attr, char *buf)
1084{
1085 return sprintf(buf, "%lu\n", reserved_size);
1086}
1087
1088static ssize_t reserved_size_store(struct kobject *kobj,
1089 struct kobj_attribute *attr,
1090 const char *buf, size_t n)
1091{
1092 unsigned long size;
1093
1094 if (sscanf(buf, "%lu", &size) == 1) {
1095 reserved_size = size;
1096 return n;
1097 }
1098
1099 return -EINVAL;
1100}
1101
1102power_attr(reserved_size);
1103
1da177e4
LT
1104static struct attribute * g[] = {
1105 &disk_attr.attr,
1106 &resume_attr.attr,
ca0aec0f 1107 &image_size_attr.attr,
ddeb6487 1108 &reserved_size_attr.attr,
1da177e4
LT
1109 NULL,
1110};
1111
1112
1113static struct attribute_group attr_group = {
1114 .attrs = g,
1115};
1116
1117
1118static int __init pm_disk_init(void)
1119{
d76e15fb 1120 return sysfs_create_group(power_kobj, &attr_group);
1da177e4
LT
1121}
1122
1123core_initcall(pm_disk_init);
1124
1125
1126static int __init resume_setup(char *str)
1127{
1128 if (noresume)
1129 return 1;
1130
1131 strncpy( resume_file, str, 255 );
1132 return 1;
1133}
1134
9a154d9d
RW
1135static int __init resume_offset_setup(char *str)
1136{
1137 unsigned long long offset;
1138
1139 if (noresume)
1140 return 1;
1141
1142 if (sscanf(str, "%llu", &offset) == 1)
1143 swsusp_resume_block = offset;
1144
1145 return 1;
1146}
1147
f996fc96
BS
1148static int __init hibernate_setup(char *str)
1149{
2f88e41a 1150 if (!strncmp(str, "noresume", 8)) {
f996fc96 1151 noresume = 1;
2f88e41a 1152 } else if (!strncmp(str, "nocompress", 10)) {
f996fc96 1153 nocompress = 1;
2f88e41a 1154 } else if (!strncmp(str, "no", 2)) {
a6e15a39
KC
1155 noresume = 1;
1156 nohibernate = 1;
4c0b6c10
RW
1157 } else if (IS_ENABLED(CONFIG_DEBUG_RODATA)
1158 && !strncmp(str, "protect_image", 13)) {
1159 enable_restore_image_protection();
a6e15a39 1160 }
f996fc96
BS
1161 return 1;
1162}
1163
1da177e4
LT
1164static int __init noresume_setup(char *str)
1165{
1166 noresume = 1;
1167 return 1;
1168}
1169
6f8d7022
BS
1170static int __init resumewait_setup(char *str)
1171{
1172 resume_wait = 1;
1173 return 1;
1174}
1175
f126f733
BS
1176static int __init resumedelay_setup(char *str)
1177{
f6514be5 1178 int rc = kstrtouint(str, 0, &resume_delay);
317cf7e5
FF
1179
1180 if (rc)
1181 return rc;
f126f733
BS
1182 return 1;
1183}
1184
a6e15a39
KC
1185static int __init nohibernate_setup(char *str)
1186{
1187 noresume = 1;
1188 nohibernate = 1;
1189 return 1;
1190}
1191
1414c7f4
LA
1192static int __init page_poison_nohibernate_setup(char *str)
1193{
1194#ifdef CONFIG_PAGE_POISONING_ZERO
1195 /*
1196 * The zeroing option for page poison skips the checks on alloc.
1197 * since hibernation doesn't save free pages there's no way to
1198 * guarantee the pages will still be zeroed.
1199 */
1200 if (!strcmp(str, "on")) {
1201 pr_info("Disabling hibernation due to page poisoning\n");
1202 return nohibernate_setup(str);
1203 }
1204#endif
1205 return 1;
1206}
1207
1da177e4 1208__setup("noresume", noresume_setup);
9a154d9d 1209__setup("resume_offset=", resume_offset_setup);
1da177e4 1210__setup("resume=", resume_setup);
f996fc96 1211__setup("hibernate=", hibernate_setup);
6f8d7022 1212__setup("resumewait", resumewait_setup);
f126f733 1213__setup("resumedelay=", resumedelay_setup);
a6e15a39 1214__setup("nohibernate", nohibernate_setup);
1414c7f4 1215__setup("page_poison=", page_poison_nohibernate_setup);
This page took 0.817877 seconds and 5 git commands to generate.