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