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