]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/power/hibernate.c
swap: revert special hibernation allocation
[net-next-2.6.git] / kernel / power / hibernate.c
CommitLineData
1da177e4 1/*
8b759b84 2 * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
1da177e4
LT
3 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
a2531293 6 * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz>
8b759b84 7 * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
1da177e4
LT
8 *
9 * This file is released under the GPLv2.
1da177e4
LT
10 */
11
12#include <linux/suspend.h>
13#include <linux/syscalls.h>
14#include <linux/reboot.h>
15#include <linux/string.h>
16#include <linux/device.h>
1bfcf130 17#include <linux/kmod.h>
1da177e4
LT
18#include <linux/delay.h>
19#include <linux/fs.h>
d53d9f16 20#include <linux/mount.h>
88d10bba 21#include <linux/pm.h>
97c7801c 22#include <linux/console.h>
e3920fb4 23#include <linux/cpu.h>
7dfb7103 24#include <linux/freezer.h>
5a0e3ad6 25#include <linux/gfp.h>
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
c125e96f 280 if (hibernation_test(TEST_CORE) || !pm_check_wakeup_events())
4cc79776
RW
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();
c125e96f
RW
291 if (!in_suspend) {
292 events_check_enabled = false;
c7e0831d 293 platform_leave(platform_mode);
c125e96f 294 }
4aecd671 295
4cc79776 296 Power_up:
770824bd 297 sysdev_resume();
d1616302 298 /* NOTE: dpm_resume_noirq() is just a resume() for devices
c7e0831d
RW
299 * that suspended with irqs off ... no overall powerup.
300 */
2ed8d2b3 301
4aecd671 302 Enable_irqs:
2ed8d2b3
RW
303 local_irq_enable();
304
4aecd671
RW
305 Enable_cpus:
306 enable_nonboot_cpus();
307
308 Platform_finish:
309 platform_finish(platform_mode);
310
d1616302 311 dpm_resume_noirq(in_suspend ?
1eede070 312 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
2ed8d2b3 313
c7e0831d
RW
314 return error;
315}
316
7777fab9
RW
317/**
318 * hibernation_snapshot - quiesce devices and create the hibernation
319 * snapshot image.
320 * @platform_mode - if set, use the platform driver, if available, to
877d0310 321 * prepare the platform firmware for the power transition.
7777fab9
RW
322 *
323 * Must be called with pm_mutex held
324 */
325
326int hibernation_snapshot(int platform_mode)
327{
cbe2f5a6 328 int error;
452aa699 329 gfp_t saved_mask;
7777fab9 330
3fe0313e 331 error = platform_begin(platform_mode);
7777fab9 332 if (error)
d074ee02 333 goto Close;
7777fab9 334
64a473cb
RW
335 /* Preallocate image memory before shutting down devices. */
336 error = hibernate_preallocate_memory();
74f270af 337 if (error)
caea99ef 338 goto Close;
74f270af 339
7777fab9 340 suspend_console();
452aa699 341 saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
d1616302 342 error = dpm_suspend_start(PMSG_FREEZE);
10a1803d 343 if (error)
d8f3de0d 344 goto Recover_platform;
10a1803d 345
4cc79776 346 if (hibernation_test(TEST_DEVICES))
d8f3de0d 347 goto Recover_platform;
7777fab9 348
4aecd671
RW
349 error = create_image(platform_mode);
350 /* Control returns here after successful restore */
4cc79776 351
4cc79776 352 Resume_devices:
64a473cb
RW
353 /* We may need to release the preallocated image pages here. */
354 if (error || !in_suspend)
355 swsusp_free();
356
d1616302 357 dpm_resume_end(in_suspend ?
1eede070 358 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
452aa699 359 set_gfp_allowed_mask(saved_mask);
7777fab9 360 resume_console();
caea99ef
RW
361 Close:
362 platform_end(platform_mode);
7777fab9 363 return error;
d8f3de0d
RW
364
365 Recover_platform:
366 platform_recover(platform_mode);
367 goto Resume_devices;
7777fab9
RW
368}
369
72df68ca
RW
370/**
371 * resume_target_kernel - prepare devices that need to be suspended with
372 * interrupts off, restore the contents of highmem that have not been
373 * restored yet from the image and run the low level code that will restore
374 * the remaining contents of memory and switch to the just restored target
375 * kernel.
376 */
377
4aecd671 378static int resume_target_kernel(bool platform_mode)
72df68ca
RW
379{
380 int error;
381
d1616302 382 error = dpm_suspend_noirq(PMSG_QUIESCE);
72df68ca 383 if (error) {
23976728 384 printk(KERN_ERR "PM: Some devices failed to power down, "
72df68ca 385 "aborting resume\n");
32bdfac5 386 return error;
72df68ca 387 }
2ed8d2b3 388
4aecd671
RW
389 error = platform_pre_restore(platform_mode);
390 if (error)
391 goto Cleanup;
392
393 error = disable_nonboot_cpus();
394 if (error)
395 goto Enable_cpus;
396
2ed8d2b3
RW
397 local_irq_disable();
398
4aecd671
RW
399 error = sysdev_suspend(PMSG_QUIESCE);
400 if (error)
401 goto Enable_irqs;
402
72df68ca
RW
403 /* We'll ignore saved state, but this gets preempt count (etc) right */
404 save_processor_state();
405 error = restore_highmem();
406 if (!error) {
407 error = swsusp_arch_resume();
408 /*
409 * The code below is only ever reached in case of a failure.
410 * Otherwise execution continues at place where
411 * swsusp_arch_suspend() was called
412 */
413 BUG_ON(!error);
414 /* This call to restore_highmem() undos the previous one */
415 restore_highmem();
416 }
417 /*
418 * The only reason why swsusp_arch_resume() can fail is memory being
419 * very tight, so we have to free it as soon as we can to avoid
420 * subsequent failures
421 */
422 swsusp_free();
423 restore_processor_state();
424 touch_softlockup_watchdog();
2ed8d2b3 425
770824bd 426 sysdev_resume();
2ed8d2b3 427
4aecd671 428 Enable_irqs:
72df68ca 429 local_irq_enable();
2ed8d2b3 430
4aecd671
RW
431 Enable_cpus:
432 enable_nonboot_cpus();
433
434 Cleanup:
435 platform_restore_cleanup(platform_mode);
436
d1616302 437 dpm_resume_noirq(PMSG_RECOVER);
2ed8d2b3 438
72df68ca
RW
439 return error;
440}
441
7777fab9
RW
442/**
443 * hibernation_restore - quiesce devices and restore the hibernation
444 * snapshot image. If successful, control returns in hibernation_snaphot()
a634cc10 445 * @platform_mode - if set, use the platform driver, if available, to
877d0310 446 * prepare the platform firmware for the transition.
7777fab9
RW
447 *
448 * Must be called with pm_mutex held
449 */
450
a634cc10 451int hibernation_restore(int platform_mode)
7777fab9 452{
cbe2f5a6 453 int error;
452aa699 454 gfp_t saved_mask;
7777fab9
RW
455
456 pm_prepare_console();
457 suspend_console();
452aa699 458 saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
d1616302 459 error = dpm_suspend_start(PMSG_QUIESCE);
a634cc10 460 if (!error) {
4aecd671 461 error = resume_target_kernel(platform_mode);
d1616302 462 dpm_resume_end(PMSG_RECOVER);
a634cc10 463 }
452aa699 464 set_gfp_allowed_mask(saved_mask);
7777fab9
RW
465 resume_console();
466 pm_restore_console();
467 return error;
468}
469
470/**
471 * hibernation_platform_enter - enter the hibernation state using the
472 * platform driver (if available)
473 */
474
475int hibernation_platform_enter(void)
476{
cbe2f5a6 477 int error;
452aa699 478 gfp_t saved_mask;
b1457bcc 479
9cd9a005
RW
480 if (!hibernation_ops)
481 return -ENOSYS;
482
483 /*
484 * We have cancelled the power transition by running
485 * hibernation_ops->finish() before saving the image, so we should let
486 * the firmware know that we're going to enter the sleep state after all
487 */
caea99ef 488 error = hibernation_ops->begin();
9cd9a005 489 if (error)
caea99ef 490 goto Close;
9cd9a005 491
abfe2d7b 492 entering_platform_hibernation = true;
9cd9a005 493 suspend_console();
452aa699 494 saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
d1616302 495 error = dpm_suspend_start(PMSG_HIBERNATE);
d8f3de0d
RW
496 if (error) {
497 if (hibernation_ops->recover)
498 hibernation_ops->recover();
499 goto Resume_devices;
500 }
9cd9a005 501
d1616302 502 error = dpm_suspend_noirq(PMSG_HIBERNATE);
4aecd671 503 if (error)
32bdfac5 504 goto Resume_devices;
4aecd671 505
9cd9a005
RW
506 error = hibernation_ops->prepare();
507 if (error)
e681c9dd 508 goto Platform_finish;
9cd9a005
RW
509
510 error = disable_nonboot_cpus();
511 if (error)
e681c9dd 512 goto Platform_finish;
2ed8d2b3 513
4aecd671
RW
514 local_irq_disable();
515 sysdev_suspend(PMSG_HIBERNATE);
c125e96f
RW
516 if (!pm_check_wakeup_events()) {
517 error = -EAGAIN;
518 goto Power_up;
519 }
520
4aecd671
RW
521 hibernation_ops->enter();
522 /* We should never get here */
523 while (1);
9cd9a005 524
c125e96f
RW
525 Power_up:
526 sysdev_resume();
527 local_irq_enable();
528 enable_nonboot_cpus();
529
e681c9dd 530 Platform_finish:
9cd9a005 531 hibernation_ops->finish();
2ed8d2b3 532
f6f71f18 533 dpm_resume_noirq(PMSG_RESTORE);
4aecd671 534
9cd9a005 535 Resume_devices:
abfe2d7b 536 entering_platform_hibernation = false;
d1616302 537 dpm_resume_end(PMSG_RESTORE);
452aa699 538 set_gfp_allowed_mask(saved_mask);
9cd9a005 539 resume_console();
2ed8d2b3 540
caea99ef
RW
541 Close:
542 hibernation_ops->end();
2ed8d2b3 543
b1457bcc 544 return error;
7777fab9
RW
545}
546
1da177e4 547/**
a3d25c27 548 * power_down - Shut the machine down for hibernation.
1da177e4 549 *
fe0c935a
JB
550 * Use the platform driver, if configured so; otherwise try
551 * to power off or reboot.
1da177e4
LT
552 */
553
fe0c935a 554static void power_down(void)
1da177e4 555{
a3d25c27
RW
556 switch (hibernation_mode) {
557 case HIBERNATION_TEST:
558 case HIBERNATION_TESTPROC:
fe0c935a 559 break;
a3d25c27 560 case HIBERNATION_REBOOT:
fdde86ac 561 kernel_restart(NULL);
1da177e4 562 break;
a3d25c27 563 case HIBERNATION_PLATFORM:
7777fab9 564 hibernation_platform_enter();
9cd9a005
RW
565 case HIBERNATION_SHUTDOWN:
566 kernel_power_off();
567 break;
1da177e4 568 }
fdde86ac 569 kernel_halt();
fe0c935a
JB
570 /*
571 * Valid image is on the disk, if we continue we risk serious data
572 * corruption after resume.
573 */
23976728 574 printk(KERN_CRIT "PM: Please power down manually\n");
1da177e4
LT
575 while(1);
576}
577
1da177e4
LT
578static int prepare_processes(void)
579{
b918f6e6 580 int error = 0;
1da177e4 581
1da177e4
LT
582 if (freeze_processes()) {
583 error = -EBUSY;
5a0a2f30 584 thaw_processes();
1da177e4 585 }
5a72e04d 586 return error;
1da177e4
LT
587}
588
1da177e4 589/**
a3d25c27 590 * hibernate - The granpappy of the built-in hibernation management
1da177e4
LT
591 */
592
a3d25c27 593int hibernate(void)
1da177e4
LT
594{
595 int error;
596
b10d9117 597 mutex_lock(&pm_mutex);
0709db60 598 /* The snapshot device should not be opened while we're running */
b10d9117
RW
599 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
600 error = -EBUSY;
601 goto Unlock;
602 }
603
5a0a2f30 604 pm_prepare_console();
b10d9117
RW
605 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
606 if (error)
607 goto Exit;
0709db60 608
1bfcf130
RW
609 error = usermodehelper_disable();
610 if (error)
611 goto Exit;
612
0709db60
RW
613 /* Allocate memory management structures */
614 error = create_basic_memory_bitmaps();
615 if (error)
616 goto Exit;
617
23976728 618 printk(KERN_INFO "PM: Syncing filesystems ... ");
232b1432
RW
619 sys_sync();
620 printk("done.\n");
621
1da177e4 622 error = prepare_processes();
5a72e04d 623 if (error)
0709db60 624 goto Finish;
1da177e4 625
4cc79776 626 if (hibernation_test(TEST_FREEZER))
ed746e3b 627 goto Thaw;
4cc79776
RW
628
629 if (hibernation_testmode(HIBERNATION_TESTPROC))
630 goto Thaw;
631
7777fab9 632 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
64a473cb
RW
633 if (error)
634 goto Thaw;
635
636 if (in_suspend) {
a634cc10
RW
637 unsigned int flags = 0;
638
639 if (hibernation_mode == HIBERNATION_PLATFORM)
640 flags |= SF_PLATFORM_MODE;
1da177e4 641 pr_debug("PM: writing image.\n");
a634cc10 642 error = swsusp_write(flags);
7777fab9 643 swsusp_free();
1da177e4 644 if (!error)
fe0c935a 645 power_down();
b918f6e6 646 } else {
1da177e4 647 pr_debug("PM: Image restored successfully.\n");
b918f6e6 648 }
64a473cb 649
b918f6e6 650 Thaw:
5a0a2f30 651 thaw_processes();
0709db60
RW
652 Finish:
653 free_basic_memory_bitmaps();
1bfcf130 654 usermodehelper_enable();
0709db60 655 Exit:
b10d9117 656 pm_notifier_call_chain(PM_POST_HIBERNATION);
5a0a2f30 657 pm_restore_console();
0709db60 658 atomic_inc(&snapshot_device_available);
b10d9117
RW
659 Unlock:
660 mutex_unlock(&pm_mutex);
1da177e4
LT
661 return error;
662}
663
664
665/**
666 * software_resume - Resume from a saved image.
667 *
668 * Called as a late_initcall (so all devices are discovered and
669 * initialized), we call swsusp to see if we have a saved image or not.
670 * If so, we quiesce devices, the restore the saved image. We will
a3d25c27 671 * return above (in hibernate() ) if everything goes well.
1da177e4
LT
672 * Otherwise, we fail gracefully and return to the normally
673 * scheduled program.
674 *
675 */
676
677static int software_resume(void)
678{
679 int error;
a634cc10 680 unsigned int flags;
1da177e4 681
eed3ee08
AV
682 /*
683 * If the user said "noresume".. bail out early.
684 */
685 if (noresume)
686 return 0;
687
60a0d233
JB
688 /*
689 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
690 * is configured into the kernel. Since the regular hibernate
691 * trigger path is via sysfs which takes a buffer mutex before
692 * calling hibernate functions (which take pm_mutex) this can
693 * cause lockdep to complain about a possible ABBA deadlock
694 * which cannot happen since we're in the boot code here and
695 * sysfs can't be invoked yet. Therefore, we use a subclass
696 * here to avoid lockdep complaining.
697 */
698 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
0c8454f5
RW
699
700 if (swsusp_resume_device)
701 goto Check_image;
702
703 if (!strlen(resume_file)) {
704 error = -ENOENT;
705 goto Unlock;
706 }
707
708 pr_debug("PM: Checking image partition %s\n", resume_file);
709
710 /* Check if the device is there */
711 swsusp_resume_device = name_to_dev_t(resume_file);
3efa147a 712 if (!swsusp_resume_device) {
eed3ee08
AV
713 /*
714 * Some device discovery might still be in progress; we need
715 * to wait for this to finish.
716 */
717 wait_for_device_probe();
0c8454f5
RW
718 /*
719 * We can't depend on SCSI devices being available after loading
720 * one of their modules until scsi_complete_async_scans() is
721 * called and the resume device usually is a SCSI one.
722 */
723 scsi_complete_async_scans();
724
3efa147a 725 swsusp_resume_device = name_to_dev_t(resume_file);
0c8454f5
RW
726 if (!swsusp_resume_device) {
727 error = -ENODEV;
728 goto Unlock;
729 }
3efa147a
PM
730 }
731
0c8454f5
RW
732 Check_image:
733 pr_debug("PM: Resume from partition %d:%d\n",
734 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
1da177e4 735
23976728 736 pr_debug("PM: Checking hibernation image.\n");
ed746e3b
RW
737 error = swsusp_check();
738 if (error)
74dfd666 739 goto Unlock;
1da177e4 740
0709db60
RW
741 /* The snapshot device should not be opened while we're running */
742 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
743 error = -EBUSY;
76b57e61 744 swsusp_close(FMODE_READ);
0709db60
RW
745 goto Unlock;
746 }
747
5a0a2f30 748 pm_prepare_console();
c3e94d89
AS
749 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
750 if (error)
76b57e61 751 goto close_finish;
c3e94d89 752
1bfcf130
RW
753 error = usermodehelper_disable();
754 if (error)
76b57e61 755 goto close_finish;
1bfcf130 756
74dfd666
RW
757 error = create_basic_memory_bitmaps();
758 if (error)
76b57e61 759 goto close_finish;
1da177e4 760
74dfd666 761 pr_debug("PM: Preparing processes for restore.\n");
ed746e3b
RW
762 error = prepare_processes();
763 if (error) {
c2dd0dae 764 swsusp_close(FMODE_READ);
5a72e04d 765 goto Done;
1da177e4
LT
766 }
767
23976728 768 pr_debug("PM: Reading hibernation image.\n");
1da177e4 769
a634cc10 770 error = swsusp_read(&flags);
76b57e61 771 swsusp_close(FMODE_READ);
ed746e3b 772 if (!error)
a634cc10 773 hibernation_restore(flags & SF_PLATFORM_MODE);
1da177e4 774
ed746e3b 775 printk(KERN_ERR "PM: Restore failed, recovering.\n");
7777fab9 776 swsusp_free();
5a0a2f30 777 thaw_processes();
1da177e4 778 Done:
74dfd666 779 free_basic_memory_bitmaps();
1bfcf130 780 usermodehelper_enable();
0709db60 781 Finish:
c3e94d89 782 pm_notifier_call_chain(PM_POST_RESTORE);
5a0a2f30 783 pm_restore_console();
0709db60 784 atomic_inc(&snapshot_device_available);
dd5d666b 785 /* For success case, the suspend path will release the lock */
74dfd666 786 Unlock:
a6d70980 787 mutex_unlock(&pm_mutex);
1da177e4 788 pr_debug("PM: Resume from disk failed.\n");
7777fab9 789 return error;
76b57e61
JS
790close_finish:
791 swsusp_close(FMODE_READ);
792 goto Finish;
1da177e4
LT
793}
794
795late_initcall(software_resume);
796
797
a3d25c27
RW
798static const char * const hibernation_modes[] = {
799 [HIBERNATION_PLATFORM] = "platform",
800 [HIBERNATION_SHUTDOWN] = "shutdown",
801 [HIBERNATION_REBOOT] = "reboot",
802 [HIBERNATION_TEST] = "test",
803 [HIBERNATION_TESTPROC] = "testproc",
1da177e4
LT
804};
805
806/**
a3d25c27 807 * disk - Control hibernation mode
1da177e4 808 *
11d77d0c
JB
809 * Suspend-to-disk can be handled in several ways. We have a few options
810 * for putting the system to sleep - using the platform driver (e.g. ACPI
a3d25c27
RW
811 * or other hibernation_ops), powering off the system or rebooting the
812 * system (for testing) as well as the two test modes.
1da177e4 813 *
11d77d0c 814 * The system can support 'platform', and that is known a priori (and
a3d25c27
RW
815 * encoded by the presence of hibernation_ops). However, the user may
816 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
817 * test modes, 'test' or 'testproc'.
1da177e4
LT
818 *
819 * show() will display what the mode is currently set to.
820 * store() will accept one of
821 *
1da177e4
LT
822 * 'platform'
823 * 'shutdown'
824 * 'reboot'
11d77d0c
JB
825 * 'test'
826 * 'testproc'
1da177e4 827 *
11d77d0c 828 * It will only change to 'platform' if the system
a3d25c27 829 * supports it (as determined by having hibernation_ops).
1da177e4
LT
830 */
831
386f275f
KS
832static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
833 char *buf)
1da177e4 834{
f0ced9b2
JB
835 int i;
836 char *start = buf;
837
a3d25c27
RW
838 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
839 if (!hibernation_modes[i])
f0ced9b2
JB
840 continue;
841 switch (i) {
a3d25c27
RW
842 case HIBERNATION_SHUTDOWN:
843 case HIBERNATION_REBOOT:
844 case HIBERNATION_TEST:
845 case HIBERNATION_TESTPROC:
f0ced9b2 846 break;
a3d25c27
RW
847 case HIBERNATION_PLATFORM:
848 if (hibernation_ops)
f0ced9b2
JB
849 break;
850 /* not a valid mode, continue with loop */
851 continue;
852 }
a3d25c27
RW
853 if (i == hibernation_mode)
854 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
f0ced9b2 855 else
a3d25c27 856 buf += sprintf(buf, "%s ", hibernation_modes[i]);
f0ced9b2
JB
857 }
858 buf += sprintf(buf, "\n");
859 return buf-start;
1da177e4
LT
860}
861
862
386f275f
KS
863static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
864 const char *buf, size_t n)
1da177e4
LT
865{
866 int error = 0;
867 int i;
868 int len;
869 char *p;
a3d25c27 870 int mode = HIBERNATION_INVALID;
1da177e4
LT
871
872 p = memchr(buf, '\n', n);
873 len = p ? p - buf : n;
874
a6d70980 875 mutex_lock(&pm_mutex);
a3d25c27 876 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
8d98a690
RW
877 if (len == strlen(hibernation_modes[i])
878 && !strncmp(buf, hibernation_modes[i], len)) {
1da177e4
LT
879 mode = i;
880 break;
881 }
882 }
a3d25c27 883 if (mode != HIBERNATION_INVALID) {
fe0c935a 884 switch (mode) {
a3d25c27
RW
885 case HIBERNATION_SHUTDOWN:
886 case HIBERNATION_REBOOT:
887 case HIBERNATION_TEST:
888 case HIBERNATION_TESTPROC:
889 hibernation_mode = mode;
fe0c935a 890 break;
a3d25c27
RW
891 case HIBERNATION_PLATFORM:
892 if (hibernation_ops)
893 hibernation_mode = mode;
1da177e4
LT
894 else
895 error = -EINVAL;
896 }
a3d25c27 897 } else
1da177e4
LT
898 error = -EINVAL;
899
a3d25c27 900 if (!error)
23976728 901 pr_debug("PM: Hibernation mode set to '%s'\n",
a3d25c27 902 hibernation_modes[mode]);
a6d70980 903 mutex_unlock(&pm_mutex);
1da177e4
LT
904 return error ? error : n;
905}
906
907power_attr(disk);
908
386f275f
KS
909static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
910 char *buf)
1da177e4
LT
911{
912 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
913 MINOR(swsusp_resume_device));
914}
915
386f275f
KS
916static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
917 const char *buf, size_t n)
1da177e4 918{
1da177e4 919 unsigned int maj, min;
1da177e4 920 dev_t res;
a576219a 921 int ret = -EINVAL;
1da177e4 922
a576219a
AM
923 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
924 goto out;
1da177e4 925
a576219a
AM
926 res = MKDEV(maj,min);
927 if (maj != MAJOR(res) || min != MINOR(res))
928 goto out;
1da177e4 929
a6d70980 930 mutex_lock(&pm_mutex);
a576219a 931 swsusp_resume_device = res;
a6d70980 932 mutex_unlock(&pm_mutex);
23976728 933 printk(KERN_INFO "PM: Starting manual resume from disk\n");
a576219a
AM
934 noresume = 0;
935 software_resume();
936 ret = n;
59a49335 937 out:
a576219a 938 return ret;
1da177e4
LT
939}
940
941power_attr(resume);
942
386f275f
KS
943static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
944 char *buf)
ca0aec0f 945{
853609b6 946 return sprintf(buf, "%lu\n", image_size);
ca0aec0f
RW
947}
948
386f275f
KS
949static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
950 const char *buf, size_t n)
ca0aec0f 951{
853609b6 952 unsigned long size;
ca0aec0f 953
853609b6 954 if (sscanf(buf, "%lu", &size) == 1) {
ca0aec0f
RW
955 image_size = size;
956 return n;
957 }
958
959 return -EINVAL;
960}
961
962power_attr(image_size);
963
1da177e4
LT
964static struct attribute * g[] = {
965 &disk_attr.attr,
966 &resume_attr.attr,
ca0aec0f 967 &image_size_attr.attr,
1da177e4
LT
968 NULL,
969};
970
971
972static struct attribute_group attr_group = {
973 .attrs = g,
974};
975
976
977static int __init pm_disk_init(void)
978{
d76e15fb 979 return sysfs_create_group(power_kobj, &attr_group);
1da177e4
LT
980}
981
982core_initcall(pm_disk_init);
983
984
985static int __init resume_setup(char *str)
986{
987 if (noresume)
988 return 1;
989
990 strncpy( resume_file, str, 255 );
991 return 1;
992}
993
9a154d9d
RW
994static int __init resume_offset_setup(char *str)
995{
996 unsigned long long offset;
997
998 if (noresume)
999 return 1;
1000
1001 if (sscanf(str, "%llu", &offset) == 1)
1002 swsusp_resume_block = offset;
1003
1004 return 1;
1005}
1006
1da177e4
LT
1007static int __init noresume_setup(char *str)
1008{
1009 noresume = 1;
1010 return 1;
1011}
1012
1013__setup("noresume", noresume_setup);
9a154d9d 1014__setup("resume_offset=", resume_offset_setup);
1da177e4 1015__setup("resume=", resume_setup);