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