blob: 8fc054e5c5011615406e4fcb987534704ecd08db [file] [log] [blame]
Thomas Gleixner55716d22019-06-01 10:08:42 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Rafael J. Wysocki8b759b82009-06-10 01:27:49 +02003 * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (c) 2003 Patrick Mochel
6 * Copyright (c) 2003 Open Source Development Lab
Pavel Macheka2531292010-07-18 14:27:13 +02007 * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz>
Rafael J. Wysocki8b759b82009-06-10 01:27:49 +02008 * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
Bojan Smojver62c552c2012-06-16 00:09:58 +02009 * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Rafael J. Wysocki2872de12017-02-24 00:26:15 +010012#define pr_fmt(fmt) "PM: " fmt
13
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/suspend.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/reboot.h>
17#include <linux/string.h>
18#include <linux/device.h>
Barry Song6f8d7022011-10-06 20:34:46 +020019#include <linux/async.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h>
21#include <linux/fs.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070022#include <linux/mount.h>
Eric W. Biederman88d10bb2005-09-22 21:43:46 -070023#include <linux/pm.h>
Ingo Molnar38b8d202017-02-08 18:51:31 +010024#include <linux/nmi.h>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070025#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070026#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080027#include <linux/freezer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/gfp.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010029#include <linux/syscore_ops.h>
Minho Ban2df83fa2012-05-14 21:45:31 +020030#include <linux/ctype.h>
31#include <linux/genhd.h>
Tina Ruchandanidb597602014-10-30 11:04:53 -070032#include <linux/ktime.h>
Todd E Brandtbb3632c2014-06-06 05:40:17 -070033#include <trace/events/power.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "power.h"
36
37
Barry Songd231ff12011-10-11 23:29:18 -070038static int nocompress;
39static int noresume;
Kees Cooka6e15a32014-06-13 13:30:35 -070040static int nohibernate;
Barry Songd231ff12011-10-11 23:29:18 -070041static int resume_wait;
Dan Carpenterf6514be2014-05-14 19:08:46 +030042static unsigned int resume_delay;
Adrian Bunk47a460d2008-02-04 22:30:06 -080043static char resume_file[256] = CONFIG_PM_STD_PARTITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080045sector_t swsusp_resume_block;
Andi Kleend6efc2f2013-08-05 15:02:49 -070046__visible int in_suspend __nosavedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070048enum {
49 HIBERNATION_INVALID,
50 HIBERNATION_PLATFORM,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070051 HIBERNATION_SHUTDOWN,
52 HIBERNATION_REBOOT,
Bojan Smojver62c552c2012-06-16 00:09:58 +020053#ifdef CONFIG_SUSPEND
54 HIBERNATION_SUSPEND,
55#endif
Chen Yufe12c002016-07-22 10:30:47 +080056 HIBERNATION_TEST_RESUME,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070057 /* keep last */
58 __HIBERNATION_AFTER_LAST
59};
60#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
61#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
62
63static int hibernation_mode = HIBERNATION_SHUTDOWN;
64
Srivatsa S. Bhat97819a22011-12-01 22:33:10 +010065bool freezer_test_done;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +010066
Lionel Debroux073ef1f2010-11-09 21:48:49 +010067static const struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070068
Kees Cooka6e15a32014-06-13 13:30:35 -070069bool hibernation_available(void)
70{
71 return (nohibernate == 0);
72}
73
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070074/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +020075 * hibernation_set_ops - Set the global hibernate operations.
76 * @ops: Hibernation operations to use in subsequent hibernation transitions.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070077 */
Lionel Debroux073ef1f2010-11-09 21:48:49 +010078void hibernation_set_ops(const struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070079{
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +010080 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
81 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
MyungJoo Ham5729c632010-11-26 23:07:48 +010082 && ops->restore_cleanup && ops->leave)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070083 WARN_ON(1);
84 return;
85 }
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010086 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070087 hibernation_ops = ops;
88 if (ops)
89 hibernation_mode = HIBERNATION_PLATFORM;
90 else if (hibernation_mode == HIBERNATION_PLATFORM)
91 hibernation_mode = HIBERNATION_SHUTDOWN;
92
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010093 unlock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070094}
Leonardo Potenzae0c78552013-11-19 12:27:41 +000095EXPORT_SYMBOL_GPL(hibernation_set_ops);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070096
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +010097static bool entering_platform_hibernation;
98
99bool system_entering_hibernation(void)
100{
101 return entering_platform_hibernation;
102}
103EXPORT_SYMBOL(system_entering_hibernation);
104
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100105#ifdef CONFIG_PM_DEBUG
106static void hibernation_debug_sleep(void)
107{
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100108 pr_info("hibernation debug: Waiting for 5 seconds.\n");
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100109 mdelay(5000);
110}
111
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100112static int hibernation_test(int level)
113{
114 if (pm_test_level == level) {
115 hibernation_debug_sleep();
116 return 1;
117 }
118 return 0;
119}
120#else /* !CONFIG_PM_DEBUG */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100121static int hibernation_test(int level) { return 0; }
122#endif /* !CONFIG_PM_DEBUG */
123
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700124/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200125 * platform_begin - Call platform to start hibernation.
126 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700127 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100128static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700129{
130 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockibb186902019-05-16 12:43:19 +0200131 hibernation_ops->begin(PMSG_FREEZE) : 0;
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100132}
133
134/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200135 * platform_end - Call platform to finish transition to the working state.
136 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100137 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100138static void platform_end(int platform_mode)
139{
140 if (platform_mode && hibernation_ops)
141 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700142}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200145 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
146 * @platform_mode: Whether or not to use the platform driver.
147 *
148 * Use the platform driver to prepare the system for creating a hibernate image,
149 * if so configured, and return an error code if that fails.
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800150 */
151
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700152static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800153{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700154 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700155 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800156}
157
158/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200159 * platform_leave - Call platform to prepare a transition to the working state.
160 * @platform_mode: Whether or not to use the platform driver.
161 *
162 * Use the platform driver prepare to prepare the machine for switching to the
163 * normal mode of operation.
164 *
165 * This routine is called on one CPU with interrupts disabled.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700166 */
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700167static void platform_leave(int platform_mode)
168{
169 if (platform_mode && hibernation_ops)
170 hibernation_ops->leave();
171}
172
173/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200174 * platform_finish - Call platform to switch the system to the working state.
175 * @platform_mode: Whether or not to use the platform driver.
176 *
177 * Use the platform driver to switch the machine to the normal mode of
178 * operation.
179 *
180 * This routine must be called after platform_prepare().
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700181 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700182static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700183{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700184 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700185 hibernation_ops->finish();
186}
187
188/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200189 * platform_pre_restore - Prepare for hibernate image restoration.
190 * @platform_mode: Whether or not to use the platform driver.
191 *
192 * Use the platform driver to prepare the system for resume from a hibernation
193 * image.
194 *
195 * If the restore fails after this function has been called,
196 * platform_restore_cleanup() must be called.
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700197 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700198static int platform_pre_restore(int platform_mode)
199{
200 return (platform_mode && hibernation_ops) ?
201 hibernation_ops->pre_restore() : 0;
202}
203
204/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200205 * platform_restore_cleanup - Switch to the working state after failing restore.
206 * @platform_mode: Whether or not to use the platform driver.
207 *
208 * Use the platform driver to switch the system to the normal mode of operation
209 * after a failing restore.
210 *
211 * If platform_pre_restore() has been called before the failing restore, this
212 * function must be called too, regardless of the result of
213 * platform_pre_restore().
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700214 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700215static void platform_restore_cleanup(int platform_mode)
216{
217 if (platform_mode && hibernation_ops)
218 hibernation_ops->restore_cleanup();
219}
220
221/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200222 * platform_recover - Recover from a failure to suspend devices.
223 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200224 */
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200225static void platform_recover(int platform_mode)
226{
227 if (platform_mode && hibernation_ops && hibernation_ops->recover)
228 hibernation_ops->recover();
229}
230
231/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200232 * swsusp_show_speed - Print time elapsed between two events during hibernation.
233 * @start: Starting event.
234 * @stop: Final event.
235 * @nr_pages: Number of memory pages processed between @start and @stop.
236 * @msg: Additional diagnostic message to print.
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100237 */
Tina Ruchandanidb597602014-10-30 11:04:53 -0700238void swsusp_show_speed(ktime_t start, ktime_t stop,
239 unsigned nr_pages, char *msg)
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100240{
Tina Ruchandanidb597602014-10-30 11:04:53 -0700241 ktime_t diff;
Chen Gang4881f602014-04-25 08:44:59 +0800242 u64 elapsed_centisecs64;
243 unsigned int centisecs;
244 unsigned int k;
245 unsigned int kps;
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100246
Tina Ruchandanidb597602014-10-30 11:04:53 -0700247 diff = ktime_sub(stop, start);
248 elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC);
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100249 centisecs = elapsed_centisecs64;
250 if (centisecs == 0)
251 centisecs = 1; /* avoid div-by-zero */
252 k = nr_pages * (PAGE_SIZE / 1024);
253 kps = (k * 100) / centisecs;
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100254 pr_info("%s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
255 msg, k, centisecs / 100, centisecs % 100, kps / 1000,
256 (kps % 1000) / 10);
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100257}
258
259/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200260 * create_image - Create a hibernation image.
261 * @platform_mode: Whether or not to use the platform driver.
262 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100263 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
264 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200265 *
266 * Control reappears in this routine after the subsequent restore.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700267 */
Adrian Bunk47a460d2008-02-04 22:30:06 -0800268static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700269{
270 int error;
271
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100272 error = dpm_suspend_end(PMSG_FREEZE);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700273 if (error) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100274 pr_err("Some devices failed to power down, aborting hibernation\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200275 return error;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700276 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100277
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100278 error = platform_pre_snapshot(platform_mode);
279 if (error || hibernation_test(TEST_PLATFORM))
280 goto Platform_finish;
281
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000282 error = suspend_disable_secondary_cpus();
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100283 if (error || hibernation_test(TEST_CPUS))
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100284 goto Enable_cpus;
285
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100286 local_irq_disable();
287
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200288 system_state = SYSTEM_SUSPEND;
289
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200290 error = syscore_suspend();
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100291 if (error) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100292 pr_err("Some system devices failed to power down, aborting hibernation\n");
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100293 goto Enable_irqs;
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100294 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700295
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100296 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100297 goto Power_up;
298
299 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700300 save_processor_state();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700301 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700302 error = swsusp_arch_suspend();
Thomas Garnier62822e22016-08-11 14:49:29 -0700303 /* Restore control flow magically appears here */
304 restore_processor_state();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700305 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700306 if (error)
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100307 pr_err("Error %d creating hibernation image\n", error);
308
Anisse Astier1ad14102016-09-09 10:43:32 +0200309 if (!in_suspend) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200310 events_check_enabled = false;
Anisse Astier1ad14102016-09-09 10:43:32 +0200311 clear_free_pages();
312 }
Bjørn Mork362e77d2013-12-04 16:06:58 +0100313
314 platform_leave(platform_mode);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100315
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100316 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100317 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100318
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100319 Enable_irqs:
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200320 system_state = SYSTEM_RUNNING;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100321 local_irq_enable();
322
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100323 Enable_cpus:
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000324 suspend_enable_secondary_cpus();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100325
326 Platform_finish:
327 platform_finish(platform_mode);
328
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100329 dpm_resume_start(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200330 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100331
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700332 return error;
333}
334
335/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200336 * hibernation_snapshot - Quiesce devices and create a hibernation image.
337 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700338 *
Pingfan Liu55f25032018-07-31 16:51:32 +0800339 * This routine must be called with system_transition_mutex held.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700340 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700341int hibernation_snapshot(int platform_mode)
342{
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100343 pm_message_t msg;
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100344 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700345
Lukas Wunner27614272016-03-23 00:11:20 +0100346 pm_suspend_clear_flags();
Zhang Rui3fe03132008-10-26 20:50:26 +0100347 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700348 if (error)
Rafael J. Wysockid074ee02010-07-07 23:43:35 +0200349 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700350
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200351 /* Preallocate image memory before shutting down devices. */
352 error = hibernate_preallocate_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700353 if (error)
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200354 goto Close;
355
356 error = freeze_kernel_threads();
357 if (error)
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100358 goto Cleanup;
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200359
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100360 if (hibernation_test(TEST_FREEZER)) {
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100361
362 /*
363 * Indicate to the caller that we are returning due to a
364 * successful freezer test.
365 */
366 freezer_test_done = true;
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100367 goto Thaw;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100368 }
369
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200370 error = dpm_prepare(PMSG_FREEZE);
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100371 if (error) {
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100372 dpm_complete(PMSG_RECOVER);
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100373 goto Thaw;
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100374 }
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700375
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700376 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100377 pm_restrict_gfp_mask();
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100378
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200379 error = dpm_suspend(PMSG_FREEZE);
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700380
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100381 if (error || hibernation_test(TEST_DEVICES))
382 platform_recover(platform_mode);
383 else
384 error = create_image(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700385
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100386 /*
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100387 * In the case that we call create_image() above, the control
388 * returns here (1) after the image has been created or the
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100389 * image creation has failed and (2) after a successful restore.
390 */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100391
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200392 /* We may need to release the preallocated image pages here. */
393 if (error || !in_suspend)
394 swsusp_free();
395
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200396 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
397 dpm_resume(msg);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100398
399 if (error || !in_suspend)
400 pm_restore_gfp_mask();
401
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700402 resume_console();
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200403 dpm_complete(msg);
404
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100405 Close:
406 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700407 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200408
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100409 Thaw:
410 thaw_kernel_threads();
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100411 Cleanup:
412 swsusp_free();
413 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700414}
415
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200416int __weak hibernate_resume_nonboot_cpu_disable(void)
417{
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000418 return suspend_disable_secondary_cpus();
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200419}
420
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700421/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200422 * resume_target_kernel - Restore system state from a hibernation image.
423 * @platform_mode: Whether or not to use the platform driver.
424 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100425 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
426 * contents of highmem that have not been restored yet from the image and run
427 * the low-level code that will restore the remaining contents of memory and
428 * switch to the just restored target kernel.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100429 */
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100430static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100431{
432 int error;
433
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100434 error = dpm_suspend_end(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100435 if (error) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100436 pr_err("Some devices failed to power down, aborting resume\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200437 return error;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100438 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100439
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100440 error = platform_pre_restore(platform_mode);
441 if (error)
442 goto Cleanup;
443
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200444 error = hibernate_resume_nonboot_cpu_disable();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100445 if (error)
446 goto Enable_cpus;
447
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100448 local_irq_disable();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200449 system_state = SYSTEM_SUSPEND;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100450
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200451 error = syscore_suspend();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100452 if (error)
453 goto Enable_irqs;
454
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100455 save_processor_state();
456 error = restore_highmem();
457 if (!error) {
458 error = swsusp_arch_resume();
459 /*
460 * The code below is only ever reached in case of a failure.
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200461 * Otherwise, execution continues at the place where
462 * swsusp_arch_suspend() was called.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100463 */
464 BUG_ON(!error);
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200465 /*
466 * This call to restore_highmem() reverts the changes made by
467 * the previous one.
468 */
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100469 restore_highmem();
470 }
471 /*
472 * The only reason why swsusp_arch_resume() can fail is memory being
473 * very tight, so we have to free it as soon as we can to avoid
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200474 * subsequent failures.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100475 */
476 swsusp_free();
477 restore_processor_state();
478 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100479
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100480 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100481
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100482 Enable_irqs:
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200483 system_state = SYSTEM_RUNNING;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100484 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100485
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100486 Enable_cpus:
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000487 suspend_enable_secondary_cpus();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100488
489 Cleanup:
490 platform_restore_cleanup(platform_mode);
491
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100492 dpm_resume_start(PMSG_RECOVER);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100493
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100494 return error;
495}
496
497/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200498 * hibernation_restore - Quiesce devices and restore from a hibernation image.
499 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700500 *
Pingfan Liu55f25032018-07-31 16:51:32 +0800501 * This routine must be called with system_transition_mutex held. If it is
502 * successful, control reappears in the restored target kernel in
503 * hibernation_snapshot().
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700504 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700505int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700506{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100507 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700508
509 pm_prepare_console();
510 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100511 pm_restrict_gfp_mask();
Alan Sternd1616302009-05-24 22:05:42 +0200512 error = dpm_suspend_start(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700513 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100514 error = resume_target_kernel(platform_mode);
Imre Deak94fb8232014-10-24 20:29:10 +0300515 /*
516 * The above should either succeed and jump to the new kernel,
517 * or return with an error. Otherwise things are just
518 * undefined, so let's be paranoid.
519 */
520 BUG_ON(!error);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700521 }
Imre Deak94fb8232014-10-24 20:29:10 +0300522 dpm_resume_end(PMSG_RECOVER);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100523 pm_restore_gfp_mask();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700524 resume_console();
525 pm_restore_console();
526 return error;
527}
528
529/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200530 * hibernation_platform_enter - Power off the system using the platform driver.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700531 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700532int hibernation_platform_enter(void)
533{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100534 int error;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700535
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700536 if (!hibernation_ops)
537 return -ENOSYS;
538
539 /*
540 * We have cancelled the power transition by running
541 * hibernation_ops->finish() before saving the image, so we should let
542 * the firmware know that we're going to enter the sleep state after all
543 */
Rafael J. Wysockibb186902019-05-16 12:43:19 +0200544 error = hibernation_ops->begin(PMSG_HIBERNATE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700545 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100546 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700547
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100548 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700549 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +0200550 error = dpm_suspend_start(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200551 if (error) {
552 if (hibernation_ops->recover)
553 hibernation_ops->recover();
554 goto Resume_devices;
555 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700556
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100557 error = dpm_suspend_end(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100558 if (error)
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200559 goto Resume_devices;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100560
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100561 error = hibernation_ops->prepare();
562 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200563 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100564
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000565 error = suspend_disable_secondary_cpus();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100566 if (error)
Vitaly Kuznetsov8c506602015-06-24 16:02:06 +0200567 goto Enable_cpus;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100568
569 local_irq_disable();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200570 system_state = SYSTEM_SUSPEND;
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100571 syscore_suspend();
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100572 if (pm_wakeup_pending()) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200573 error = -EAGAIN;
574 goto Power_up;
575 }
576
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100577 hibernation_ops->enter();
578 /* We should never get here */
579 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700580
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200581 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100582 syscore_resume();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200583 system_state = SYSTEM_RUNNING;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200584 local_irq_enable();
Vitaly Kuznetsov8c506602015-06-24 16:02:06 +0200585
586 Enable_cpus:
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000587 suspend_enable_secondary_cpus();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200588
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200589 Platform_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700590 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100591
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100592 dpm_resume_start(PMSG_RESTORE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100593
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700594 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100595 entering_platform_hibernation = false;
Alan Sternd1616302009-05-24 22:05:42 +0200596 dpm_resume_end(PMSG_RESTORE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700597 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100598
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100599 Close:
600 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100601
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700602 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700603}
604
605/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200606 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200608 * Use the platform driver, if configured, to put the system into the sleep
609 * state corresponding to hibernation, or try to power it off or reboot,
610 * depending on the value of hibernation_mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 */
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700612static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Bojan Smojver62c552c2012-06-16 00:09:58 +0200614#ifdef CONFIG_SUSPEND
615 int error;
Rafael J. Wysocki81d45bd2017-02-24 00:25:28 +0100616
617 if (hibernation_mode == HIBERNATION_SUSPEND) {
618 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
619 if (error) {
620 hibernation_mode = hibernation_ops ?
621 HIBERNATION_PLATFORM :
622 HIBERNATION_SHUTDOWN;
623 } else {
624 /* Restore swap signature. */
625 error = swsusp_unmark();
626 if (error)
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100627 pr_err("Swap will be unusable! Try swapon -a.\n");
Rafael J. Wysocki81d45bd2017-02-24 00:25:28 +0100628
629 return;
630 }
631 }
Bojan Smojver62c552c2012-06-16 00:09:58 +0200632#endif
633
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700634 switch (hibernation_mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700635 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600636 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700638 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700639 hibernation_platform_enter();
Gustavo A. R. Silva82837ad2018-08-04 21:12:00 -0500640 /* Fall through */
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700641 case HIBERNATION_SHUTDOWN:
Sebastian Capella2c730782014-04-21 17:30:46 -0700642 if (pm_power_off)
643 kernel_power_off();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700644 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600646 kernel_halt();
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700647 /*
648 * Valid image is on the disk, if we continue we risk serious data
649 * corruption after resume.
650 */
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100651 pr_crit("Power down manually\n");
Sebastian Capella2c730782014-04-21 17:30:46 -0700652 while (1)
653 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Chen Yufe12c002016-07-22 10:30:47 +0800656static int load_image_and_restore(void)
657{
658 int error;
659 unsigned int flags;
660
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200661 pm_pr_dbg("Loading hibernation image.\n");
Chen Yufe12c002016-07-22 10:30:47 +0800662
663 lock_device_hotplug();
664 error = create_basic_memory_bitmaps();
665 if (error)
666 goto Unlock;
667
668 error = swsusp_read(&flags);
669 swsusp_close(FMODE_READ);
670 if (!error)
671 hibernation_restore(flags & SF_PLATFORM_MODE);
672
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100673 pr_err("Failed to load hibernation image, recovering.\n");
Chen Yufe12c002016-07-22 10:30:47 +0800674 swsusp_free();
675 free_basic_memory_bitmaps();
676 Unlock:
677 unlock_device_hotplug();
678
679 return error;
680}
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200683 * hibernate - Carry out system hibernation, including saving the image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700685int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700687 int error, nr_calls = 0;
Chen Yufe12c002016-07-22 10:30:47 +0800688 bool snapshot_test = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Kees Cooka6e15a32014-06-13 13:30:35 -0700690 if (!hibernation_available()) {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200691 pm_pr_dbg("Hibernation not available.\n");
Kees Cooka6e15a32014-06-13 13:30:35 -0700692 return -EPERM;
693 }
694
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100695 lock_system_sleep();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700696 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700697 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
698 error = -EBUSY;
699 goto Unlock;
700 }
701
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200702 pr_info("hibernation entry\n");
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100703 pm_prepare_console();
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700704 error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
705 if (error) {
706 nr_calls--;
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700707 goto Exit;
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700708 }
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700709
Harry Panb5dee312019-02-25 20:36:41 +0800710 ksys_sync_helper();
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700711
Tejun Heo03afed82011-11-21 12:32:24 -0800712 error = freeze_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700713 if (error)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200714 goto Exit;
715
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200716 lock_device_hotplug();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200717 /* Allocate memory management structures */
718 error = create_basic_memory_bitmaps();
719 if (error)
720 goto Thaw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700722 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100723 if (error || freezer_test_done)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200724 goto Free_bitmaps;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200725
726 if (in_suspend) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700727 unsigned int flags = 0;
728
729 if (hibernation_mode == HIBERNATION_PLATFORM)
730 flags |= SF_PLATFORM_MODE;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200731 if (nocompress)
732 flags |= SF_NOCOMPRESS_MODE;
Bojan Smojver081a9d02011-10-13 23:58:07 +0200733 else
734 flags |= SF_CRC32_MODE;
735
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200736 pm_pr_dbg("Writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700737 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700738 swsusp_free();
Chen Yufe12c002016-07-22 10:30:47 +0800739 if (!error) {
740 if (hibernation_mode == HIBERNATION_TEST_RESUME)
741 snapshot_test = true;
742 else
743 power_down();
744 }
MyungJoo Ham5262a472010-11-26 23:07:56 +0100745 in_suspend = 0;
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100746 pm_restore_gfp_mask();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800747 } else {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200748 pm_pr_dbg("Image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800749 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200750
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200751 Free_bitmaps:
752 free_basic_memory_bitmaps();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800753 Thaw:
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200754 unlock_device_hotplug();
Chen Yufe12c002016-07-22 10:30:47 +0800755 if (snapshot_test) {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200756 pm_pr_dbg("Checking hibernation image\n");
Chen Yufe12c002016-07-22 10:30:47 +0800757 error = swsusp_check();
758 if (!error)
759 error = load_image_and_restore();
760 }
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100761 thaw_processes();
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100762
763 /* Don't bother checking whether freezer_test_done is true */
764 freezer_test_done = false;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700765 Exit:
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700766 __pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100767 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700768 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700769 Unlock:
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100770 unlock_system_sleep();
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200771 pr_info("hibernation exit\n");
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return error;
774}
775
776
777/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200778 * software_resume - Resume from a saved hibernation image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200780 * This routine is called as a late initcall, when all devices have been
781 * discovered and initialized already.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200783 * The image reading code is called to see if there is a hibernation image
784 * available for reading. If that is the case, devices are quiesced and the
785 * contents of memory is restored from the saved image.
786 *
787 * If this is successful, control reappears in the restored target kernel in
Geliang Tangd439e642015-09-29 20:36:58 -0700788 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200789 * attempts to recover gracefully and make the kernel return to the normal mode
790 * of operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792static int software_resume(void)
793{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700794 int error, nr_calls = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Johannes Berg60a0d232007-11-14 17:00:16 -0800796 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100797 * If the user said "noresume".. bail out early.
798 */
Kees Cooka6e15a32014-06-13 13:30:35 -0700799 if (noresume || !hibernation_available())
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100800 return 0;
801
802 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800803 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
804 * is configured into the kernel. Since the regular hibernate
805 * trigger path is via sysfs which takes a buffer mutex before
Pingfan Liu55f25032018-07-31 16:51:32 +0800806 * calling hibernate functions (which take system_transition_mutex)
807 * this can cause lockdep to complain about a possible ABBA deadlock
Johannes Berg60a0d232007-11-14 17:00:16 -0800808 * which cannot happen since we're in the boot code here and
809 * sysfs can't be invoked yet. Therefore, we use a subclass
810 * here to avoid lockdep complaining.
811 */
Pingfan Liu55f25032018-07-31 16:51:32 +0800812 mutex_lock_nested(&system_transition_mutex, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200813
814 if (swsusp_resume_device)
815 goto Check_image;
816
817 if (!strlen(resume_file)) {
818 error = -ENOENT;
819 goto Unlock;
820 }
821
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200822 pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200823
Barry Songf126f732011-10-10 23:38:41 +0200824 if (resume_delay) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100825 pr_info("Waiting %dsec before reading resume device ...\n",
Barry Songf126f732011-10-10 23:38:41 +0200826 resume_delay);
827 ssleep(resume_delay);
828 }
829
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200830 /* Check if the device is there */
831 swsusp_resume_device = name_to_dev_t(resume_file);
Minho Ban2df83fa2012-05-14 21:45:31 +0200832
833 /*
834 * name_to_dev_t is ineffective to verify parition if resume_file is in
835 * integer format. (e.g. major:minor)
836 */
837 if (isdigit(resume_file[0]) && resume_wait) {
838 int partno;
839 while (!get_gendisk(swsusp_resume_device, &partno))
840 msleep(10);
841 }
842
Pavel Machek3efa1472005-07-07 17:56:43 -0700843 if (!swsusp_resume_device) {
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100844 /*
845 * Some device discovery might still be in progress; we need
846 * to wait for this to finish.
847 */
848 wait_for_device_probe();
Barry Song6f8d7022011-10-06 20:34:46 +0200849
850 if (resume_wait) {
851 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
852 msleep(10);
853 async_synchronize_full();
854 }
855
Pavel Machek3efa1472005-07-07 17:56:43 -0700856 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200857 if (!swsusp_resume_device) {
858 error = -ENODEV;
859 goto Unlock;
860 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700861 }
862
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200863 Check_image:
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200864 pm_pr_dbg("Hibernation image partition %d:%d present\n",
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200865 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200867 pm_pr_dbg("Looking for hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800868 error = swsusp_check();
869 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700870 goto Unlock;
871
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700872 /* The snapshot device should not be opened while we're running */
873 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
874 error = -EBUSY;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200875 swsusp_close(FMODE_READ);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700876 goto Unlock;
877 }
878
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200879 pr_info("resume from hibernation\n");
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100880 pm_prepare_console();
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700881 error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
882 if (error) {
883 nr_calls--;
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200884 goto Close_Finish;
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700885 }
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700886
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200887 pm_pr_dbg("Preparing processes for restore.\n");
Tejun Heo03afed82011-11-21 12:32:24 -0800888 error = freeze_processes();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200889 if (error)
890 goto Close_Finish;
Chen Yufe12c002016-07-22 10:30:47 +0800891 error = load_image_and_restore();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200892 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700893 Finish:
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700894 __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100895 pm_restore_console();
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200896 pr_info("resume from hibernation failed (%d)\n", error);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700897 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700898 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700899 Unlock:
Pingfan Liu55f25032018-07-31 16:51:32 +0800900 mutex_unlock(&system_transition_mutex);
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200901 pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700902 return error;
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200903 Close_Finish:
Jiri Slaby76b57e62009-10-07 22:37:35 +0200904 swsusp_close(FMODE_READ);
905 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
Russ Dilld3c345d2013-10-24 14:25:26 +0100908late_initcall_sync(software_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700911static const char * const hibernation_modes[] = {
912 [HIBERNATION_PLATFORM] = "platform",
913 [HIBERNATION_SHUTDOWN] = "shutdown",
914 [HIBERNATION_REBOOT] = "reboot",
Bojan Smojver62c552c2012-06-16 00:09:58 +0200915#ifdef CONFIG_SUSPEND
916 [HIBERNATION_SUSPEND] = "suspend",
917#endif
Chen Yufe12c002016-07-22 10:30:47 +0800918 [HIBERNATION_TEST_RESUME] = "test_resume",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919};
920
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200921/*
922 * /sys/power/disk - Control hibernation mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200924 * Hibernation can be handled in several ways. There are a few different ways
925 * to put the system into the sleep state: using the platform driver (e.g. ACPI
926 * or other hibernation_ops), powering it off or rebooting it (for testing
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100927 * mostly).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200929 * The sysfs file /sys/power/disk provides an interface for selecting the
930 * hibernation mode to use. Reading from this file causes the available modes
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100931 * to be printed. There are 3 modes that can be supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 * 'platform'
934 * 'shutdown'
935 * 'reboot'
936 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200937 * If a platform hibernation driver is in use, 'platform' will be supported
938 * and will be used by default. Otherwise, 'shutdown' will be used by default.
939 * The selected option (i.e. the one corresponding to the current value of
940 * hibernation_mode) is enclosed by a square bracket.
941 *
942 * To select a given hibernation mode it is necessary to write the mode's
943 * string representation (as returned by reading from /sys/power/disk) back
944 * into /sys/power/disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
946
Kay Sievers386f2752007-11-02 13:47:53 +0100947static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
948 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700950 int i;
951 char *start = buf;
952
Kees Cooka6e15a32014-06-13 13:30:35 -0700953 if (!hibernation_available())
954 return sprintf(buf, "[disabled]\n");
955
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700956 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
957 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700958 continue;
959 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700960 case HIBERNATION_SHUTDOWN:
961 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +0200962#ifdef CONFIG_SUSPEND
963 case HIBERNATION_SUSPEND:
964#endif
Chen Yufe12c002016-07-22 10:30:47 +0800965 case HIBERNATION_TEST_RESUME:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700966 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700967 case HIBERNATION_PLATFORM:
968 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700969 break;
970 /* not a valid mode, continue with loop */
971 continue;
972 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700973 if (i == hibernation_mode)
974 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700975 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700976 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700977 }
978 buf += sprintf(buf, "\n");
979 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
Kay Sievers386f2752007-11-02 13:47:53 +0100982static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
983 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 int error = 0;
986 int i;
987 int len;
988 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700989 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Kees Cooka6e15a32014-06-13 13:30:35 -0700991 if (!hibernation_available())
992 return -EPERM;
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 p = memchr(buf, '\n', n);
995 len = p ? p - buf : n;
996
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100997 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700998 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700999 if (len == strlen(hibernation_modes[i])
1000 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 mode = i;
1002 break;
1003 }
1004 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001005 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001006 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001007 case HIBERNATION_SHUTDOWN:
1008 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +02001009#ifdef CONFIG_SUSPEND
1010 case HIBERNATION_SUSPEND:
1011#endif
Chen Yufe12c002016-07-22 10:30:47 +08001012 case HIBERNATION_TEST_RESUME:
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001013 hibernation_mode = mode;
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001014 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001015 case HIBERNATION_PLATFORM:
1016 if (hibernation_ops)
1017 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 else
1019 error = -EINVAL;
1020 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001021 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 error = -EINVAL;
1023
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001024 if (!error)
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +02001025 pm_pr_dbg("Hibernation mode set to '%s'\n",
1026 hibernation_modes[mode]);
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001027 unlock_system_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return error ? error : n;
1029}
1030
1031power_attr(disk);
1032
Kay Sievers386f2752007-11-02 13:47:53 +01001033static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1034 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
1037 MINOR(swsusp_resume_device));
1038}
1039
Kay Sievers386f2752007-11-02 13:47:53 +01001040static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1041 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 dev_t res;
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001044 int len = n;
1045 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001047 if (len && buf[len-1] == '\n')
1048 len--;
1049 name = kstrndup(buf, len, GFP_KERNEL);
1050 if (!name)
1051 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001053 res = name_to_dev_t(name);
1054 kfree(name);
1055 if (!res)
1056 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001058 lock_system_sleep();
Andrew Mortona5762192006-01-06 00:09:50 -08001059 swsusp_resume_device = res;
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001060 unlock_system_sleep();
Mario Limonciello64846402018-03-28 12:01:10 -05001061 pm_pr_dbg("Configured resume from disk to %u\n", swsusp_resume_device);
Andrew Mortona5762192006-01-06 00:09:50 -08001062 noresume = 0;
1063 software_resume();
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001064 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
1067power_attr(resume);
1068
Mario Limonciello35506462018-03-28 12:01:09 -05001069static ssize_t resume_offset_show(struct kobject *kobj,
1070 struct kobj_attribute *attr, char *buf)
1071{
1072 return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
1073}
1074
1075static ssize_t resume_offset_store(struct kobject *kobj,
1076 struct kobj_attribute *attr, const char *buf,
1077 size_t n)
1078{
1079 unsigned long long offset;
1080 int rc;
1081
1082 rc = kstrtoull(buf, 0, &offset);
1083 if (rc)
1084 return rc;
1085 swsusp_resume_block = offset;
1086
1087 return n;
1088}
1089
1090power_attr(resume_offset);
1091
Kay Sievers386f2752007-11-02 13:47:53 +01001092static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1093 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001094{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001095 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001096}
1097
Kay Sievers386f2752007-11-02 13:47:53 +01001098static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1099 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001100{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001101 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001102
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001103 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001104 image_size = size;
1105 return n;
1106 }
1107
1108 return -EINVAL;
1109}
1110
1111power_attr(image_size);
1112
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001113static ssize_t reserved_size_show(struct kobject *kobj,
1114 struct kobj_attribute *attr, char *buf)
1115{
1116 return sprintf(buf, "%lu\n", reserved_size);
1117}
1118
1119static ssize_t reserved_size_store(struct kobject *kobj,
1120 struct kobj_attribute *attr,
1121 const char *buf, size_t n)
1122{
1123 unsigned long size;
1124
1125 if (sscanf(buf, "%lu", &size) == 1) {
1126 reserved_size = size;
1127 return n;
1128 }
1129
1130 return -EINVAL;
1131}
1132
1133power_attr(reserved_size);
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135static struct attribute * g[] = {
1136 &disk_attr.attr,
Mario Limonciello35506462018-03-28 12:01:09 -05001137 &resume_offset_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001139 &image_size_attr.attr,
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001140 &reserved_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 NULL,
1142};
1143
1144
Arvind Yadav59494fe2017-06-29 16:58:40 +05301145static const struct attribute_group attr_group = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 .attrs = g,
1147};
1148
1149
1150static int __init pm_disk_init(void)
1151{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -08001152 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
1155core_initcall(pm_disk_init);
1156
1157
1158static int __init resume_setup(char *str)
1159{
1160 if (noresume)
1161 return 1;
1162
1163 strncpy( resume_file, str, 255 );
1164 return 1;
1165}
1166
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001167static int __init resume_offset_setup(char *str)
1168{
1169 unsigned long long offset;
1170
1171 if (noresume)
1172 return 1;
1173
1174 if (sscanf(str, "%llu", &offset) == 1)
1175 swsusp_resume_block = offset;
1176
1177 return 1;
1178}
1179
Bojan Smojverf996fc92010-09-09 23:06:23 +02001180static int __init hibernate_setup(char *str)
1181{
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001182 if (!strncmp(str, "noresume", 8)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001183 noresume = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001184 } else if (!strncmp(str, "nocompress", 10)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001185 nocompress = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001186 } else if (!strncmp(str, "no", 2)) {
Kees Cooka6e15a32014-06-13 13:30:35 -07001187 noresume = 1;
1188 nohibernate = 1;
Laura Abbott0f5bf6d2017-02-06 16:31:58 -08001189 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
Rafael J. Wysocki4c0b6c12016-07-10 02:12:10 +02001190 && !strncmp(str, "protect_image", 13)) {
1191 enable_restore_image_protection();
Kees Cooka6e15a32014-06-13 13:30:35 -07001192 }
Bojan Smojverf996fc92010-09-09 23:06:23 +02001193 return 1;
1194}
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196static int __init noresume_setup(char *str)
1197{
1198 noresume = 1;
1199 return 1;
1200}
1201
Barry Song6f8d7022011-10-06 20:34:46 +02001202static int __init resumewait_setup(char *str)
1203{
1204 resume_wait = 1;
1205 return 1;
1206}
1207
Barry Songf126f732011-10-10 23:38:41 +02001208static int __init resumedelay_setup(char *str)
1209{
Dan Carpenterf6514be2014-05-14 19:08:46 +03001210 int rc = kstrtouint(str, 0, &resume_delay);
Fabian Frederick317cf7e2014-05-09 23:32:08 +02001211
1212 if (rc)
1213 return rc;
Barry Songf126f732011-10-10 23:38:41 +02001214 return 1;
1215}
1216
Kees Cooka6e15a32014-06-13 13:30:35 -07001217static int __init nohibernate_setup(char *str)
1218{
1219 noresume = 1;
1220 nohibernate = 1;
1221 return 1;
1222}
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001225__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226__setup("resume=", resume_setup);
Bojan Smojverf996fc92010-09-09 23:06:23 +02001227__setup("hibernate=", hibernate_setup);
Barry Song6f8d7022011-10-06 20:34:46 +02001228__setup("resumewait", resumewait_setup);
Barry Songf126f732011-10-10 23:38:41 +02001229__setup("resumedelay=", resumedelay_setup);
Kees Cooka6e15a32014-06-13 13:30:35 -07001230__setup("nohibernate", nohibernate_setup);