ARM: shmobile: Lager: Correct I2C bus for VDD MPU regulator
[deliverable/linux.git] / kernel / power / hibernate.c
CommitLineData
1da177e4 1/*
8b759b84 2 * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
1da177e4
LT
3 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
a2531293 6 * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz>
8b759b84 7 * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
62c552cc 8 * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com>
1da177e4
LT
9 *
10 * This file is released under the GPLv2.
1da177e4
LT
11 */
12
6e5fdeed 13#include <linux/export.h>
1da177e4
LT
14#include <linux/suspend.h>
15#include <linux/syscalls.h>
16#include <linux/reboot.h>
17#include <linux/string.h>
18#include <linux/device.h>
6f8d7022 19#include <linux/async.h>
1da177e4
LT
20#include <linux/delay.h>
21#include <linux/fs.h>
d53d9f16 22#include <linux/mount.h>
88d10bba 23#include <linux/pm.h>
97c7801c 24#include <linux/console.h>
e3920fb4 25#include <linux/cpu.h>
7dfb7103 26#include <linux/freezer.h>
5a0e3ad6 27#include <linux/gfp.h>
40dc166c 28#include <linux/syscore_ops.h>
2df83fa4
MB
29#include <linux/ctype.h>
30#include <linux/genhd.h>
bb3632c6 31#include <trace/events/power.h>
d53d9f16 32
1da177e4
LT
33#include "power.h"
34
35
d231ff1a
BS
36static int nocompress;
37static int noresume;
38static int resume_wait;
f6514be5 39static unsigned int resume_delay;
47a460d5 40static char resume_file[256] = CONFIG_PM_STD_PARTITION;
1da177e4 41dev_t swsusp_resume_device;
9a154d9d 42sector_t swsusp_resume_block;
d6efc2f7 43__visible int in_suspend __nosavedata;
1da177e4 44
a3d25c27
RW
45enum {
46 HIBERNATION_INVALID,
47 HIBERNATION_PLATFORM,
a3d25c27
RW
48 HIBERNATION_SHUTDOWN,
49 HIBERNATION_REBOOT,
62c552cc
BS
50#ifdef CONFIG_SUSPEND
51 HIBERNATION_SUSPEND,
52#endif
a3d25c27
RW
53 /* keep last */
54 __HIBERNATION_AFTER_LAST
55};
56#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
57#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
58
59static int hibernation_mode = HIBERNATION_SHUTDOWN;
60
97819a26 61bool freezer_test_done;
aa9a7b11 62
073ef1f6 63static const struct platform_hibernation_ops *hibernation_ops;
a3d25c27
RW
64
65/**
f42a9813
RW
66 * hibernation_set_ops - Set the global hibernate operations.
67 * @ops: Hibernation operations to use in subsequent hibernation transitions.
a3d25c27 68 */
073ef1f6 69void hibernation_set_ops(const struct platform_hibernation_ops *ops)
a3d25c27 70{
caea99ef
RW
71 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
72 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
5729c63a 73 && ops->restore_cleanup && ops->leave)) {
a3d25c27
RW
74 WARN_ON(1);
75 return;
76 }
bcda53fa 77 lock_system_sleep();
a3d25c27
RW
78 hibernation_ops = ops;
79 if (ops)
80 hibernation_mode = HIBERNATION_PLATFORM;
81 else if (hibernation_mode == HIBERNATION_PLATFORM)
82 hibernation_mode = HIBERNATION_SHUTDOWN;
83
bcda53fa 84 unlock_system_sleep();
a3d25c27 85}
e0c7855e 86EXPORT_SYMBOL_GPL(hibernation_set_ops);
a3d25c27 87
abfe2d7b
RW
88static bool entering_platform_hibernation;
89
90bool system_entering_hibernation(void)
91{
92 return entering_platform_hibernation;
93}
94EXPORT_SYMBOL(system_entering_hibernation);
95
4cc79776
RW
96#ifdef CONFIG_PM_DEBUG
97static void hibernation_debug_sleep(void)
98{
99 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
100 mdelay(5000);
101}
102
4cc79776
RW
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 */
4cc79776
RW
112static int hibernation_test(int level) { return 0; }
113#endif /* !CONFIG_PM_DEBUG */
114
74f270af 115/**
f42a9813
RW
116 * platform_begin - Call platform to start hibernation.
117 * @platform_mode: Whether or not to use the platform driver.
74f270af 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/**
f42a9813
RW
126 * platform_end - Call platform to finish transition to the working state.
127 * @platform_mode: Whether or not to use the platform driver.
caea99ef 128 */
caea99ef
RW
129static void platform_end(int platform_mode)
130{
131 if (platform_mode && hibernation_ops)
132 hibernation_ops->end();
74f270af 133}
a3d25c27 134
8a05aac2 135/**
f42a9813
RW
136 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
137 * @platform_mode: Whether or not to use the platform driver.
138 *
139 * Use the platform driver to prepare the system for creating a hibernate image,
140 * if so configured, and return an error code if that fails.
8a05aac2
SS
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 149/**
f42a9813
RW
150 * platform_leave - Call platform to prepare a transition to the working state.
151 * @platform_mode: Whether or not to use the platform driver.
152 *
153 * Use the platform driver prepare to prepare the machine for switching to the
154 * normal mode of operation.
155 *
156 * This routine is called on one CPU with interrupts disabled.
c7e0831d 157 */
c7e0831d
RW
158static void platform_leave(int platform_mode)
159{
160 if (platform_mode && hibernation_ops)
161 hibernation_ops->leave();
162}
163
a3d25c27 164/**
f42a9813
RW
165 * platform_finish - Call platform to switch the system to the working state.
166 * @platform_mode: Whether or not to use the platform driver.
167 *
168 * Use the platform driver to switch the machine to the normal mode of
169 * operation.
170 *
171 * This routine must be called after platform_prepare().
a3d25c27 172 */
7777fab9 173static void platform_finish(int platform_mode)
a3d25c27 174{
7777fab9 175 if (platform_mode && hibernation_ops)
a3d25c27 176 hibernation_ops->finish();
8a05aac2
SS
177}
178
a634cc10 179/**
f42a9813
RW
180 * platform_pre_restore - Prepare for hibernate image restoration.
181 * @platform_mode: Whether or not to use the platform driver.
182 *
183 * Use the platform driver to prepare the system for resume from a hibernation
184 * image.
185 *
186 * If the restore fails after this function has been called,
187 * platform_restore_cleanup() must be called.
a634cc10 188 */
a634cc10
RW
189static int platform_pre_restore(int platform_mode)
190{
191 return (platform_mode && hibernation_ops) ?
192 hibernation_ops->pre_restore() : 0;
193}
194
195/**
f42a9813
RW
196 * platform_restore_cleanup - Switch to the working state after failing restore.
197 * @platform_mode: Whether or not to use the platform driver.
198 *
199 * Use the platform driver to switch the system to the normal mode of operation
200 * after a failing restore.
201 *
202 * If platform_pre_restore() has been called before the failing restore, this
203 * function must be called too, regardless of the result of
204 * platform_pre_restore().
a634cc10 205 */
a634cc10
RW
206static void platform_restore_cleanup(int platform_mode)
207{
208 if (platform_mode && hibernation_ops)
209 hibernation_ops->restore_cleanup();
210}
211
d8f3de0d 212/**
f42a9813
RW
213 * platform_recover - Recover from a failure to suspend devices.
214 * @platform_mode: Whether or not to use the platform driver.
d8f3de0d 215 */
d8f3de0d
RW
216static void platform_recover(int platform_mode)
217{
218 if (platform_mode && hibernation_ops && hibernation_ops->recover)
219 hibernation_ops->recover();
220}
221
8e60c6a1 222/**
f42a9813
RW
223 * swsusp_show_speed - Print time elapsed between two events during hibernation.
224 * @start: Starting event.
225 * @stop: Final event.
226 * @nr_pages: Number of memory pages processed between @start and @stop.
227 * @msg: Additional diagnostic message to print.
8e60c6a1 228 */
8e60c6a1
NC
229void swsusp_show_speed(struct timeval *start, struct timeval *stop,
230 unsigned nr_pages, char *msg)
231{
4881f603
CG
232 u64 elapsed_centisecs64;
233 unsigned int centisecs;
234 unsigned int k;
235 unsigned int kps;
8e60c6a1
NC
236
237 elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
4881f603
CG
238 /*
239 * If "(s64)elapsed_centisecs64 < 0", it will print long elapsed time,
240 * it is obvious enough for what went wrong.
241 */
8e60c6a1
NC
242 do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
243 centisecs = elapsed_centisecs64;
244 if (centisecs == 0)
245 centisecs = 1; /* avoid div-by-zero */
246 k = nr_pages * (PAGE_SIZE / 1024);
247 kps = (k * 100) / centisecs;
4881f603 248 printk(KERN_INFO "PM: %s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
8e60c6a1
NC
249 msg, k,
250 centisecs / 100, centisecs % 100,
251 kps / 1000, (kps % 1000) / 10);
252}
253
c7e0831d 254/**
f42a9813
RW
255 * create_image - Create a hibernation image.
256 * @platform_mode: Whether or not to use the platform driver.
257 *
cf579dfb
RW
258 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
259 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
f42a9813
RW
260 *
261 * Control reappears in this routine after the subsequent restore.
c7e0831d 262 */
47a460d5 263static int create_image(int platform_mode)
c7e0831d
RW
264{
265 int error;
266
cf579dfb 267 error = dpm_suspend_end(PMSG_FREEZE);
c7e0831d 268 if (error) {
23976728
RW
269 printk(KERN_ERR "PM: Some devices failed to power down, "
270 "aborting hibernation\n");
32bdfac5 271 return error;
c7e0831d 272 }
2ed8d2b3 273
4aecd671
RW
274 error = platform_pre_snapshot(platform_mode);
275 if (error || hibernation_test(TEST_PLATFORM))
276 goto Platform_finish;
277
278 error = disable_nonboot_cpus();
48580ab8 279 if (error || hibernation_test(TEST_CPUS))
4aecd671
RW
280 goto Enable_cpus;
281
2ed8d2b3
RW
282 local_irq_disable();
283
2e711c04 284 error = syscore_suspend();
770824bd 285 if (error) {
4484079d 286 printk(KERN_ERR "PM: Some system devices failed to power down, "
770824bd 287 "aborting hibernation\n");
4aecd671 288 goto Enable_irqs;
770824bd 289 }
c7e0831d 290
a2867e08 291 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
4cc79776
RW
292 goto Power_up;
293
294 in_suspend = 1;
c7e0831d 295 save_processor_state();
bb3632c6 296 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
c7e0831d 297 error = swsusp_arch_suspend();
bb3632c6 298 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
c7e0831d 299 if (error)
23976728
RW
300 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
301 error);
c7e0831d
RW
302 /* Restore control flow magically appears here */
303 restore_processor_state();
362e77d1 304 if (!in_suspend)
c125e96f 305 events_check_enabled = false;
362e77d1
BM
306
307 platform_leave(platform_mode);
4aecd671 308
4cc79776 309 Power_up:
40dc166c 310 syscore_resume();
2ed8d2b3 311
4aecd671 312 Enable_irqs:
2ed8d2b3
RW
313 local_irq_enable();
314
4aecd671
RW
315 Enable_cpus:
316 enable_nonboot_cpus();
317
318 Platform_finish:
319 platform_finish(platform_mode);
320
cf579dfb 321 dpm_resume_start(in_suspend ?
1eede070 322 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
2ed8d2b3 323
c7e0831d
RW
324 return error;
325}
326
7777fab9 327/**
f42a9813
RW
328 * hibernation_snapshot - Quiesce devices and create a hibernation image.
329 * @platform_mode: If set, use platform driver to prepare for the transition.
7777fab9 330 *
f42a9813 331 * This routine must be called with pm_mutex held.
7777fab9 332 */
7777fab9
RW
333int hibernation_snapshot(int platform_mode)
334{
953a2063 335 pm_message_t msg;
cbe2f5a6 336 int error;
7777fab9 337
3fe0313e 338 error = platform_begin(platform_mode);
7777fab9 339 if (error)
d074ee02 340 goto Close;
7777fab9 341
64a473cb
RW
342 /* Preallocate image memory before shutting down devices. */
343 error = hibernate_preallocate_memory();
2aede851
RW
344 if (error)
345 goto Close;
346
347 error = freeze_kernel_threads();
348 if (error)
bb58dd5d 349 goto Cleanup;
2aede851 350
48580ab8 351 if (hibernation_test(TEST_FREEZER)) {
aa9a7b11
SB
352
353 /*
354 * Indicate to the caller that we are returning due to a
355 * successful freezer test.
356 */
357 freezer_test_done = true;
51d6ff7a 358 goto Thaw;
aa9a7b11
SB
359 }
360
2aede851 361 error = dpm_prepare(PMSG_FREEZE);
bb58dd5d 362 if (error) {
953a2063 363 dpm_complete(PMSG_RECOVER);
51d6ff7a 364 goto Thaw;
bb58dd5d 365 }
74f270af 366
7777fab9 367 suspend_console();
443772d4 368 ftrace_stop();
c9e664f1 369 pm_restrict_gfp_mask();
953a2063 370
91e7c75b 371 error = dpm_suspend(PMSG_FREEZE);
10a1803d 372
953a2063
SB
373 if (error || hibernation_test(TEST_DEVICES))
374 platform_recover(platform_mode);
375 else
376 error = create_image(platform_mode);
7777fab9 377
c9e664f1 378 /*
953a2063
SB
379 * In the case that we call create_image() above, the control
380 * returns here (1) after the image has been created or the
c9e664f1
RW
381 * image creation has failed and (2) after a successful restore.
382 */
4cc79776 383
64a473cb
RW
384 /* We may need to release the preallocated image pages here. */
385 if (error || !in_suspend)
386 swsusp_free();
387
91e7c75b
RW
388 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
389 dpm_resume(msg);
c9e664f1
RW
390
391 if (error || !in_suspend)
392 pm_restore_gfp_mask();
393
443772d4 394 ftrace_start();
7777fab9 395 resume_console();
91e7c75b
RW
396 dpm_complete(msg);
397
caea99ef
RW
398 Close:
399 platform_end(platform_mode);
7777fab9 400 return error;
d8f3de0d 401
51d6ff7a
SB
402 Thaw:
403 thaw_kernel_threads();
bb58dd5d
RW
404 Cleanup:
405 swsusp_free();
406 goto Close;
7777fab9
RW
407}
408
72df68ca 409/**
f42a9813
RW
410 * resume_target_kernel - Restore system state from a hibernation image.
411 * @platform_mode: Whether or not to use the platform driver.
412 *
cf579dfb
RW
413 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
414 * contents of highmem that have not been restored yet from the image and run
415 * the low-level code that will restore the remaining contents of memory and
416 * switch to the just restored target kernel.
72df68ca 417 */
4aecd671 418static int resume_target_kernel(bool platform_mode)
72df68ca
RW
419{
420 int error;
421
cf579dfb 422 error = dpm_suspend_end(PMSG_QUIESCE);
72df68ca 423 if (error) {
23976728 424 printk(KERN_ERR "PM: Some devices failed to power down, "
72df68ca 425 "aborting resume\n");
32bdfac5 426 return error;
72df68ca 427 }
2ed8d2b3 428
4aecd671
RW
429 error = platform_pre_restore(platform_mode);
430 if (error)
431 goto Cleanup;
432
433 error = disable_nonboot_cpus();
434 if (error)
435 goto Enable_cpus;
436
2ed8d2b3
RW
437 local_irq_disable();
438
2e711c04 439 error = syscore_suspend();
4aecd671
RW
440 if (error)
441 goto Enable_irqs;
442
72df68ca
RW
443 save_processor_state();
444 error = restore_highmem();
445 if (!error) {
446 error = swsusp_arch_resume();
447 /*
448 * The code below is only ever reached in case of a failure.
4e2d9491
RW
449 * Otherwise, execution continues at the place where
450 * swsusp_arch_suspend() was called.
72df68ca
RW
451 */
452 BUG_ON(!error);
4e2d9491
RW
453 /*
454 * This call to restore_highmem() reverts the changes made by
455 * the previous one.
456 */
72df68ca
RW
457 restore_highmem();
458 }
459 /*
460 * The only reason why swsusp_arch_resume() can fail is memory being
461 * very tight, so we have to free it as soon as we can to avoid
4e2d9491 462 * subsequent failures.
72df68ca
RW
463 */
464 swsusp_free();
465 restore_processor_state();
466 touch_softlockup_watchdog();
2ed8d2b3 467
40dc166c 468 syscore_resume();
2ed8d2b3 469
4aecd671 470 Enable_irqs:
72df68ca 471 local_irq_enable();
2ed8d2b3 472
4aecd671
RW
473 Enable_cpus:
474 enable_nonboot_cpus();
475
476 Cleanup:
477 platform_restore_cleanup(platform_mode);
478
cf579dfb 479 dpm_resume_start(PMSG_RECOVER);
2ed8d2b3 480
72df68ca
RW
481 return error;
482}
483
7777fab9 484/**
f42a9813
RW
485 * hibernation_restore - Quiesce devices and restore from a hibernation image.
486 * @platform_mode: If set, use platform driver to prepare for the transition.
7777fab9 487 *
f42a9813 488 * This routine must be called with pm_mutex held. If it is successful, control
21e82808 489 * reappears in the restored target kernel in hibernation_snapshot().
7777fab9 490 */
a634cc10 491int hibernation_restore(int platform_mode)
7777fab9 492{
cbe2f5a6 493 int error;
7777fab9
RW
494
495 pm_prepare_console();
496 suspend_console();
443772d4 497 ftrace_stop();
c9e664f1 498 pm_restrict_gfp_mask();
d1616302 499 error = dpm_suspend_start(PMSG_QUIESCE);
a634cc10 500 if (!error) {
4aecd671 501 error = resume_target_kernel(platform_mode);
d1616302 502 dpm_resume_end(PMSG_RECOVER);
a634cc10 503 }
c9e664f1 504 pm_restore_gfp_mask();
443772d4 505 ftrace_start();
7777fab9
RW
506 resume_console();
507 pm_restore_console();
508 return error;
509}
510
511/**
f42a9813 512 * hibernation_platform_enter - Power off the system using the platform driver.
7777fab9 513 */
7777fab9
RW
514int hibernation_platform_enter(void)
515{
cbe2f5a6 516 int error;
b1457bcc 517
9cd9a005
RW
518 if (!hibernation_ops)
519 return -ENOSYS;
520
521 /*
522 * We have cancelled the power transition by running
523 * hibernation_ops->finish() before saving the image, so we should let
524 * the firmware know that we're going to enter the sleep state after all
525 */
caea99ef 526 error = hibernation_ops->begin();
9cd9a005 527 if (error)
caea99ef 528 goto Close;
9cd9a005 529
abfe2d7b 530 entering_platform_hibernation = true;
9cd9a005 531 suspend_console();
443772d4 532 ftrace_stop();
d1616302 533 error = dpm_suspend_start(PMSG_HIBERNATE);
d8f3de0d
RW
534 if (error) {
535 if (hibernation_ops->recover)
536 hibernation_ops->recover();
537 goto Resume_devices;
538 }
9cd9a005 539
cf579dfb 540 error = dpm_suspend_end(PMSG_HIBERNATE);
4aecd671 541 if (error)
32bdfac5 542 goto Resume_devices;
4aecd671 543
9cd9a005
RW
544 error = hibernation_ops->prepare();
545 if (error)
e681c9dd 546 goto Platform_finish;
9cd9a005
RW
547
548 error = disable_nonboot_cpus();
549 if (error)
e681c9dd 550 goto Platform_finish;
2ed8d2b3 551
4aecd671 552 local_irq_disable();
40dc166c 553 syscore_suspend();
a2867e08 554 if (pm_wakeup_pending()) {
c125e96f
RW
555 error = -EAGAIN;
556 goto Power_up;
557 }
558
4aecd671
RW
559 hibernation_ops->enter();
560 /* We should never get here */
561 while (1);
9cd9a005 562
c125e96f 563 Power_up:
40dc166c 564 syscore_resume();
c125e96f
RW
565 local_irq_enable();
566 enable_nonboot_cpus();
567
e681c9dd 568 Platform_finish:
9cd9a005 569 hibernation_ops->finish();
2ed8d2b3 570
cf579dfb 571 dpm_resume_start(PMSG_RESTORE);
4aecd671 572
9cd9a005 573 Resume_devices:
abfe2d7b 574 entering_platform_hibernation = false;
d1616302 575 dpm_resume_end(PMSG_RESTORE);
443772d4 576 ftrace_start();
9cd9a005 577 resume_console();
2ed8d2b3 578
caea99ef
RW
579 Close:
580 hibernation_ops->end();
2ed8d2b3 581
b1457bcc 582 return error;
7777fab9
RW
583}
584
1da177e4 585/**
f42a9813 586 * power_down - Shut the machine down for hibernation.
1da177e4 587 *
f42a9813
RW
588 * Use the platform driver, if configured, to put the system into the sleep
589 * state corresponding to hibernation, or try to power it off or reboot,
590 * depending on the value of hibernation_mode.
1da177e4 591 */
fe0c935a 592static void power_down(void)
1da177e4 593{
62c552cc
BS
594#ifdef CONFIG_SUSPEND
595 int error;
596#endif
597
a3d25c27 598 switch (hibernation_mode) {
a3d25c27 599 case HIBERNATION_REBOOT:
fdde86ac 600 kernel_restart(NULL);
1da177e4 601 break;
a3d25c27 602 case HIBERNATION_PLATFORM:
7777fab9 603 hibernation_platform_enter();
9cd9a005 604 case HIBERNATION_SHUTDOWN:
2c730785
SC
605 if (pm_power_off)
606 kernel_power_off();
9cd9a005 607 break;
62c552cc
BS
608#ifdef CONFIG_SUSPEND
609 case HIBERNATION_SUSPEND:
610 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
611 if (error) {
612 if (hibernation_ops)
613 hibernation_mode = HIBERNATION_PLATFORM;
614 else
615 hibernation_mode = HIBERNATION_SHUTDOWN;
616 power_down();
617 }
618 /*
619 * Restore swap signature.
620 */
621 error = swsusp_unmark();
622 if (error)
623 printk(KERN_ERR "PM: Swap will be unusable! "
624 "Try swapon -a.\n");
625 return;
626#endif
1da177e4 627 }
fdde86ac 628 kernel_halt();
fe0c935a
JB
629 /*
630 * Valid image is on the disk, if we continue we risk serious data
631 * corruption after resume.
632 */
23976728 633 printk(KERN_CRIT "PM: Please power down manually\n");
2c730785
SC
634 while (1)
635 cpu_relax();
1da177e4
LT
636}
637
1da177e4 638/**
f42a9813 639 * hibernate - Carry out system hibernation, including saving the image.
1da177e4 640 */
a3d25c27 641int hibernate(void)
1da177e4
LT
642{
643 int error;
644
bcda53fa 645 lock_system_sleep();
0709db60 646 /* The snapshot device should not be opened while we're running */
b10d9117
RW
647 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
648 error = -EBUSY;
649 goto Unlock;
650 }
651
5a0a2f30 652 pm_prepare_console();
b10d9117
RW
653 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
654 if (error)
655 goto Exit;
0709db60 656
23976728 657 printk(KERN_INFO "PM: Syncing filesystems ... ");
232b1432
RW
658 sys_sync();
659 printk("done.\n");
660
03afed8b 661 error = freeze_processes();
5a72e04d 662 if (error)
8fd37a4c
RW
663 goto Exit;
664
942f4015 665 lock_device_hotplug();
8fd37a4c
RW
666 /* Allocate memory management structures */
667 error = create_basic_memory_bitmaps();
668 if (error)
669 goto Thaw;
1da177e4 670
7777fab9 671 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
a556d5b5 672 if (error || freezer_test_done)
8fd37a4c 673 goto Free_bitmaps;
64a473cb
RW
674
675 if (in_suspend) {
a634cc10
RW
676 unsigned int flags = 0;
677
678 if (hibernation_mode == HIBERNATION_PLATFORM)
679 flags |= SF_PLATFORM_MODE;
f996fc96
BS
680 if (nocompress)
681 flags |= SF_NOCOMPRESS_MODE;
081a9d04
BS
682 else
683 flags |= SF_CRC32_MODE;
684
1da177e4 685 pr_debug("PM: writing image.\n");
a634cc10 686 error = swsusp_write(flags);
7777fab9 687 swsusp_free();
1da177e4 688 if (!error)
fe0c935a 689 power_down();
5262a475 690 in_suspend = 0;
c9e664f1 691 pm_restore_gfp_mask();
b918f6e6 692 } else {
1da177e4 693 pr_debug("PM: Image restored successfully.\n");
b918f6e6 694 }
64a473cb 695
8fd37a4c
RW
696 Free_bitmaps:
697 free_basic_memory_bitmaps();
b918f6e6 698 Thaw:
942f4015 699 unlock_device_hotplug();
5a0a2f30 700 thaw_processes();
a556d5b5
SB
701
702 /* Don't bother checking whether freezer_test_done is true */
703 freezer_test_done = false;
0709db60 704 Exit:
b10d9117 705 pm_notifier_call_chain(PM_POST_HIBERNATION);
5a0a2f30 706 pm_restore_console();
0709db60 707 atomic_inc(&snapshot_device_available);
b10d9117 708 Unlock:
bcda53fa 709 unlock_system_sleep();
1da177e4
LT
710 return error;
711}
712
713
714/**
f42a9813
RW
715 * software_resume - Resume from a saved hibernation image.
716 *
717 * This routine is called as a late initcall, when all devices have been
718 * discovered and initialized already.
1da177e4 719 *
f42a9813
RW
720 * The image reading code is called to see if there is a hibernation image
721 * available for reading. If that is the case, devices are quiesced and the
722 * contents of memory is restored from the saved image.
1da177e4 723 *
f42a9813
RW
724 * If this is successful, control reappears in the restored target kernel in
725 * hibernation_snaphot() which returns to hibernate(). Otherwise, the routine
726 * attempts to recover gracefully and make the kernel return to the normal mode
727 * of operation.
1da177e4 728 */
1da177e4
LT
729static int software_resume(void)
730{
731 int error;
a634cc10 732 unsigned int flags;
1da177e4 733
eed3ee08
AV
734 /*
735 * If the user said "noresume".. bail out early.
736 */
737 if (noresume)
738 return 0;
739
60a0d233
JB
740 /*
741 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
742 * is configured into the kernel. Since the regular hibernate
743 * trigger path is via sysfs which takes a buffer mutex before
744 * calling hibernate functions (which take pm_mutex) this can
745 * cause lockdep to complain about a possible ABBA deadlock
746 * which cannot happen since we're in the boot code here and
747 * sysfs can't be invoked yet. Therefore, we use a subclass
748 * here to avoid lockdep complaining.
749 */
750 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
0c8454f5
RW
751
752 if (swsusp_resume_device)
753 goto Check_image;
754
755 if (!strlen(resume_file)) {
756 error = -ENOENT;
757 goto Unlock;
758 }
759
d0941ead 760 pr_debug("PM: Checking hibernation image partition %s\n", resume_file);
0c8454f5 761
f126f733
BS
762 if (resume_delay) {
763 printk(KERN_INFO "Waiting %dsec before reading resume device...\n",
764 resume_delay);
765 ssleep(resume_delay);
766 }
767
0c8454f5
RW
768 /* Check if the device is there */
769 swsusp_resume_device = name_to_dev_t(resume_file);
2df83fa4
MB
770
771 /*
772 * name_to_dev_t is ineffective to verify parition if resume_file is in
773 * integer format. (e.g. major:minor)
774 */
775 if (isdigit(resume_file[0]) && resume_wait) {
776 int partno;
777 while (!get_gendisk(swsusp_resume_device, &partno))
778 msleep(10);
779 }
780
3efa147a 781 if (!swsusp_resume_device) {
eed3ee08
AV
782 /*
783 * Some device discovery might still be in progress; we need
784 * to wait for this to finish.
785 */
786 wait_for_device_probe();
6f8d7022
BS
787
788 if (resume_wait) {
789 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
790 msleep(10);
791 async_synchronize_full();
792 }
793
3efa147a 794 swsusp_resume_device = name_to_dev_t(resume_file);
0c8454f5
RW
795 if (!swsusp_resume_device) {
796 error = -ENODEV;
797 goto Unlock;
798 }
3efa147a
PM
799 }
800
0c8454f5 801 Check_image:
d0941ead 802 pr_debug("PM: Hibernation image partition %d:%d present\n",
0c8454f5 803 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
1da177e4 804
d0941ead 805 pr_debug("PM: Looking for hibernation image.\n");
ed746e3b
RW
806 error = swsusp_check();
807 if (error)
74dfd666 808 goto Unlock;
1da177e4 809
0709db60
RW
810 /* The snapshot device should not be opened while we're running */
811 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
812 error = -EBUSY;
76b57e61 813 swsusp_close(FMODE_READ);
0709db60
RW
814 goto Unlock;
815 }
816
5a0a2f30 817 pm_prepare_console();
c3e94d89
AS
818 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
819 if (error)
8fd37a4c 820 goto Close_Finish;
1bfcf130 821
74dfd666 822 pr_debug("PM: Preparing processes for restore.\n");
03afed8b 823 error = freeze_processes();
8fd37a4c
RW
824 if (error)
825 goto Close_Finish;
1da177e4 826
d0941ead 827 pr_debug("PM: Loading hibernation image.\n");
1da177e4 828
942f4015 829 lock_device_hotplug();
8fd37a4c
RW
830 error = create_basic_memory_bitmaps();
831 if (error)
832 goto Thaw;
833
a634cc10 834 error = swsusp_read(&flags);
76b57e61 835 swsusp_close(FMODE_READ);
ed746e3b 836 if (!error)
a634cc10 837 hibernation_restore(flags & SF_PLATFORM_MODE);
1da177e4 838
d0941ead 839 printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n");
7777fab9 840 swsusp_free();
7b5179ac 841 free_basic_memory_bitmaps();
8fd37a4c 842 Thaw:
942f4015 843 unlock_device_hotplug();
8fd37a4c 844 thaw_processes();
0709db60 845 Finish:
c3e94d89 846 pm_notifier_call_chain(PM_POST_RESTORE);
5a0a2f30 847 pm_restore_console();
0709db60 848 atomic_inc(&snapshot_device_available);
dd5d666b 849 /* For success case, the suspend path will release the lock */
74dfd666 850 Unlock:
a6d70980 851 mutex_unlock(&pm_mutex);
d0941ead 852 pr_debug("PM: Hibernation image not present or could not be loaded.\n");
7777fab9 853 return error;
8fd37a4c 854 Close_Finish:
76b57e61
JS
855 swsusp_close(FMODE_READ);
856 goto Finish;
1da177e4
LT
857}
858
d3c345db 859late_initcall_sync(software_resume);
1da177e4
LT
860
861
a3d25c27
RW
862static const char * const hibernation_modes[] = {
863 [HIBERNATION_PLATFORM] = "platform",
864 [HIBERNATION_SHUTDOWN] = "shutdown",
865 [HIBERNATION_REBOOT] = "reboot",
62c552cc
BS
866#ifdef CONFIG_SUSPEND
867 [HIBERNATION_SUSPEND] = "suspend",
868#endif
1da177e4
LT
869};
870
f42a9813
RW
871/*
872 * /sys/power/disk - Control hibernation mode.
1da177e4 873 *
f42a9813
RW
874 * Hibernation can be handled in several ways. There are a few different ways
875 * to put the system into the sleep state: using the platform driver (e.g. ACPI
876 * or other hibernation_ops), powering it off or rebooting it (for testing
48580ab8 877 * mostly).
1da177e4 878 *
f42a9813
RW
879 * The sysfs file /sys/power/disk provides an interface for selecting the
880 * hibernation mode to use. Reading from this file causes the available modes
48580ab8 881 * to be printed. There are 3 modes that can be supported:
1da177e4 882 *
1da177e4
LT
883 * 'platform'
884 * 'shutdown'
885 * 'reboot'
886 *
f42a9813
RW
887 * If a platform hibernation driver is in use, 'platform' will be supported
888 * and will be used by default. Otherwise, 'shutdown' will be used by default.
889 * The selected option (i.e. the one corresponding to the current value of
890 * hibernation_mode) is enclosed by a square bracket.
891 *
892 * To select a given hibernation mode it is necessary to write the mode's
893 * string representation (as returned by reading from /sys/power/disk) back
894 * into /sys/power/disk.
1da177e4
LT
895 */
896
386f275f
KS
897static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
898 char *buf)
1da177e4 899{
f0ced9b2
JB
900 int i;
901 char *start = buf;
902
a3d25c27
RW
903 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
904 if (!hibernation_modes[i])
f0ced9b2
JB
905 continue;
906 switch (i) {
a3d25c27
RW
907 case HIBERNATION_SHUTDOWN:
908 case HIBERNATION_REBOOT:
62c552cc
BS
909#ifdef CONFIG_SUSPEND
910 case HIBERNATION_SUSPEND:
911#endif
f0ced9b2 912 break;
a3d25c27
RW
913 case HIBERNATION_PLATFORM:
914 if (hibernation_ops)
f0ced9b2
JB
915 break;
916 /* not a valid mode, continue with loop */
917 continue;
918 }
a3d25c27
RW
919 if (i == hibernation_mode)
920 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
f0ced9b2 921 else
a3d25c27 922 buf += sprintf(buf, "%s ", hibernation_modes[i]);
f0ced9b2
JB
923 }
924 buf += sprintf(buf, "\n");
925 return buf-start;
1da177e4
LT
926}
927
386f275f
KS
928static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
929 const char *buf, size_t n)
1da177e4
LT
930{
931 int error = 0;
932 int i;
933 int len;
934 char *p;
a3d25c27 935 int mode = HIBERNATION_INVALID;
1da177e4
LT
936
937 p = memchr(buf, '\n', n);
938 len = p ? p - buf : n;
939
bcda53fa 940 lock_system_sleep();
a3d25c27 941 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
8d98a690
RW
942 if (len == strlen(hibernation_modes[i])
943 && !strncmp(buf, hibernation_modes[i], len)) {
1da177e4
LT
944 mode = i;
945 break;
946 }
947 }
a3d25c27 948 if (mode != HIBERNATION_INVALID) {
fe0c935a 949 switch (mode) {
a3d25c27
RW
950 case HIBERNATION_SHUTDOWN:
951 case HIBERNATION_REBOOT:
62c552cc
BS
952#ifdef CONFIG_SUSPEND
953 case HIBERNATION_SUSPEND:
954#endif
a3d25c27 955 hibernation_mode = mode;
fe0c935a 956 break;
a3d25c27
RW
957 case HIBERNATION_PLATFORM:
958 if (hibernation_ops)
959 hibernation_mode = mode;
1da177e4
LT
960 else
961 error = -EINVAL;
962 }
a3d25c27 963 } else
1da177e4
LT
964 error = -EINVAL;
965
a3d25c27 966 if (!error)
23976728 967 pr_debug("PM: Hibernation mode set to '%s'\n",
a3d25c27 968 hibernation_modes[mode]);
bcda53fa 969 unlock_system_sleep();
1da177e4
LT
970 return error ? error : n;
971}
972
973power_attr(disk);
974
386f275f
KS
975static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
976 char *buf)
1da177e4
LT
977{
978 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
979 MINOR(swsusp_resume_device));
980}
981
386f275f
KS
982static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
983 const char *buf, size_t n)
1da177e4 984{
1da177e4 985 dev_t res;
421a5fa1
SC
986 int len = n;
987 char *name;
1da177e4 988
421a5fa1
SC
989 if (len && buf[len-1] == '\n')
990 len--;
991 name = kstrndup(buf, len, GFP_KERNEL);
992 if (!name)
993 return -ENOMEM;
1da177e4 994
421a5fa1
SC
995 res = name_to_dev_t(name);
996 kfree(name);
997 if (!res)
998 return -EINVAL;
1da177e4 999
bcda53fa 1000 lock_system_sleep();
a576219a 1001 swsusp_resume_device = res;
bcda53fa 1002 unlock_system_sleep();
23976728 1003 printk(KERN_INFO "PM: Starting manual resume from disk\n");
a576219a
AM
1004 noresume = 0;
1005 software_resume();
421a5fa1 1006 return n;
1da177e4
LT
1007}
1008
1009power_attr(resume);
1010
386f275f
KS
1011static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1012 char *buf)
ca0aec0f 1013{
853609b6 1014 return sprintf(buf, "%lu\n", image_size);
ca0aec0f
RW
1015}
1016
386f275f
KS
1017static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1018 const char *buf, size_t n)
ca0aec0f 1019{
853609b6 1020 unsigned long size;
ca0aec0f 1021
853609b6 1022 if (sscanf(buf, "%lu", &size) == 1) {
ca0aec0f
RW
1023 image_size = size;
1024 return n;
1025 }
1026
1027 return -EINVAL;
1028}
1029
1030power_attr(image_size);
1031
ddeb6487
RW
1032static ssize_t reserved_size_show(struct kobject *kobj,
1033 struct kobj_attribute *attr, char *buf)
1034{
1035 return sprintf(buf, "%lu\n", reserved_size);
1036}
1037
1038static ssize_t reserved_size_store(struct kobject *kobj,
1039 struct kobj_attribute *attr,
1040 const char *buf, size_t n)
1041{
1042 unsigned long size;
1043
1044 if (sscanf(buf, "%lu", &size) == 1) {
1045 reserved_size = size;
1046 return n;
1047 }
1048
1049 return -EINVAL;
1050}
1051
1052power_attr(reserved_size);
1053
1da177e4
LT
1054static struct attribute * g[] = {
1055 &disk_attr.attr,
1056 &resume_attr.attr,
ca0aec0f 1057 &image_size_attr.attr,
ddeb6487 1058 &reserved_size_attr.attr,
1da177e4
LT
1059 NULL,
1060};
1061
1062
1063static struct attribute_group attr_group = {
1064 .attrs = g,
1065};
1066
1067
1068static int __init pm_disk_init(void)
1069{
d76e15fb 1070 return sysfs_create_group(power_kobj, &attr_group);
1da177e4
LT
1071}
1072
1073core_initcall(pm_disk_init);
1074
1075
1076static int __init resume_setup(char *str)
1077{
1078 if (noresume)
1079 return 1;
1080
1081 strncpy( resume_file, str, 255 );
1082 return 1;
1083}
1084
9a154d9d
RW
1085static int __init resume_offset_setup(char *str)
1086{
1087 unsigned long long offset;
1088
1089 if (noresume)
1090 return 1;
1091
1092 if (sscanf(str, "%llu", &offset) == 1)
1093 swsusp_resume_block = offset;
1094
1095 return 1;
1096}
1097
f996fc96
BS
1098static int __init hibernate_setup(char *str)
1099{
1100 if (!strncmp(str, "noresume", 8))
1101 noresume = 1;
1102 else if (!strncmp(str, "nocompress", 10))
1103 nocompress = 1;
1104 return 1;
1105}
1106
1da177e4
LT
1107static int __init noresume_setup(char *str)
1108{
1109 noresume = 1;
1110 return 1;
1111}
1112
6f8d7022
BS
1113static int __init resumewait_setup(char *str)
1114{
1115 resume_wait = 1;
1116 return 1;
1117}
1118
f126f733
BS
1119static int __init resumedelay_setup(char *str)
1120{
f6514be5 1121 int rc = kstrtouint(str, 0, &resume_delay);
317cf7e5
FF
1122
1123 if (rc)
1124 return rc;
f126f733
BS
1125 return 1;
1126}
1127
1da177e4 1128__setup("noresume", noresume_setup);
9a154d9d 1129__setup("resume_offset=", resume_offset_setup);
1da177e4 1130__setup("resume=", resume_setup);
f996fc96 1131__setup("hibernate=", hibernate_setup);
6f8d7022 1132__setup("resumewait", resumewait_setup);
f126f733 1133__setup("resumedelay=", resumedelay_setup);
This page took 0.686766 seconds and 5 git commands to generate.