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