]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/power/hibernate.c
Merge branch 'x86-apic-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-artful-kernel.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);
1ad1410f 309 if (!in_suspend) {
c125e96f 310 events_check_enabled = false;
1ad1410f
AA
311 clear_free_pages();
312 }
362e77d1
BM
313
314 platform_leave(platform_mode);
4aecd671 315
4cc79776 316 Power_up:
40dc166c 317 syscore_resume();
2ed8d2b3 318
4aecd671 319 Enable_irqs:
2ed8d2b3
RW
320 local_irq_enable();
321
4aecd671
RW
322 Enable_cpus:
323 enable_nonboot_cpus();
324
325 Platform_finish:
326 platform_finish(platform_mode);
327
cf579dfb 328 dpm_resume_start(in_suspend ?
1eede070 329 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
2ed8d2b3 330
c7e0831d
RW
331 return error;
332}
333
7777fab9 334/**
f42a9813
RW
335 * hibernation_snapshot - Quiesce devices and create a hibernation image.
336 * @platform_mode: If set, use platform driver to prepare for the transition.
7777fab9 337 *
f42a9813 338 * This routine must be called with pm_mutex held.
7777fab9 339 */
7777fab9
RW
340int hibernation_snapshot(int platform_mode)
341{
953a2063 342 pm_message_t msg;
cbe2f5a6 343 int error;
7777fab9 344
27614273 345 pm_suspend_clear_flags();
3fe0313e 346 error = platform_begin(platform_mode);
7777fab9 347 if (error)
d074ee02 348 goto Close;
7777fab9 349
64a473cb
RW
350 /* Preallocate image memory before shutting down devices. */
351 error = hibernate_preallocate_memory();
2aede851
RW
352 if (error)
353 goto Close;
354
355 error = freeze_kernel_threads();
356 if (error)
bb58dd5d 357 goto Cleanup;
2aede851 358
48580ab8 359 if (hibernation_test(TEST_FREEZER)) {
aa9a7b11
SB
360
361 /*
362 * Indicate to the caller that we are returning due to a
363 * successful freezer test.
364 */
365 freezer_test_done = true;
51d6ff7a 366 goto Thaw;
aa9a7b11
SB
367 }
368
2aede851 369 error = dpm_prepare(PMSG_FREEZE);
bb58dd5d 370 if (error) {
953a2063 371 dpm_complete(PMSG_RECOVER);
51d6ff7a 372 goto Thaw;
bb58dd5d 373 }
74f270af 374
7777fab9 375 suspend_console();
c9e664f1 376 pm_restrict_gfp_mask();
953a2063 377
91e7c75b 378 error = dpm_suspend(PMSG_FREEZE);
10a1803d 379
953a2063
SB
380 if (error || hibernation_test(TEST_DEVICES))
381 platform_recover(platform_mode);
382 else
383 error = create_image(platform_mode);
7777fab9 384
c9e664f1 385 /*
953a2063
SB
386 * In the case that we call create_image() above, the control
387 * returns here (1) after the image has been created or the
c9e664f1
RW
388 * image creation has failed and (2) after a successful restore.
389 */
4cc79776 390
64a473cb
RW
391 /* We may need to release the preallocated image pages here. */
392 if (error || !in_suspend)
393 swsusp_free();
394
91e7c75b
RW
395 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
396 dpm_resume(msg);
c9e664f1
RW
397
398 if (error || !in_suspend)
399 pm_restore_gfp_mask();
400
7777fab9 401 resume_console();
91e7c75b
RW
402 dpm_complete(msg);
403
caea99ef
RW
404 Close:
405 platform_end(platform_mode);
7777fab9 406 return error;
d8f3de0d 407
51d6ff7a
SB
408 Thaw:
409 thaw_kernel_threads();
bb58dd5d
RW
410 Cleanup:
411 swsusp_free();
412 goto Close;
7777fab9
RW
413}
414
406f992e
RW
415int __weak hibernate_resume_nonboot_cpu_disable(void)
416{
417 return disable_nonboot_cpus();
418}
419
72df68ca 420/**
f42a9813
RW
421 * resume_target_kernel - Restore system state from a hibernation image.
422 * @platform_mode: Whether or not to use the platform driver.
423 *
cf579dfb
RW
424 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
425 * contents of highmem that have not been restored yet from the image and run
426 * the low-level code that will restore the remaining contents of memory and
427 * switch to the just restored target kernel.
72df68ca 428 */
4aecd671 429static int resume_target_kernel(bool platform_mode)
72df68ca
RW
430{
431 int error;
432
cf579dfb 433 error = dpm_suspend_end(PMSG_QUIESCE);
72df68ca 434 if (error) {
23976728 435 printk(KERN_ERR "PM: Some devices failed to power down, "
72df68ca 436 "aborting resume\n");
32bdfac5 437 return error;
72df68ca 438 }
2ed8d2b3 439
4aecd671
RW
440 error = platform_pre_restore(platform_mode);
441 if (error)
442 goto Cleanup;
443
406f992e 444 error = hibernate_resume_nonboot_cpu_disable();
4aecd671
RW
445 if (error)
446 goto Enable_cpus;
447
2ed8d2b3
RW
448 local_irq_disable();
449
2e711c04 450 error = syscore_suspend();
4aecd671
RW
451 if (error)
452 goto Enable_irqs;
453
72df68ca
RW
454 save_processor_state();
455 error = restore_highmem();
456 if (!error) {
457 error = swsusp_arch_resume();
458 /*
459 * The code below is only ever reached in case of a failure.
4e2d9491
RW
460 * Otherwise, execution continues at the place where
461 * swsusp_arch_suspend() was called.
72df68ca
RW
462 */
463 BUG_ON(!error);
4e2d9491
RW
464 /*
465 * This call to restore_highmem() reverts the changes made by
466 * the previous one.
467 */
72df68ca
RW
468 restore_highmem();
469 }
470 /*
471 * The only reason why swsusp_arch_resume() can fail is memory being
472 * very tight, so we have to free it as soon as we can to avoid
4e2d9491 473 * subsequent failures.
72df68ca
RW
474 */
475 swsusp_free();
476 restore_processor_state();
477 touch_softlockup_watchdog();
2ed8d2b3 478
40dc166c 479 syscore_resume();
2ed8d2b3 480
4aecd671 481 Enable_irqs:
72df68ca 482 local_irq_enable();
2ed8d2b3 483
4aecd671
RW
484 Enable_cpus:
485 enable_nonboot_cpus();
486
487 Cleanup:
488 platform_restore_cleanup(platform_mode);
489
cf579dfb 490 dpm_resume_start(PMSG_RECOVER);
2ed8d2b3 491
72df68ca
RW
492 return error;
493}
494
7777fab9 495/**
f42a9813
RW
496 * hibernation_restore - Quiesce devices and restore from a hibernation image.
497 * @platform_mode: If set, use platform driver to prepare for the transition.
7777fab9 498 *
f42a9813 499 * This routine must be called with pm_mutex held. If it is successful, control
21e82808 500 * reappears in the restored target kernel in hibernation_snapshot().
7777fab9 501 */
a634cc10 502int hibernation_restore(int platform_mode)
7777fab9 503{
cbe2f5a6 504 int error;
7777fab9
RW
505
506 pm_prepare_console();
507 suspend_console();
c9e664f1 508 pm_restrict_gfp_mask();
d1616302 509 error = dpm_suspend_start(PMSG_QUIESCE);
a634cc10 510 if (!error) {
4aecd671 511 error = resume_target_kernel(platform_mode);
94fb823f
ID
512 /*
513 * The above should either succeed and jump to the new kernel,
514 * or return with an error. Otherwise things are just
515 * undefined, so let's be paranoid.
516 */
517 BUG_ON(!error);
a634cc10 518 }
94fb823f 519 dpm_resume_end(PMSG_RECOVER);
c9e664f1 520 pm_restore_gfp_mask();
7777fab9
RW
521 resume_console();
522 pm_restore_console();
523 return error;
524}
525
526/**
f42a9813 527 * hibernation_platform_enter - Power off the system using the platform driver.
7777fab9 528 */
7777fab9
RW
529int hibernation_platform_enter(void)
530{
cbe2f5a6 531 int error;
b1457bcc 532
9cd9a005
RW
533 if (!hibernation_ops)
534 return -ENOSYS;
535
536 /*
537 * We have cancelled the power transition by running
538 * hibernation_ops->finish() before saving the image, so we should let
539 * the firmware know that we're going to enter the sleep state after all
540 */
caea99ef 541 error = hibernation_ops->begin();
9cd9a005 542 if (error)
caea99ef 543 goto Close;
9cd9a005 544
abfe2d7b 545 entering_platform_hibernation = true;
9cd9a005 546 suspend_console();
d1616302 547 error = dpm_suspend_start(PMSG_HIBERNATE);
d8f3de0d
RW
548 if (error) {
549 if (hibernation_ops->recover)
550 hibernation_ops->recover();
551 goto Resume_devices;
552 }
9cd9a005 553
cf579dfb 554 error = dpm_suspend_end(PMSG_HIBERNATE);
4aecd671 555 if (error)
32bdfac5 556 goto Resume_devices;
4aecd671 557
9cd9a005
RW
558 error = hibernation_ops->prepare();
559 if (error)
e681c9dd 560 goto Platform_finish;
9cd9a005
RW
561
562 error = disable_nonboot_cpus();
563 if (error)
8c506608 564 goto Enable_cpus;
2ed8d2b3 565
4aecd671 566 local_irq_disable();
40dc166c 567 syscore_suspend();
a2867e08 568 if (pm_wakeup_pending()) {
c125e96f
RW
569 error = -EAGAIN;
570 goto Power_up;
571 }
572
4aecd671
RW
573 hibernation_ops->enter();
574 /* We should never get here */
575 while (1);
9cd9a005 576
c125e96f 577 Power_up:
40dc166c 578 syscore_resume();
c125e96f 579 local_irq_enable();
8c506608
VK
580
581 Enable_cpus:
c125e96f
RW
582 enable_nonboot_cpus();
583
e681c9dd 584 Platform_finish:
9cd9a005 585 hibernation_ops->finish();
2ed8d2b3 586
cf579dfb 587 dpm_resume_start(PMSG_RESTORE);
4aecd671 588
9cd9a005 589 Resume_devices:
abfe2d7b 590 entering_platform_hibernation = false;
d1616302 591 dpm_resume_end(PMSG_RESTORE);
9cd9a005 592 resume_console();
2ed8d2b3 593
caea99ef
RW
594 Close:
595 hibernation_ops->end();
2ed8d2b3 596
b1457bcc 597 return error;
7777fab9
RW
598}
599
1da177e4 600/**
f42a9813 601 * power_down - Shut the machine down for hibernation.
1da177e4 602 *
f42a9813
RW
603 * Use the platform driver, if configured, to put the system into the sleep
604 * state corresponding to hibernation, or try to power it off or reboot,
605 * depending on the value of hibernation_mode.
1da177e4 606 */
fe0c935a 607static void power_down(void)
1da177e4 608{
62c552cc
BS
609#ifdef CONFIG_SUSPEND
610 int error;
611#endif
612
a3d25c27 613 switch (hibernation_mode) {
a3d25c27 614 case HIBERNATION_REBOOT:
fdde86ac 615 kernel_restart(NULL);
1da177e4 616 break;
a3d25c27 617 case HIBERNATION_PLATFORM:
7777fab9 618 hibernation_platform_enter();
9cd9a005 619 case HIBERNATION_SHUTDOWN:
2c730785
SC
620 if (pm_power_off)
621 kernel_power_off();
9cd9a005 622 break;
62c552cc
BS
623#ifdef CONFIG_SUSPEND
624 case HIBERNATION_SUSPEND:
625 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
626 if (error) {
627 if (hibernation_ops)
628 hibernation_mode = HIBERNATION_PLATFORM;
629 else
630 hibernation_mode = HIBERNATION_SHUTDOWN;
631 power_down();
632 }
633 /*
634 * Restore swap signature.
635 */
636 error = swsusp_unmark();
637 if (error)
638 printk(KERN_ERR "PM: Swap will be unusable! "
639 "Try swapon -a.\n");
640 return;
641#endif
1da177e4 642 }
fdde86ac 643 kernel_halt();
fe0c935a
JB
644 /*
645 * Valid image is on the disk, if we continue we risk serious data
646 * corruption after resume.
647 */
23976728 648 printk(KERN_CRIT "PM: Please power down manually\n");
2c730785
SC
649 while (1)
650 cpu_relax();
1da177e4
LT
651}
652
fe12c00d
CY
653static int load_image_and_restore(void)
654{
655 int error;
656 unsigned int flags;
657
658 pr_debug("PM: Loading hibernation image.\n");
659
660 lock_device_hotplug();
661 error = create_basic_memory_bitmaps();
662 if (error)
663 goto Unlock;
664
665 error = swsusp_read(&flags);
666 swsusp_close(FMODE_READ);
667 if (!error)
668 hibernation_restore(flags & SF_PLATFORM_MODE);
669
670 printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n");
671 swsusp_free();
672 free_basic_memory_bitmaps();
673 Unlock:
674 unlock_device_hotplug();
675
676 return error;
677}
678
1da177e4 679/**
f42a9813 680 * hibernate - Carry out system hibernation, including saving the image.
1da177e4 681 */
a3d25c27 682int hibernate(void)
1da177e4 683{
ea00f4f4 684 int error, nr_calls = 0;
fe12c00d 685 bool snapshot_test = false;
1da177e4 686
a6e15a39
KC
687 if (!hibernation_available()) {
688 pr_debug("PM: Hibernation not available.\n");
689 return -EPERM;
690 }
691
bcda53fa 692 lock_system_sleep();
0709db60 693 /* The snapshot device should not be opened while we're running */
b10d9117
RW
694 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
695 error = -EBUSY;
696 goto Unlock;
697 }
698
5a0a2f30 699 pm_prepare_console();
ea00f4f4
LW
700 error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
701 if (error) {
702 nr_calls--;
b10d9117 703 goto Exit;
ea00f4f4 704 }
0709db60 705
23976728 706 printk(KERN_INFO "PM: Syncing filesystems ... ");
232b1432
RW
707 sys_sync();
708 printk("done.\n");
709
03afed8b 710 error = freeze_processes();
5a72e04d 711 if (error)
8fd37a4c
RW
712 goto Exit;
713
942f4015 714 lock_device_hotplug();
8fd37a4c
RW
715 /* Allocate memory management structures */
716 error = create_basic_memory_bitmaps();
717 if (error)
718 goto Thaw;
1da177e4 719
7777fab9 720 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
a556d5b5 721 if (error || freezer_test_done)
8fd37a4c 722 goto Free_bitmaps;
64a473cb
RW
723
724 if (in_suspend) {
a634cc10
RW
725 unsigned int flags = 0;
726
727 if (hibernation_mode == HIBERNATION_PLATFORM)
728 flags |= SF_PLATFORM_MODE;
f996fc96
BS
729 if (nocompress)
730 flags |= SF_NOCOMPRESS_MODE;
081a9d04
BS
731 else
732 flags |= SF_CRC32_MODE;
733
1da177e4 734 pr_debug("PM: writing image.\n");
a634cc10 735 error = swsusp_write(flags);
7777fab9 736 swsusp_free();
fe12c00d
CY
737 if (!error) {
738 if (hibernation_mode == HIBERNATION_TEST_RESUME)
739 snapshot_test = true;
740 else
741 power_down();
742 }
5262a475 743 in_suspend = 0;
c9e664f1 744 pm_restore_gfp_mask();
b918f6e6 745 } else {
1da177e4 746 pr_debug("PM: Image restored successfully.\n");
b918f6e6 747 }
64a473cb 748
8fd37a4c
RW
749 Free_bitmaps:
750 free_basic_memory_bitmaps();
b918f6e6 751 Thaw:
942f4015 752 unlock_device_hotplug();
fe12c00d
CY
753 if (snapshot_test) {
754 pr_debug("PM: Checking hibernation image\n");
755 error = swsusp_check();
756 if (!error)
757 error = load_image_and_restore();
758 }
5a0a2f30 759 thaw_processes();
a556d5b5
SB
760
761 /* Don't bother checking whether freezer_test_done is true */
762 freezer_test_done = false;
0709db60 763 Exit:
ea00f4f4 764 __pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL);
5a0a2f30 765 pm_restore_console();
0709db60 766 atomic_inc(&snapshot_device_available);
b10d9117 767 Unlock:
bcda53fa 768 unlock_system_sleep();
1da177e4
LT
769 return error;
770}
771
772
773/**
f42a9813
RW
774 * software_resume - Resume from a saved hibernation image.
775 *
776 * This routine is called as a late initcall, when all devices have been
777 * discovered and initialized already.
1da177e4 778 *
f42a9813
RW
779 * The image reading code is called to see if there is a hibernation image
780 * available for reading. If that is the case, devices are quiesced and the
781 * contents of memory is restored from the saved image.
1da177e4 782 *
f42a9813 783 * If this is successful, control reappears in the restored target kernel in
d439e64f 784 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
f42a9813
RW
785 * attempts to recover gracefully and make the kernel return to the normal mode
786 * of operation.
1da177e4 787 */
1da177e4
LT
788static int software_resume(void)
789{
ea00f4f4 790 int error, nr_calls = 0;
1da177e4 791
eed3ee08
AV
792 /*
793 * If the user said "noresume".. bail out early.
794 */
a6e15a39 795 if (noresume || !hibernation_available())
eed3ee08
AV
796 return 0;
797
60a0d233
JB
798 /*
799 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
800 * is configured into the kernel. Since the regular hibernate
801 * trigger path is via sysfs which takes a buffer mutex before
802 * calling hibernate functions (which take pm_mutex) this can
803 * cause lockdep to complain about a possible ABBA deadlock
804 * which cannot happen since we're in the boot code here and
805 * sysfs can't be invoked yet. Therefore, we use a subclass
806 * here to avoid lockdep complaining.
807 */
808 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
0c8454f5
RW
809
810 if (swsusp_resume_device)
811 goto Check_image;
812
813 if (!strlen(resume_file)) {
814 error = -ENOENT;
815 goto Unlock;
816 }
817
d0941ead 818 pr_debug("PM: Checking hibernation image partition %s\n", resume_file);
0c8454f5 819
f126f733
BS
820 if (resume_delay) {
821 printk(KERN_INFO "Waiting %dsec before reading resume device...\n",
822 resume_delay);
823 ssleep(resume_delay);
824 }
825
0c8454f5
RW
826 /* Check if the device is there */
827 swsusp_resume_device = name_to_dev_t(resume_file);
2df83fa4
MB
828
829 /*
830 * name_to_dev_t is ineffective to verify parition if resume_file is in
831 * integer format. (e.g. major:minor)
832 */
833 if (isdigit(resume_file[0]) && resume_wait) {
834 int partno;
835 while (!get_gendisk(swsusp_resume_device, &partno))
836 msleep(10);
837 }
838
3efa147a 839 if (!swsusp_resume_device) {
eed3ee08
AV
840 /*
841 * Some device discovery might still be in progress; we need
842 * to wait for this to finish.
843 */
844 wait_for_device_probe();
6f8d7022
BS
845
846 if (resume_wait) {
847 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
848 msleep(10);
849 async_synchronize_full();
850 }
851
3efa147a 852 swsusp_resume_device = name_to_dev_t(resume_file);
0c8454f5
RW
853 if (!swsusp_resume_device) {
854 error = -ENODEV;
855 goto Unlock;
856 }
3efa147a
PM
857 }
858
0c8454f5 859 Check_image:
d0941ead 860 pr_debug("PM: Hibernation image partition %d:%d present\n",
0c8454f5 861 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
1da177e4 862
d0941ead 863 pr_debug("PM: Looking for hibernation image.\n");
ed746e3b
RW
864 error = swsusp_check();
865 if (error)
74dfd666 866 goto Unlock;
1da177e4 867
0709db60
RW
868 /* The snapshot device should not be opened while we're running */
869 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
870 error = -EBUSY;
76b57e61 871 swsusp_close(FMODE_READ);
0709db60
RW
872 goto Unlock;
873 }
874
5a0a2f30 875 pm_prepare_console();
ea00f4f4
LW
876 error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
877 if (error) {
878 nr_calls--;
8fd37a4c 879 goto Close_Finish;
ea00f4f4 880 }
1bfcf130 881
74dfd666 882 pr_debug("PM: Preparing processes for restore.\n");
03afed8b 883 error = freeze_processes();
8fd37a4c
RW
884 if (error)
885 goto Close_Finish;
fe12c00d 886 error = load_image_and_restore();
8fd37a4c 887 thaw_processes();
0709db60 888 Finish:
ea00f4f4 889 __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
5a0a2f30 890 pm_restore_console();
0709db60 891 atomic_inc(&snapshot_device_available);
dd5d666b 892 /* For success case, the suspend path will release the lock */
74dfd666 893 Unlock:
a6d70980 894 mutex_unlock(&pm_mutex);
d0941ead 895 pr_debug("PM: Hibernation image not present or could not be loaded.\n");
7777fab9 896 return error;
8fd37a4c 897 Close_Finish:
76b57e61
JS
898 swsusp_close(FMODE_READ);
899 goto Finish;
1da177e4
LT
900}
901
d3c345db 902late_initcall_sync(software_resume);
1da177e4
LT
903
904
a3d25c27
RW
905static const char * const hibernation_modes[] = {
906 [HIBERNATION_PLATFORM] = "platform",
907 [HIBERNATION_SHUTDOWN] = "shutdown",
908 [HIBERNATION_REBOOT] = "reboot",
62c552cc
BS
909#ifdef CONFIG_SUSPEND
910 [HIBERNATION_SUSPEND] = "suspend",
911#endif
fe12c00d 912 [HIBERNATION_TEST_RESUME] = "test_resume",
1da177e4
LT
913};
914
f42a9813
RW
915/*
916 * /sys/power/disk - Control hibernation mode.
1da177e4 917 *
f42a9813
RW
918 * Hibernation can be handled in several ways. There are a few different ways
919 * to put the system into the sleep state: using the platform driver (e.g. ACPI
920 * or other hibernation_ops), powering it off or rebooting it (for testing
48580ab8 921 * mostly).
1da177e4 922 *
f42a9813
RW
923 * The sysfs file /sys/power/disk provides an interface for selecting the
924 * hibernation mode to use. Reading from this file causes the available modes
48580ab8 925 * to be printed. There are 3 modes that can be supported:
1da177e4 926 *
1da177e4
LT
927 * 'platform'
928 * 'shutdown'
929 * 'reboot'
930 *
f42a9813
RW
931 * If a platform hibernation driver is in use, 'platform' will be supported
932 * and will be used by default. Otherwise, 'shutdown' will be used by default.
933 * The selected option (i.e. the one corresponding to the current value of
934 * hibernation_mode) is enclosed by a square bracket.
935 *
936 * To select a given hibernation mode it is necessary to write the mode's
937 * string representation (as returned by reading from /sys/power/disk) back
938 * into /sys/power/disk.
1da177e4
LT
939 */
940
386f275f
KS
941static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
942 char *buf)
1da177e4 943{
f0ced9b2
JB
944 int i;
945 char *start = buf;
946
a6e15a39
KC
947 if (!hibernation_available())
948 return sprintf(buf, "[disabled]\n");
949
a3d25c27
RW
950 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
951 if (!hibernation_modes[i])
f0ced9b2
JB
952 continue;
953 switch (i) {
a3d25c27
RW
954 case HIBERNATION_SHUTDOWN:
955 case HIBERNATION_REBOOT:
62c552cc
BS
956#ifdef CONFIG_SUSPEND
957 case HIBERNATION_SUSPEND:
958#endif
fe12c00d 959 case HIBERNATION_TEST_RESUME:
f0ced9b2 960 break;
a3d25c27
RW
961 case HIBERNATION_PLATFORM:
962 if (hibernation_ops)
f0ced9b2
JB
963 break;
964 /* not a valid mode, continue with loop */
965 continue;
966 }
a3d25c27
RW
967 if (i == hibernation_mode)
968 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
f0ced9b2 969 else
a3d25c27 970 buf += sprintf(buf, "%s ", hibernation_modes[i]);
f0ced9b2
JB
971 }
972 buf += sprintf(buf, "\n");
973 return buf-start;
1da177e4
LT
974}
975
386f275f
KS
976static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
977 const char *buf, size_t n)
1da177e4
LT
978{
979 int error = 0;
980 int i;
981 int len;
982 char *p;
a3d25c27 983 int mode = HIBERNATION_INVALID;
1da177e4 984
a6e15a39
KC
985 if (!hibernation_available())
986 return -EPERM;
987
1da177e4
LT
988 p = memchr(buf, '\n', n);
989 len = p ? p - buf : n;
990
bcda53fa 991 lock_system_sleep();
a3d25c27 992 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
8d98a690
RW
993 if (len == strlen(hibernation_modes[i])
994 && !strncmp(buf, hibernation_modes[i], len)) {
1da177e4
LT
995 mode = i;
996 break;
997 }
998 }
a3d25c27 999 if (mode != HIBERNATION_INVALID) {
fe0c935a 1000 switch (mode) {
a3d25c27
RW
1001 case HIBERNATION_SHUTDOWN:
1002 case HIBERNATION_REBOOT:
62c552cc
BS
1003#ifdef CONFIG_SUSPEND
1004 case HIBERNATION_SUSPEND:
1005#endif
fe12c00d 1006 case HIBERNATION_TEST_RESUME:
a3d25c27 1007 hibernation_mode = mode;
fe0c935a 1008 break;
a3d25c27
RW
1009 case HIBERNATION_PLATFORM:
1010 if (hibernation_ops)
1011 hibernation_mode = mode;
1da177e4
LT
1012 else
1013 error = -EINVAL;
1014 }
a3d25c27 1015 } else
1da177e4
LT
1016 error = -EINVAL;
1017
a3d25c27 1018 if (!error)
23976728 1019 pr_debug("PM: Hibernation mode set to '%s'\n",
a3d25c27 1020 hibernation_modes[mode]);
bcda53fa 1021 unlock_system_sleep();
1da177e4
LT
1022 return error ? error : n;
1023}
1024
1025power_attr(disk);
1026
386f275f
KS
1027static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1028 char *buf)
1da177e4
LT
1029{
1030 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
1031 MINOR(swsusp_resume_device));
1032}
1033
386f275f
KS
1034static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1035 const char *buf, size_t n)
1da177e4 1036{
1da177e4 1037 dev_t res;
421a5fa1
SC
1038 int len = n;
1039 char *name;
1da177e4 1040
421a5fa1
SC
1041 if (len && buf[len-1] == '\n')
1042 len--;
1043 name = kstrndup(buf, len, GFP_KERNEL);
1044 if (!name)
1045 return -ENOMEM;
1da177e4 1046
421a5fa1
SC
1047 res = name_to_dev_t(name);
1048 kfree(name);
1049 if (!res)
1050 return -EINVAL;
1da177e4 1051
bcda53fa 1052 lock_system_sleep();
a576219a 1053 swsusp_resume_device = res;
bcda53fa 1054 unlock_system_sleep();
23976728 1055 printk(KERN_INFO "PM: Starting manual resume from disk\n");
a576219a
AM
1056 noresume = 0;
1057 software_resume();
421a5fa1 1058 return n;
1da177e4
LT
1059}
1060
1061power_attr(resume);
1062
386f275f
KS
1063static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1064 char *buf)
ca0aec0f 1065{
853609b6 1066 return sprintf(buf, "%lu\n", image_size);
ca0aec0f
RW
1067}
1068
386f275f
KS
1069static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1070 const char *buf, size_t n)
ca0aec0f 1071{
853609b6 1072 unsigned long size;
ca0aec0f 1073
853609b6 1074 if (sscanf(buf, "%lu", &size) == 1) {
ca0aec0f
RW
1075 image_size = size;
1076 return n;
1077 }
1078
1079 return -EINVAL;
1080}
1081
1082power_attr(image_size);
1083
ddeb6487
RW
1084static ssize_t reserved_size_show(struct kobject *kobj,
1085 struct kobj_attribute *attr, char *buf)
1086{
1087 return sprintf(buf, "%lu\n", reserved_size);
1088}
1089
1090static ssize_t reserved_size_store(struct kobject *kobj,
1091 struct kobj_attribute *attr,
1092 const char *buf, size_t n)
1093{
1094 unsigned long size;
1095
1096 if (sscanf(buf, "%lu", &size) == 1) {
1097 reserved_size = size;
1098 return n;
1099 }
1100
1101 return -EINVAL;
1102}
1103
1104power_attr(reserved_size);
1105
1da177e4
LT
1106static struct attribute * g[] = {
1107 &disk_attr.attr,
1108 &resume_attr.attr,
ca0aec0f 1109 &image_size_attr.attr,
ddeb6487 1110 &reserved_size_attr.attr,
1da177e4
LT
1111 NULL,
1112};
1113
1114
1115static struct attribute_group attr_group = {
1116 .attrs = g,
1117};
1118
1119
1120static int __init pm_disk_init(void)
1121{
d76e15fb 1122 return sysfs_create_group(power_kobj, &attr_group);
1da177e4
LT
1123}
1124
1125core_initcall(pm_disk_init);
1126
1127
1128static int __init resume_setup(char *str)
1129{
1130 if (noresume)
1131 return 1;
1132
1133 strncpy( resume_file, str, 255 );
1134 return 1;
1135}
1136
9a154d9d
RW
1137static int __init resume_offset_setup(char *str)
1138{
1139 unsigned long long offset;
1140
1141 if (noresume)
1142 return 1;
1143
1144 if (sscanf(str, "%llu", &offset) == 1)
1145 swsusp_resume_block = offset;
1146
1147 return 1;
1148}
1149
f996fc96
BS
1150static int __init hibernate_setup(char *str)
1151{
2f88e41a 1152 if (!strncmp(str, "noresume", 8)) {
f996fc96 1153 noresume = 1;
2f88e41a 1154 } else if (!strncmp(str, "nocompress", 10)) {
f996fc96 1155 nocompress = 1;
2f88e41a 1156 } else if (!strncmp(str, "no", 2)) {
a6e15a39
KC
1157 noresume = 1;
1158 nohibernate = 1;
4c0b6c10
RW
1159 } else if (IS_ENABLED(CONFIG_DEBUG_RODATA)
1160 && !strncmp(str, "protect_image", 13)) {
1161 enable_restore_image_protection();
a6e15a39 1162 }
f996fc96
BS
1163 return 1;
1164}
1165
1da177e4
LT
1166static int __init noresume_setup(char *str)
1167{
1168 noresume = 1;
1169 return 1;
1170}
1171
6f8d7022
BS
1172static int __init resumewait_setup(char *str)
1173{
1174 resume_wait = 1;
1175 return 1;
1176}
1177
f126f733
BS
1178static int __init resumedelay_setup(char *str)
1179{
f6514be5 1180 int rc = kstrtouint(str, 0, &resume_delay);
317cf7e5
FF
1181
1182 if (rc)
1183 return rc;
f126f733
BS
1184 return 1;
1185}
1186
a6e15a39
KC
1187static int __init nohibernate_setup(char *str)
1188{
1189 noresume = 1;
1190 nohibernate = 1;
1191 return 1;
1192}
1193
1da177e4 1194__setup("noresume", noresume_setup);
9a154d9d 1195__setup("resume_offset=", resume_offset_setup);
1da177e4 1196__setup("resume=", resume_setup);
f996fc96 1197__setup("hibernate=", hibernate_setup);
6f8d7022 1198__setup("resumewait", resumewait_setup);
f126f733 1199__setup("resumedelay=", resumedelay_setup);
a6e15a39 1200__setup("nohibernate", nohibernate_setup);