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