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