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