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