blob: cd7434e6000d90eb5be5a179ec6815e77b21201a [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
Jiri Kosinaec527c32019-05-30 00:09:39 +0200259__weak int arch_resume_nosmt(void)
260{
261 return 0;
262}
263
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100264/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200265 * create_image - Create a hibernation image.
266 * @platform_mode: Whether or not to use the platform driver.
267 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100268 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
269 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200270 *
271 * Control reappears in this routine after the subsequent restore.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700272 */
Adrian Bunk47a460d2008-02-04 22:30:06 -0800273static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700274{
275 int error;
276
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100277 error = dpm_suspend_end(PMSG_FREEZE);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700278 if (error) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100279 pr_err("Some devices failed to power down, aborting hibernation\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200280 return error;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700281 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100282
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100283 error = platform_pre_snapshot(platform_mode);
284 if (error || hibernation_test(TEST_PLATFORM))
285 goto Platform_finish;
286
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000287 error = suspend_disable_secondary_cpus();
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100288 if (error || hibernation_test(TEST_CPUS))
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100289 goto Enable_cpus;
290
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100291 local_irq_disable();
292
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200293 system_state = SYSTEM_SUSPEND;
294
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200295 error = syscore_suspend();
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100296 if (error) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100297 pr_err("Some system devices failed to power down, aborting hibernation\n");
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100298 goto Enable_irqs;
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100299 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700300
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100301 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100302 goto Power_up;
303
304 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700305 save_processor_state();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700306 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700307 error = swsusp_arch_suspend();
Thomas Garnier62822e22016-08-11 14:49:29 -0700308 /* Restore control flow magically appears here */
309 restore_processor_state();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700310 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700311 if (error)
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100312 pr_err("Error %d creating hibernation image\n", error);
313
Anisse Astier1ad14102016-09-09 10:43:32 +0200314 if (!in_suspend) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200315 events_check_enabled = false;
Anisse Astier1ad14102016-09-09 10:43:32 +0200316 clear_free_pages();
317 }
Bjørn Mork362e77d2013-12-04 16:06:58 +0100318
319 platform_leave(platform_mode);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100320
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100321 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100322 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100323
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100324 Enable_irqs:
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200325 system_state = SYSTEM_RUNNING;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100326 local_irq_enable();
327
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100328 Enable_cpus:
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000329 suspend_enable_secondary_cpus();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100330
Jiri Kosinaec527c32019-05-30 00:09:39 +0200331 /* Allow architectures to do nosmt-specific post-resume dances */
332 if (!in_suspend)
333 error = arch_resume_nosmt();
334
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100335 Platform_finish:
336 platform_finish(platform_mode);
337
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100338 dpm_resume_start(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200339 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100340
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700341 return error;
342}
343
344/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200345 * hibernation_snapshot - Quiesce devices and create a hibernation image.
346 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700347 *
Pingfan Liu55f25032018-07-31 16:51:32 +0800348 * This routine must be called with system_transition_mutex held.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700349 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700350int hibernation_snapshot(int platform_mode)
351{
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100352 pm_message_t msg;
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100353 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700354
Lukas Wunner27614272016-03-23 00:11:20 +0100355 pm_suspend_clear_flags();
Zhang Rui3fe03132008-10-26 20:50:26 +0100356 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700357 if (error)
Rafael J. Wysockid074ee02010-07-07 23:43:35 +0200358 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700359
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200360 /* Preallocate image memory before shutting down devices. */
361 error = hibernate_preallocate_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700362 if (error)
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200363 goto Close;
364
365 error = freeze_kernel_threads();
366 if (error)
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100367 goto Cleanup;
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200368
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100369 if (hibernation_test(TEST_FREEZER)) {
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100370
371 /*
372 * Indicate to the caller that we are returning due to a
373 * successful freezer test.
374 */
375 freezer_test_done = true;
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100376 goto Thaw;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100377 }
378
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200379 error = dpm_prepare(PMSG_FREEZE);
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100380 if (error) {
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100381 dpm_complete(PMSG_RECOVER);
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100382 goto Thaw;
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100383 }
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700384
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700385 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100386 pm_restrict_gfp_mask();
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100387
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200388 error = dpm_suspend(PMSG_FREEZE);
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700389
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100390 if (error || hibernation_test(TEST_DEVICES))
391 platform_recover(platform_mode);
392 else
393 error = create_image(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700394
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100395 /*
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100396 * In the case that we call create_image() above, the control
397 * returns here (1) after the image has been created or the
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100398 * image creation has failed and (2) after a successful restore.
399 */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100400
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200401 /* We may need to release the preallocated image pages here. */
402 if (error || !in_suspend)
403 swsusp_free();
404
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200405 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
406 dpm_resume(msg);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100407
408 if (error || !in_suspend)
409 pm_restore_gfp_mask();
410
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700411 resume_console();
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200412 dpm_complete(msg);
413
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100414 Close:
415 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700416 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200417
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100418 Thaw:
419 thaw_kernel_threads();
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100420 Cleanup:
421 swsusp_free();
422 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700423}
424
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200425int __weak hibernate_resume_nonboot_cpu_disable(void)
426{
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000427 return suspend_disable_secondary_cpus();
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200428}
429
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700430/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200431 * resume_target_kernel - Restore system state from a hibernation image.
432 * @platform_mode: Whether or not to use the platform driver.
433 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100434 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
435 * contents of highmem that have not been restored yet from the image and run
436 * the low-level code that will restore the remaining contents of memory and
437 * switch to the just restored target kernel.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100438 */
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100439static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100440{
441 int error;
442
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100443 error = dpm_suspend_end(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100444 if (error) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100445 pr_err("Some devices failed to power down, aborting resume\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200446 return error;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100447 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100448
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100449 error = platform_pre_restore(platform_mode);
450 if (error)
451 goto Cleanup;
452
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200453 error = hibernate_resume_nonboot_cpu_disable();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100454 if (error)
455 goto Enable_cpus;
456
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100457 local_irq_disable();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200458 system_state = SYSTEM_SUSPEND;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100459
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200460 error = syscore_suspend();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100461 if (error)
462 goto Enable_irqs;
463
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100464 save_processor_state();
465 error = restore_highmem();
466 if (!error) {
467 error = swsusp_arch_resume();
468 /*
469 * The code below is only ever reached in case of a failure.
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200470 * Otherwise, execution continues at the place where
471 * swsusp_arch_suspend() was called.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100472 */
473 BUG_ON(!error);
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200474 /*
475 * This call to restore_highmem() reverts the changes made by
476 * the previous one.
477 */
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100478 restore_highmem();
479 }
480 /*
481 * The only reason why swsusp_arch_resume() can fail is memory being
482 * very tight, so we have to free it as soon as we can to avoid
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200483 * subsequent failures.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100484 */
485 swsusp_free();
486 restore_processor_state();
487 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100488
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100489 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100490
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100491 Enable_irqs:
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200492 system_state = SYSTEM_RUNNING;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100493 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100494
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100495 Enable_cpus:
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000496 suspend_enable_secondary_cpus();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100497
498 Cleanup:
499 platform_restore_cleanup(platform_mode);
500
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100501 dpm_resume_start(PMSG_RECOVER);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100502
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100503 return error;
504}
505
506/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200507 * hibernation_restore - Quiesce devices and restore from a hibernation image.
508 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700509 *
Pingfan Liu55f25032018-07-31 16:51:32 +0800510 * This routine must be called with system_transition_mutex held. If it is
511 * successful, control reappears in the restored target kernel in
512 * hibernation_snapshot().
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700513 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700514int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700515{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100516 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700517
518 pm_prepare_console();
519 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100520 pm_restrict_gfp_mask();
Alan Sternd1616302009-05-24 22:05:42 +0200521 error = dpm_suspend_start(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700522 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100523 error = resume_target_kernel(platform_mode);
Imre Deak94fb8232014-10-24 20:29:10 +0300524 /*
525 * The above should either succeed and jump to the new kernel,
526 * or return with an error. Otherwise things are just
527 * undefined, so let's be paranoid.
528 */
529 BUG_ON(!error);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700530 }
Imre Deak94fb8232014-10-24 20:29:10 +0300531 dpm_resume_end(PMSG_RECOVER);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100532 pm_restore_gfp_mask();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700533 resume_console();
534 pm_restore_console();
535 return error;
536}
537
538/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200539 * hibernation_platform_enter - Power off the system using the platform driver.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700540 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700541int hibernation_platform_enter(void)
542{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100543 int error;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700544
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700545 if (!hibernation_ops)
546 return -ENOSYS;
547
548 /*
549 * We have cancelled the power transition by running
550 * hibernation_ops->finish() before saving the image, so we should let
551 * the firmware know that we're going to enter the sleep state after all
552 */
Rafael J. Wysockibb186902019-05-16 12:43:19 +0200553 error = hibernation_ops->begin(PMSG_HIBERNATE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700554 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100555 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700556
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100557 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700558 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +0200559 error = dpm_suspend_start(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200560 if (error) {
561 if (hibernation_ops->recover)
562 hibernation_ops->recover();
563 goto Resume_devices;
564 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700565
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100566 error = dpm_suspend_end(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100567 if (error)
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200568 goto Resume_devices;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100569
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100570 error = hibernation_ops->prepare();
571 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200572 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100573
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000574 error = suspend_disable_secondary_cpus();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100575 if (error)
Vitaly Kuznetsov8c506602015-06-24 16:02:06 +0200576 goto Enable_cpus;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100577
578 local_irq_disable();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200579 system_state = SYSTEM_SUSPEND;
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100580 syscore_suspend();
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100581 if (pm_wakeup_pending()) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200582 error = -EAGAIN;
583 goto Power_up;
584 }
585
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100586 hibernation_ops->enter();
587 /* We should never get here */
588 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700589
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200590 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100591 syscore_resume();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200592 system_state = SYSTEM_RUNNING;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200593 local_irq_enable();
Vitaly Kuznetsov8c506602015-06-24 16:02:06 +0200594
595 Enable_cpus:
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000596 suspend_enable_secondary_cpus();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200597
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200598 Platform_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700599 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100600
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100601 dpm_resume_start(PMSG_RESTORE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100602
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700603 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100604 entering_platform_hibernation = false;
Alan Sternd1616302009-05-24 22:05:42 +0200605 dpm_resume_end(PMSG_RESTORE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700606 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100607
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100608 Close:
609 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100610
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700611 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700612}
613
614/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200615 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200617 * Use the platform driver, if configured, to put the system into the sleep
618 * state corresponding to hibernation, or try to power it off or reboot,
619 * depending on the value of hibernation_mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 */
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700621static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Bojan Smojver62c552c2012-06-16 00:09:58 +0200623#ifdef CONFIG_SUSPEND
624 int error;
Rafael J. Wysocki81d45bd2017-02-24 00:25:28 +0100625
626 if (hibernation_mode == HIBERNATION_SUSPEND) {
627 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
628 if (error) {
629 hibernation_mode = hibernation_ops ?
630 HIBERNATION_PLATFORM :
631 HIBERNATION_SHUTDOWN;
632 } else {
633 /* Restore swap signature. */
634 error = swsusp_unmark();
635 if (error)
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100636 pr_err("Swap will be unusable! Try swapon -a.\n");
Rafael J. Wysocki81d45bd2017-02-24 00:25:28 +0100637
638 return;
639 }
640 }
Bojan Smojver62c552c2012-06-16 00:09:58 +0200641#endif
642
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700643 switch (hibernation_mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700644 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600645 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700647 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700648 hibernation_platform_enter();
Gustavo A. R. Silva82837ad2018-08-04 21:12:00 -0500649 /* Fall through */
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700650 case HIBERNATION_SHUTDOWN:
Sebastian Capella2c730782014-04-21 17:30:46 -0700651 if (pm_power_off)
652 kernel_power_off();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700653 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600655 kernel_halt();
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700656 /*
657 * Valid image is on the disk, if we continue we risk serious data
658 * corruption after resume.
659 */
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100660 pr_crit("Power down manually\n");
Sebastian Capella2c730782014-04-21 17:30:46 -0700661 while (1)
662 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Chen Yufe12c002016-07-22 10:30:47 +0800665static int load_image_and_restore(void)
666{
667 int error;
668 unsigned int flags;
669
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200670 pm_pr_dbg("Loading hibernation image.\n");
Chen Yufe12c002016-07-22 10:30:47 +0800671
672 lock_device_hotplug();
673 error = create_basic_memory_bitmaps();
674 if (error)
675 goto Unlock;
676
677 error = swsusp_read(&flags);
678 swsusp_close(FMODE_READ);
679 if (!error)
680 hibernation_restore(flags & SF_PLATFORM_MODE);
681
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100682 pr_err("Failed to load hibernation image, recovering.\n");
Chen Yufe12c002016-07-22 10:30:47 +0800683 swsusp_free();
684 free_basic_memory_bitmaps();
685 Unlock:
686 unlock_device_hotplug();
687
688 return error;
689}
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200692 * hibernate - Carry out system hibernation, including saving the image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700694int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700696 int error, nr_calls = 0;
Chen Yufe12c002016-07-22 10:30:47 +0800697 bool snapshot_test = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Kees Cooka6e15a32014-06-13 13:30:35 -0700699 if (!hibernation_available()) {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200700 pm_pr_dbg("Hibernation not available.\n");
Kees Cooka6e15a32014-06-13 13:30:35 -0700701 return -EPERM;
702 }
703
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100704 lock_system_sleep();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700705 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700706 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
707 error = -EBUSY;
708 goto Unlock;
709 }
710
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200711 pr_info("hibernation entry\n");
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100712 pm_prepare_console();
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700713 error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
714 if (error) {
715 nr_calls--;
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700716 goto Exit;
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700717 }
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700718
Harry Panb5dee312019-02-25 20:36:41 +0800719 ksys_sync_helper();
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700720
Tejun Heo03afed82011-11-21 12:32:24 -0800721 error = freeze_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700722 if (error)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200723 goto Exit;
724
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200725 lock_device_hotplug();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200726 /* Allocate memory management structures */
727 error = create_basic_memory_bitmaps();
728 if (error)
729 goto Thaw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700731 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100732 if (error || freezer_test_done)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200733 goto Free_bitmaps;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200734
735 if (in_suspend) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700736 unsigned int flags = 0;
737
738 if (hibernation_mode == HIBERNATION_PLATFORM)
739 flags |= SF_PLATFORM_MODE;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200740 if (nocompress)
741 flags |= SF_NOCOMPRESS_MODE;
Bojan Smojver081a9d02011-10-13 23:58:07 +0200742 else
743 flags |= SF_CRC32_MODE;
744
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200745 pm_pr_dbg("Writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700746 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700747 swsusp_free();
Chen Yufe12c002016-07-22 10:30:47 +0800748 if (!error) {
749 if (hibernation_mode == HIBERNATION_TEST_RESUME)
750 snapshot_test = true;
751 else
752 power_down();
753 }
MyungJoo Ham5262a472010-11-26 23:07:56 +0100754 in_suspend = 0;
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100755 pm_restore_gfp_mask();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800756 } else {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200757 pm_pr_dbg("Image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800758 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200759
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200760 Free_bitmaps:
761 free_basic_memory_bitmaps();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800762 Thaw:
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200763 unlock_device_hotplug();
Chen Yufe12c002016-07-22 10:30:47 +0800764 if (snapshot_test) {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200765 pm_pr_dbg("Checking hibernation image\n");
Chen Yufe12c002016-07-22 10:30:47 +0800766 error = swsusp_check();
767 if (!error)
768 error = load_image_and_restore();
769 }
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100770 thaw_processes();
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100771
772 /* Don't bother checking whether freezer_test_done is true */
773 freezer_test_done = false;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700774 Exit:
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700775 __pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100776 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700777 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700778 Unlock:
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100779 unlock_system_sleep();
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200780 pr_info("hibernation exit\n");
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return error;
783}
784
785
786/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200787 * software_resume - Resume from a saved hibernation image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200789 * This routine is called as a late initcall, when all devices have been
790 * discovered and initialized already.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200792 * The image reading code is called to see if there is a hibernation image
793 * available for reading. If that is the case, devices are quiesced and the
794 * contents of memory is restored from the saved image.
795 *
796 * If this is successful, control reappears in the restored target kernel in
Geliang Tangd439e642015-09-29 20:36:58 -0700797 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200798 * attempts to recover gracefully and make the kernel return to the normal mode
799 * of operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801static int software_resume(void)
802{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700803 int error, nr_calls = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Johannes Berg60a0d232007-11-14 17:00:16 -0800805 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100806 * If the user said "noresume".. bail out early.
807 */
Kees Cooka6e15a32014-06-13 13:30:35 -0700808 if (noresume || !hibernation_available())
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100809 return 0;
810
811 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800812 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
813 * is configured into the kernel. Since the regular hibernate
814 * trigger path is via sysfs which takes a buffer mutex before
Pingfan Liu55f25032018-07-31 16:51:32 +0800815 * calling hibernate functions (which take system_transition_mutex)
816 * this can cause lockdep to complain about a possible ABBA deadlock
Johannes Berg60a0d232007-11-14 17:00:16 -0800817 * which cannot happen since we're in the boot code here and
818 * sysfs can't be invoked yet. Therefore, we use a subclass
819 * here to avoid lockdep complaining.
820 */
Pingfan Liu55f25032018-07-31 16:51:32 +0800821 mutex_lock_nested(&system_transition_mutex, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200822
823 if (swsusp_resume_device)
824 goto Check_image;
825
826 if (!strlen(resume_file)) {
827 error = -ENOENT;
828 goto Unlock;
829 }
830
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200831 pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200832
Barry Songf126f732011-10-10 23:38:41 +0200833 if (resume_delay) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100834 pr_info("Waiting %dsec before reading resume device ...\n",
Barry Songf126f732011-10-10 23:38:41 +0200835 resume_delay);
836 ssleep(resume_delay);
837 }
838
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200839 /* Check if the device is there */
840 swsusp_resume_device = name_to_dev_t(resume_file);
Minho Ban2df83fa2012-05-14 21:45:31 +0200841
842 /*
843 * name_to_dev_t is ineffective to verify parition if resume_file is in
844 * integer format. (e.g. major:minor)
845 */
846 if (isdigit(resume_file[0]) && resume_wait) {
847 int partno;
848 while (!get_gendisk(swsusp_resume_device, &partno))
849 msleep(10);
850 }
851
Pavel Machek3efa1472005-07-07 17:56:43 -0700852 if (!swsusp_resume_device) {
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100853 /*
854 * Some device discovery might still be in progress; we need
855 * to wait for this to finish.
856 */
857 wait_for_device_probe();
Barry Song6f8d7022011-10-06 20:34:46 +0200858
859 if (resume_wait) {
860 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
861 msleep(10);
862 async_synchronize_full();
863 }
864
Pavel Machek3efa1472005-07-07 17:56:43 -0700865 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200866 if (!swsusp_resume_device) {
867 error = -ENODEV;
868 goto Unlock;
869 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700870 }
871
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200872 Check_image:
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200873 pm_pr_dbg("Hibernation image partition %d:%d present\n",
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200874 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200876 pm_pr_dbg("Looking for hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800877 error = swsusp_check();
878 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700879 goto Unlock;
880
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700881 /* The snapshot device should not be opened while we're running */
882 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
883 error = -EBUSY;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200884 swsusp_close(FMODE_READ);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700885 goto Unlock;
886 }
887
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200888 pr_info("resume from hibernation\n");
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100889 pm_prepare_console();
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700890 error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
891 if (error) {
892 nr_calls--;
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200893 goto Close_Finish;
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700894 }
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700895
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200896 pm_pr_dbg("Preparing processes for restore.\n");
Tejun Heo03afed82011-11-21 12:32:24 -0800897 error = freeze_processes();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200898 if (error)
899 goto Close_Finish;
Chen Yufe12c002016-07-22 10:30:47 +0800900 error = load_image_and_restore();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200901 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700902 Finish:
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700903 __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100904 pm_restore_console();
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200905 pr_info("resume from hibernation failed (%d)\n", error);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700906 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700907 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700908 Unlock:
Pingfan Liu55f25032018-07-31 16:51:32 +0800909 mutex_unlock(&system_transition_mutex);
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200910 pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700911 return error;
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200912 Close_Finish:
Jiri Slaby76b57e62009-10-07 22:37:35 +0200913 swsusp_close(FMODE_READ);
914 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Russ Dilld3c345d2013-10-24 14:25:26 +0100917late_initcall_sync(software_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700920static const char * const hibernation_modes[] = {
921 [HIBERNATION_PLATFORM] = "platform",
922 [HIBERNATION_SHUTDOWN] = "shutdown",
923 [HIBERNATION_REBOOT] = "reboot",
Bojan Smojver62c552c2012-06-16 00:09:58 +0200924#ifdef CONFIG_SUSPEND
925 [HIBERNATION_SUSPEND] = "suspend",
926#endif
Chen Yufe12c002016-07-22 10:30:47 +0800927 [HIBERNATION_TEST_RESUME] = "test_resume",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928};
929
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200930/*
931 * /sys/power/disk - Control hibernation mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200933 * Hibernation can be handled in several ways. There are a few different ways
934 * to put the system into the sleep state: using the platform driver (e.g. ACPI
935 * or other hibernation_ops), powering it off or rebooting it (for testing
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100936 * mostly).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200938 * The sysfs file /sys/power/disk provides an interface for selecting the
939 * hibernation mode to use. Reading from this file causes the available modes
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100940 * to be printed. There are 3 modes that can be supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 * 'platform'
943 * 'shutdown'
944 * 'reboot'
945 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200946 * If a platform hibernation driver is in use, 'platform' will be supported
947 * and will be used by default. Otherwise, 'shutdown' will be used by default.
948 * The selected option (i.e. the one corresponding to the current value of
949 * hibernation_mode) is enclosed by a square bracket.
950 *
951 * To select a given hibernation mode it is necessary to write the mode's
952 * string representation (as returned by reading from /sys/power/disk) back
953 * into /sys/power/disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 */
955
Kay Sievers386f2752007-11-02 13:47:53 +0100956static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
957 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700959 int i;
960 char *start = buf;
961
Kees Cooka6e15a32014-06-13 13:30:35 -0700962 if (!hibernation_available())
963 return sprintf(buf, "[disabled]\n");
964
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700965 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
966 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700967 continue;
968 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700969 case HIBERNATION_SHUTDOWN:
970 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +0200971#ifdef CONFIG_SUSPEND
972 case HIBERNATION_SUSPEND:
973#endif
Chen Yufe12c002016-07-22 10:30:47 +0800974 case HIBERNATION_TEST_RESUME:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700975 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700976 case HIBERNATION_PLATFORM:
977 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700978 break;
979 /* not a valid mode, continue with loop */
980 continue;
981 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700982 if (i == hibernation_mode)
983 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700984 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700985 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700986 }
987 buf += sprintf(buf, "\n");
988 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
Kay Sievers386f2752007-11-02 13:47:53 +0100991static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
992 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
994 int error = 0;
995 int i;
996 int len;
997 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700998 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Kees Cooka6e15a32014-06-13 13:30:35 -07001000 if (!hibernation_available())
1001 return -EPERM;
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 p = memchr(buf, '\n', n);
1004 len = p ? p - buf : n;
1005
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001006 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001007 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -07001008 if (len == strlen(hibernation_modes[i])
1009 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 mode = i;
1011 break;
1012 }
1013 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001014 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001015 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001016 case HIBERNATION_SHUTDOWN:
1017 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +02001018#ifdef CONFIG_SUSPEND
1019 case HIBERNATION_SUSPEND:
1020#endif
Chen Yufe12c002016-07-22 10:30:47 +08001021 case HIBERNATION_TEST_RESUME:
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001022 hibernation_mode = mode;
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001023 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001024 case HIBERNATION_PLATFORM:
1025 if (hibernation_ops)
1026 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 else
1028 error = -EINVAL;
1029 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001030 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 error = -EINVAL;
1032
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001033 if (!error)
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +02001034 pm_pr_dbg("Hibernation mode set to '%s'\n",
1035 hibernation_modes[mode]);
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001036 unlock_system_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return error ? error : n;
1038}
1039
1040power_attr(disk);
1041
Kay Sievers386f2752007-11-02 13:47:53 +01001042static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1043 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
1046 MINOR(swsusp_resume_device));
1047}
1048
Kay Sievers386f2752007-11-02 13:47:53 +01001049static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1050 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 dev_t res;
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001053 int len = n;
1054 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001056 if (len && buf[len-1] == '\n')
1057 len--;
1058 name = kstrndup(buf, len, GFP_KERNEL);
1059 if (!name)
1060 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001062 res = name_to_dev_t(name);
1063 kfree(name);
1064 if (!res)
1065 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001067 lock_system_sleep();
Andrew Mortona5762192006-01-06 00:09:50 -08001068 swsusp_resume_device = res;
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001069 unlock_system_sleep();
Mario Limonciello64846402018-03-28 12:01:10 -05001070 pm_pr_dbg("Configured resume from disk to %u\n", swsusp_resume_device);
Andrew Mortona5762192006-01-06 00:09:50 -08001071 noresume = 0;
1072 software_resume();
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001073 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
1076power_attr(resume);
1077
Mario Limonciello35506462018-03-28 12:01:09 -05001078static ssize_t resume_offset_show(struct kobject *kobj,
1079 struct kobj_attribute *attr, char *buf)
1080{
1081 return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
1082}
1083
1084static ssize_t resume_offset_store(struct kobject *kobj,
1085 struct kobj_attribute *attr, const char *buf,
1086 size_t n)
1087{
1088 unsigned long long offset;
1089 int rc;
1090
1091 rc = kstrtoull(buf, 0, &offset);
1092 if (rc)
1093 return rc;
1094 swsusp_resume_block = offset;
1095
1096 return n;
1097}
1098
1099power_attr(resume_offset);
1100
Kay Sievers386f2752007-11-02 13:47:53 +01001101static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1102 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001103{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001104 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001105}
1106
Kay Sievers386f2752007-11-02 13:47:53 +01001107static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1108 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001109{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001110 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001111
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001112 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001113 image_size = size;
1114 return n;
1115 }
1116
1117 return -EINVAL;
1118}
1119
1120power_attr(image_size);
1121
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001122static ssize_t reserved_size_show(struct kobject *kobj,
1123 struct kobj_attribute *attr, char *buf)
1124{
1125 return sprintf(buf, "%lu\n", reserved_size);
1126}
1127
1128static ssize_t reserved_size_store(struct kobject *kobj,
1129 struct kobj_attribute *attr,
1130 const char *buf, size_t n)
1131{
1132 unsigned long size;
1133
1134 if (sscanf(buf, "%lu", &size) == 1) {
1135 reserved_size = size;
1136 return n;
1137 }
1138
1139 return -EINVAL;
1140}
1141
1142power_attr(reserved_size);
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144static struct attribute * g[] = {
1145 &disk_attr.attr,
Mario Limonciello35506462018-03-28 12:01:09 -05001146 &resume_offset_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001148 &image_size_attr.attr,
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001149 &reserved_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 NULL,
1151};
1152
1153
Arvind Yadav59494fe2017-06-29 16:58:40 +05301154static const struct attribute_group attr_group = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 .attrs = g,
1156};
1157
1158
1159static int __init pm_disk_init(void)
1160{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -08001161 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
1164core_initcall(pm_disk_init);
1165
1166
1167static int __init resume_setup(char *str)
1168{
1169 if (noresume)
1170 return 1;
1171
1172 strncpy( resume_file, str, 255 );
1173 return 1;
1174}
1175
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001176static int __init resume_offset_setup(char *str)
1177{
1178 unsigned long long offset;
1179
1180 if (noresume)
1181 return 1;
1182
1183 if (sscanf(str, "%llu", &offset) == 1)
1184 swsusp_resume_block = offset;
1185
1186 return 1;
1187}
1188
Bojan Smojverf996fc92010-09-09 23:06:23 +02001189static int __init hibernate_setup(char *str)
1190{
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001191 if (!strncmp(str, "noresume", 8)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001192 noresume = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001193 } else if (!strncmp(str, "nocompress", 10)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001194 nocompress = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001195 } else if (!strncmp(str, "no", 2)) {
Kees Cooka6e15a32014-06-13 13:30:35 -07001196 noresume = 1;
1197 nohibernate = 1;
Laura Abbott0f5bf6d2017-02-06 16:31:58 -08001198 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
Rafael J. Wysocki4c0b6c12016-07-10 02:12:10 +02001199 && !strncmp(str, "protect_image", 13)) {
1200 enable_restore_image_protection();
Kees Cooka6e15a32014-06-13 13:30:35 -07001201 }
Bojan Smojverf996fc92010-09-09 23:06:23 +02001202 return 1;
1203}
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205static int __init noresume_setup(char *str)
1206{
1207 noresume = 1;
1208 return 1;
1209}
1210
Barry Song6f8d7022011-10-06 20:34:46 +02001211static int __init resumewait_setup(char *str)
1212{
1213 resume_wait = 1;
1214 return 1;
1215}
1216
Barry Songf126f732011-10-10 23:38:41 +02001217static int __init resumedelay_setup(char *str)
1218{
Dan Carpenterf6514be2014-05-14 19:08:46 +03001219 int rc = kstrtouint(str, 0, &resume_delay);
Fabian Frederick317cf7e2014-05-09 23:32:08 +02001220
1221 if (rc)
1222 return rc;
Barry Songf126f732011-10-10 23:38:41 +02001223 return 1;
1224}
1225
Kees Cooka6e15a32014-06-13 13:30:35 -07001226static int __init nohibernate_setup(char *str)
1227{
1228 noresume = 1;
1229 nohibernate = 1;
1230 return 1;
1231}
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001234__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235__setup("resume=", resume_setup);
Bojan Smojverf996fc92010-09-09 23:06:23 +02001236__setup("hibernate=", hibernate_setup);
Barry Song6f8d7022011-10-06 20:34:46 +02001237__setup("resumewait", resumewait_setup);
Barry Songf126f732011-10-10 23:38:41 +02001238__setup("resumedelay=", resumedelay_setup);
Kees Cooka6e15a32014-06-13 13:30:35 -07001239__setup("nohibernate", nohibernate_setup);