blob: abef759de7c8fb4a8ece278fd7b7730d5b5e41ab [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
Rafael J. Wysocki2872de12017-02-24 00:26:15 +010013#define pr_fmt(fmt) "PM: " fmt
14
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040015#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/suspend.h>
17#include <linux/syscalls.h>
18#include <linux/reboot.h>
19#include <linux/string.h>
20#include <linux/device.h>
Barry Song6f8d7022011-10-06 20:34:46 +020021#include <linux/async.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/delay.h>
23#include <linux/fs.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070024#include <linux/mount.h>
Eric W. Biederman88d10bb2005-09-22 21:43:46 -070025#include <linux/pm.h>
Ingo Molnar38b8d202017-02-08 18:51:31 +010026#include <linux/nmi.h>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070027#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070028#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080029#include <linux/freezer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/gfp.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010031#include <linux/syscore_ops.h>
Minho Ban2df83fa2012-05-14 21:45:31 +020032#include <linux/ctype.h>
33#include <linux/genhd.h>
Tina Ruchandanidb597602014-10-30 11:04:53 -070034#include <linux/ktime.h>
Todd E Brandtbb3632c2014-06-06 05:40:17 -070035#include <trace/events/power.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "power.h"
38
39
Barry Songd231ff12011-10-11 23:29:18 -070040static int nocompress;
41static int noresume;
Kees Cooka6e15a32014-06-13 13:30:35 -070042static int nohibernate;
Barry Songd231ff12011-10-11 23:29:18 -070043static int resume_wait;
Dan Carpenterf6514be2014-05-14 19:08:46 +030044static unsigned int resume_delay;
Adrian Bunk47a460d2008-02-04 22:30:06 -080045static char resume_file[256] = CONFIG_PM_STD_PARTITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080047sector_t swsusp_resume_block;
Andi Kleend6efc2f2013-08-05 15:02:49 -070048__visible int in_suspend __nosavedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070050enum {
51 HIBERNATION_INVALID,
52 HIBERNATION_PLATFORM,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070053 HIBERNATION_SHUTDOWN,
54 HIBERNATION_REBOOT,
Bojan Smojver62c552c2012-06-16 00:09:58 +020055#ifdef CONFIG_SUSPEND
56 HIBERNATION_SUSPEND,
57#endif
Chen Yufe12c002016-07-22 10:30:47 +080058 HIBERNATION_TEST_RESUME,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070059 /* keep last */
60 __HIBERNATION_AFTER_LAST
61};
62#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
63#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
64
65static int hibernation_mode = HIBERNATION_SHUTDOWN;
66
Srivatsa S. Bhat97819a22011-12-01 22:33:10 +010067bool freezer_test_done;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +010068
Lionel Debroux073ef1f2010-11-09 21:48:49 +010069static const struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070070
Kees Cooka6e15a32014-06-13 13:30:35 -070071bool hibernation_available(void)
72{
73 return (nohibernate == 0);
74}
75
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070076/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +020077 * hibernation_set_ops - Set the global hibernate operations.
78 * @ops: Hibernation operations to use in subsequent hibernation transitions.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070079 */
Lionel Debroux073ef1f2010-11-09 21:48:49 +010080void hibernation_set_ops(const struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070081{
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +010082 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
83 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
MyungJoo Ham5729c632010-11-26 23:07:48 +010084 && ops->restore_cleanup && ops->leave)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070085 WARN_ON(1);
86 return;
87 }
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010088 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070089 hibernation_ops = ops;
90 if (ops)
91 hibernation_mode = HIBERNATION_PLATFORM;
92 else if (hibernation_mode == HIBERNATION_PLATFORM)
93 hibernation_mode = HIBERNATION_SHUTDOWN;
94
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010095 unlock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070096}
Leonardo Potenzae0c78552013-11-19 12:27:41 +000097EXPORT_SYMBOL_GPL(hibernation_set_ops);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070098
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +010099static bool entering_platform_hibernation;
100
101bool system_entering_hibernation(void)
102{
103 return entering_platform_hibernation;
104}
105EXPORT_SYMBOL(system_entering_hibernation);
106
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100107#ifdef CONFIG_PM_DEBUG
108static void hibernation_debug_sleep(void)
109{
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100110 pr_info("hibernation debug: Waiting for 5 seconds.\n");
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100111 mdelay(5000);
112}
113
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100114static int hibernation_test(int level)
115{
116 if (pm_test_level == level) {
117 hibernation_debug_sleep();
118 return 1;
119 }
120 return 0;
121}
122#else /* !CONFIG_PM_DEBUG */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100123static int hibernation_test(int level) { return 0; }
124#endif /* !CONFIG_PM_DEBUG */
125
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700126/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200127 * platform_begin - Call platform to start hibernation.
128 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700129 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100130static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700131{
132 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100133 hibernation_ops->begin() : 0;
134}
135
136/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200137 * platform_end - Call platform to finish transition to the working state.
138 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100139 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100140static void platform_end(int platform_mode)
141{
142 if (platform_mode && hibernation_ops)
143 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700144}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200147 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
148 * @platform_mode: Whether or not to use the platform driver.
149 *
150 * Use the platform driver to prepare the system for creating a hibernate image,
151 * if so configured, and return an error code if that fails.
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800152 */
153
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700154static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800155{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700156 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700157 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800158}
159
160/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200161 * platform_leave - Call platform to prepare a transition to the working state.
162 * @platform_mode: Whether or not to use the platform driver.
163 *
164 * Use the platform driver prepare to prepare the machine for switching to the
165 * normal mode of operation.
166 *
167 * This routine is called on one CPU with interrupts disabled.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700168 */
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700169static void platform_leave(int platform_mode)
170{
171 if (platform_mode && hibernation_ops)
172 hibernation_ops->leave();
173}
174
175/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200176 * platform_finish - Call platform to switch the system to the working state.
177 * @platform_mode: Whether or not to use the platform driver.
178 *
179 * Use the platform driver to switch the machine to the normal mode of
180 * operation.
181 *
182 * This routine must be called after platform_prepare().
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700183 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700184static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700185{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700186 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700187 hibernation_ops->finish();
188}
189
190/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200191 * platform_pre_restore - Prepare for hibernate image restoration.
192 * @platform_mode: Whether or not to use the platform driver.
193 *
194 * Use the platform driver to prepare the system for resume from a hibernation
195 * image.
196 *
197 * If the restore fails after this function has been called,
198 * platform_restore_cleanup() must be called.
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700199 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700200static int platform_pre_restore(int platform_mode)
201{
202 return (platform_mode && hibernation_ops) ?
203 hibernation_ops->pre_restore() : 0;
204}
205
206/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200207 * platform_restore_cleanup - Switch to the working state after failing restore.
208 * @platform_mode: Whether or not to use the platform driver.
209 *
210 * Use the platform driver to switch the system to the normal mode of operation
211 * after a failing restore.
212 *
213 * If platform_pre_restore() has been called before the failing restore, this
214 * function must be called too, regardless of the result of
215 * platform_pre_restore().
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700216 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700217static void platform_restore_cleanup(int platform_mode)
218{
219 if (platform_mode && hibernation_ops)
220 hibernation_ops->restore_cleanup();
221}
222
223/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200224 * platform_recover - Recover from a failure to suspend devices.
225 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200226 */
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200227static void platform_recover(int platform_mode)
228{
229 if (platform_mode && hibernation_ops && hibernation_ops->recover)
230 hibernation_ops->recover();
231}
232
233/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200234 * swsusp_show_speed - Print time elapsed between two events during hibernation.
235 * @start: Starting event.
236 * @stop: Final event.
237 * @nr_pages: Number of memory pages processed between @start and @stop.
238 * @msg: Additional diagnostic message to print.
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100239 */
Tina Ruchandanidb597602014-10-30 11:04:53 -0700240void swsusp_show_speed(ktime_t start, ktime_t stop,
241 unsigned nr_pages, char *msg)
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100242{
Tina Ruchandanidb597602014-10-30 11:04:53 -0700243 ktime_t diff;
Chen Gang4881f602014-04-25 08:44:59 +0800244 u64 elapsed_centisecs64;
245 unsigned int centisecs;
246 unsigned int k;
247 unsigned int kps;
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100248
Tina Ruchandanidb597602014-10-30 11:04:53 -0700249 diff = ktime_sub(stop, start);
250 elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC);
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100251 centisecs = elapsed_centisecs64;
252 if (centisecs == 0)
253 centisecs = 1; /* avoid div-by-zero */
254 k = nr_pages * (PAGE_SIZE / 1024);
255 kps = (k * 100) / centisecs;
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100256 pr_info("%s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
257 msg, k, centisecs / 100, centisecs % 100, kps / 1000,
258 (kps % 1000) / 10);
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100259}
260
261/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200262 * create_image - Create a hibernation image.
263 * @platform_mode: Whether or not to use the platform driver.
264 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100265 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
266 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200267 *
268 * Control reappears in this routine after the subsequent restore.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700269 */
Adrian Bunk47a460d2008-02-04 22:30:06 -0800270static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700271{
272 int error;
273
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100274 error = dpm_suspend_end(PMSG_FREEZE);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700275 if (error) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100276 pr_err("Some devices failed to power down, 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
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200290 system_state = SYSTEM_SUSPEND;
291
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200292 error = syscore_suspend();
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100293 if (error) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100294 pr_err("Some system devices failed to power down, aborting hibernation\n");
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100295 goto Enable_irqs;
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100296 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700297
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100298 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100299 goto Power_up;
300
301 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700302 save_processor_state();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700303 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700304 error = swsusp_arch_suspend();
Thomas Garnier62822e22016-08-11 14:49:29 -0700305 /* Restore control flow magically appears here */
306 restore_processor_state();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700307 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700308 if (error)
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100309 pr_err("Error %d creating hibernation image\n", error);
310
Anisse Astier1ad14102016-09-09 10:43:32 +0200311 if (!in_suspend) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200312 events_check_enabled = false;
Anisse Astier1ad14102016-09-09 10:43:32 +0200313 clear_free_pages();
314 }
Bjørn Mork362e77d2013-12-04 16:06:58 +0100315
316 platform_leave(platform_mode);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100317
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100318 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100319 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100320
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100321 Enable_irqs:
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200322 system_state = SYSTEM_RUNNING;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100323 local_irq_enable();
324
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100325 Enable_cpus:
326 enable_nonboot_cpus();
327
328 Platform_finish:
329 platform_finish(platform_mode);
330
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100331 dpm_resume_start(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200332 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100333
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700334 return error;
335}
336
337/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200338 * hibernation_snapshot - Quiesce devices and create a hibernation image.
339 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700340 *
Pingfan Liu55f25032018-07-31 16:51:32 +0800341 * This routine must be called with system_transition_mutex held.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700342 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700343int hibernation_snapshot(int platform_mode)
344{
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100345 pm_message_t msg;
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100346 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700347
Lukas Wunner27614272016-03-23 00:11:20 +0100348 pm_suspend_clear_flags();
Zhang Rui3fe03132008-10-26 20:50:26 +0100349 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700350 if (error)
Rafael J. Wysockid074ee02010-07-07 23:43:35 +0200351 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700352
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200353 /* Preallocate image memory before shutting down devices. */
354 error = hibernate_preallocate_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700355 if (error)
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200356 goto Close;
357
358 error = freeze_kernel_threads();
359 if (error)
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100360 goto Cleanup;
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200361
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100362 if (hibernation_test(TEST_FREEZER)) {
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100363
364 /*
365 * Indicate to the caller that we are returning due to a
366 * successful freezer test.
367 */
368 freezer_test_done = true;
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100369 goto Thaw;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100370 }
371
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200372 error = dpm_prepare(PMSG_FREEZE);
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100373 if (error) {
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100374 dpm_complete(PMSG_RECOVER);
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100375 goto Thaw;
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100376 }
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700377
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700378 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100379 pm_restrict_gfp_mask();
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100380
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200381 error = dpm_suspend(PMSG_FREEZE);
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700382
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100383 if (error || hibernation_test(TEST_DEVICES))
384 platform_recover(platform_mode);
385 else
386 error = create_image(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700387
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100388 /*
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100389 * In the case that we call create_image() above, the control
390 * returns here (1) after the image has been created or the
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100391 * image creation has failed and (2) after a successful restore.
392 */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100393
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200394 /* We may need to release the preallocated image pages here. */
395 if (error || !in_suspend)
396 swsusp_free();
397
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200398 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
399 dpm_resume(msg);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100400
401 if (error || !in_suspend)
402 pm_restore_gfp_mask();
403
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700404 resume_console();
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200405 dpm_complete(msg);
406
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100407 Close:
408 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700409 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200410
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100411 Thaw:
412 thaw_kernel_threads();
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100413 Cleanup:
414 swsusp_free();
415 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700416}
417
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200418int __weak hibernate_resume_nonboot_cpu_disable(void)
419{
420 return disable_nonboot_cpus();
421}
422
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700423/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200424 * resume_target_kernel - Restore system state from a hibernation image.
425 * @platform_mode: Whether or not to use the platform driver.
426 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100427 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
428 * contents of highmem that have not been restored yet from the image and run
429 * the low-level code that will restore the remaining contents of memory and
430 * switch to the just restored target kernel.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100431 */
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100432static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100433{
434 int error;
435
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100436 error = dpm_suspend_end(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100437 if (error) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100438 pr_err("Some devices failed to power down, aborting resume\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200439 return error;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100440 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100441
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100442 error = platform_pre_restore(platform_mode);
443 if (error)
444 goto Cleanup;
445
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200446 error = hibernate_resume_nonboot_cpu_disable();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100447 if (error)
448 goto Enable_cpus;
449
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100450 local_irq_disable();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200451 system_state = SYSTEM_SUSPEND;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100452
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200453 error = syscore_suspend();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100454 if (error)
455 goto Enable_irqs;
456
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100457 save_processor_state();
458 error = restore_highmem();
459 if (!error) {
460 error = swsusp_arch_resume();
461 /*
462 * The code below is only ever reached in case of a failure.
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200463 * Otherwise, execution continues at the place where
464 * swsusp_arch_suspend() was called.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100465 */
466 BUG_ON(!error);
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200467 /*
468 * This call to restore_highmem() reverts the changes made by
469 * the previous one.
470 */
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100471 restore_highmem();
472 }
473 /*
474 * The only reason why swsusp_arch_resume() can fail is memory being
475 * very tight, so we have to free it as soon as we can to avoid
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200476 * subsequent failures.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100477 */
478 swsusp_free();
479 restore_processor_state();
480 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100481
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100482 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100483
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100484 Enable_irqs:
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200485 system_state = SYSTEM_RUNNING;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100486 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100487
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100488 Enable_cpus:
489 enable_nonboot_cpus();
490
491 Cleanup:
492 platform_restore_cleanup(platform_mode);
493
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100494 dpm_resume_start(PMSG_RECOVER);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100495
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100496 return error;
497}
498
499/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200500 * hibernation_restore - Quiesce devices and restore from a hibernation image.
501 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700502 *
Pingfan Liu55f25032018-07-31 16:51:32 +0800503 * This routine must be called with system_transition_mutex held. If it is
504 * successful, control reappears in the restored target kernel in
505 * hibernation_snapshot().
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700506 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700507int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700508{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100509 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700510
511 pm_prepare_console();
512 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100513 pm_restrict_gfp_mask();
Alan Sternd1616302009-05-24 22:05:42 +0200514 error = dpm_suspend_start(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700515 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100516 error = resume_target_kernel(platform_mode);
Imre Deak94fb8232014-10-24 20:29:10 +0300517 /*
518 * The above should either succeed and jump to the new kernel,
519 * or return with an error. Otherwise things are just
520 * undefined, so let's be paranoid.
521 */
522 BUG_ON(!error);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700523 }
Imre Deak94fb8232014-10-24 20:29:10 +0300524 dpm_resume_end(PMSG_RECOVER);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100525 pm_restore_gfp_mask();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700526 resume_console();
527 pm_restore_console();
528 return error;
529}
530
531/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200532 * hibernation_platform_enter - Power off the system using the platform driver.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700533 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700534int hibernation_platform_enter(void)
535{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100536 int error;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700537
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700538 if (!hibernation_ops)
539 return -ENOSYS;
540
541 /*
542 * We have cancelled the power transition by running
543 * hibernation_ops->finish() before saving the image, so we should let
544 * the firmware know that we're going to enter the sleep state after all
545 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100546 error = hibernation_ops->begin();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700547 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100548 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700549
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100550 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700551 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +0200552 error = dpm_suspend_start(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200553 if (error) {
554 if (hibernation_ops->recover)
555 hibernation_ops->recover();
556 goto Resume_devices;
557 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700558
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100559 error = dpm_suspend_end(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100560 if (error)
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200561 goto Resume_devices;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100562
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100563 error = hibernation_ops->prepare();
564 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200565 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100566
567 error = disable_nonboot_cpus();
568 if (error)
Vitaly Kuznetsov8c506602015-06-24 16:02:06 +0200569 goto Enable_cpus;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100570
571 local_irq_disable();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200572 system_state = SYSTEM_SUSPEND;
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100573 syscore_suspend();
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100574 if (pm_wakeup_pending()) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200575 error = -EAGAIN;
576 goto Power_up;
577 }
578
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100579 hibernation_ops->enter();
580 /* We should never get here */
581 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700582
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200583 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100584 syscore_resume();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200585 system_state = SYSTEM_RUNNING;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200586 local_irq_enable();
Vitaly Kuznetsov8c506602015-06-24 16:02:06 +0200587
588 Enable_cpus:
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200589 enable_nonboot_cpus();
590
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200591 Platform_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700592 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100593
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100594 dpm_resume_start(PMSG_RESTORE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100595
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700596 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100597 entering_platform_hibernation = false;
Alan Sternd1616302009-05-24 22:05:42 +0200598 dpm_resume_end(PMSG_RESTORE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700599 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100600
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100601 Close:
602 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100603
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700604 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700605}
606
607/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200608 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200610 * Use the platform driver, if configured, to put the system into the sleep
611 * state corresponding to hibernation, or try to power it off or reboot,
612 * depending on the value of hibernation_mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700614static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Bojan Smojver62c552c2012-06-16 00:09:58 +0200616#ifdef CONFIG_SUSPEND
617 int error;
Rafael J. Wysocki81d45bd2017-02-24 00:25:28 +0100618
619 if (hibernation_mode == HIBERNATION_SUSPEND) {
620 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
621 if (error) {
622 hibernation_mode = hibernation_ops ?
623 HIBERNATION_PLATFORM :
624 HIBERNATION_SHUTDOWN;
625 } else {
626 /* Restore swap signature. */
627 error = swsusp_unmark();
628 if (error)
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100629 pr_err("Swap will be unusable! Try swapon -a.\n");
Rafael J. Wysocki81d45bd2017-02-24 00:25:28 +0100630
631 return;
632 }
633 }
Bojan Smojver62c552c2012-06-16 00:09:58 +0200634#endif
635
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700636 switch (hibernation_mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700637 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600638 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700640 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700641 hibernation_platform_enter();
Gustavo A. R. Silva82837ad2018-08-04 21:12:00 -0500642 /* Fall through */
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700643 case HIBERNATION_SHUTDOWN:
Sebastian Capella2c730782014-04-21 17:30:46 -0700644 if (pm_power_off)
645 kernel_power_off();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700646 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600648 kernel_halt();
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700649 /*
650 * Valid image is on the disk, if we continue we risk serious data
651 * corruption after resume.
652 */
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100653 pr_crit("Power down manually\n");
Sebastian Capella2c730782014-04-21 17:30:46 -0700654 while (1)
655 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Chen Yufe12c002016-07-22 10:30:47 +0800658static int load_image_and_restore(void)
659{
660 int error;
661 unsigned int flags;
662
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200663 pm_pr_dbg("Loading hibernation image.\n");
Chen Yufe12c002016-07-22 10:30:47 +0800664
665 lock_device_hotplug();
666 error = create_basic_memory_bitmaps();
667 if (error)
668 goto Unlock;
669
670 error = swsusp_read(&flags);
671 swsusp_close(FMODE_READ);
672 if (!error)
673 hibernation_restore(flags & SF_PLATFORM_MODE);
674
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100675 pr_err("Failed to load hibernation image, recovering.\n");
Chen Yufe12c002016-07-22 10:30:47 +0800676 swsusp_free();
677 free_basic_memory_bitmaps();
678 Unlock:
679 unlock_device_hotplug();
680
681 return error;
682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200685 * hibernate - Carry out system hibernation, including saving the image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700687int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700689 int error, nr_calls = 0;
Chen Yufe12c002016-07-22 10:30:47 +0800690 bool snapshot_test = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Kees Cooka6e15a32014-06-13 13:30:35 -0700692 if (!hibernation_available()) {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200693 pm_pr_dbg("Hibernation not available.\n");
Kees Cooka6e15a32014-06-13 13:30:35 -0700694 return -EPERM;
695 }
696
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100697 lock_system_sleep();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700698 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700699 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
700 error = -EBUSY;
701 goto Unlock;
702 }
703
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200704 pr_info("hibernation entry\n");
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100705 pm_prepare_console();
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700706 error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
707 if (error) {
708 nr_calls--;
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700709 goto Exit;
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700710 }
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700711
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100712 pr_info("Syncing filesystems ... \n");
Dominik Brodowski70f68ee2018-03-14 22:35:11 +0100713 ksys_sync();
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100714 pr_info("done.\n");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700715
Tejun Heo03afed82011-11-21 12:32:24 -0800716 error = freeze_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700717 if (error)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200718 goto Exit;
719
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200720 lock_device_hotplug();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200721 /* Allocate memory management structures */
722 error = create_basic_memory_bitmaps();
723 if (error)
724 goto Thaw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700726 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100727 if (error || freezer_test_done)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200728 goto Free_bitmaps;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200729
730 if (in_suspend) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700731 unsigned int flags = 0;
732
733 if (hibernation_mode == HIBERNATION_PLATFORM)
734 flags |= SF_PLATFORM_MODE;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200735 if (nocompress)
736 flags |= SF_NOCOMPRESS_MODE;
Bojan Smojver081a9d02011-10-13 23:58:07 +0200737 else
738 flags |= SF_CRC32_MODE;
739
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200740 pm_pr_dbg("Writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700741 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700742 swsusp_free();
Chen Yufe12c002016-07-22 10:30:47 +0800743 if (!error) {
744 if (hibernation_mode == HIBERNATION_TEST_RESUME)
745 snapshot_test = true;
746 else
747 power_down();
748 }
MyungJoo Ham5262a472010-11-26 23:07:56 +0100749 in_suspend = 0;
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100750 pm_restore_gfp_mask();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800751 } else {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200752 pm_pr_dbg("Image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800753 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200754
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200755 Free_bitmaps:
756 free_basic_memory_bitmaps();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800757 Thaw:
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200758 unlock_device_hotplug();
Chen Yufe12c002016-07-22 10:30:47 +0800759 if (snapshot_test) {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200760 pm_pr_dbg("Checking hibernation image\n");
Chen Yufe12c002016-07-22 10:30:47 +0800761 error = swsusp_check();
762 if (!error)
763 error = load_image_and_restore();
764 }
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100765 thaw_processes();
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100766
767 /* Don't bother checking whether freezer_test_done is true */
768 freezer_test_done = false;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700769 Exit:
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700770 __pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100771 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700772 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700773 Unlock:
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100774 unlock_system_sleep();
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200775 pr_info("hibernation exit\n");
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return error;
778}
779
780
781/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200782 * software_resume - Resume from a saved hibernation image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200784 * This routine is called as a late initcall, when all devices have been
785 * discovered and initialized already.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200787 * The image reading code is called to see if there is a hibernation image
788 * available for reading. If that is the case, devices are quiesced and the
789 * contents of memory is restored from the saved image.
790 *
791 * If this is successful, control reappears in the restored target kernel in
Geliang Tangd439e642015-09-29 20:36:58 -0700792 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200793 * attempts to recover gracefully and make the kernel return to the normal mode
794 * of operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796static int software_resume(void)
797{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700798 int error, nr_calls = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Johannes Berg60a0d232007-11-14 17:00:16 -0800800 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100801 * If the user said "noresume".. bail out early.
802 */
Kees Cooka6e15a32014-06-13 13:30:35 -0700803 if (noresume || !hibernation_available())
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100804 return 0;
805
806 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800807 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
808 * is configured into the kernel. Since the regular hibernate
809 * trigger path is via sysfs which takes a buffer mutex before
Pingfan Liu55f25032018-07-31 16:51:32 +0800810 * calling hibernate functions (which take system_transition_mutex)
811 * this can cause lockdep to complain about a possible ABBA deadlock
Johannes Berg60a0d232007-11-14 17:00:16 -0800812 * which cannot happen since we're in the boot code here and
813 * sysfs can't be invoked yet. Therefore, we use a subclass
814 * here to avoid lockdep complaining.
815 */
Pingfan Liu55f25032018-07-31 16:51:32 +0800816 mutex_lock_nested(&system_transition_mutex, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200817
818 if (swsusp_resume_device)
819 goto Check_image;
820
821 if (!strlen(resume_file)) {
822 error = -ENOENT;
823 goto Unlock;
824 }
825
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200826 pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200827
Barry Songf126f732011-10-10 23:38:41 +0200828 if (resume_delay) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100829 pr_info("Waiting %dsec before reading resume device ...\n",
Barry Songf126f732011-10-10 23:38:41 +0200830 resume_delay);
831 ssleep(resume_delay);
832 }
833
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200834 /* Check if the device is there */
835 swsusp_resume_device = name_to_dev_t(resume_file);
Minho Ban2df83fa2012-05-14 21:45:31 +0200836
837 /*
838 * name_to_dev_t is ineffective to verify parition if resume_file is in
839 * integer format. (e.g. major:minor)
840 */
841 if (isdigit(resume_file[0]) && resume_wait) {
842 int partno;
843 while (!get_gendisk(swsusp_resume_device, &partno))
844 msleep(10);
845 }
846
Pavel Machek3efa1472005-07-07 17:56:43 -0700847 if (!swsusp_resume_device) {
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100848 /*
849 * Some device discovery might still be in progress; we need
850 * to wait for this to finish.
851 */
852 wait_for_device_probe();
Barry Song6f8d7022011-10-06 20:34:46 +0200853
854 if (resume_wait) {
855 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
856 msleep(10);
857 async_synchronize_full();
858 }
859
Pavel Machek3efa1472005-07-07 17:56:43 -0700860 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200861 if (!swsusp_resume_device) {
862 error = -ENODEV;
863 goto Unlock;
864 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700865 }
866
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200867 Check_image:
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200868 pm_pr_dbg("Hibernation image partition %d:%d present\n",
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200869 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200871 pm_pr_dbg("Looking for hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800872 error = swsusp_check();
873 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700874 goto Unlock;
875
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700876 /* The snapshot device should not be opened while we're running */
877 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
878 error = -EBUSY;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200879 swsusp_close(FMODE_READ);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700880 goto Unlock;
881 }
882
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200883 pr_info("resume from hibernation\n");
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
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200891 pm_pr_dbg("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. Wysocki8915aa202017-07-20 03:38:07 +0200900 pr_info("resume from hibernation failed (%d)\n", error);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700901 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700902 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700903 Unlock:
Pingfan Liu55f25032018-07-31 16:51:32 +0800904 mutex_unlock(&system_transition_mutex);
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200905 pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700906 return error;
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200907 Close_Finish:
Jiri Slaby76b57e62009-10-07 22:37:35 +0200908 swsusp_close(FMODE_READ);
909 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Russ Dilld3c345d2013-10-24 14:25:26 +0100912late_initcall_sync(software_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700915static const char * const hibernation_modes[] = {
916 [HIBERNATION_PLATFORM] = "platform",
917 [HIBERNATION_SHUTDOWN] = "shutdown",
918 [HIBERNATION_REBOOT] = "reboot",
Bojan Smojver62c552c2012-06-16 00:09:58 +0200919#ifdef CONFIG_SUSPEND
920 [HIBERNATION_SUSPEND] = "suspend",
921#endif
Chen Yufe12c002016-07-22 10:30:47 +0800922 [HIBERNATION_TEST_RESUME] = "test_resume",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923};
924
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200925/*
926 * /sys/power/disk - Control hibernation mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200928 * Hibernation can be handled in several ways. There are a few different ways
929 * to put the system into the sleep state: using the platform driver (e.g. ACPI
930 * or other hibernation_ops), powering it off or rebooting it (for testing
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100931 * mostly).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200933 * The sysfs file /sys/power/disk provides an interface for selecting the
934 * hibernation mode to use. Reading from this file causes the available modes
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100935 * to be printed. There are 3 modes that can be supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 * 'platform'
938 * 'shutdown'
939 * 'reboot'
940 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200941 * If a platform hibernation driver is in use, 'platform' will be supported
942 * and will be used by default. Otherwise, 'shutdown' will be used by default.
943 * The selected option (i.e. the one corresponding to the current value of
944 * hibernation_mode) is enclosed by a square bracket.
945 *
946 * To select a given hibernation mode it is necessary to write the mode's
947 * string representation (as returned by reading from /sys/power/disk) back
948 * into /sys/power/disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 */
950
Kay Sievers386f2752007-11-02 13:47:53 +0100951static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
952 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700954 int i;
955 char *start = buf;
956
Kees Cooka6e15a32014-06-13 13:30:35 -0700957 if (!hibernation_available())
958 return sprintf(buf, "[disabled]\n");
959
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700960 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
961 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700962 continue;
963 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700964 case HIBERNATION_SHUTDOWN:
965 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +0200966#ifdef CONFIG_SUSPEND
967 case HIBERNATION_SUSPEND:
968#endif
Chen Yufe12c002016-07-22 10:30:47 +0800969 case HIBERNATION_TEST_RESUME:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700970 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700971 case HIBERNATION_PLATFORM:
972 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700973 break;
974 /* not a valid mode, continue with loop */
975 continue;
976 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700977 if (i == hibernation_mode)
978 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700979 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700980 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700981 }
982 buf += sprintf(buf, "\n");
983 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Kay Sievers386f2752007-11-02 13:47:53 +0100986static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
987 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 int error = 0;
990 int i;
991 int len;
992 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700993 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Kees Cooka6e15a32014-06-13 13:30:35 -0700995 if (!hibernation_available())
996 return -EPERM;
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 p = memchr(buf, '\n', n);
999 len = p ? p - buf : n;
1000
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001001 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001002 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -07001003 if (len == strlen(hibernation_modes[i])
1004 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 mode = i;
1006 break;
1007 }
1008 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001009 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001010 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001011 case HIBERNATION_SHUTDOWN:
1012 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +02001013#ifdef CONFIG_SUSPEND
1014 case HIBERNATION_SUSPEND:
1015#endif
Chen Yufe12c002016-07-22 10:30:47 +08001016 case HIBERNATION_TEST_RESUME:
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001017 hibernation_mode = mode;
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001018 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001019 case HIBERNATION_PLATFORM:
1020 if (hibernation_ops)
1021 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 else
1023 error = -EINVAL;
1024 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001025 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 error = -EINVAL;
1027
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001028 if (!error)
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +02001029 pm_pr_dbg("Hibernation mode set to '%s'\n",
1030 hibernation_modes[mode]);
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001031 unlock_system_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return error ? error : n;
1033}
1034
1035power_attr(disk);
1036
Kay Sievers386f2752007-11-02 13:47:53 +01001037static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1038 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
1041 MINOR(swsusp_resume_device));
1042}
1043
Kay Sievers386f2752007-11-02 13:47:53 +01001044static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1045 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 dev_t res;
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001048 int len = n;
1049 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001051 if (len && buf[len-1] == '\n')
1052 len--;
1053 name = kstrndup(buf, len, GFP_KERNEL);
1054 if (!name)
1055 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001057 res = name_to_dev_t(name);
1058 kfree(name);
1059 if (!res)
1060 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001062 lock_system_sleep();
Andrew Mortona5762192006-01-06 00:09:50 -08001063 swsusp_resume_device = res;
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001064 unlock_system_sleep();
Mario Limonciello64846402018-03-28 12:01:10 -05001065 pm_pr_dbg("Configured resume from disk to %u\n", swsusp_resume_device);
Andrew Mortona5762192006-01-06 00:09:50 -08001066 noresume = 0;
1067 software_resume();
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001068 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
1071power_attr(resume);
1072
Mario Limonciello35506462018-03-28 12:01:09 -05001073static ssize_t resume_offset_show(struct kobject *kobj,
1074 struct kobj_attribute *attr, char *buf)
1075{
1076 return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
1077}
1078
1079static ssize_t resume_offset_store(struct kobject *kobj,
1080 struct kobj_attribute *attr, const char *buf,
1081 size_t n)
1082{
1083 unsigned long long offset;
1084 int rc;
1085
1086 rc = kstrtoull(buf, 0, &offset);
1087 if (rc)
1088 return rc;
1089 swsusp_resume_block = offset;
1090
1091 return n;
1092}
1093
1094power_attr(resume_offset);
1095
Kay Sievers386f2752007-11-02 13:47:53 +01001096static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1097 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001098{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001099 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001100}
1101
Kay Sievers386f2752007-11-02 13:47:53 +01001102static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1103 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001104{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001105 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001106
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001107 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001108 image_size = size;
1109 return n;
1110 }
1111
1112 return -EINVAL;
1113}
1114
1115power_attr(image_size);
1116
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001117static ssize_t reserved_size_show(struct kobject *kobj,
1118 struct kobj_attribute *attr, char *buf)
1119{
1120 return sprintf(buf, "%lu\n", reserved_size);
1121}
1122
1123static ssize_t reserved_size_store(struct kobject *kobj,
1124 struct kobj_attribute *attr,
1125 const char *buf, size_t n)
1126{
1127 unsigned long size;
1128
1129 if (sscanf(buf, "%lu", &size) == 1) {
1130 reserved_size = size;
1131 return n;
1132 }
1133
1134 return -EINVAL;
1135}
1136
1137power_attr(reserved_size);
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139static struct attribute * g[] = {
1140 &disk_attr.attr,
Mario Limonciello35506462018-03-28 12:01:09 -05001141 &resume_offset_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001143 &image_size_attr.attr,
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001144 &reserved_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 NULL,
1146};
1147
1148
Arvind Yadav59494fe2017-06-29 16:58:40 +05301149static const struct attribute_group attr_group = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 .attrs = g,
1151};
1152
1153
1154static int __init pm_disk_init(void)
1155{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -08001156 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
1159core_initcall(pm_disk_init);
1160
1161
1162static int __init resume_setup(char *str)
1163{
1164 if (noresume)
1165 return 1;
1166
1167 strncpy( resume_file, str, 255 );
1168 return 1;
1169}
1170
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001171static int __init resume_offset_setup(char *str)
1172{
1173 unsigned long long offset;
1174
1175 if (noresume)
1176 return 1;
1177
1178 if (sscanf(str, "%llu", &offset) == 1)
1179 swsusp_resume_block = offset;
1180
1181 return 1;
1182}
1183
Bojan Smojverf996fc92010-09-09 23:06:23 +02001184static int __init hibernate_setup(char *str)
1185{
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001186 if (!strncmp(str, "noresume", 8)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001187 noresume = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001188 } else if (!strncmp(str, "nocompress", 10)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001189 nocompress = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001190 } else if (!strncmp(str, "no", 2)) {
Kees Cooka6e15a32014-06-13 13:30:35 -07001191 noresume = 1;
1192 nohibernate = 1;
Laura Abbott0f5bf6d2017-02-06 16:31:58 -08001193 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
Rafael J. Wysocki4c0b6c12016-07-10 02:12:10 +02001194 && !strncmp(str, "protect_image", 13)) {
1195 enable_restore_image_protection();
Kees Cooka6e15a32014-06-13 13:30:35 -07001196 }
Bojan Smojverf996fc92010-09-09 23:06:23 +02001197 return 1;
1198}
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200static int __init noresume_setup(char *str)
1201{
1202 noresume = 1;
1203 return 1;
1204}
1205
Barry Song6f8d7022011-10-06 20:34:46 +02001206static int __init resumewait_setup(char *str)
1207{
1208 resume_wait = 1;
1209 return 1;
1210}
1211
Barry Songf126f732011-10-10 23:38:41 +02001212static int __init resumedelay_setup(char *str)
1213{
Dan Carpenterf6514be2014-05-14 19:08:46 +03001214 int rc = kstrtouint(str, 0, &resume_delay);
Fabian Frederick317cf7e2014-05-09 23:32:08 +02001215
1216 if (rc)
1217 return rc;
Barry Songf126f732011-10-10 23:38:41 +02001218 return 1;
1219}
1220
Kees Cooka6e15a32014-06-13 13:30:35 -07001221static int __init nohibernate_setup(char *str)
1222{
1223 noresume = 1;
1224 nohibernate = 1;
1225 return 1;
1226}
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001229__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230__setup("resume=", resume_setup);
Bojan Smojverf996fc92010-09-09 23:06:23 +02001231__setup("hibernate=", hibernate_setup);
Barry Song6f8d7022011-10-06 20:34:46 +02001232__setup("resumewait", resumewait_setup);
Barry Songf126f732011-10-10 23:38:41 +02001233__setup("resumedelay=", resumedelay_setup);
Kees Cooka6e15a32014-06-13 13:30:35 -07001234__setup("nohibernate", nohibernate_setup);