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