Merge tag 'usb-6.3-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb
[linux-2.6-block.git] / kernel / power / hibernate.c
CommitLineData
55716d26 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4 2/*
8b759b84 3 * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
1da177e4
LT
4 *
5 * Copyright (c) 2003 Patrick Mochel
6 * Copyright (c) 2003 Open Source Development Lab
a2531293 7 * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz>
8b759b84 8 * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
62c552cc 9 * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com>
1da177e4
LT
10 */
11
7a7b99bf 12#define pr_fmt(fmt) "PM: hibernation: " fmt
2872de13 13
6e5fdeed 14#include <linux/export.h>
1da177e4 15#include <linux/suspend.h>
1da177e4
LT
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>
38b8d208 24#include <linux/nmi.h>
97c7801c 25#include <linux/console.h>
e3920fb4 26#include <linux/cpu.h>
7dfb7103 27#include <linux/freezer.h>
5a0e3ad6 28#include <linux/gfp.h>
40dc166c 29#include <linux/syscore_ops.h>
2df83fa4 30#include <linux/ctype.h>
db597605 31#include <linux/ktime.h>
38bd94b8 32#include <linux/security.h>
9a436f8f 33#include <linux/secretmem.h>
bb3632c6 34#include <trace/events/power.h>
d53d9f16 35
1da177e4
LT
36#include "power.h"
37
38
d231ff1a
BS
39static int nocompress;
40static int noresume;
a6e15a39 41static int nohibernate;
d231ff1a 42static int resume_wait;
f6514be5 43static unsigned int resume_delay;
47a460d5 44static char resume_file[256] = CONFIG_PM_STD_PARTITION;
1da177e4 45dev_t swsusp_resume_device;
9a154d9d 46sector_t swsusp_resume_block;
d6efc2f7 47__visible int in_suspend __nosavedata;
1da177e4 48
a3d25c27
RW
49enum {
50 HIBERNATION_INVALID,
51 HIBERNATION_PLATFORM,
a3d25c27
RW
52 HIBERNATION_SHUTDOWN,
53 HIBERNATION_REBOOT,
62c552cc
BS
54#ifdef CONFIG_SUSPEND
55 HIBERNATION_SUSPEND,
56#endif
fe12c00d 57 HIBERNATION_TEST_RESUME,
a3d25c27
RW
58 /* keep last */
59 __HIBERNATION_AFTER_LAST
60};
61#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
62#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
63
64static int hibernation_mode = HIBERNATION_SHUTDOWN;
65
97819a26 66bool freezer_test_done;
aa9a7b11 67
073ef1f6 68static const struct platform_hibernation_ops *hibernation_ops;
a3d25c27 69
ab7e9b06
DA
70static atomic_t hibernate_atomic = ATOMIC_INIT(1);
71
72bool hibernate_acquire(void)
73{
74 return atomic_add_unless(&hibernate_atomic, -1, 0);
75}
76
77void hibernate_release(void)
78{
79 atomic_inc(&hibernate_atomic);
80}
81
a6e15a39
KC
82bool hibernation_available(void)
83{
9a436f8f
MR
84 return nohibernate == 0 &&
85 !security_locked_down(LOCKDOWN_HIBERNATION) &&
9ea4dcf4 86 !secretmem_active() && !cxl_mem_active();
a6e15a39
KC
87}
88
a3d25c27 89/**
f42a9813
RW
90 * hibernation_set_ops - Set the global hibernate operations.
91 * @ops: Hibernation operations to use in subsequent hibernation transitions.
a3d25c27 92 */
073ef1f6 93void hibernation_set_ops(const struct platform_hibernation_ops *ops)
a3d25c27 94{
5950e5d5
PZ
95 unsigned int sleep_flags;
96
caea99ef
RW
97 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
98 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
5729c63a 99 && ops->restore_cleanup && ops->leave)) {
a3d25c27
RW
100 WARN_ON(1);
101 return;
102 }
5950e5d5
PZ
103
104 sleep_flags = lock_system_sleep();
105
a3d25c27
RW
106 hibernation_ops = ops;
107 if (ops)
108 hibernation_mode = HIBERNATION_PLATFORM;
109 else if (hibernation_mode == HIBERNATION_PLATFORM)
110 hibernation_mode = HIBERNATION_SHUTDOWN;
111
5950e5d5 112 unlock_system_sleep(sleep_flags);
a3d25c27 113}
e0c7855e 114EXPORT_SYMBOL_GPL(hibernation_set_ops);
a3d25c27 115
abfe2d7b
RW
116static bool entering_platform_hibernation;
117
118bool system_entering_hibernation(void)
119{
120 return entering_platform_hibernation;
121}
122EXPORT_SYMBOL(system_entering_hibernation);
123
4cc79776
RW
124#ifdef CONFIG_PM_DEBUG
125static void hibernation_debug_sleep(void)
126{
7a7b99bf 127 pr_info("debug: Waiting for 5 seconds.\n");
4cc79776
RW
128 mdelay(5000);
129}
130
4cc79776
RW
131static int hibernation_test(int level)
132{
133 if (pm_test_level == level) {
134 hibernation_debug_sleep();
135 return 1;
136 }
137 return 0;
138}
139#else /* !CONFIG_PM_DEBUG */
4cc79776
RW
140static int hibernation_test(int level) { return 0; }
141#endif /* !CONFIG_PM_DEBUG */
142
74f270af 143/**
f42a9813
RW
144 * platform_begin - Call platform to start hibernation.
145 * @platform_mode: Whether or not to use the platform driver.
74f270af 146 */
caea99ef 147static int platform_begin(int platform_mode)
74f270af
RW
148{
149 return (platform_mode && hibernation_ops) ?
bb186901 150 hibernation_ops->begin(PMSG_FREEZE) : 0;
caea99ef
RW
151}
152
153/**
f42a9813
RW
154 * platform_end - Call platform to finish transition to the working state.
155 * @platform_mode: Whether or not to use the platform driver.
caea99ef 156 */
caea99ef
RW
157static void platform_end(int platform_mode)
158{
159 if (platform_mode && hibernation_ops)
160 hibernation_ops->end();
74f270af 161}
a3d25c27 162
8a05aac2 163/**
f42a9813
RW
164 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
165 * @platform_mode: Whether or not to use the platform driver.
166 *
167 * Use the platform driver to prepare the system for creating a hibernate image,
168 * if so configured, and return an error code if that fails.
8a05aac2
SS
169 */
170
74f270af 171static int platform_pre_snapshot(int platform_mode)
8a05aac2 172{
7777fab9 173 return (platform_mode && hibernation_ops) ?
74f270af 174 hibernation_ops->pre_snapshot() : 0;
a3d25c27 175}
8a05aac2 176
c7e0831d 177/**
f42a9813
RW
178 * platform_leave - Call platform to prepare a transition to the working state.
179 * @platform_mode: Whether or not to use the platform driver.
180 *
181 * Use the platform driver prepare to prepare the machine for switching to the
182 * normal mode of operation.
183 *
184 * This routine is called on one CPU with interrupts disabled.
c7e0831d 185 */
c7e0831d
RW
186static void platform_leave(int platform_mode)
187{
188 if (platform_mode && hibernation_ops)
189 hibernation_ops->leave();
190}
191
a3d25c27 192/**
f42a9813
RW
193 * platform_finish - Call platform to switch the system to the working state.
194 * @platform_mode: Whether or not to use the platform driver.
195 *
196 * Use the platform driver to switch the machine to the normal mode of
197 * operation.
198 *
199 * This routine must be called after platform_prepare().
a3d25c27 200 */
7777fab9 201static void platform_finish(int platform_mode)
a3d25c27 202{
7777fab9 203 if (platform_mode && hibernation_ops)
a3d25c27 204 hibernation_ops->finish();
8a05aac2
SS
205}
206
a634cc10 207/**
f42a9813
RW
208 * platform_pre_restore - Prepare for hibernate image restoration.
209 * @platform_mode: Whether or not to use the platform driver.
210 *
211 * Use the platform driver to prepare the system for resume from a hibernation
212 * image.
213 *
214 * If the restore fails after this function has been called,
215 * platform_restore_cleanup() must be called.
a634cc10 216 */
a634cc10
RW
217static int platform_pre_restore(int platform_mode)
218{
219 return (platform_mode && hibernation_ops) ?
220 hibernation_ops->pre_restore() : 0;
221}
222
223/**
f42a9813
RW
224 * platform_restore_cleanup - Switch to the working state after failing restore.
225 * @platform_mode: Whether or not to use the platform driver.
226 *
227 * Use the platform driver to switch the system to the normal mode of operation
228 * after a failing restore.
229 *
230 * If platform_pre_restore() has been called before the failing restore, this
231 * function must be called too, regardless of the result of
232 * platform_pre_restore().
a634cc10 233 */
a634cc10
RW
234static void platform_restore_cleanup(int platform_mode)
235{
236 if (platform_mode && hibernation_ops)
237 hibernation_ops->restore_cleanup();
238}
239
d8f3de0d 240/**
f42a9813
RW
241 * platform_recover - Recover from a failure to suspend devices.
242 * @platform_mode: Whether or not to use the platform driver.
d8f3de0d 243 */
d8f3de0d
RW
244static void platform_recover(int platform_mode)
245{
246 if (platform_mode && hibernation_ops && hibernation_ops->recover)
247 hibernation_ops->recover();
248}
249
8e60c6a1 250/**
f42a9813
RW
251 * swsusp_show_speed - Print time elapsed between two events during hibernation.
252 * @start: Starting event.
253 * @stop: Final event.
254 * @nr_pages: Number of memory pages processed between @start and @stop.
255 * @msg: Additional diagnostic message to print.
8e60c6a1 256 */
db597605
TR
257void swsusp_show_speed(ktime_t start, ktime_t stop,
258 unsigned nr_pages, char *msg)
8e60c6a1 259{
db597605 260 ktime_t diff;
4881f603
CG
261 u64 elapsed_centisecs64;
262 unsigned int centisecs;
263 unsigned int k;
264 unsigned int kps;
8e60c6a1 265
db597605
TR
266 diff = ktime_sub(stop, start);
267 elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC);
8e60c6a1
NC
268 centisecs = elapsed_centisecs64;
269 if (centisecs == 0)
270 centisecs = 1; /* avoid div-by-zero */
271 k = nr_pages * (PAGE_SIZE / 1024);
272 kps = (k * 100) / centisecs;
2872de13
RW
273 pr_info("%s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
274 msg, k, centisecs / 100, centisecs % 100, kps / 1000,
275 (kps % 1000) / 10);
8e60c6a1
NC
276}
277
ec527c31
JK
278__weak int arch_resume_nosmt(void)
279{
280 return 0;
281}
282
c7e0831d 283/**
f42a9813
RW
284 * create_image - Create a hibernation image.
285 * @platform_mode: Whether or not to use the platform driver.
286 *
cf579dfb
RW
287 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
288 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
f42a9813
RW
289 *
290 * Control reappears in this routine after the subsequent restore.
c7e0831d 291 */
47a460d5 292static int create_image(int platform_mode)
c7e0831d
RW
293{
294 int error;
295
cf579dfb 296 error = dpm_suspend_end(PMSG_FREEZE);
c7e0831d 297 if (error) {
7a7b99bf 298 pr_err("Some devices failed to power down, aborting\n");
32bdfac5 299 return error;
c7e0831d 300 }
2ed8d2b3 301
4aecd671
RW
302 error = platform_pre_snapshot(platform_mode);
303 if (error || hibernation_test(TEST_PLATFORM))
304 goto Platform_finish;
305
23f62d7a 306 error = pm_sleep_disable_secondary_cpus();
48580ab8 307 if (error || hibernation_test(TEST_CPUS))
4aecd671
RW
308 goto Enable_cpus;
309
2ed8d2b3
RW
310 local_irq_disable();
311
c1a957d1
TG
312 system_state = SYSTEM_SUSPEND;
313
2e711c04 314 error = syscore_suspend();
770824bd 315 if (error) {
7a7b99bf 316 pr_err("Some system devices failed to power down, aborting\n");
4aecd671 317 goto Enable_irqs;
770824bd 318 }
c7e0831d 319
a2867e08 320 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
4cc79776
RW
321 goto Power_up;
322
323 in_suspend = 1;
c7e0831d 324 save_processor_state();
bb3632c6 325 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
c7e0831d 326 error = swsusp_arch_suspend();
62822e2e
TG
327 /* Restore control flow magically appears here */
328 restore_processor_state();
bb3632c6 329 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
c7e0831d 330 if (error)
7a7b99bf 331 pr_err("Error %d creating image\n", error);
2872de13 332
1ad1410f 333 if (!in_suspend) {
c125e96f 334 events_check_enabled = false;
03b6c9a3 335 clear_or_poison_free_pages();
1ad1410f 336 }
362e77d1
BM
337
338 platform_leave(platform_mode);
4aecd671 339
4cc79776 340 Power_up:
40dc166c 341 syscore_resume();
2ed8d2b3 342
4aecd671 343 Enable_irqs:
c1a957d1 344 system_state = SYSTEM_RUNNING;
2ed8d2b3
RW
345 local_irq_enable();
346
4aecd671 347 Enable_cpus:
23f62d7a 348 pm_sleep_enable_secondary_cpus();
4aecd671 349
ec527c31
JK
350 /* Allow architectures to do nosmt-specific post-resume dances */
351 if (!in_suspend)
352 error = arch_resume_nosmt();
353
4aecd671
RW
354 Platform_finish:
355 platform_finish(platform_mode);
356
cf579dfb 357 dpm_resume_start(in_suspend ?
1eede070 358 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
2ed8d2b3 359
c7e0831d
RW
360 return error;
361}
362
7777fab9 363/**
f42a9813
RW
364 * hibernation_snapshot - Quiesce devices and create a hibernation image.
365 * @platform_mode: If set, use platform driver to prepare for the transition.
7777fab9 366 *
55f2503c 367 * This routine must be called with system_transition_mutex held.
7777fab9 368 */
7777fab9
RW
369int hibernation_snapshot(int platform_mode)
370{
953a2063 371 pm_message_t msg;
cbe2f5a6 372 int error;
7777fab9 373
27614273 374 pm_suspend_clear_flags();
3fe0313e 375 error = platform_begin(platform_mode);
7777fab9 376 if (error)
d074ee02 377 goto Close;
7777fab9 378
64a473cb
RW
379 /* Preallocate image memory before shutting down devices. */
380 error = hibernate_preallocate_memory();
2aede851
RW
381 if (error)
382 goto Close;
383
384 error = freeze_kernel_threads();
385 if (error)
bb58dd5d 386 goto Cleanup;
2aede851 387
48580ab8 388 if (hibernation_test(TEST_FREEZER)) {
aa9a7b11
SB
389
390 /*
391 * Indicate to the caller that we are returning due to a
392 * successful freezer test.
393 */
394 freezer_test_done = true;
51d6ff7a 395 goto Thaw;
aa9a7b11
SB
396 }
397
2aede851 398 error = dpm_prepare(PMSG_FREEZE);
bb58dd5d 399 if (error) {
953a2063 400 dpm_complete(PMSG_RECOVER);
51d6ff7a 401 goto Thaw;
bb58dd5d 402 }
74f270af 403
7777fab9 404 suspend_console();
c9e664f1 405 pm_restrict_gfp_mask();
953a2063 406
91e7c75b 407 error = dpm_suspend(PMSG_FREEZE);
10a1803d 408
953a2063
SB
409 if (error || hibernation_test(TEST_DEVICES))
410 platform_recover(platform_mode);
411 else
412 error = create_image(platform_mode);
7777fab9 413
c9e664f1 414 /*
953a2063
SB
415 * In the case that we call create_image() above, the control
416 * returns here (1) after the image has been created or the
c9e664f1
RW
417 * image creation has failed and (2) after a successful restore.
418 */
4cc79776 419
64a473cb
RW
420 /* We may need to release the preallocated image pages here. */
421 if (error || !in_suspend)
422 swsusp_free();
423
91e7c75b
RW
424 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
425 dpm_resume(msg);
c9e664f1
RW
426
427 if (error || !in_suspend)
428 pm_restore_gfp_mask();
429
7777fab9 430 resume_console();
91e7c75b
RW
431 dpm_complete(msg);
432
caea99ef
RW
433 Close:
434 platform_end(platform_mode);
7777fab9 435 return error;
d8f3de0d 436
51d6ff7a
SB
437 Thaw:
438 thaw_kernel_threads();
bb58dd5d
RW
439 Cleanup:
440 swsusp_free();
441 goto Close;
7777fab9
RW
442}
443
406f992e
RW
444int __weak hibernate_resume_nonboot_cpu_disable(void)
445{
2f1a6fbb 446 return suspend_disable_secondary_cpus();
406f992e
RW
447}
448
72df68ca 449/**
f42a9813
RW
450 * resume_target_kernel - Restore system state from a hibernation image.
451 * @platform_mode: Whether or not to use the platform driver.
452 *
cf579dfb
RW
453 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
454 * contents of highmem that have not been restored yet from the image and run
455 * the low-level code that will restore the remaining contents of memory and
456 * switch to the just restored target kernel.
72df68ca 457 */
4aecd671 458static int resume_target_kernel(bool platform_mode)
72df68ca
RW
459{
460 int error;
461
cf579dfb 462 error = dpm_suspend_end(PMSG_QUIESCE);
72df68ca 463 if (error) {
2872de13 464 pr_err("Some devices failed to power down, aborting resume\n");
32bdfac5 465 return error;
72df68ca 466 }
2ed8d2b3 467
4aecd671
RW
468 error = platform_pre_restore(platform_mode);
469 if (error)
470 goto Cleanup;
471
23f62d7a
RW
472 cpuidle_pause();
473
406f992e 474 error = hibernate_resume_nonboot_cpu_disable();
4aecd671
RW
475 if (error)
476 goto Enable_cpus;
477
2ed8d2b3 478 local_irq_disable();
c1a957d1 479 system_state = SYSTEM_SUSPEND;
2ed8d2b3 480
2e711c04 481 error = syscore_suspend();
4aecd671
RW
482 if (error)
483 goto Enable_irqs;
484
72df68ca
RW
485 save_processor_state();
486 error = restore_highmem();
487 if (!error) {
488 error = swsusp_arch_resume();
489 /*
490 * The code below is only ever reached in case of a failure.
4e2d9491
RW
491 * Otherwise, execution continues at the place where
492 * swsusp_arch_suspend() was called.
72df68ca
RW
493 */
494 BUG_ON(!error);
4e2d9491
RW
495 /*
496 * This call to restore_highmem() reverts the changes made by
497 * the previous one.
498 */
72df68ca
RW
499 restore_highmem();
500 }
501 /*
502 * The only reason why swsusp_arch_resume() can fail is memory being
503 * very tight, so we have to free it as soon as we can to avoid
4e2d9491 504 * subsequent failures.
72df68ca
RW
505 */
506 swsusp_free();
507 restore_processor_state();
508 touch_softlockup_watchdog();
2ed8d2b3 509
40dc166c 510 syscore_resume();
2ed8d2b3 511
4aecd671 512 Enable_irqs:
c1a957d1 513 system_state = SYSTEM_RUNNING;
72df68ca 514 local_irq_enable();
2ed8d2b3 515
4aecd671 516 Enable_cpus:
23f62d7a 517 pm_sleep_enable_secondary_cpus();
4aecd671
RW
518
519 Cleanup:
520 platform_restore_cleanup(platform_mode);
521
cf579dfb 522 dpm_resume_start(PMSG_RECOVER);
2ed8d2b3 523
72df68ca
RW
524 return error;
525}
526
7777fab9 527/**
f42a9813
RW
528 * hibernation_restore - Quiesce devices and restore from a hibernation image.
529 * @platform_mode: If set, use platform driver to prepare for the transition.
7777fab9 530 *
55f2503c
PL
531 * This routine must be called with system_transition_mutex held. If it is
532 * successful, control reappears in the restored target kernel in
533 * hibernation_snapshot().
7777fab9 534 */
a634cc10 535int hibernation_restore(int platform_mode)
7777fab9 536{
cbe2f5a6 537 int error;
7777fab9
RW
538
539 pm_prepare_console();
540 suspend_console();
c9e664f1 541 pm_restrict_gfp_mask();
d1616302 542 error = dpm_suspend_start(PMSG_QUIESCE);
a634cc10 543 if (!error) {
4aecd671 544 error = resume_target_kernel(platform_mode);
94fb823f
ID
545 /*
546 * The above should either succeed and jump to the new kernel,
547 * or return with an error. Otherwise things are just
548 * undefined, so let's be paranoid.
549 */
550 BUG_ON(!error);
a634cc10 551 }
94fb823f 552 dpm_resume_end(PMSG_RECOVER);
c9e664f1 553 pm_restore_gfp_mask();
7777fab9
RW
554 resume_console();
555 pm_restore_console();
556 return error;
557}
558
559/**
f42a9813 560 * hibernation_platform_enter - Power off the system using the platform driver.
7777fab9 561 */
7777fab9
RW
562int hibernation_platform_enter(void)
563{
cbe2f5a6 564 int error;
b1457bcc 565
9cd9a005
RW
566 if (!hibernation_ops)
567 return -ENOSYS;
568
569 /*
570 * We have cancelled the power transition by running
571 * hibernation_ops->finish() before saving the image, so we should let
572 * the firmware know that we're going to enter the sleep state after all
573 */
bb186901 574 error = hibernation_ops->begin(PMSG_HIBERNATE);
9cd9a005 575 if (error)
caea99ef 576 goto Close;
9cd9a005 577
abfe2d7b 578 entering_platform_hibernation = true;
9cd9a005 579 suspend_console();
d1616302 580 error = dpm_suspend_start(PMSG_HIBERNATE);
d8f3de0d
RW
581 if (error) {
582 if (hibernation_ops->recover)
583 hibernation_ops->recover();
584 goto Resume_devices;
585 }
9cd9a005 586
cf579dfb 587 error = dpm_suspend_end(PMSG_HIBERNATE);
4aecd671 588 if (error)
32bdfac5 589 goto Resume_devices;
4aecd671 590
9cd9a005
RW
591 error = hibernation_ops->prepare();
592 if (error)
e681c9dd 593 goto Platform_finish;
9cd9a005 594
23f62d7a 595 error = pm_sleep_disable_secondary_cpus();
9cd9a005 596 if (error)
8c506608 597 goto Enable_cpus;
2ed8d2b3 598
4aecd671 599 local_irq_disable();
c1a957d1 600 system_state = SYSTEM_SUSPEND;
40dc166c 601 syscore_suspend();
a2867e08 602 if (pm_wakeup_pending()) {
c125e96f
RW
603 error = -EAGAIN;
604 goto Power_up;
605 }
606
4aecd671
RW
607 hibernation_ops->enter();
608 /* We should never get here */
609 while (1);
9cd9a005 610
c125e96f 611 Power_up:
40dc166c 612 syscore_resume();
c1a957d1 613 system_state = SYSTEM_RUNNING;
c125e96f 614 local_irq_enable();
8c506608
VK
615
616 Enable_cpus:
23f62d7a 617 pm_sleep_enable_secondary_cpus();
c125e96f 618
e681c9dd 619 Platform_finish:
9cd9a005 620 hibernation_ops->finish();
2ed8d2b3 621
cf579dfb 622 dpm_resume_start(PMSG_RESTORE);
4aecd671 623
9cd9a005 624 Resume_devices:
abfe2d7b 625 entering_platform_hibernation = false;
d1616302 626 dpm_resume_end(PMSG_RESTORE);
9cd9a005 627 resume_console();
2ed8d2b3 628
caea99ef
RW
629 Close:
630 hibernation_ops->end();
2ed8d2b3 631
b1457bcc 632 return error;
7777fab9
RW
633}
634
1da177e4 635/**
f42a9813 636 * power_down - Shut the machine down for hibernation.
1da177e4 637 *
f42a9813
RW
638 * Use the platform driver, if configured, to put the system into the sleep
639 * state corresponding to hibernation, or try to power it off or reboot,
640 * depending on the value of hibernation_mode.
1da177e4 641 */
fe0c935a 642static void power_down(void)
1da177e4 643{
62c552cc
BS
644#ifdef CONFIG_SUSPEND
645 int error;
81d45bdf
RW
646
647 if (hibernation_mode == HIBERNATION_SUSPEND) {
85850af4 648 error = suspend_devices_and_enter(mem_sleep_current);
81d45bdf
RW
649 if (error) {
650 hibernation_mode = hibernation_ops ?
651 HIBERNATION_PLATFORM :
652 HIBERNATION_SHUTDOWN;
653 } else {
654 /* Restore swap signature. */
655 error = swsusp_unmark();
656 if (error)
2872de13 657 pr_err("Swap will be unusable! Try swapon -a.\n");
81d45bdf
RW
658
659 return;
660 }
661 }
62c552cc
BS
662#endif
663
a3d25c27 664 switch (hibernation_mode) {
a3d25c27 665 case HIBERNATION_REBOOT:
fdde86ac 666 kernel_restart(NULL);
1da177e4 667 break;
a3d25c27 668 case HIBERNATION_PLATFORM:
7777fab9 669 hibernation_platform_enter();
df561f66 670 fallthrough;
9cd9a005 671 case HIBERNATION_SHUTDOWN:
20277326 672 if (kernel_can_power_off())
2c730785 673 kernel_power_off();
9cd9a005 674 break;
1da177e4 675 }
fdde86ac 676 kernel_halt();
fe0c935a
JB
677 /*
678 * Valid image is on the disk, if we continue we risk serious data
679 * corruption after resume.
680 */
2872de13 681 pr_crit("Power down manually\n");
2c730785
SC
682 while (1)
683 cpu_relax();
1da177e4
LT
684}
685
fe12c00d
CY
686static int load_image_and_restore(void)
687{
688 int error;
689 unsigned int flags;
690
8d8b2441 691 pm_pr_dbg("Loading hibernation image.\n");
fe12c00d
CY
692
693 lock_device_hotplug();
694 error = create_basic_memory_bitmaps();
3f51aa9e
YB
695 if (error) {
696 swsusp_close(FMODE_READ | FMODE_EXCL);
fe12c00d 697 goto Unlock;
3f51aa9e 698 }
fe12c00d
CY
699
700 error = swsusp_read(&flags);
cefcf24b 701 swsusp_close(FMODE_READ | FMODE_EXCL);
fe12c00d 702 if (!error)
3704a6a4 703 error = hibernation_restore(flags & SF_PLATFORM_MODE);
fe12c00d 704
7a7b99bf 705 pr_err("Failed to load image, recovering.\n");
fe12c00d
CY
706 swsusp_free();
707 free_basic_memory_bitmaps();
708 Unlock:
709 unlock_device_hotplug();
710
711 return error;
712}
713
1da177e4 714/**
f42a9813 715 * hibernate - Carry out system hibernation, including saving the image.
1da177e4 716 */
a3d25c27 717int hibernate(void)
1da177e4 718{
fe12c00d 719 bool snapshot_test = false;
5950e5d5 720 unsigned int sleep_flags;
70d93298 721 int error;
1da177e4 722
a6e15a39 723 if (!hibernation_available()) {
8d8b2441 724 pm_pr_dbg("Hibernation not available.\n");
a6e15a39
KC
725 return -EPERM;
726 }
727
5950e5d5 728 sleep_flags = lock_system_sleep();
0709db60 729 /* The snapshot device should not be opened while we're running */
ab7e9b06 730 if (!hibernate_acquire()) {
b10d9117
RW
731 error = -EBUSY;
732 goto Unlock;
733 }
734
8915aa20 735 pr_info("hibernation entry\n");
5a0a2f30 736 pm_prepare_console();
70d93298
PZ
737 error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION);
738 if (error)
739 goto Restore;
0709db60 740
b5dee313 741 ksys_sync_helper();
232b1432 742
03afed8b 743 error = freeze_processes();
5a72e04d 744 if (error)
8fd37a4c
RW
745 goto Exit;
746
942f4015 747 lock_device_hotplug();
8fd37a4c
RW
748 /* Allocate memory management structures */
749 error = create_basic_memory_bitmaps();
750 if (error)
751 goto Thaw;
1da177e4 752
7777fab9 753 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
a556d5b5 754 if (error || freezer_test_done)
8fd37a4c 755 goto Free_bitmaps;
64a473cb
RW
756
757 if (in_suspend) {
a634cc10
RW
758 unsigned int flags = 0;
759
760 if (hibernation_mode == HIBERNATION_PLATFORM)
761 flags |= SF_PLATFORM_MODE;
f996fc96
BS
762 if (nocompress)
763 flags |= SF_NOCOMPRESS_MODE;
081a9d04
BS
764 else
765 flags |= SF_CRC32_MODE;
766
7a7b99bf 767 pm_pr_dbg("Writing hibernation image.\n");
a634cc10 768 error = swsusp_write(flags);
7777fab9 769 swsusp_free();
fe12c00d
CY
770 if (!error) {
771 if (hibernation_mode == HIBERNATION_TEST_RESUME)
772 snapshot_test = true;
773 else
774 power_down();
775 }
5262a475 776 in_suspend = 0;
c9e664f1 777 pm_restore_gfp_mask();
b918f6e6 778 } else {
7a7b99bf 779 pm_pr_dbg("Hibernation image restored successfully.\n");
b918f6e6 780 }
64a473cb 781
8fd37a4c
RW
782 Free_bitmaps:
783 free_basic_memory_bitmaps();
b918f6e6 784 Thaw:
942f4015 785 unlock_device_hotplug();
fe12c00d 786 if (snapshot_test) {
8d8b2441 787 pm_pr_dbg("Checking hibernation image\n");
fe12c00d
CY
788 error = swsusp_check();
789 if (!error)
790 error = load_image_and_restore();
791 }
5a0a2f30 792 thaw_processes();
a556d5b5
SB
793
794 /* Don't bother checking whether freezer_test_done is true */
795 freezer_test_done = false;
0709db60 796 Exit:
70d93298
PZ
797 pm_notifier_call_chain(PM_POST_HIBERNATION);
798 Restore:
5a0a2f30 799 pm_restore_console();
ab7e9b06 800 hibernate_release();
b10d9117 801 Unlock:
5950e5d5 802 unlock_system_sleep(sleep_flags);
8915aa20
RW
803 pr_info("hibernation exit\n");
804
1da177e4
LT
805 return error;
806}
807
48001ea5
DW
808/**
809 * hibernate_quiet_exec - Execute a function with all devices frozen.
810 * @func: Function to execute.
811 * @data: Data pointer to pass to @func.
812 *
813 * Return the @func return value or an error code if it cannot be executed.
814 */
815int hibernate_quiet_exec(int (*func)(void *data), void *data)
816{
5950e5d5 817 unsigned int sleep_flags;
70d93298 818 int error;
48001ea5 819
5950e5d5 820 sleep_flags = lock_system_sleep();
48001ea5
DW
821
822 if (!hibernate_acquire()) {
823 error = -EBUSY;
824 goto unlock;
825 }
826
827 pm_prepare_console();
828
70d93298
PZ
829 error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION);
830 if (error)
831 goto restore;
48001ea5
DW
832
833 error = freeze_processes();
834 if (error)
835 goto exit;
836
837 lock_device_hotplug();
838
839 pm_suspend_clear_flags();
840
841 error = platform_begin(true);
842 if (error)
843 goto thaw;
844
845 error = freeze_kernel_threads();
846 if (error)
847 goto thaw;
848
849 error = dpm_prepare(PMSG_FREEZE);
850 if (error)
851 goto dpm_complete;
852
853 suspend_console();
854
855 error = dpm_suspend(PMSG_FREEZE);
856 if (error)
857 goto dpm_resume;
858
859 error = dpm_suspend_end(PMSG_FREEZE);
860 if (error)
861 goto dpm_resume;
862
863 error = platform_pre_snapshot(true);
864 if (error)
865 goto skip;
866
867 error = func(data);
868
869skip:
870 platform_finish(true);
871
872 dpm_resume_start(PMSG_THAW);
873
874dpm_resume:
875 dpm_resume(PMSG_THAW);
876
877 resume_console();
878
879dpm_complete:
880 dpm_complete(PMSG_THAW);
881
882 thaw_kernel_threads();
883
884thaw:
885 platform_end(true);
886
887 unlock_device_hotplug();
888
889 thaw_processes();
890
891exit:
70d93298 892 pm_notifier_call_chain(PM_POST_HIBERNATION);
48001ea5 893
70d93298 894restore:
48001ea5
DW
895 pm_restore_console();
896
897 hibernate_release();
898
899unlock:
5950e5d5 900 unlock_system_sleep(sleep_flags);
48001ea5
DW
901
902 return error;
903}
904EXPORT_SYMBOL_GPL(hibernate_quiet_exec);
1da177e4
LT
905
906/**
f42a9813
RW
907 * software_resume - Resume from a saved hibernation image.
908 *
909 * This routine is called as a late initcall, when all devices have been
910 * discovered and initialized already.
1da177e4 911 *
f42a9813
RW
912 * The image reading code is called to see if there is a hibernation image
913 * available for reading. If that is the case, devices are quiesced and the
914 * contents of memory is restored from the saved image.
1da177e4 915 *
f42a9813 916 * If this is successful, control reappears in the restored target kernel in
d439e64f 917 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
f42a9813
RW
918 * attempts to recover gracefully and make the kernel return to the normal mode
919 * of operation.
1da177e4 920 */
1da177e4
LT
921static int software_resume(void)
922{
70d93298 923 int error;
1da177e4 924
eed3ee08
AV
925 /*
926 * If the user said "noresume".. bail out early.
927 */
a6e15a39 928 if (noresume || !hibernation_available())
eed3ee08
AV
929 return 0;
930
60a0d233
JB
931 /*
932 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
933 * is configured into the kernel. Since the regular hibernate
934 * trigger path is via sysfs which takes a buffer mutex before
55f2503c
PL
935 * calling hibernate functions (which take system_transition_mutex)
936 * this can cause lockdep to complain about a possible ABBA deadlock
60a0d233
JB
937 * which cannot happen since we're in the boot code here and
938 * sysfs can't be invoked yet. Therefore, we use a subclass
939 * here to avoid lockdep complaining.
940 */
55f2503c 941 mutex_lock_nested(&system_transition_mutex, SINGLE_DEPTH_NESTING);
0c8454f5
RW
942
943 if (swsusp_resume_device)
944 goto Check_image;
945
946 if (!strlen(resume_file)) {
947 error = -ENOENT;
948 goto Unlock;
949 }
950
8d8b2441 951 pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
0c8454f5 952
f126f733 953 if (resume_delay) {
2872de13 954 pr_info("Waiting %dsec before reading resume device ...\n",
f126f733
BS
955 resume_delay);
956 ssleep(resume_delay);
957 }
958
0c8454f5
RW
959 /* Check if the device is there */
960 swsusp_resume_device = name_to_dev_t(resume_file);
3efa147a 961 if (!swsusp_resume_device) {
eed3ee08
AV
962 /*
963 * Some device discovery might still be in progress; we need
964 * to wait for this to finish.
965 */
966 wait_for_device_probe();
6f8d7022
BS
967
968 if (resume_wait) {
969 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
970 msleep(10);
971 async_synchronize_full();
972 }
973
3efa147a 974 swsusp_resume_device = name_to_dev_t(resume_file);
0c8454f5
RW
975 if (!swsusp_resume_device) {
976 error = -ENODEV;
977 goto Unlock;
978 }
3efa147a
PM
979 }
980
0c8454f5 981 Check_image:
8d8b2441 982 pm_pr_dbg("Hibernation image partition %d:%d present\n",
0c8454f5 983 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
1da177e4 984
8d8b2441 985 pm_pr_dbg("Looking for hibernation image.\n");
ed746e3b
RW
986 error = swsusp_check();
987 if (error)
74dfd666 988 goto Unlock;
1da177e4 989
0709db60 990 /* The snapshot device should not be opened while we're running */
ab7e9b06 991 if (!hibernate_acquire()) {
0709db60 992 error = -EBUSY;
cefcf24b 993 swsusp_close(FMODE_READ | FMODE_EXCL);
0709db60
RW
994 goto Unlock;
995 }
996
8915aa20 997 pr_info("resume from hibernation\n");
5a0a2f30 998 pm_prepare_console();
70d93298
PZ
999 error = pm_notifier_call_chain_robust(PM_RESTORE_PREPARE, PM_POST_RESTORE);
1000 if (error)
1001 goto Restore;
1bfcf130 1002
7a7b99bf 1003 pm_pr_dbg("Preparing processes for hibernation restore.\n");
03afed8b 1004 error = freeze_processes();
8fd37a4c
RW
1005 if (error)
1006 goto Close_Finish;
2351f8d2
DC
1007
1008 error = freeze_kernel_threads();
1009 if (error) {
1010 thaw_processes();
1011 goto Close_Finish;
1012 }
1013
fe12c00d 1014 error = load_image_and_restore();
8fd37a4c 1015 thaw_processes();
0709db60 1016 Finish:
70d93298
PZ
1017 pm_notifier_call_chain(PM_POST_RESTORE);
1018 Restore:
5a0a2f30 1019 pm_restore_console();
7a7b99bf 1020 pr_info("resume failed (%d)\n", error);
ab7e9b06 1021 hibernate_release();
dd5d666b 1022 /* For success case, the suspend path will release the lock */
74dfd666 1023 Unlock:
55f2503c 1024 mutex_unlock(&system_transition_mutex);
8d8b2441 1025 pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
7777fab9 1026 return error;
8fd37a4c 1027 Close_Finish:
cefcf24b 1028 swsusp_close(FMODE_READ | FMODE_EXCL);
76b57e61 1029 goto Finish;
1da177e4
LT
1030}
1031
d3c345db 1032late_initcall_sync(software_resume);
1da177e4
LT
1033
1034
a3d25c27
RW
1035static const char * const hibernation_modes[] = {
1036 [HIBERNATION_PLATFORM] = "platform",
1037 [HIBERNATION_SHUTDOWN] = "shutdown",
1038 [HIBERNATION_REBOOT] = "reboot",
62c552cc
BS
1039#ifdef CONFIG_SUSPEND
1040 [HIBERNATION_SUSPEND] = "suspend",
1041#endif
fe12c00d 1042 [HIBERNATION_TEST_RESUME] = "test_resume",
1da177e4
LT
1043};
1044
f42a9813
RW
1045/*
1046 * /sys/power/disk - Control hibernation mode.
1da177e4 1047 *
f42a9813
RW
1048 * Hibernation can be handled in several ways. There are a few different ways
1049 * to put the system into the sleep state: using the platform driver (e.g. ACPI
1050 * or other hibernation_ops), powering it off or rebooting it (for testing
48580ab8 1051 * mostly).
1da177e4 1052 *
f42a9813
RW
1053 * The sysfs file /sys/power/disk provides an interface for selecting the
1054 * hibernation mode to use. Reading from this file causes the available modes
48580ab8 1055 * to be printed. There are 3 modes that can be supported:
1da177e4 1056 *
1da177e4
LT
1057 * 'platform'
1058 * 'shutdown'
1059 * 'reboot'
1060 *
f42a9813
RW
1061 * If a platform hibernation driver is in use, 'platform' will be supported
1062 * and will be used by default. Otherwise, 'shutdown' will be used by default.
1063 * The selected option (i.e. the one corresponding to the current value of
1064 * hibernation_mode) is enclosed by a square bracket.
1065 *
1066 * To select a given hibernation mode it is necessary to write the mode's
1067 * string representation (as returned by reading from /sys/power/disk) back
1068 * into /sys/power/disk.
1da177e4
LT
1069 */
1070
386f275f
KS
1071static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
1072 char *buf)
1da177e4 1073{
f0ced9b2
JB
1074 int i;
1075 char *start = buf;
1076
a6e15a39
KC
1077 if (!hibernation_available())
1078 return sprintf(buf, "[disabled]\n");
1079
a3d25c27
RW
1080 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
1081 if (!hibernation_modes[i])
f0ced9b2
JB
1082 continue;
1083 switch (i) {
a3d25c27
RW
1084 case HIBERNATION_SHUTDOWN:
1085 case HIBERNATION_REBOOT:
62c552cc
BS
1086#ifdef CONFIG_SUSPEND
1087 case HIBERNATION_SUSPEND:
1088#endif
fe12c00d 1089 case HIBERNATION_TEST_RESUME:
f0ced9b2 1090 break;
a3d25c27
RW
1091 case HIBERNATION_PLATFORM:
1092 if (hibernation_ops)
f0ced9b2
JB
1093 break;
1094 /* not a valid mode, continue with loop */
1095 continue;
1096 }
a3d25c27
RW
1097 if (i == hibernation_mode)
1098 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
f0ced9b2 1099 else
a3d25c27 1100 buf += sprintf(buf, "%s ", hibernation_modes[i]);
f0ced9b2
JB
1101 }
1102 buf += sprintf(buf, "\n");
1103 return buf-start;
1da177e4
LT
1104}
1105
386f275f
KS
1106static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
1107 const char *buf, size_t n)
1da177e4 1108{
5950e5d5
PZ
1109 int mode = HIBERNATION_INVALID;
1110 unsigned int sleep_flags;
1da177e4 1111 int error = 0;
1da177e4
LT
1112 int len;
1113 char *p;
5950e5d5 1114 int i;
1da177e4 1115
a6e15a39
KC
1116 if (!hibernation_available())
1117 return -EPERM;
1118
1da177e4
LT
1119 p = memchr(buf, '\n', n);
1120 len = p ? p - buf : n;
1121
5950e5d5 1122 sleep_flags = lock_system_sleep();
a3d25c27 1123 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
8d98a690
RW
1124 if (len == strlen(hibernation_modes[i])
1125 && !strncmp(buf, hibernation_modes[i], len)) {
1da177e4
LT
1126 mode = i;
1127 break;
1128 }
1129 }
a3d25c27 1130 if (mode != HIBERNATION_INVALID) {
fe0c935a 1131 switch (mode) {
a3d25c27
RW
1132 case HIBERNATION_SHUTDOWN:
1133 case HIBERNATION_REBOOT:
62c552cc
BS
1134#ifdef CONFIG_SUSPEND
1135 case HIBERNATION_SUSPEND:
1136#endif
fe12c00d 1137 case HIBERNATION_TEST_RESUME:
a3d25c27 1138 hibernation_mode = mode;
fe0c935a 1139 break;
a3d25c27
RW
1140 case HIBERNATION_PLATFORM:
1141 if (hibernation_ops)
1142 hibernation_mode = mode;
1da177e4
LT
1143 else
1144 error = -EINVAL;
1145 }
a3d25c27 1146 } else
1da177e4
LT
1147 error = -EINVAL;
1148
a3d25c27 1149 if (!error)
8d8b2441
RW
1150 pm_pr_dbg("Hibernation mode set to '%s'\n",
1151 hibernation_modes[mode]);
5950e5d5 1152 unlock_system_sleep(sleep_flags);
1da177e4
LT
1153 return error ? error : n;
1154}
1155
1156power_attr(disk);
1157
386f275f
KS
1158static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1159 char *buf)
1da177e4 1160{
6ada7ba2 1161 return sprintf(buf, "%d:%d\n", MAJOR(swsusp_resume_device),
1da177e4
LT
1162 MINOR(swsusp_resume_device));
1163}
1164
386f275f
KS
1165static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1166 const char *buf, size_t n)
1da177e4 1167{
5950e5d5 1168 unsigned int sleep_flags;
421a5fa1
SC
1169 int len = n;
1170 char *name;
5950e5d5 1171 dev_t res;
1da177e4 1172
421a5fa1
SC
1173 if (len && buf[len-1] == '\n')
1174 len--;
1175 name = kstrndup(buf, len, GFP_KERNEL);
1176 if (!name)
1177 return -ENOMEM;
1da177e4 1178
421a5fa1
SC
1179 res = name_to_dev_t(name);
1180 kfree(name);
1181 if (!res)
1182 return -EINVAL;
1da177e4 1183
5950e5d5 1184 sleep_flags = lock_system_sleep();
a576219a 1185 swsusp_resume_device = res;
5950e5d5
PZ
1186 unlock_system_sleep(sleep_flags);
1187
7a7b99bf
LS
1188 pm_pr_dbg("Configured hibernation resume from disk to %u\n",
1189 swsusp_resume_device);
a576219a
AM
1190 noresume = 0;
1191 software_resume();
421a5fa1 1192 return n;
1da177e4
LT
1193}
1194
1195power_attr(resume);
1196
35506467
ML
1197static ssize_t resume_offset_show(struct kobject *kobj,
1198 struct kobj_attribute *attr, char *buf)
1199{
1200 return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
1201}
1202
1203static ssize_t resume_offset_store(struct kobject *kobj,
1204 struct kobj_attribute *attr, const char *buf,
1205 size_t n)
1206{
1207 unsigned long long offset;
1208 int rc;
1209
1210 rc = kstrtoull(buf, 0, &offset);
1211 if (rc)
1212 return rc;
1213 swsusp_resume_block = offset;
1214
1215 return n;
1216}
1217
1218power_attr(resume_offset);
1219
386f275f
KS
1220static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1221 char *buf)
ca0aec0f 1222{
853609b6 1223 return sprintf(buf, "%lu\n", image_size);
ca0aec0f
RW
1224}
1225
386f275f
KS
1226static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1227 const char *buf, size_t n)
ca0aec0f 1228{
853609b6 1229 unsigned long size;
ca0aec0f 1230
853609b6 1231 if (sscanf(buf, "%lu", &size) == 1) {
ca0aec0f
RW
1232 image_size = size;
1233 return n;
1234 }
1235
1236 return -EINVAL;
1237}
1238
1239power_attr(image_size);
1240
ddeb6487
RW
1241static ssize_t reserved_size_show(struct kobject *kobj,
1242 struct kobj_attribute *attr, char *buf)
1243{
1244 return sprintf(buf, "%lu\n", reserved_size);
1245}
1246
1247static ssize_t reserved_size_store(struct kobject *kobj,
1248 struct kobj_attribute *attr,
1249 const char *buf, size_t n)
1250{
1251 unsigned long size;
1252
1253 if (sscanf(buf, "%lu", &size) == 1) {
1254 reserved_size = size;
1255 return n;
1256 }
1257
1258 return -EINVAL;
1259}
1260
1261power_attr(reserved_size);
1262
6ada7ba2 1263static struct attribute *g[] = {
1da177e4 1264 &disk_attr.attr,
35506467 1265 &resume_offset_attr.attr,
1da177e4 1266 &resume_attr.attr,
ca0aec0f 1267 &image_size_attr.attr,
ddeb6487 1268 &reserved_size_attr.attr,
1da177e4
LT
1269 NULL,
1270};
1271
1272
59494fe2 1273static const struct attribute_group attr_group = {
1da177e4
LT
1274 .attrs = g,
1275};
1276
1277
1278static int __init pm_disk_init(void)
1279{
d76e15fb 1280 return sysfs_create_group(power_kobj, &attr_group);
1da177e4
LT
1281}
1282
1283core_initcall(pm_disk_init);
1284
1285
1286static int __init resume_setup(char *str)
1287{
1288 if (noresume)
1289 return 1;
1290
6ada7ba2 1291 strncpy(resume_file, str, 255);
1da177e4
LT
1292 return 1;
1293}
1294
9a154d9d
RW
1295static int __init resume_offset_setup(char *str)
1296{
1297 unsigned long long offset;
1298
1299 if (noresume)
1300 return 1;
1301
1302 if (sscanf(str, "%llu", &offset) == 1)
1303 swsusp_resume_block = offset;
1304
1305 return 1;
1306}
1307
f996fc96
BS
1308static int __init hibernate_setup(char *str)
1309{
2f88e41a 1310 if (!strncmp(str, "noresume", 8)) {
f996fc96 1311 noresume = 1;
2f88e41a 1312 } else if (!strncmp(str, "nocompress", 10)) {
f996fc96 1313 nocompress = 1;
2f88e41a 1314 } else if (!strncmp(str, "no", 2)) {
a6e15a39
KC
1315 noresume = 1;
1316 nohibernate = 1;
0f5bf6d0 1317 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
4c0b6c10
RW
1318 && !strncmp(str, "protect_image", 13)) {
1319 enable_restore_image_protection();
a6e15a39 1320 }
f996fc96
BS
1321 return 1;
1322}
1323
1da177e4
LT
1324static int __init noresume_setup(char *str)
1325{
1326 noresume = 1;
1327 return 1;
1328}
1329
6f8d7022
BS
1330static int __init resumewait_setup(char *str)
1331{
1332 resume_wait = 1;
1333 return 1;
1334}
1335
f126f733
BS
1336static int __init resumedelay_setup(char *str)
1337{
f6514be5 1338 int rc = kstrtouint(str, 0, &resume_delay);
317cf7e5
FF
1339
1340 if (rc)
ba7ffcd4 1341 pr_warn("resumedelay: bad option string '%s'\n", str);
f126f733
BS
1342 return 1;
1343}
1344
a6e15a39
KC
1345static int __init nohibernate_setup(char *str)
1346{
1347 noresume = 1;
1348 nohibernate = 1;
1349 return 1;
1350}
1351
1da177e4 1352__setup("noresume", noresume_setup);
9a154d9d 1353__setup("resume_offset=", resume_offset_setup);
1da177e4 1354__setup("resume=", resume_setup);
f996fc96 1355__setup("hibernate=", hibernate_setup);
6f8d7022 1356__setup("resumewait", resumewait_setup);
f126f733 1357__setup("resumedelay=", resumedelay_setup);
a6e15a39 1358__setup("nohibernate", nohibernate_setup);