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