blob: 81695a492ebef2d810b7b2226e4da3718b14dd9f [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>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070024#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070025#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080026#include <linux/freezer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/gfp.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010028#include <linux/syscore_ops.h>
Minho Ban2df83fa2012-05-14 21:45:31 +020029#include <linux/ctype.h>
30#include <linux/genhd.h>
Tina Ruchandanidb597602014-10-30 11:04:53 -070031#include <linux/ktime.h>
Todd E Brandtbb3632c2014-06-06 05:40:17 -070032#include <trace/events/power.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "power.h"
35
36
Barry Songd231ff12011-10-11 23:29:18 -070037static int nocompress;
38static int noresume;
Kees Cooka6e15a32014-06-13 13:30:35 -070039static int nohibernate;
Barry Songd231ff12011-10-11 23:29:18 -070040static int resume_wait;
Dan Carpenterf6514be2014-05-14 19:08:46 +030041static unsigned int resume_delay;
Adrian Bunk47a460d2008-02-04 22:30:06 -080042static char resume_file[256] = CONFIG_PM_STD_PARTITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080044sector_t swsusp_resume_block;
Andi Kleend6efc2f2013-08-05 15:02:49 -070045__visible int in_suspend __nosavedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070047enum {
48 HIBERNATION_INVALID,
49 HIBERNATION_PLATFORM,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070050 HIBERNATION_SHUTDOWN,
51 HIBERNATION_REBOOT,
Bojan Smojver62c552c2012-06-16 00:09:58 +020052#ifdef CONFIG_SUSPEND
53 HIBERNATION_SUSPEND,
54#endif
Chen Yufe12c002016-07-22 10:30:47 +080055 HIBERNATION_TEST_RESUME,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070056 /* keep last */
57 __HIBERNATION_AFTER_LAST
58};
59#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
60#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
61
62static int hibernation_mode = HIBERNATION_SHUTDOWN;
63
Srivatsa S. Bhat97819a22011-12-01 22:33:10 +010064bool freezer_test_done;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +010065
Lionel Debroux073ef1f2010-11-09 21:48:49 +010066static const struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070067
Kees Cooka6e15a32014-06-13 13:30:35 -070068bool hibernation_available(void)
69{
70 return (nohibernate == 0);
71}
72
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070073/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +020074 * hibernation_set_ops - Set the global hibernate operations.
75 * @ops: Hibernation operations to use in subsequent hibernation transitions.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070076 */
Lionel Debroux073ef1f2010-11-09 21:48:49 +010077void hibernation_set_ops(const struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070078{
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +010079 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
80 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
MyungJoo Ham5729c632010-11-26 23:07:48 +010081 && ops->restore_cleanup && ops->leave)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070082 WARN_ON(1);
83 return;
84 }
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010085 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070086 hibernation_ops = ops;
87 if (ops)
88 hibernation_mode = HIBERNATION_PLATFORM;
89 else if (hibernation_mode == HIBERNATION_PLATFORM)
90 hibernation_mode = HIBERNATION_SHUTDOWN;
91
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010092 unlock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070093}
Leonardo Potenzae0c78552013-11-19 12:27:41 +000094EXPORT_SYMBOL_GPL(hibernation_set_ops);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070095
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +010096static bool entering_platform_hibernation;
97
98bool system_entering_hibernation(void)
99{
100 return entering_platform_hibernation;
101}
102EXPORT_SYMBOL(system_entering_hibernation);
103
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100104#ifdef CONFIG_PM_DEBUG
105static void hibernation_debug_sleep(void)
106{
107 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
108 mdelay(5000);
109}
110
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100111static int hibernation_test(int level)
112{
113 if (pm_test_level == level) {
114 hibernation_debug_sleep();
115 return 1;
116 }
117 return 0;
118}
119#else /* !CONFIG_PM_DEBUG */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100120static int hibernation_test(int level) { return 0; }
121#endif /* !CONFIG_PM_DEBUG */
122
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700123/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200124 * platform_begin - Call platform to start hibernation.
125 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700126 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100127static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700128{
129 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100130 hibernation_ops->begin() : 0;
131}
132
133/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200134 * platform_end - Call platform to finish transition to the working state.
135 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100136 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100137static void platform_end(int platform_mode)
138{
139 if (platform_mode && hibernation_ops)
140 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700141}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200144 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
145 * @platform_mode: Whether or not to use the platform driver.
146 *
147 * Use the platform driver to prepare the system for creating a hibernate image,
148 * if so configured, and return an error code if that fails.
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800149 */
150
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700151static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800152{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700153 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700154 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800155}
156
157/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200158 * platform_leave - Call platform to prepare a transition to the working state.
159 * @platform_mode: Whether or not to use the platform driver.
160 *
161 * Use the platform driver prepare to prepare the machine for switching to the
162 * normal mode of operation.
163 *
164 * This routine is called on one CPU with interrupts disabled.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700165 */
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700166static void platform_leave(int platform_mode)
167{
168 if (platform_mode && hibernation_ops)
169 hibernation_ops->leave();
170}
171
172/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200173 * platform_finish - Call platform to switch the system to the working state.
174 * @platform_mode: Whether or not to use the platform driver.
175 *
176 * Use the platform driver to switch the machine to the normal mode of
177 * operation.
178 *
179 * This routine must be called after platform_prepare().
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700180 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700181static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700182{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700183 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700184 hibernation_ops->finish();
185}
186
187/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200188 * platform_pre_restore - Prepare for hibernate image restoration.
189 * @platform_mode: Whether or not to use the platform driver.
190 *
191 * Use the platform driver to prepare the system for resume from a hibernation
192 * image.
193 *
194 * If the restore fails after this function has been called,
195 * platform_restore_cleanup() must be called.
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700196 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700197static int platform_pre_restore(int platform_mode)
198{
199 return (platform_mode && hibernation_ops) ?
200 hibernation_ops->pre_restore() : 0;
201}
202
203/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200204 * platform_restore_cleanup - Switch to the working state after failing restore.
205 * @platform_mode: Whether or not to use the platform driver.
206 *
207 * Use the platform driver to switch the system to the normal mode of operation
208 * after a failing restore.
209 *
210 * If platform_pre_restore() has been called before the failing restore, this
211 * function must be called too, regardless of the result of
212 * platform_pre_restore().
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700213 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700214static void platform_restore_cleanup(int platform_mode)
215{
216 if (platform_mode && hibernation_ops)
217 hibernation_ops->restore_cleanup();
218}
219
220/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200221 * platform_recover - Recover from a failure to suspend devices.
222 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200223 */
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200224static void platform_recover(int platform_mode)
225{
226 if (platform_mode && hibernation_ops && hibernation_ops->recover)
227 hibernation_ops->recover();
228}
229
230/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200231 * swsusp_show_speed - Print time elapsed between two events during hibernation.
232 * @start: Starting event.
233 * @stop: Final event.
234 * @nr_pages: Number of memory pages processed between @start and @stop.
235 * @msg: Additional diagnostic message to print.
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100236 */
Tina Ruchandanidb597602014-10-30 11:04:53 -0700237void swsusp_show_speed(ktime_t start, ktime_t stop,
238 unsigned nr_pages, char *msg)
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100239{
Tina Ruchandanidb597602014-10-30 11:04:53 -0700240 ktime_t diff;
Chen Gang4881f602014-04-25 08:44:59 +0800241 u64 elapsed_centisecs64;
242 unsigned int centisecs;
243 unsigned int k;
244 unsigned int kps;
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100245
Tina Ruchandanidb597602014-10-30 11:04:53 -0700246 diff = ktime_sub(stop, start);
247 elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC);
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100248 centisecs = elapsed_centisecs64;
249 if (centisecs == 0)
250 centisecs = 1; /* avoid div-by-zero */
251 k = nr_pages * (PAGE_SIZE / 1024);
252 kps = (k * 100) / centisecs;
Chen Gang4881f602014-04-25 08:44:59 +0800253 printk(KERN_INFO "PM: %s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100254 msg, k,
255 centisecs / 100, centisecs % 100,
256 kps / 1000, (kps % 1000) / 10);
257}
258
Jiri Kosina5bdc5362019-05-30 00:09:39 +0200259__weak int arch_resume_nosmt(void)
260{
261 return 0;
262}
263
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100264/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200265 * create_image - Create a hibernation image.
266 * @platform_mode: Whether or not to use the platform driver.
267 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100268 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
269 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200270 *
271 * Control reappears in this routine after the subsequent restore.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700272 */
Adrian Bunk47a460d2008-02-04 22:30:06 -0800273static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700274{
275 int error;
276
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100277 error = dpm_suspend_end(PMSG_FREEZE);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700278 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100279 printk(KERN_ERR "PM: Some devices failed to power down, "
280 "aborting hibernation\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200281 return error;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700282 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100283
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100284 error = platform_pre_snapshot(platform_mode);
285 if (error || hibernation_test(TEST_PLATFORM))
286 goto Platform_finish;
287
288 error = disable_nonboot_cpus();
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100289 if (error || hibernation_test(TEST_CPUS))
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100290 goto Enable_cpus;
291
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100292 local_irq_disable();
293
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200294 error = syscore_suspend();
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100295 if (error) {
Bjorn Helgaas44840792009-05-15 23:30:50 +0200296 printk(KERN_ERR "PM: Some system devices failed to power down, "
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100297 "aborting hibernation\n");
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100298 goto Enable_irqs;
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100299 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700300
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100301 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100302 goto Power_up;
303
304 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700305 save_processor_state();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700306 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700307 error = swsusp_arch_suspend();
Thomas Garnier62822e22016-08-11 14:49:29 -0700308 /* Restore control flow magically appears here */
309 restore_processor_state();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700310 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700311 if (error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100312 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
313 error);
Anisse Astier1ad14102016-09-09 10:43:32 +0200314 if (!in_suspend) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200315 events_check_enabled = false;
Anisse Astier1ad14102016-09-09 10:43:32 +0200316 clear_free_pages();
317 }
Bjørn Mork362e77d2013-12-04 16:06:58 +0100318
319 platform_leave(platform_mode);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100320
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100321 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100322 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100323
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100324 Enable_irqs:
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100325 local_irq_enable();
326
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100327 Enable_cpus:
328 enable_nonboot_cpus();
329
Jiri Kosina5bdc5362019-05-30 00:09:39 +0200330 /* Allow architectures to do nosmt-specific post-resume dances */
331 if (!in_suspend)
332 error = arch_resume_nosmt();
333
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100334 Platform_finish:
335 platform_finish(platform_mode);
336
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100337 dpm_resume_start(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200338 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100339
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700340 return error;
341}
342
343/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200344 * hibernation_snapshot - Quiesce devices and create a hibernation image.
345 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700346 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200347 * This routine must be called with pm_mutex held.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700348 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700349int hibernation_snapshot(int platform_mode)
350{
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100351 pm_message_t msg;
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100352 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700353
Lukas Wunner27614272016-03-23 00:11:20 +0100354 pm_suspend_clear_flags();
Zhang Rui3fe03132008-10-26 20:50:26 +0100355 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700356 if (error)
Rafael J. Wysockid074ee02010-07-07 23:43:35 +0200357 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700358
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200359 /* Preallocate image memory before shutting down devices. */
360 error = hibernate_preallocate_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700361 if (error)
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200362 goto Close;
363
364 error = freeze_kernel_threads();
365 if (error)
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100366 goto Cleanup;
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200367
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100368 if (hibernation_test(TEST_FREEZER)) {
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100369
370 /*
371 * Indicate to the caller that we are returning due to a
372 * successful freezer test.
373 */
374 freezer_test_done = true;
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100375 goto Thaw;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100376 }
377
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200378 error = dpm_prepare(PMSG_FREEZE);
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100379 if (error) {
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100380 dpm_complete(PMSG_RECOVER);
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100381 goto Thaw;
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100382 }
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700383
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700384 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100385 pm_restrict_gfp_mask();
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100386
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200387 error = dpm_suspend(PMSG_FREEZE);
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700388
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100389 if (error || hibernation_test(TEST_DEVICES))
390 platform_recover(platform_mode);
391 else
392 error = create_image(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700393
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100394 /*
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100395 * In the case that we call create_image() above, the control
396 * returns here (1) after the image has been created or the
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100397 * image creation has failed and (2) after a successful restore.
398 */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100399
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200400 /* We may need to release the preallocated image pages here. */
401 if (error || !in_suspend)
402 swsusp_free();
403
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200404 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
405 dpm_resume(msg);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100406
407 if (error || !in_suspend)
408 pm_restore_gfp_mask();
409
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700410 resume_console();
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200411 dpm_complete(msg);
412
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100413 Close:
414 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700415 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200416
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100417 Thaw:
418 thaw_kernel_threads();
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100419 Cleanup:
420 swsusp_free();
421 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700422}
423
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200424int __weak hibernate_resume_nonboot_cpu_disable(void)
425{
426 return disable_nonboot_cpus();
427}
428
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700429/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200430 * resume_target_kernel - Restore system state from a hibernation image.
431 * @platform_mode: Whether or not to use the platform driver.
432 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100433 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
434 * contents of highmem that have not been restored yet from the image and run
435 * the low-level code that will restore the remaining contents of memory and
436 * switch to the just restored target kernel.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100437 */
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100438static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100439{
440 int error;
441
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100442 error = dpm_suspend_end(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100443 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100444 printk(KERN_ERR "PM: Some devices failed to power down, "
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100445 "aborting resume\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200446 return error;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100447 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100448
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100449 error = platform_pre_restore(platform_mode);
450 if (error)
451 goto Cleanup;
452
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200453 error = hibernate_resume_nonboot_cpu_disable();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100454 if (error)
455 goto Enable_cpus;
456
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100457 local_irq_disable();
458
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200459 error = syscore_suspend();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100460 if (error)
461 goto Enable_irqs;
462
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100463 save_processor_state();
464 error = restore_highmem();
465 if (!error) {
466 error = swsusp_arch_resume();
467 /*
468 * The code below is only ever reached in case of a failure.
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200469 * Otherwise, execution continues at the place where
470 * swsusp_arch_suspend() was called.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100471 */
472 BUG_ON(!error);
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200473 /*
474 * This call to restore_highmem() reverts the changes made by
475 * the previous one.
476 */
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100477 restore_highmem();
478 }
479 /*
480 * The only reason why swsusp_arch_resume() can fail is memory being
481 * very tight, so we have to free it as soon as we can to avoid
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200482 * subsequent failures.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100483 */
484 swsusp_free();
485 restore_processor_state();
486 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100487
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100488 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100489
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100490 Enable_irqs:
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100491 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100492
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100493 Enable_cpus:
494 enable_nonboot_cpus();
495
496 Cleanup:
497 platform_restore_cleanup(platform_mode);
498
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100499 dpm_resume_start(PMSG_RECOVER);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100500
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100501 return error;
502}
503
504/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200505 * hibernation_restore - Quiesce devices and restore from a hibernation image.
506 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700507 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200508 * This routine must be called with pm_mutex held. If it is successful, control
Barry Song21e82802011-09-27 22:05:44 +0200509 * reappears in the restored target kernel in hibernation_snapshot().
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700510 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700511int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700512{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100513 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700514
515 pm_prepare_console();
516 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100517 pm_restrict_gfp_mask();
Alan Sternd1616302009-05-24 22:05:42 +0200518 error = dpm_suspend_start(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700519 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100520 error = resume_target_kernel(platform_mode);
Imre Deak94fb8232014-10-24 20:29:10 +0300521 /*
522 * The above should either succeed and jump to the new kernel,
523 * or return with an error. Otherwise things are just
524 * undefined, so let's be paranoid.
525 */
526 BUG_ON(!error);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700527 }
Imre Deak94fb8232014-10-24 20:29:10 +0300528 dpm_resume_end(PMSG_RECOVER);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100529 pm_restore_gfp_mask();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700530 resume_console();
531 pm_restore_console();
532 return error;
533}
534
535/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200536 * hibernation_platform_enter - Power off the system using the platform driver.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700537 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700538int hibernation_platform_enter(void)
539{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100540 int error;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700541
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700542 if (!hibernation_ops)
543 return -ENOSYS;
544
545 /*
546 * We have cancelled the power transition by running
547 * hibernation_ops->finish() before saving the image, so we should let
548 * the firmware know that we're going to enter the sleep state after all
549 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100550 error = hibernation_ops->begin();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700551 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100552 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700553
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100554 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700555 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +0200556 error = dpm_suspend_start(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200557 if (error) {
558 if (hibernation_ops->recover)
559 hibernation_ops->recover();
560 goto Resume_devices;
561 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700562
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100563 error = dpm_suspend_end(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100564 if (error)
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200565 goto Resume_devices;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100566
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100567 error = hibernation_ops->prepare();
568 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200569 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100570
571 error = disable_nonboot_cpus();
572 if (error)
Vitaly Kuznetsov8c506602015-06-24 16:02:06 +0200573 goto Enable_cpus;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100574
575 local_irq_disable();
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100576 syscore_suspend();
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100577 if (pm_wakeup_pending()) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200578 error = -EAGAIN;
579 goto Power_up;
580 }
581
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100582 hibernation_ops->enter();
583 /* We should never get here */
584 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700585
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200586 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100587 syscore_resume();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200588 local_irq_enable();
Vitaly Kuznetsov8c506602015-06-24 16:02:06 +0200589
590 Enable_cpus:
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200591 enable_nonboot_cpus();
592
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200593 Platform_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700594 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100595
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100596 dpm_resume_start(PMSG_RESTORE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100597
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700598 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100599 entering_platform_hibernation = false;
Alan Sternd1616302009-05-24 22:05:42 +0200600 dpm_resume_end(PMSG_RESTORE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700601 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100602
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100603 Close:
604 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100605
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700606 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700607}
608
609/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200610 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200612 * Use the platform driver, if configured, to put the system into the sleep
613 * state corresponding to hibernation, or try to power it off or reboot,
614 * depending on the value of hibernation_mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 */
Johannes Bergfe0c9352007-04-30 15:09:51 -0700616static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Bojan Smojver62c552c2012-06-16 00:09:58 +0200618#ifdef CONFIG_SUSPEND
619 int error;
620#endif
621
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700622 switch (hibernation_mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700623 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600624 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700626 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700627 hibernation_platform_enter();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700628 case HIBERNATION_SHUTDOWN:
Sebastian Capella2c730782014-04-21 17:30:46 -0700629 if (pm_power_off)
630 kernel_power_off();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700631 break;
Bojan Smojver62c552c2012-06-16 00:09:58 +0200632#ifdef CONFIG_SUSPEND
633 case HIBERNATION_SUSPEND:
634 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
635 if (error) {
636 if (hibernation_ops)
637 hibernation_mode = HIBERNATION_PLATFORM;
638 else
639 hibernation_mode = HIBERNATION_SHUTDOWN;
640 power_down();
641 }
642 /*
643 * Restore swap signature.
644 */
645 error = swsusp_unmark();
646 if (error)
647 printk(KERN_ERR "PM: Swap will be unusable! "
648 "Try swapon -a.\n");
649 return;
650#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600652 kernel_halt();
Johannes Bergfe0c9352007-04-30 15:09:51 -0700653 /*
654 * Valid image is on the disk, if we continue we risk serious data
655 * corruption after resume.
656 */
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100657 printk(KERN_CRIT "PM: Please power down manually\n");
Sebastian Capella2c730782014-04-21 17:30:46 -0700658 while (1)
659 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Chen Yufe12c002016-07-22 10:30:47 +0800662static int load_image_and_restore(void)
663{
664 int error;
665 unsigned int flags;
666
667 pr_debug("PM: Loading hibernation image.\n");
668
669 lock_device_hotplug();
670 error = create_basic_memory_bitmaps();
671 if (error)
672 goto Unlock;
673
674 error = swsusp_read(&flags);
675 swsusp_close(FMODE_READ);
676 if (!error)
677 hibernation_restore(flags & SF_PLATFORM_MODE);
678
679 printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n");
680 swsusp_free();
681 free_basic_memory_bitmaps();
682 Unlock:
683 unlock_device_hotplug();
684
685 return error;
686}
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200689 * hibernate - Carry out system hibernation, including saving the image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700691int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700693 int error, nr_calls = 0;
Chen Yufe12c002016-07-22 10:30:47 +0800694 bool snapshot_test = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Kees Cooka6e15a32014-06-13 13:30:35 -0700696 if (!hibernation_available()) {
697 pr_debug("PM: Hibernation not available.\n");
698 return -EPERM;
699 }
700
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100701 lock_system_sleep();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700702 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700703 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
704 error = -EBUSY;
705 goto Unlock;
706 }
707
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100708 pm_prepare_console();
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700709 error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
710 if (error) {
711 nr_calls--;
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700712 goto Exit;
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700713 }
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700714
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100715 printk(KERN_INFO "PM: Syncing filesystems ... ");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700716 sys_sync();
717 printk("done.\n");
718
Tejun Heo03afed82011-11-21 12:32:24 -0800719 error = freeze_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700720 if (error)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200721 goto Exit;
722
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200723 lock_device_hotplug();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200724 /* Allocate memory management structures */
725 error = create_basic_memory_bitmaps();
726 if (error)
727 goto Thaw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700729 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100730 if (error || freezer_test_done)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200731 goto Free_bitmaps;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200732
733 if (in_suspend) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700734 unsigned int flags = 0;
735
736 if (hibernation_mode == HIBERNATION_PLATFORM)
737 flags |= SF_PLATFORM_MODE;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200738 if (nocompress)
739 flags |= SF_NOCOMPRESS_MODE;
Bojan Smojver081a9d02011-10-13 23:58:07 +0200740 else
741 flags |= SF_CRC32_MODE;
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700744 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700745 swsusp_free();
Chen Yufe12c002016-07-22 10:30:47 +0800746 if (!error) {
747 if (hibernation_mode == HIBERNATION_TEST_RESUME)
748 snapshot_test = true;
749 else
750 power_down();
751 }
MyungJoo Ham5262a472010-11-26 23:07:56 +0100752 in_suspend = 0;
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100753 pm_restore_gfp_mask();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800754 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800756 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200757
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200758 Free_bitmaps:
759 free_basic_memory_bitmaps();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800760 Thaw:
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200761 unlock_device_hotplug();
Chen Yufe12c002016-07-22 10:30:47 +0800762 if (snapshot_test) {
763 pr_debug("PM: Checking hibernation image\n");
764 error = swsusp_check();
765 if (!error)
766 error = load_image_and_restore();
767 }
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100768 thaw_processes();
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100769
770 /* Don't bother checking whether freezer_test_done is true */
771 freezer_test_done = false;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700772 Exit:
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700773 __pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100774 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700775 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700776 Unlock:
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100777 unlock_system_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return error;
779}
780
781
782/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200783 * software_resume - Resume from a saved hibernation image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200785 * This routine is called as a late initcall, when all devices have been
786 * discovered and initialized already.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200788 * The image reading code is called to see if there is a hibernation image
789 * available for reading. If that is the case, devices are quiesced and the
790 * contents of memory is restored from the saved image.
791 *
792 * If this is successful, control reappears in the restored target kernel in
Geliang Tangd439e642015-09-29 20:36:58 -0700793 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200794 * attempts to recover gracefully and make the kernel return to the normal mode
795 * of operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797static int software_resume(void)
798{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700799 int error, nr_calls = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Johannes Berg60a0d232007-11-14 17:00:16 -0800801 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100802 * If the user said "noresume".. bail out early.
803 */
Kees Cooka6e15a32014-06-13 13:30:35 -0700804 if (noresume || !hibernation_available())
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100805 return 0;
806
807 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800808 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
809 * is configured into the kernel. Since the regular hibernate
810 * trigger path is via sysfs which takes a buffer mutex before
811 * calling hibernate functions (which take pm_mutex) this can
812 * cause lockdep to complain about a possible ABBA deadlock
813 * which cannot happen since we're in the boot code here and
814 * sysfs can't be invoked yet. Therefore, we use a subclass
815 * here to avoid lockdep complaining.
816 */
817 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200818
819 if (swsusp_resume_device)
820 goto Check_image;
821
822 if (!strlen(resume_file)) {
823 error = -ENOENT;
824 goto Unlock;
825 }
826
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200827 pr_debug("PM: Checking hibernation image partition %s\n", resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200828
Barry Songf126f732011-10-10 23:38:41 +0200829 if (resume_delay) {
830 printk(KERN_INFO "Waiting %dsec before reading resume device...\n",
831 resume_delay);
832 ssleep(resume_delay);
833 }
834
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200835 /* Check if the device is there */
836 swsusp_resume_device = name_to_dev_t(resume_file);
Minho Ban2df83fa2012-05-14 21:45:31 +0200837
838 /*
839 * name_to_dev_t is ineffective to verify parition if resume_file is in
840 * integer format. (e.g. major:minor)
841 */
842 if (isdigit(resume_file[0]) && resume_wait) {
843 int partno;
844 while (!get_gendisk(swsusp_resume_device, &partno))
845 msleep(10);
846 }
847
Pavel Machek3efa1472005-07-07 17:56:43 -0700848 if (!swsusp_resume_device) {
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100849 /*
850 * Some device discovery might still be in progress; we need
851 * to wait for this to finish.
852 */
853 wait_for_device_probe();
Barry Song6f8d7022011-10-06 20:34:46 +0200854
855 if (resume_wait) {
856 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
857 msleep(10);
858 async_synchronize_full();
859 }
860
Pavel Machek3efa1472005-07-07 17:56:43 -0700861 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200862 if (!swsusp_resume_device) {
863 error = -ENODEV;
864 goto Unlock;
865 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700866 }
867
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200868 Check_image:
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200869 pr_debug("PM: Hibernation image partition %d:%d present\n",
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200870 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200872 pr_debug("PM: Looking for hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800873 error = swsusp_check();
874 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700875 goto Unlock;
876
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700877 /* The snapshot device should not be opened while we're running */
878 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
879 error = -EBUSY;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200880 swsusp_close(FMODE_READ);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700881 goto Unlock;
882 }
883
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100884 pm_prepare_console();
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700885 error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
886 if (error) {
887 nr_calls--;
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200888 goto Close_Finish;
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700889 }
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 pr_debug("PM: Preparing processes for restore.\n");
Tejun Heo03afed82011-11-21 12:32:24 -0800892 error = freeze_processes();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200893 if (error)
894 goto Close_Finish;
Chen Yufe12c002016-07-22 10:30:47 +0800895 error = load_image_and_restore();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200896 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700897 Finish:
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700898 __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100899 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700900 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700901 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700902 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800903 mutex_unlock(&pm_mutex);
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200904 pr_debug("PM: Hibernation image not present or could not be loaded.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700905 return error;
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200906 Close_Finish:
Jiri Slaby76b57e62009-10-07 22:37:35 +0200907 swsusp_close(FMODE_READ);
908 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Russ Dilld3c345d2013-10-24 14:25:26 +0100911late_initcall_sync(software_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700914static const char * const hibernation_modes[] = {
915 [HIBERNATION_PLATFORM] = "platform",
916 [HIBERNATION_SHUTDOWN] = "shutdown",
917 [HIBERNATION_REBOOT] = "reboot",
Bojan Smojver62c552c2012-06-16 00:09:58 +0200918#ifdef CONFIG_SUSPEND
919 [HIBERNATION_SUSPEND] = "suspend",
920#endif
Chen Yufe12c002016-07-22 10:30:47 +0800921 [HIBERNATION_TEST_RESUME] = "test_resume",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922};
923
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200924/*
925 * /sys/power/disk - Control hibernation mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200927 * Hibernation can be handled in several ways. There are a few different ways
928 * to put the system into the sleep state: using the platform driver (e.g. ACPI
929 * or other hibernation_ops), powering it off or rebooting it (for testing
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100930 * mostly).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200932 * The sysfs file /sys/power/disk provides an interface for selecting the
933 * hibernation mode to use. Reading from this file causes the available modes
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100934 * to be printed. There are 3 modes that can be supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 * 'platform'
937 * 'shutdown'
938 * 'reboot'
939 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200940 * If a platform hibernation driver is in use, 'platform' will be supported
941 * and will be used by default. Otherwise, 'shutdown' will be used by default.
942 * The selected option (i.e. the one corresponding to the current value of
943 * hibernation_mode) is enclosed by a square bracket.
944 *
945 * To select a given hibernation mode it is necessary to write the mode's
946 * string representation (as returned by reading from /sys/power/disk) back
947 * into /sys/power/disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 */
949
Kay Sievers386f2752007-11-02 13:47:53 +0100950static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
951 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700953 int i;
954 char *start = buf;
955
Kees Cooka6e15a32014-06-13 13:30:35 -0700956 if (!hibernation_available())
957 return sprintf(buf, "[disabled]\n");
958
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700959 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
960 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700961 continue;
962 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700963 case HIBERNATION_SHUTDOWN:
964 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +0200965#ifdef CONFIG_SUSPEND
966 case HIBERNATION_SUSPEND:
967#endif
Chen Yufe12c002016-07-22 10:30:47 +0800968 case HIBERNATION_TEST_RESUME:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700969 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700970 case HIBERNATION_PLATFORM:
971 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700972 break;
973 /* not a valid mode, continue with loop */
974 continue;
975 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700976 if (i == hibernation_mode)
977 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700978 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700979 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700980 }
981 buf += sprintf(buf, "\n");
982 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
Kay Sievers386f2752007-11-02 13:47:53 +0100985static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
986 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 int error = 0;
989 int i;
990 int len;
991 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700992 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Kees Cooka6e15a32014-06-13 13:30:35 -0700994 if (!hibernation_available())
995 return -EPERM;
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 p = memchr(buf, '\n', n);
998 len = p ? p - buf : n;
999
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001000 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001001 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -07001002 if (len == strlen(hibernation_modes[i])
1003 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 mode = i;
1005 break;
1006 }
1007 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001008 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c9352007-04-30 15:09:51 -07001009 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001010 case HIBERNATION_SHUTDOWN:
1011 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +02001012#ifdef CONFIG_SUSPEND
1013 case HIBERNATION_SUSPEND:
1014#endif
Chen Yufe12c002016-07-22 10:30:47 +08001015 case HIBERNATION_TEST_RESUME:
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001016 hibernation_mode = mode;
Johannes Bergfe0c9352007-04-30 15:09:51 -07001017 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001018 case HIBERNATION_PLATFORM:
1019 if (hibernation_ops)
1020 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 else
1022 error = -EINVAL;
1023 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001024 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 error = -EINVAL;
1026
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001027 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001028 pr_debug("PM: Hibernation mode set to '%s'\n",
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001029 hibernation_modes[mode]);
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001030 unlock_system_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return error ? error : n;
1032}
1033
1034power_attr(disk);
1035
Kay Sievers386f2752007-11-02 13:47:53 +01001036static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1037 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
1040 MINOR(swsusp_resume_device));
1041}
1042
Kay Sievers386f2752007-11-02 13:47:53 +01001043static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1044 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 dev_t res;
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001047 int len = n;
1048 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001050 if (len && buf[len-1] == '\n')
1051 len--;
1052 name = kstrndup(buf, len, GFP_KERNEL);
1053 if (!name)
1054 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001056 res = name_to_dev_t(name);
1057 kfree(name);
1058 if (!res)
1059 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001061 lock_system_sleep();
Andrew Mortona5762192006-01-06 00:09:50 -08001062 swsusp_resume_device = res;
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001063 unlock_system_sleep();
Rafael J. Wysocki23976722007-12-08 02:09:43 +01001064 printk(KERN_INFO "PM: Starting manual resume from disk\n");
Andrew Mortona5762192006-01-06 00:09:50 -08001065 noresume = 0;
1066 software_resume();
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001067 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
1070power_attr(resume);
1071
Kay Sievers386f2752007-11-02 13:47:53 +01001072static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1073 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001074{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001075 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001076}
1077
Kay Sievers386f2752007-11-02 13:47:53 +01001078static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1079 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001080{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001081 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001082
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001083 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001084 image_size = size;
1085 return n;
1086 }
1087
1088 return -EINVAL;
1089}
1090
1091power_attr(image_size);
1092
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001093static ssize_t reserved_size_show(struct kobject *kobj,
1094 struct kobj_attribute *attr, char *buf)
1095{
1096 return sprintf(buf, "%lu\n", reserved_size);
1097}
1098
1099static ssize_t reserved_size_store(struct kobject *kobj,
1100 struct kobj_attribute *attr,
1101 const char *buf, size_t n)
1102{
1103 unsigned long size;
1104
1105 if (sscanf(buf, "%lu", &size) == 1) {
1106 reserved_size = size;
1107 return n;
1108 }
1109
1110 return -EINVAL;
1111}
1112
1113power_attr(reserved_size);
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115static struct attribute * g[] = {
1116 &disk_attr.attr,
1117 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001118 &image_size_attr.attr,
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001119 &reserved_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 NULL,
1121};
1122
1123
1124static struct attribute_group attr_group = {
1125 .attrs = g,
1126};
1127
1128
1129static int __init pm_disk_init(void)
1130{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -08001131 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
1134core_initcall(pm_disk_init);
1135
1136
1137static int __init resume_setup(char *str)
1138{
1139 if (noresume)
1140 return 1;
1141
1142 strncpy( resume_file, str, 255 );
1143 return 1;
1144}
1145
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001146static int __init resume_offset_setup(char *str)
1147{
1148 unsigned long long offset;
1149
1150 if (noresume)
1151 return 1;
1152
1153 if (sscanf(str, "%llu", &offset) == 1)
1154 swsusp_resume_block = offset;
1155
1156 return 1;
1157}
1158
Bojan Smojverf996fc92010-09-09 23:06:23 +02001159static int __init hibernate_setup(char *str)
1160{
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001161 if (!strncmp(str, "noresume", 8)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001162 noresume = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001163 } else if (!strncmp(str, "nocompress", 10)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001164 nocompress = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001165 } else if (!strncmp(str, "no", 2)) {
Kees Cooka6e15a32014-06-13 13:30:35 -07001166 noresume = 1;
1167 nohibernate = 1;
Rafael J. Wysocki4c0b6c12016-07-10 02:12:10 +02001168 } else if (IS_ENABLED(CONFIG_DEBUG_RODATA)
1169 && !strncmp(str, "protect_image", 13)) {
1170 enable_restore_image_protection();
Kees Cooka6e15a32014-06-13 13:30:35 -07001171 }
Bojan Smojverf996fc92010-09-09 23:06:23 +02001172 return 1;
1173}
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175static int __init noresume_setup(char *str)
1176{
1177 noresume = 1;
1178 return 1;
1179}
1180
Barry Song6f8d7022011-10-06 20:34:46 +02001181static int __init resumewait_setup(char *str)
1182{
1183 resume_wait = 1;
1184 return 1;
1185}
1186
Barry Songf126f732011-10-10 23:38:41 +02001187static int __init resumedelay_setup(char *str)
1188{
Dan Carpenterf6514be2014-05-14 19:08:46 +03001189 int rc = kstrtouint(str, 0, &resume_delay);
Fabian Frederick317cf7e2014-05-09 23:32:08 +02001190
1191 if (rc)
1192 return rc;
Barry Songf126f732011-10-10 23:38:41 +02001193 return 1;
1194}
1195
Kees Cooka6e15a32014-06-13 13:30:35 -07001196static int __init nohibernate_setup(char *str)
1197{
1198 noresume = 1;
1199 nohibernate = 1;
1200 return 1;
1201}
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001204__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205__setup("resume=", resume_setup);
Bojan Smojverf996fc92010-09-09 23:06:23 +02001206__setup("hibernate=", hibernate_setup);
Barry Song6f8d7022011-10-06 20:34:46 +02001207__setup("resumewait", resumewait_setup);
Barry Songf126f732011-10-10 23:38:41 +02001208__setup("resumedelay=", resumedelay_setup);
Kees Cooka6e15a32014-06-13 13:30:35 -07001209__setup("nohibernate", nohibernate_setup);