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