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