blob: b8be5c803cdda568b4f6ff31aacb9731e7461db0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Rafael J. Wysocki8b759b82009-06-10 01:27:49 +02002 * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
Pavel Macheka2531292010-07-18 14:27:13 +02006 * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz>
Rafael J. Wysocki8b759b82009-06-10 01:27:49 +02007 * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
Bojan Smojver62c552c2012-06-16 00:09:58 +02008 * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This file is released under the GPLv2.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040013#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/suspend.h>
15#include <linux/syscalls.h>
16#include <linux/reboot.h>
17#include <linux/string.h>
18#include <linux/device.h>
Barry Song6f8d7022011-10-06 20:34:46 +020019#include <linux/async.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h>
21#include <linux/fs.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070022#include <linux/mount.h>
Eric W. Biederman88d10bb2005-09-22 21:43:46 -070023#include <linux/pm.h>
Ingo Molnar38b8d202017-02-08 18:51:31 +010024#include <linux/nmi.h>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070025#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070026#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080027#include <linux/freezer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/gfp.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010029#include <linux/syscore_ops.h>
Minho Ban2df83fa2012-05-14 21:45:31 +020030#include <linux/ctype.h>
31#include <linux/genhd.h>
Tina Ruchandanidb597602014-10-30 11:04:53 -070032#include <linux/ktime.h>
Todd E Brandtbb3632c2014-06-06 05:40:17 -070033#include <trace/events/power.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "power.h"
36
37
Barry Songd231ff12011-10-11 23:29:18 -070038static int nocompress;
39static int noresume;
Kees Cooka6e15a32014-06-13 13:30:35 -070040static int nohibernate;
Barry Songd231ff12011-10-11 23:29:18 -070041static int resume_wait;
Dan Carpenterf6514be2014-05-14 19:08:46 +030042static unsigned int resume_delay;
Adrian Bunk47a460d2008-02-04 22:30:06 -080043static char resume_file[256] = CONFIG_PM_STD_PARTITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080045sector_t swsusp_resume_block;
Andi Kleend6efc2f2013-08-05 15:02:49 -070046__visible int in_suspend __nosavedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070048enum {
49 HIBERNATION_INVALID,
50 HIBERNATION_PLATFORM,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070051 HIBERNATION_SHUTDOWN,
52 HIBERNATION_REBOOT,
Bojan Smojver62c552c2012-06-16 00:09:58 +020053#ifdef CONFIG_SUSPEND
54 HIBERNATION_SUSPEND,
55#endif
Chen Yufe12c002016-07-22 10:30:47 +080056 HIBERNATION_TEST_RESUME,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070057 /* keep last */
58 __HIBERNATION_AFTER_LAST
59};
60#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
61#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
62
63static int hibernation_mode = HIBERNATION_SHUTDOWN;
64
Srivatsa S. Bhat97819a22011-12-01 22:33:10 +010065bool freezer_test_done;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +010066
Lionel Debroux073ef1f2010-11-09 21:48:49 +010067static const struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070068
Kees Cooka6e15a32014-06-13 13:30:35 -070069bool hibernation_available(void)
70{
71 return (nohibernate == 0);
72}
73
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070074/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +020075 * hibernation_set_ops - Set the global hibernate operations.
76 * @ops: Hibernation operations to use in subsequent hibernation transitions.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070077 */
Lionel Debroux073ef1f2010-11-09 21:48:49 +010078void hibernation_set_ops(const struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070079{
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +010080 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
81 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
MyungJoo Ham5729c632010-11-26 23:07:48 +010082 && ops->restore_cleanup && ops->leave)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070083 WARN_ON(1);
84 return;
85 }
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010086 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070087 hibernation_ops = ops;
88 if (ops)
89 hibernation_mode = HIBERNATION_PLATFORM;
90 else if (hibernation_mode == HIBERNATION_PLATFORM)
91 hibernation_mode = HIBERNATION_SHUTDOWN;
92
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010093 unlock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070094}
Leonardo Potenzae0c78552013-11-19 12:27:41 +000095EXPORT_SYMBOL_GPL(hibernation_set_ops);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070096
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +010097static bool entering_platform_hibernation;
98
99bool system_entering_hibernation(void)
100{
101 return entering_platform_hibernation;
102}
103EXPORT_SYMBOL(system_entering_hibernation);
104
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100105#ifdef CONFIG_PM_DEBUG
106static void hibernation_debug_sleep(void)
107{
108 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
109 mdelay(5000);
110}
111
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100112static int hibernation_test(int level)
113{
114 if (pm_test_level == level) {
115 hibernation_debug_sleep();
116 return 1;
117 }
118 return 0;
119}
120#else /* !CONFIG_PM_DEBUG */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100121static int hibernation_test(int level) { return 0; }
122#endif /* !CONFIG_PM_DEBUG */
123
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700124/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200125 * platform_begin - Call platform to start hibernation.
126 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700127 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100128static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700129{
130 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100131 hibernation_ops->begin() : 0;
132}
133
134/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200135 * platform_end - Call platform to finish transition to the working state.
136 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100137 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100138static void platform_end(int platform_mode)
139{
140 if (platform_mode && hibernation_ops)
141 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700142}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200145 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
146 * @platform_mode: Whether or not to use the platform driver.
147 *
148 * Use the platform driver to prepare the system for creating a hibernate image,
149 * if so configured, and return an error code if that fails.
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800150 */
151
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700152static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800153{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700154 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700155 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800156}
157
158/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200159 * platform_leave - Call platform to prepare a transition to the working state.
160 * @platform_mode: Whether or not to use the platform driver.
161 *
162 * Use the platform driver prepare to prepare the machine for switching to the
163 * normal mode of operation.
164 *
165 * This routine is called on one CPU with interrupts disabled.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700166 */
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700167static void platform_leave(int platform_mode)
168{
169 if (platform_mode && hibernation_ops)
170 hibernation_ops->leave();
171}
172
173/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200174 * platform_finish - Call platform to switch the system to the working state.
175 * @platform_mode: Whether or not to use the platform driver.
176 *
177 * Use the platform driver to switch the machine to the normal mode of
178 * operation.
179 *
180 * This routine must be called after platform_prepare().
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700181 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700182static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700183{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700184 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700185 hibernation_ops->finish();
186}
187
188/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200189 * platform_pre_restore - Prepare for hibernate image restoration.
190 * @platform_mode: Whether or not to use the platform driver.
191 *
192 * Use the platform driver to prepare the system for resume from a hibernation
193 * image.
194 *
195 * If the restore fails after this function has been called,
196 * platform_restore_cleanup() must be called.
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700197 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700198static int platform_pre_restore(int platform_mode)
199{
200 return (platform_mode && hibernation_ops) ?
201 hibernation_ops->pre_restore() : 0;
202}
203
204/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200205 * platform_restore_cleanup - Switch to the working state after failing restore.
206 * @platform_mode: Whether or not to use the platform driver.
207 *
208 * Use the platform driver to switch the system to the normal mode of operation
209 * after a failing restore.
210 *
211 * If platform_pre_restore() has been called before the failing restore, this
212 * function must be called too, regardless of the result of
213 * platform_pre_restore().
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700214 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700215static void platform_restore_cleanup(int platform_mode)
216{
217 if (platform_mode && hibernation_ops)
218 hibernation_ops->restore_cleanup();
219}
220
221/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200222 * platform_recover - Recover from a failure to suspend devices.
223 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200224 */
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200225static void platform_recover(int platform_mode)
226{
227 if (platform_mode && hibernation_ops && hibernation_ops->recover)
228 hibernation_ops->recover();
229}
230
231/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200232 * swsusp_show_speed - Print time elapsed between two events during hibernation.
233 * @start: Starting event.
234 * @stop: Final event.
235 * @nr_pages: Number of memory pages processed between @start and @stop.
236 * @msg: Additional diagnostic message to print.
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100237 */
Tina Ruchandanidb597602014-10-30 11:04:53 -0700238void swsusp_show_speed(ktime_t start, ktime_t stop,
239 unsigned nr_pages, char *msg)
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100240{
Tina Ruchandanidb597602014-10-30 11:04:53 -0700241 ktime_t diff;
Chen Gang4881f602014-04-25 08:44:59 +0800242 u64 elapsed_centisecs64;
243 unsigned int centisecs;
244 unsigned int k;
245 unsigned int kps;
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100246
Tina Ruchandanidb597602014-10-30 11:04:53 -0700247 diff = ktime_sub(stop, start);
248 elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC);
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100249 centisecs = elapsed_centisecs64;
250 if (centisecs == 0)
251 centisecs = 1; /* avoid div-by-zero */
252 k = nr_pages * (PAGE_SIZE / 1024);
253 kps = (k * 100) / centisecs;
Chen Gang4881f602014-04-25 08:44:59 +0800254 printk(KERN_INFO "PM: %s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100255 msg, k,
256 centisecs / 100, centisecs % 100,
257 kps / 1000, (kps % 1000) / 10);
258}
259
260/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200261 * create_image - Create a hibernation image.
262 * @platform_mode: Whether or not to use the platform driver.
263 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100264 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
265 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200266 *
267 * Control reappears in this routine after the subsequent restore.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700268 */
Adrian Bunk47a460d2008-02-04 22:30:06 -0800269static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700270{
271 int error;
272
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100273 error = dpm_suspend_end(PMSG_FREEZE);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700274 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100275 printk(KERN_ERR "PM: Some devices failed to power down, "
276 "aborting hibernation\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200277 return error;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700278 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100279
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100280 error = platform_pre_snapshot(platform_mode);
281 if (error || hibernation_test(TEST_PLATFORM))
282 goto Platform_finish;
283
284 error = disable_nonboot_cpus();
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100285 if (error || hibernation_test(TEST_CPUS))
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100286 goto Enable_cpus;
287
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100288 local_irq_disable();
289
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200290 error = syscore_suspend();
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100291 if (error) {
Bjorn Helgaas44840792009-05-15 23:30:50 +0200292 printk(KERN_ERR "PM: Some system devices failed to power down, "
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100293 "aborting hibernation\n");
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100294 goto Enable_irqs;
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100295 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700296
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100297 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100298 goto Power_up;
299
300 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700301 save_processor_state();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700302 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700303 error = swsusp_arch_suspend();
Thomas Garnier62822e22016-08-11 14:49:29 -0700304 /* Restore control flow magically appears here */
305 restore_processor_state();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700306 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700307 if (error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100308 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
309 error);
Anisse Astier1ad14102016-09-09 10:43:32 +0200310 if (!in_suspend) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200311 events_check_enabled = false;
Anisse Astier1ad14102016-09-09 10:43:32 +0200312 clear_free_pages();
313 }
Bjørn Mork362e77d2013-12-04 16:06:58 +0100314
315 platform_leave(platform_mode);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100316
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100317 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100318 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100319
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100320 Enable_irqs:
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100321 local_irq_enable();
322
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100323 Enable_cpus:
324 enable_nonboot_cpus();
325
326 Platform_finish:
327 platform_finish(platform_mode);
328
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100329 dpm_resume_start(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200330 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100331
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700332 return error;
333}
334
335/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200336 * hibernation_snapshot - Quiesce devices and create a hibernation image.
337 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700338 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200339 * This routine must be called with pm_mutex held.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700340 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700341int hibernation_snapshot(int platform_mode)
342{
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100343 pm_message_t msg;
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100344 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700345
Lukas Wunner27614272016-03-23 00:11:20 +0100346 pm_suspend_clear_flags();
Zhang Rui3fe03132008-10-26 20:50:26 +0100347 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700348 if (error)
Rafael J. Wysockid074ee02010-07-07 23:43:35 +0200349 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700350
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200351 /* Preallocate image memory before shutting down devices. */
352 error = hibernate_preallocate_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700353 if (error)
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200354 goto Close;
355
356 error = freeze_kernel_threads();
357 if (error)
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100358 goto Cleanup;
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200359
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100360 if (hibernation_test(TEST_FREEZER)) {
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100361
362 /*
363 * Indicate to the caller that we are returning due to a
364 * successful freezer test.
365 */
366 freezer_test_done = true;
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100367 goto Thaw;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100368 }
369
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200370 error = dpm_prepare(PMSG_FREEZE);
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100371 if (error) {
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100372 dpm_complete(PMSG_RECOVER);
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100373 goto Thaw;
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100374 }
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700375
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700376 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100377 pm_restrict_gfp_mask();
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100378
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200379 error = dpm_suspend(PMSG_FREEZE);
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700380
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100381 if (error || hibernation_test(TEST_DEVICES))
382 platform_recover(platform_mode);
383 else
384 error = create_image(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700385
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100386 /*
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100387 * In the case that we call create_image() above, the control
388 * returns here (1) after the image has been created or the
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100389 * image creation has failed and (2) after a successful restore.
390 */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100391
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200392 /* We may need to release the preallocated image pages here. */
393 if (error || !in_suspend)
394 swsusp_free();
395
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200396 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
397 dpm_resume(msg);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100398
399 if (error || !in_suspend)
400 pm_restore_gfp_mask();
401
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700402 resume_console();
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200403 dpm_complete(msg);
404
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100405 Close:
406 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700407 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200408
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100409 Thaw:
410 thaw_kernel_threads();
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100411 Cleanup:
412 swsusp_free();
413 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700414}
415
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200416int __weak hibernate_resume_nonboot_cpu_disable(void)
417{
418 return disable_nonboot_cpus();
419}
420
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700421/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200422 * resume_target_kernel - Restore system state from a hibernation image.
423 * @platform_mode: Whether or not to use the platform driver.
424 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100425 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
426 * contents of highmem that have not been restored yet from the image and run
427 * the low-level code that will restore the remaining contents of memory and
428 * switch to the just restored target kernel.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100429 */
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100430static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100431{
432 int error;
433
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100434 error = dpm_suspend_end(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100435 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100436 printk(KERN_ERR "PM: Some devices failed to power down, "
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100437 "aborting resume\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200438 return error;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100439 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100440
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100441 error = platform_pre_restore(platform_mode);
442 if (error)
443 goto Cleanup;
444
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200445 error = hibernate_resume_nonboot_cpu_disable();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100446 if (error)
447 goto Enable_cpus;
448
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100449 local_irq_disable();
450
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200451 error = syscore_suspend();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100452 if (error)
453 goto Enable_irqs;
454
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100455 save_processor_state();
456 error = restore_highmem();
457 if (!error) {
458 error = swsusp_arch_resume();
459 /*
460 * The code below is only ever reached in case of a failure.
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200461 * Otherwise, execution continues at the place where
462 * swsusp_arch_suspend() was called.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100463 */
464 BUG_ON(!error);
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200465 /*
466 * This call to restore_highmem() reverts the changes made by
467 * the previous one.
468 */
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100469 restore_highmem();
470 }
471 /*
472 * The only reason why swsusp_arch_resume() can fail is memory being
473 * very tight, so we have to free it as soon as we can to avoid
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200474 * subsequent failures.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100475 */
476 swsusp_free();
477 restore_processor_state();
478 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100479
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100480 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100481
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100482 Enable_irqs:
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100483 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100484
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100485 Enable_cpus:
486 enable_nonboot_cpus();
487
488 Cleanup:
489 platform_restore_cleanup(platform_mode);
490
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100491 dpm_resume_start(PMSG_RECOVER);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100492
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100493 return error;
494}
495
496/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200497 * hibernation_restore - Quiesce devices and restore from a hibernation image.
498 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700499 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200500 * This routine must be called with pm_mutex held. If it is successful, control
Barry Song21e82802011-09-27 22:05:44 +0200501 * reappears in the restored target kernel in hibernation_snapshot().
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700502 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700503int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700504{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100505 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700506
507 pm_prepare_console();
508 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100509 pm_restrict_gfp_mask();
Alan Sternd1616302009-05-24 22:05:42 +0200510 error = dpm_suspend_start(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700511 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100512 error = resume_target_kernel(platform_mode);
Imre Deak94fb8232014-10-24 20:29:10 +0300513 /*
514 * The above should either succeed and jump to the new kernel,
515 * or return with an error. Otherwise things are just
516 * undefined, so let's be paranoid.
517 */
518 BUG_ON(!error);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700519 }
Imre Deak94fb8232014-10-24 20:29:10 +0300520 dpm_resume_end(PMSG_RECOVER);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100521 pm_restore_gfp_mask();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700522 resume_console();
523 pm_restore_console();
524 return error;
525}
526
527/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200528 * hibernation_platform_enter - Power off the system using the platform driver.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700529 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700530int hibernation_platform_enter(void)
531{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100532 int error;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700533
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700534 if (!hibernation_ops)
535 return -ENOSYS;
536
537 /*
538 * We have cancelled the power transition by running
539 * hibernation_ops->finish() before saving the image, so we should let
540 * the firmware know that we're going to enter the sleep state after all
541 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100542 error = hibernation_ops->begin();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700543 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100544 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700545
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100546 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700547 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +0200548 error = dpm_suspend_start(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200549 if (error) {
550 if (hibernation_ops->recover)
551 hibernation_ops->recover();
552 goto Resume_devices;
553 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700554
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100555 error = dpm_suspend_end(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100556 if (error)
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200557 goto Resume_devices;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100558
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100559 error = hibernation_ops->prepare();
560 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200561 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100562
563 error = disable_nonboot_cpus();
564 if (error)
Vitaly Kuznetsov8c506602015-06-24 16:02:06 +0200565 goto Enable_cpus;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100566
567 local_irq_disable();
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100568 syscore_suspend();
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100569 if (pm_wakeup_pending()) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200570 error = -EAGAIN;
571 goto Power_up;
572 }
573
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100574 hibernation_ops->enter();
575 /* We should never get here */
576 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700577
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200578 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100579 syscore_resume();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200580 local_irq_enable();
Vitaly Kuznetsov8c506602015-06-24 16:02:06 +0200581
582 Enable_cpus:
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200583 enable_nonboot_cpus();
584
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200585 Platform_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700586 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100587
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100588 dpm_resume_start(PMSG_RESTORE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100589
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700590 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100591 entering_platform_hibernation = false;
Alan Sternd1616302009-05-24 22:05:42 +0200592 dpm_resume_end(PMSG_RESTORE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700593 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100594
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100595 Close:
596 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100597
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700598 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700599}
600
601/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200602 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200604 * Use the platform driver, if configured, to put the system into the sleep
605 * state corresponding to hibernation, or try to power it off or reboot,
606 * depending on the value of hibernation_mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700608static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Bojan Smojver62c552c2012-06-16 00:09:58 +0200610#ifdef CONFIG_SUSPEND
611 int error;
612#endif
613
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700614 switch (hibernation_mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700615 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600616 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700618 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700619 hibernation_platform_enter();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700620 case HIBERNATION_SHUTDOWN:
Sebastian Capella2c730782014-04-21 17:30:46 -0700621 if (pm_power_off)
622 kernel_power_off();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700623 break;
Bojan Smojver62c552c2012-06-16 00:09:58 +0200624#ifdef CONFIG_SUSPEND
625 case HIBERNATION_SUSPEND:
626 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
627 if (error) {
628 if (hibernation_ops)
629 hibernation_mode = HIBERNATION_PLATFORM;
630 else
631 hibernation_mode = HIBERNATION_SHUTDOWN;
632 power_down();
633 }
634 /*
635 * Restore swap signature.
636 */
637 error = swsusp_unmark();
638 if (error)
639 printk(KERN_ERR "PM: Swap will be unusable! "
640 "Try swapon -a.\n");
641 return;
642#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600644 kernel_halt();
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700645 /*
646 * Valid image is on the disk, if we continue we risk serious data
647 * corruption after resume.
648 */
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100649 printk(KERN_CRIT "PM: Please power down manually\n");
Sebastian Capella2c730782014-04-21 17:30:46 -0700650 while (1)
651 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Chen Yufe12c002016-07-22 10:30:47 +0800654static int load_image_and_restore(void)
655{
656 int error;
657 unsigned int flags;
658
659 pr_debug("PM: Loading hibernation image.\n");
660
661 lock_device_hotplug();
662 error = create_basic_memory_bitmaps();
663 if (error)
664 goto Unlock;
665
666 error = swsusp_read(&flags);
667 swsusp_close(FMODE_READ);
668 if (!error)
669 hibernation_restore(flags & SF_PLATFORM_MODE);
670
671 printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n");
672 swsusp_free();
673 free_basic_memory_bitmaps();
674 Unlock:
675 unlock_device_hotplug();
676
677 return error;
678}
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200681 * hibernate - Carry out system hibernation, including saving the image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700683int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700685 int error, nr_calls = 0;
Chen Yufe12c002016-07-22 10:30:47 +0800686 bool snapshot_test = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Kees Cooka6e15a32014-06-13 13:30:35 -0700688 if (!hibernation_available()) {
689 pr_debug("PM: Hibernation not available.\n");
690 return -EPERM;
691 }
692
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100693 lock_system_sleep();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700694 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700695 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
696 error = -EBUSY;
697 goto Unlock;
698 }
699
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100700 pm_prepare_console();
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700701 error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
702 if (error) {
703 nr_calls--;
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700704 goto Exit;
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700705 }
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700706
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100707 printk(KERN_INFO "PM: Syncing filesystems ... ");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700708 sys_sync();
709 printk("done.\n");
710
Tejun Heo03afed82011-11-21 12:32:24 -0800711 error = freeze_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700712 if (error)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200713 goto Exit;
714
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200715 lock_device_hotplug();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200716 /* Allocate memory management structures */
717 error = create_basic_memory_bitmaps();
718 if (error)
719 goto Thaw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700721 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100722 if (error || freezer_test_done)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200723 goto Free_bitmaps;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200724
725 if (in_suspend) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700726 unsigned int flags = 0;
727
728 if (hibernation_mode == HIBERNATION_PLATFORM)
729 flags |= SF_PLATFORM_MODE;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200730 if (nocompress)
731 flags |= SF_NOCOMPRESS_MODE;
Bojan Smojver081a9d02011-10-13 23:58:07 +0200732 else
733 flags |= SF_CRC32_MODE;
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700736 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700737 swsusp_free();
Chen Yufe12c002016-07-22 10:30:47 +0800738 if (!error) {
739 if (hibernation_mode == HIBERNATION_TEST_RESUME)
740 snapshot_test = true;
741 else
742 power_down();
743 }
MyungJoo Ham5262a472010-11-26 23:07:56 +0100744 in_suspend = 0;
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100745 pm_restore_gfp_mask();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800746 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800748 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200749
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200750 Free_bitmaps:
751 free_basic_memory_bitmaps();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800752 Thaw:
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200753 unlock_device_hotplug();
Chen Yufe12c002016-07-22 10:30:47 +0800754 if (snapshot_test) {
755 pr_debug("PM: Checking hibernation image\n");
756 error = swsusp_check();
757 if (!error)
758 error = load_image_and_restore();
759 }
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100760 thaw_processes();
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100761
762 /* Don't bother checking whether freezer_test_done is true */
763 freezer_test_done = false;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700764 Exit:
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700765 __pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100766 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700767 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700768 Unlock:
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100769 unlock_system_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return error;
771}
772
773
774/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200775 * software_resume - Resume from a saved hibernation image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200777 * This routine is called as a late initcall, when all devices have been
778 * discovered and initialized already.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200780 * The image reading code is called to see if there is a hibernation image
781 * available for reading. If that is the case, devices are quiesced and the
782 * contents of memory is restored from the saved image.
783 *
784 * If this is successful, control reappears in the restored target kernel in
Geliang Tangd439e642015-09-29 20:36:58 -0700785 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200786 * attempts to recover gracefully and make the kernel return to the normal mode
787 * of operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789static int software_resume(void)
790{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700791 int error, nr_calls = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Johannes Berg60a0d232007-11-14 17:00:16 -0800793 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100794 * If the user said "noresume".. bail out early.
795 */
Kees Cooka6e15a32014-06-13 13:30:35 -0700796 if (noresume || !hibernation_available())
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100797 return 0;
798
799 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800800 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
801 * is configured into the kernel. Since the regular hibernate
802 * trigger path is via sysfs which takes a buffer mutex before
803 * calling hibernate functions (which take pm_mutex) this can
804 * cause lockdep to complain about a possible ABBA deadlock
805 * which cannot happen since we're in the boot code here and
806 * sysfs can't be invoked yet. Therefore, we use a subclass
807 * here to avoid lockdep complaining.
808 */
809 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200810
811 if (swsusp_resume_device)
812 goto Check_image;
813
814 if (!strlen(resume_file)) {
815 error = -ENOENT;
816 goto Unlock;
817 }
818
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200819 pr_debug("PM: Checking hibernation image partition %s\n", resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200820
Barry Songf126f732011-10-10 23:38:41 +0200821 if (resume_delay) {
822 printk(KERN_INFO "Waiting %dsec before reading resume device...\n",
823 resume_delay);
824 ssleep(resume_delay);
825 }
826
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200827 /* Check if the device is there */
828 swsusp_resume_device = name_to_dev_t(resume_file);
Minho Ban2df83fa2012-05-14 21:45:31 +0200829
830 /*
831 * name_to_dev_t is ineffective to verify parition if resume_file is in
832 * integer format. (e.g. major:minor)
833 */
834 if (isdigit(resume_file[0]) && resume_wait) {
835 int partno;
836 while (!get_gendisk(swsusp_resume_device, &partno))
837 msleep(10);
838 }
839
Pavel Machek3efa1472005-07-07 17:56:43 -0700840 if (!swsusp_resume_device) {
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100841 /*
842 * Some device discovery might still be in progress; we need
843 * to wait for this to finish.
844 */
845 wait_for_device_probe();
Barry Song6f8d7022011-10-06 20:34:46 +0200846
847 if (resume_wait) {
848 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
849 msleep(10);
850 async_synchronize_full();
851 }
852
Pavel Machek3efa1472005-07-07 17:56:43 -0700853 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200854 if (!swsusp_resume_device) {
855 error = -ENODEV;
856 goto Unlock;
857 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700858 }
859
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200860 Check_image:
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200861 pr_debug("PM: Hibernation image partition %d:%d present\n",
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200862 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200864 pr_debug("PM: Looking for hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800865 error = swsusp_check();
866 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700867 goto Unlock;
868
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700869 /* The snapshot device should not be opened while we're running */
870 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
871 error = -EBUSY;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200872 swsusp_close(FMODE_READ);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700873 goto Unlock;
874 }
875
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100876 pm_prepare_console();
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700877 error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
878 if (error) {
879 nr_calls--;
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200880 goto Close_Finish;
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700881 }
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 pr_debug("PM: Preparing processes for restore.\n");
Tejun Heo03afed82011-11-21 12:32:24 -0800884 error = freeze_processes();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200885 if (error)
886 goto Close_Finish;
Chen Yufe12c002016-07-22 10:30:47 +0800887 error = load_image_and_restore();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200888 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700889 Finish:
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700890 __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100891 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700892 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700893 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700894 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800895 mutex_unlock(&pm_mutex);
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200896 pr_debug("PM: Hibernation image not present or could not be loaded.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700897 return error;
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200898 Close_Finish:
Jiri Slaby76b57e62009-10-07 22:37:35 +0200899 swsusp_close(FMODE_READ);
900 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
Russ Dilld3c345d2013-10-24 14:25:26 +0100903late_initcall_sync(software_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700906static const char * const hibernation_modes[] = {
907 [HIBERNATION_PLATFORM] = "platform",
908 [HIBERNATION_SHUTDOWN] = "shutdown",
909 [HIBERNATION_REBOOT] = "reboot",
Bojan Smojver62c552c2012-06-16 00:09:58 +0200910#ifdef CONFIG_SUSPEND
911 [HIBERNATION_SUSPEND] = "suspend",
912#endif
Chen Yufe12c002016-07-22 10:30:47 +0800913 [HIBERNATION_TEST_RESUME] = "test_resume",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914};
915
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200916/*
917 * /sys/power/disk - Control hibernation mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200919 * Hibernation can be handled in several ways. There are a few different ways
920 * to put the system into the sleep state: using the platform driver (e.g. ACPI
921 * or other hibernation_ops), powering it off or rebooting it (for testing
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100922 * mostly).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200924 * The sysfs file /sys/power/disk provides an interface for selecting the
925 * hibernation mode to use. Reading from this file causes the available modes
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100926 * to be printed. There are 3 modes that can be supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 * 'platform'
929 * 'shutdown'
930 * 'reboot'
931 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200932 * If a platform hibernation driver is in use, 'platform' will be supported
933 * and will be used by default. Otherwise, 'shutdown' will be used by default.
934 * The selected option (i.e. the one corresponding to the current value of
935 * hibernation_mode) is enclosed by a square bracket.
936 *
937 * To select a given hibernation mode it is necessary to write the mode's
938 * string representation (as returned by reading from /sys/power/disk) back
939 * into /sys/power/disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 */
941
Kay Sievers386f2752007-11-02 13:47:53 +0100942static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
943 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700945 int i;
946 char *start = buf;
947
Kees Cooka6e15a32014-06-13 13:30:35 -0700948 if (!hibernation_available())
949 return sprintf(buf, "[disabled]\n");
950
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700951 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
952 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700953 continue;
954 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700955 case HIBERNATION_SHUTDOWN:
956 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +0200957#ifdef CONFIG_SUSPEND
958 case HIBERNATION_SUSPEND:
959#endif
Chen Yufe12c002016-07-22 10:30:47 +0800960 case HIBERNATION_TEST_RESUME:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700961 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700962 case HIBERNATION_PLATFORM:
963 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700964 break;
965 /* not a valid mode, continue with loop */
966 continue;
967 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700968 if (i == hibernation_mode)
969 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700970 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700971 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700972 }
973 buf += sprintf(buf, "\n");
974 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Kay Sievers386f2752007-11-02 13:47:53 +0100977static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
978 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 int error = 0;
981 int i;
982 int len;
983 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700984 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Kees Cooka6e15a32014-06-13 13:30:35 -0700986 if (!hibernation_available())
987 return -EPERM;
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 p = memchr(buf, '\n', n);
990 len = p ? p - buf : n;
991
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100992 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700993 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700994 if (len == strlen(hibernation_modes[i])
995 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 mode = i;
997 break;
998 }
999 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001000 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001001 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001002 case HIBERNATION_SHUTDOWN:
1003 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +02001004#ifdef CONFIG_SUSPEND
1005 case HIBERNATION_SUSPEND:
1006#endif
Chen Yufe12c002016-07-22 10:30:47 +08001007 case HIBERNATION_TEST_RESUME:
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001008 hibernation_mode = mode;
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001009 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001010 case HIBERNATION_PLATFORM:
1011 if (hibernation_ops)
1012 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 else
1014 error = -EINVAL;
1015 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001016 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 error = -EINVAL;
1018
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001019 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001020 pr_debug("PM: Hibernation mode set to '%s'\n",
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001021 hibernation_modes[mode]);
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001022 unlock_system_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return error ? error : n;
1024}
1025
1026power_attr(disk);
1027
Kay Sievers386f2752007-11-02 13:47:53 +01001028static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1029 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
1032 MINOR(swsusp_resume_device));
1033}
1034
Kay Sievers386f2752007-11-02 13:47:53 +01001035static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1036 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 dev_t res;
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001039 int len = n;
1040 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001042 if (len && buf[len-1] == '\n')
1043 len--;
1044 name = kstrndup(buf, len, GFP_KERNEL);
1045 if (!name)
1046 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001048 res = name_to_dev_t(name);
1049 kfree(name);
1050 if (!res)
1051 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001053 lock_system_sleep();
Andrew Mortona5762192006-01-06 00:09:50 -08001054 swsusp_resume_device = res;
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001055 unlock_system_sleep();
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001056 printk(KERN_INFO "PM: Starting manual resume from disk\n");
Andrew Mortona5762192006-01-06 00:09:50 -08001057 noresume = 0;
1058 software_resume();
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001059 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062power_attr(resume);
1063
Kay Sievers386f2752007-11-02 13:47:53 +01001064static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1065 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001066{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001067 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001068}
1069
Kay Sievers386f2752007-11-02 13:47:53 +01001070static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1071 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001072{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001073 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001074
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001075 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001076 image_size = size;
1077 return n;
1078 }
1079
1080 return -EINVAL;
1081}
1082
1083power_attr(image_size);
1084
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001085static ssize_t reserved_size_show(struct kobject *kobj,
1086 struct kobj_attribute *attr, char *buf)
1087{
1088 return sprintf(buf, "%lu\n", reserved_size);
1089}
1090
1091static ssize_t reserved_size_store(struct kobject *kobj,
1092 struct kobj_attribute *attr,
1093 const char *buf, size_t n)
1094{
1095 unsigned long size;
1096
1097 if (sscanf(buf, "%lu", &size) == 1) {
1098 reserved_size = size;
1099 return n;
1100 }
1101
1102 return -EINVAL;
1103}
1104
1105power_attr(reserved_size);
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107static struct attribute * g[] = {
1108 &disk_attr.attr,
1109 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001110 &image_size_attr.attr,
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001111 &reserved_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 NULL,
1113};
1114
1115
1116static struct attribute_group attr_group = {
1117 .attrs = g,
1118};
1119
1120
1121static int __init pm_disk_init(void)
1122{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -08001123 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
1126core_initcall(pm_disk_init);
1127
1128
1129static int __init resume_setup(char *str)
1130{
1131 if (noresume)
1132 return 1;
1133
1134 strncpy( resume_file, str, 255 );
1135 return 1;
1136}
1137
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001138static int __init resume_offset_setup(char *str)
1139{
1140 unsigned long long offset;
1141
1142 if (noresume)
1143 return 1;
1144
1145 if (sscanf(str, "%llu", &offset) == 1)
1146 swsusp_resume_block = offset;
1147
1148 return 1;
1149}
1150
Bojan Smojverf996fc92010-09-09 23:06:23 +02001151static int __init hibernate_setup(char *str)
1152{
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001153 if (!strncmp(str, "noresume", 8)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001154 noresume = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001155 } else if (!strncmp(str, "nocompress", 10)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001156 nocompress = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001157 } else if (!strncmp(str, "no", 2)) {
Kees Cooka6e15a32014-06-13 13:30:35 -07001158 noresume = 1;
1159 nohibernate = 1;
Laura Abbott0f5bf6d2017-02-06 16:31:58 -08001160 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
Rafael J. Wysocki4c0b6c12016-07-10 02:12:10 +02001161 && !strncmp(str, "protect_image", 13)) {
1162 enable_restore_image_protection();
Kees Cooka6e15a32014-06-13 13:30:35 -07001163 }
Bojan Smojverf996fc92010-09-09 23:06:23 +02001164 return 1;
1165}
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167static int __init noresume_setup(char *str)
1168{
1169 noresume = 1;
1170 return 1;
1171}
1172
Barry Song6f8d7022011-10-06 20:34:46 +02001173static int __init resumewait_setup(char *str)
1174{
1175 resume_wait = 1;
1176 return 1;
1177}
1178
Barry Songf126f732011-10-10 23:38:41 +02001179static int __init resumedelay_setup(char *str)
1180{
Dan Carpenterf6514be2014-05-14 19:08:46 +03001181 int rc = kstrtouint(str, 0, &resume_delay);
Fabian Frederick317cf7e2014-05-09 23:32:08 +02001182
1183 if (rc)
1184 return rc;
Barry Songf126f732011-10-10 23:38:41 +02001185 return 1;
1186}
1187
Kees Cooka6e15a32014-06-13 13:30:35 -07001188static int __init nohibernate_setup(char *str)
1189{
1190 noresume = 1;
1191 nohibernate = 1;
1192 return 1;
1193}
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001196__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197__setup("resume=", resume_setup);
Bojan Smojverf996fc92010-09-09 23:06:23 +02001198__setup("hibernate=", hibernate_setup);
Barry Song6f8d7022011-10-06 20:34:46 +02001199__setup("resumewait", resumewait_setup);
Barry Songf126f732011-10-10 23:38:41 +02001200__setup("resumedelay=", resumedelay_setup);
Kees Cooka6e15a32014-06-13 13:30:35 -07001201__setup("nohibernate", nohibernate_setup);