blob: 86aba8706b1654b83162fb2888916f2de013f6b0 [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
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -080012#define pr_fmt(fmt) "PM: hibernation: " fmt
Rafael J. Wysocki2872de12017-02-24 00:26:15 +010013
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>
Josh Boyer38bd94b2019-08-19 17:17:46 -070033#include <linux/security.h>
Todd E Brandtbb3632c2014-06-06 05:40:17 -070034#include <trace/events/power.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "power.h"
37
38
Barry Songd231ff12011-10-11 23:29:18 -070039static int nocompress;
40static int noresume;
Kees Cooka6e15a32014-06-13 13:30:35 -070041static int nohibernate;
Barry Songd231ff12011-10-11 23:29:18 -070042static int resume_wait;
Dan Carpenterf6514be2014-05-14 19:08:46 +030043static unsigned int resume_delay;
Adrian Bunk47a460d2008-02-04 22:30:06 -080044static char resume_file[256] = CONFIG_PM_STD_PARTITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080046sector_t swsusp_resume_block;
Andi Kleend6efc2f2013-08-05 15:02:49 -070047__visible int in_suspend __nosavedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070049enum {
50 HIBERNATION_INVALID,
51 HIBERNATION_PLATFORM,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070052 HIBERNATION_SHUTDOWN,
53 HIBERNATION_REBOOT,
Bojan Smojver62c552c2012-06-16 00:09:58 +020054#ifdef CONFIG_SUSPEND
55 HIBERNATION_SUSPEND,
56#endif
Chen Yufe12c002016-07-22 10:30:47 +080057 HIBERNATION_TEST_RESUME,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070058 /* keep last */
59 __HIBERNATION_AFTER_LAST
60};
61#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
62#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
63
64static int hibernation_mode = HIBERNATION_SHUTDOWN;
65
Srivatsa S. Bhat97819a22011-12-01 22:33:10 +010066bool freezer_test_done;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +010067
Lionel Debroux073ef1f2010-11-09 21:48:49 +010068static const struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070069
Kees Cooka6e15a32014-06-13 13:30:35 -070070bool hibernation_available(void)
71{
Josh Boyer38bd94b2019-08-19 17:17:46 -070072 return nohibernate == 0 && !security_locked_down(LOCKDOWN_HIBERNATION);
Kees Cooka6e15a32014-06-13 13:30:35 -070073}
74
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070075/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +020076 * hibernation_set_ops - Set the global hibernate operations.
77 * @ops: Hibernation operations to use in subsequent hibernation transitions.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070078 */
Lionel Debroux073ef1f2010-11-09 21:48:49 +010079void hibernation_set_ops(const struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070080{
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +010081 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
82 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
MyungJoo Ham5729c632010-11-26 23:07:48 +010083 && ops->restore_cleanup && ops->leave)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070084 WARN_ON(1);
85 return;
86 }
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010087 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070088 hibernation_ops = ops;
89 if (ops)
90 hibernation_mode = HIBERNATION_PLATFORM;
91 else if (hibernation_mode == HIBERNATION_PLATFORM)
92 hibernation_mode = HIBERNATION_SHUTDOWN;
93
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010094 unlock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070095}
Leonardo Potenzae0c78552013-11-19 12:27:41 +000096EXPORT_SYMBOL_GPL(hibernation_set_ops);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070097
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +010098static bool entering_platform_hibernation;
99
100bool system_entering_hibernation(void)
101{
102 return entering_platform_hibernation;
103}
104EXPORT_SYMBOL(system_entering_hibernation);
105
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100106#ifdef CONFIG_PM_DEBUG
107static void hibernation_debug_sleep(void)
108{
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800109 pr_info("debug: Waiting for 5 seconds.\n");
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100110 mdelay(5000);
111}
112
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100113static int hibernation_test(int level)
114{
115 if (pm_test_level == level) {
116 hibernation_debug_sleep();
117 return 1;
118 }
119 return 0;
120}
121#else /* !CONFIG_PM_DEBUG */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100122static int hibernation_test(int level) { return 0; }
123#endif /* !CONFIG_PM_DEBUG */
124
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700125/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200126 * platform_begin - Call platform to start hibernation.
127 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700128 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100129static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700130{
131 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockibb186902019-05-16 12:43:19 +0200132 hibernation_ops->begin(PMSG_FREEZE) : 0;
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100133}
134
135/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200136 * platform_end - Call platform to finish transition to the working state.
137 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100138 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100139static void platform_end(int platform_mode)
140{
141 if (platform_mode && hibernation_ops)
142 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700143}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200146 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
147 * @platform_mode: Whether or not to use the platform driver.
148 *
149 * Use the platform driver to prepare the system for creating a hibernate image,
150 * if so configured, and return an error code if that fails.
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800151 */
152
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700153static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800154{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700155 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700156 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800157}
158
159/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200160 * platform_leave - Call platform to prepare a transition to the working state.
161 * @platform_mode: Whether or not to use the platform driver.
162 *
163 * Use the platform driver prepare to prepare the machine for switching to the
164 * normal mode of operation.
165 *
166 * This routine is called on one CPU with interrupts disabled.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700167 */
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700168static void platform_leave(int platform_mode)
169{
170 if (platform_mode && hibernation_ops)
171 hibernation_ops->leave();
172}
173
174/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200175 * platform_finish - Call platform to switch the system to the working state.
176 * @platform_mode: Whether or not to use the platform driver.
177 *
178 * Use the platform driver to switch the machine to the normal mode of
179 * operation.
180 *
181 * This routine must be called after platform_prepare().
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700182 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700183static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700184{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700185 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700186 hibernation_ops->finish();
187}
188
189/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200190 * platform_pre_restore - Prepare for hibernate image restoration.
191 * @platform_mode: Whether or not to use the platform driver.
192 *
193 * Use the platform driver to prepare the system for resume from a hibernation
194 * image.
195 *
196 * If the restore fails after this function has been called,
197 * platform_restore_cleanup() must be called.
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700198 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700199static int platform_pre_restore(int platform_mode)
200{
201 return (platform_mode && hibernation_ops) ?
202 hibernation_ops->pre_restore() : 0;
203}
204
205/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200206 * platform_restore_cleanup - Switch to the working state after failing restore.
207 * @platform_mode: Whether or not to use the platform driver.
208 *
209 * Use the platform driver to switch the system to the normal mode of operation
210 * after a failing restore.
211 *
212 * If platform_pre_restore() has been called before the failing restore, this
213 * function must be called too, regardless of the result of
214 * platform_pre_restore().
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700215 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700216static void platform_restore_cleanup(int platform_mode)
217{
218 if (platform_mode && hibernation_ops)
219 hibernation_ops->restore_cleanup();
220}
221
222/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200223 * platform_recover - Recover from a failure to suspend devices.
224 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200225 */
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200226static void platform_recover(int platform_mode)
227{
228 if (platform_mode && hibernation_ops && hibernation_ops->recover)
229 hibernation_ops->recover();
230}
231
232/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200233 * swsusp_show_speed - Print time elapsed between two events during hibernation.
234 * @start: Starting event.
235 * @stop: Final event.
236 * @nr_pages: Number of memory pages processed between @start and @stop.
237 * @msg: Additional diagnostic message to print.
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100238 */
Tina Ruchandanidb597602014-10-30 11:04:53 -0700239void swsusp_show_speed(ktime_t start, ktime_t stop,
240 unsigned nr_pages, char *msg)
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100241{
Tina Ruchandanidb597602014-10-30 11:04:53 -0700242 ktime_t diff;
Chen Gang4881f602014-04-25 08:44:59 +0800243 u64 elapsed_centisecs64;
244 unsigned int centisecs;
245 unsigned int k;
246 unsigned int kps;
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100247
Tina Ruchandanidb597602014-10-30 11:04:53 -0700248 diff = ktime_sub(stop, start);
249 elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC);
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100250 centisecs = elapsed_centisecs64;
251 if (centisecs == 0)
252 centisecs = 1; /* avoid div-by-zero */
253 k = nr_pages * (PAGE_SIZE / 1024);
254 kps = (k * 100) / centisecs;
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100255 pr_info("%s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
256 msg, k, centisecs / 100, centisecs % 100, kps / 1000,
257 (kps % 1000) / 10);
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100258}
259
Jiri Kosinaec527c32019-05-30 00:09:39 +0200260__weak int arch_resume_nosmt(void)
261{
262 return 0;
263}
264
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100265/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200266 * create_image - Create a hibernation image.
267 * @platform_mode: Whether or not to use the platform driver.
268 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100269 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
270 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200271 *
272 * Control reappears in this routine after the subsequent restore.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700273 */
Adrian Bunk47a460d2008-02-04 22:30:06 -0800274static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700275{
276 int error;
277
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100278 error = dpm_suspend_end(PMSG_FREEZE);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700279 if (error) {
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800280 pr_err("Some devices failed to power down, aborting\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200281 return error;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700282 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100283
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100284 error = platform_pre_snapshot(platform_mode);
285 if (error || hibernation_test(TEST_PLATFORM))
286 goto Platform_finish;
287
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000288 error = suspend_disable_secondary_cpus();
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100289 if (error || hibernation_test(TEST_CPUS))
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100290 goto Enable_cpus;
291
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100292 local_irq_disable();
293
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200294 system_state = SYSTEM_SUSPEND;
295
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200296 error = syscore_suspend();
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100297 if (error) {
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800298 pr_err("Some system devices failed to power down, aborting\n");
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100299 goto Enable_irqs;
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100300 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700301
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100302 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100303 goto Power_up;
304
305 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700306 save_processor_state();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700307 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700308 error = swsusp_arch_suspend();
Thomas Garnier62822e22016-08-11 14:49:29 -0700309 /* Restore control flow magically appears here */
310 restore_processor_state();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700311 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700312 if (error)
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800313 pr_err("Error %d creating image\n", error);
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100314
Anisse Astier1ad14102016-09-09 10:43:32 +0200315 if (!in_suspend) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200316 events_check_enabled = false;
Anisse Astier1ad14102016-09-09 10:43:32 +0200317 clear_free_pages();
318 }
Bjørn Mork362e77d2013-12-04 16:06:58 +0100319
320 platform_leave(platform_mode);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100321
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100322 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100323 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100324
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100325 Enable_irqs:
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200326 system_state = SYSTEM_RUNNING;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100327 local_irq_enable();
328
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100329 Enable_cpus:
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000330 suspend_enable_secondary_cpus();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100331
Jiri Kosinaec527c32019-05-30 00:09:39 +0200332 /* Allow architectures to do nosmt-specific post-resume dances */
333 if (!in_suspend)
334 error = arch_resume_nosmt();
335
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100336 Platform_finish:
337 platform_finish(platform_mode);
338
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100339 dpm_resume_start(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200340 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100341
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700342 return error;
343}
344
345/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200346 * hibernation_snapshot - Quiesce devices and create a hibernation image.
347 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700348 *
Pingfan Liu55f25032018-07-31 16:51:32 +0800349 * This routine must be called with system_transition_mutex held.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700350 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700351int hibernation_snapshot(int platform_mode)
352{
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100353 pm_message_t msg;
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100354 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700355
Lukas Wunner27614272016-03-23 00:11:20 +0100356 pm_suspend_clear_flags();
Zhang Rui3fe03132008-10-26 20:50:26 +0100357 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700358 if (error)
Rafael J. Wysockid074ee02010-07-07 23:43:35 +0200359 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700360
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200361 /* Preallocate image memory before shutting down devices. */
362 error = hibernate_preallocate_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700363 if (error)
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200364 goto Close;
365
366 error = freeze_kernel_threads();
367 if (error)
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100368 goto Cleanup;
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200369
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100370 if (hibernation_test(TEST_FREEZER)) {
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100371
372 /*
373 * Indicate to the caller that we are returning due to a
374 * successful freezer test.
375 */
376 freezer_test_done = true;
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100377 goto Thaw;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100378 }
379
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200380 error = dpm_prepare(PMSG_FREEZE);
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100381 if (error) {
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100382 dpm_complete(PMSG_RECOVER);
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100383 goto Thaw;
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100384 }
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700385
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700386 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100387 pm_restrict_gfp_mask();
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100388
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200389 error = dpm_suspend(PMSG_FREEZE);
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700390
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100391 if (error || hibernation_test(TEST_DEVICES))
392 platform_recover(platform_mode);
393 else
394 error = create_image(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700395
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100396 /*
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100397 * In the case that we call create_image() above, the control
398 * returns here (1) after the image has been created or the
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100399 * image creation has failed and (2) after a successful restore.
400 */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100401
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200402 /* We may need to release the preallocated image pages here. */
403 if (error || !in_suspend)
404 swsusp_free();
405
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200406 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
407 dpm_resume(msg);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100408
409 if (error || !in_suspend)
410 pm_restore_gfp_mask();
411
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700412 resume_console();
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200413 dpm_complete(msg);
414
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100415 Close:
416 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700417 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200418
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100419 Thaw:
420 thaw_kernel_threads();
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100421 Cleanup:
422 swsusp_free();
423 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700424}
425
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200426int __weak hibernate_resume_nonboot_cpu_disable(void)
427{
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000428 return suspend_disable_secondary_cpus();
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200429}
430
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700431/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200432 * resume_target_kernel - Restore system state from a hibernation image.
433 * @platform_mode: Whether or not to use the platform driver.
434 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100435 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
436 * contents of highmem that have not been restored yet from the image and run
437 * the low-level code that will restore the remaining contents of memory and
438 * switch to the just restored target kernel.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100439 */
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100440static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100441{
442 int error;
443
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100444 error = dpm_suspend_end(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100445 if (error) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100446 pr_err("Some devices failed to power down, aborting resume\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200447 return error;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100448 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100449
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100450 error = platform_pre_restore(platform_mode);
451 if (error)
452 goto Cleanup;
453
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200454 error = hibernate_resume_nonboot_cpu_disable();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100455 if (error)
456 goto Enable_cpus;
457
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100458 local_irq_disable();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200459 system_state = SYSTEM_SUSPEND;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100460
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200461 error = syscore_suspend();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100462 if (error)
463 goto Enable_irqs;
464
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100465 save_processor_state();
466 error = restore_highmem();
467 if (!error) {
468 error = swsusp_arch_resume();
469 /*
470 * The code below is only ever reached in case of a failure.
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200471 * Otherwise, execution continues at the place where
472 * swsusp_arch_suspend() was called.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100473 */
474 BUG_ON(!error);
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200475 /*
476 * This call to restore_highmem() reverts the changes made by
477 * the previous one.
478 */
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100479 restore_highmem();
480 }
481 /*
482 * The only reason why swsusp_arch_resume() can fail is memory being
483 * very tight, so we have to free it as soon as we can to avoid
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200484 * subsequent failures.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100485 */
486 swsusp_free();
487 restore_processor_state();
488 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100489
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100490 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100491
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100492 Enable_irqs:
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200493 system_state = SYSTEM_RUNNING;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100494 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100495
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100496 Enable_cpus:
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000497 suspend_enable_secondary_cpus();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100498
499 Cleanup:
500 platform_restore_cleanup(platform_mode);
501
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100502 dpm_resume_start(PMSG_RECOVER);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100503
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100504 return error;
505}
506
507/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200508 * hibernation_restore - Quiesce devices and restore from a hibernation image.
509 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700510 *
Pingfan Liu55f25032018-07-31 16:51:32 +0800511 * This routine must be called with system_transition_mutex held. If it is
512 * successful, control reappears in the restored target kernel in
513 * hibernation_snapshot().
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700514 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700515int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700516{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100517 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700518
519 pm_prepare_console();
520 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100521 pm_restrict_gfp_mask();
Alan Sternd1616302009-05-24 22:05:42 +0200522 error = dpm_suspend_start(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700523 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100524 error = resume_target_kernel(platform_mode);
Imre Deak94fb8232014-10-24 20:29:10 +0300525 /*
526 * The above should either succeed and jump to the new kernel,
527 * or return with an error. Otherwise things are just
528 * undefined, so let's be paranoid.
529 */
530 BUG_ON(!error);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700531 }
Imre Deak94fb8232014-10-24 20:29:10 +0300532 dpm_resume_end(PMSG_RECOVER);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100533 pm_restore_gfp_mask();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700534 resume_console();
535 pm_restore_console();
536 return error;
537}
538
539/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200540 * hibernation_platform_enter - Power off the system using the platform driver.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700541 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700542int hibernation_platform_enter(void)
543{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100544 int error;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700545
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700546 if (!hibernation_ops)
547 return -ENOSYS;
548
549 /*
550 * We have cancelled the power transition by running
551 * hibernation_ops->finish() before saving the image, so we should let
552 * the firmware know that we're going to enter the sleep state after all
553 */
Rafael J. Wysockibb186902019-05-16 12:43:19 +0200554 error = hibernation_ops->begin(PMSG_HIBERNATE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700555 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100556 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700557
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100558 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700559 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +0200560 error = dpm_suspend_start(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200561 if (error) {
562 if (hibernation_ops->recover)
563 hibernation_ops->recover();
564 goto Resume_devices;
565 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700566
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100567 error = dpm_suspend_end(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100568 if (error)
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200569 goto Resume_devices;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100570
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100571 error = hibernation_ops->prepare();
572 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200573 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100574
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000575 error = suspend_disable_secondary_cpus();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100576 if (error)
Vitaly Kuznetsov8c506602015-06-24 16:02:06 +0200577 goto Enable_cpus;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100578
579 local_irq_disable();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200580 system_state = SYSTEM_SUSPEND;
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100581 syscore_suspend();
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100582 if (pm_wakeup_pending()) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200583 error = -EAGAIN;
584 goto Power_up;
585 }
586
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100587 hibernation_ops->enter();
588 /* We should never get here */
589 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700590
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200591 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100592 syscore_resume();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200593 system_state = SYSTEM_RUNNING;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200594 local_irq_enable();
Vitaly Kuznetsov8c506602015-06-24 16:02:06 +0200595
596 Enable_cpus:
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000597 suspend_enable_secondary_cpus();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200598
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200599 Platform_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700600 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100601
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100602 dpm_resume_start(PMSG_RESTORE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100603
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700604 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100605 entering_platform_hibernation = false;
Alan Sternd1616302009-05-24 22:05:42 +0200606 dpm_resume_end(PMSG_RESTORE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700607 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100608
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100609 Close:
610 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100611
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700612 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700613}
614
615/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200616 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200618 * Use the platform driver, if configured, to put the system into the sleep
619 * state corresponding to hibernation, or try to power it off or reboot,
620 * depending on the value of hibernation_mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700622static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Bojan Smojver62c552c2012-06-16 00:09:58 +0200624#ifdef CONFIG_SUSPEND
625 int error;
Rafael J. Wysocki81d45bd2017-02-24 00:25:28 +0100626
627 if (hibernation_mode == HIBERNATION_SUSPEND) {
628 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
629 if (error) {
630 hibernation_mode = hibernation_ops ?
631 HIBERNATION_PLATFORM :
632 HIBERNATION_SHUTDOWN;
633 } else {
634 /* Restore swap signature. */
635 error = swsusp_unmark();
636 if (error)
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100637 pr_err("Swap will be unusable! Try swapon -a.\n");
Rafael J. Wysocki81d45bd2017-02-24 00:25:28 +0100638
639 return;
640 }
641 }
Bojan Smojver62c552c2012-06-16 00:09:58 +0200642#endif
643
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700644 switch (hibernation_mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700645 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600646 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700648 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700649 hibernation_platform_enter();
Gustavo A. R. Silva82837ad2018-08-04 21:12:00 -0500650 /* Fall through */
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700651 case HIBERNATION_SHUTDOWN:
Sebastian Capella2c730782014-04-21 17:30:46 -0700652 if (pm_power_off)
653 kernel_power_off();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700654 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600656 kernel_halt();
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700657 /*
658 * Valid image is on the disk, if we continue we risk serious data
659 * corruption after resume.
660 */
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100661 pr_crit("Power down manually\n");
Sebastian Capella2c730782014-04-21 17:30:46 -0700662 while (1)
663 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Chen Yufe12c002016-07-22 10:30:47 +0800666static int load_image_and_restore(void)
667{
668 int error;
669 unsigned int flags;
670
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200671 pm_pr_dbg("Loading hibernation image.\n");
Chen Yufe12c002016-07-22 10:30:47 +0800672
673 lock_device_hotplug();
674 error = create_basic_memory_bitmaps();
675 if (error)
676 goto Unlock;
677
678 error = swsusp_read(&flags);
679 swsusp_close(FMODE_READ);
680 if (!error)
Dexuan Cui3704a6a2020-03-31 08:55:25 -0700681 error = hibernation_restore(flags & SF_PLATFORM_MODE);
Chen Yufe12c002016-07-22 10:30:47 +0800682
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800683 pr_err("Failed to load image, recovering.\n");
Chen Yufe12c002016-07-22 10:30:47 +0800684 swsusp_free();
685 free_basic_memory_bitmaps();
686 Unlock:
687 unlock_device_hotplug();
688
689 return error;
690}
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200693 * hibernate - Carry out system hibernation, including saving the image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700695int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700697 int error, nr_calls = 0;
Chen Yufe12c002016-07-22 10:30:47 +0800698 bool snapshot_test = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Kees Cooka6e15a32014-06-13 13:30:35 -0700700 if (!hibernation_available()) {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200701 pm_pr_dbg("Hibernation not available.\n");
Kees Cooka6e15a32014-06-13 13:30:35 -0700702 return -EPERM;
703 }
704
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100705 lock_system_sleep();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700706 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700707 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
708 error = -EBUSY;
709 goto Unlock;
710 }
711
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200712 pr_info("hibernation entry\n");
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100713 pm_prepare_console();
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700714 error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
715 if (error) {
716 nr_calls--;
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700717 goto Exit;
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700718 }
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700719
Harry Panb5dee312019-02-25 20:36:41 +0800720 ksys_sync_helper();
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700721
Tejun Heo03afed82011-11-21 12:32:24 -0800722 error = freeze_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700723 if (error)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200724 goto Exit;
725
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200726 lock_device_hotplug();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200727 /* Allocate memory management structures */
728 error = create_basic_memory_bitmaps();
729 if (error)
730 goto Thaw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700732 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100733 if (error || freezer_test_done)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200734 goto Free_bitmaps;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200735
736 if (in_suspend) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700737 unsigned int flags = 0;
738
739 if (hibernation_mode == HIBERNATION_PLATFORM)
740 flags |= SF_PLATFORM_MODE;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200741 if (nocompress)
742 flags |= SF_NOCOMPRESS_MODE;
Bojan Smojver081a9d02011-10-13 23:58:07 +0200743 else
744 flags |= SF_CRC32_MODE;
745
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800746 pm_pr_dbg("Writing hibernation image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700747 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700748 swsusp_free();
Chen Yufe12c002016-07-22 10:30:47 +0800749 if (!error) {
750 if (hibernation_mode == HIBERNATION_TEST_RESUME)
751 snapshot_test = true;
752 else
753 power_down();
754 }
MyungJoo Ham5262a472010-11-26 23:07:56 +0100755 in_suspend = 0;
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100756 pm_restore_gfp_mask();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800757 } else {
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800758 pm_pr_dbg("Hibernation image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800759 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200760
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200761 Free_bitmaps:
762 free_basic_memory_bitmaps();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800763 Thaw:
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200764 unlock_device_hotplug();
Chen Yufe12c002016-07-22 10:30:47 +0800765 if (snapshot_test) {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200766 pm_pr_dbg("Checking hibernation image\n");
Chen Yufe12c002016-07-22 10:30:47 +0800767 error = swsusp_check();
768 if (!error)
769 error = load_image_and_restore();
770 }
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100771 thaw_processes();
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100772
773 /* Don't bother checking whether freezer_test_done is true */
774 freezer_test_done = false;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700775 Exit:
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700776 __pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100777 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700778 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700779 Unlock:
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100780 unlock_system_sleep();
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200781 pr_info("hibernation exit\n");
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return error;
784}
785
786
787/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200788 * software_resume - Resume from a saved hibernation image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200790 * This routine is called as a late initcall, when all devices have been
791 * discovered and initialized already.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200793 * The image reading code is called to see if there is a hibernation image
794 * available for reading. If that is the case, devices are quiesced and the
795 * contents of memory is restored from the saved image.
796 *
797 * If this is successful, control reappears in the restored target kernel in
Geliang Tangd439e642015-09-29 20:36:58 -0700798 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200799 * attempts to recover gracefully and make the kernel return to the normal mode
800 * of operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802static int software_resume(void)
803{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700804 int error, nr_calls = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Johannes Berg60a0d232007-11-14 17:00:16 -0800806 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100807 * If the user said "noresume".. bail out early.
808 */
Kees Cooka6e15a32014-06-13 13:30:35 -0700809 if (noresume || !hibernation_available())
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100810 return 0;
811
812 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800813 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
814 * is configured into the kernel. Since the regular hibernate
815 * trigger path is via sysfs which takes a buffer mutex before
Pingfan Liu55f25032018-07-31 16:51:32 +0800816 * calling hibernate functions (which take system_transition_mutex)
817 * this can cause lockdep to complain about a possible ABBA deadlock
Johannes Berg60a0d232007-11-14 17:00:16 -0800818 * which cannot happen since we're in the boot code here and
819 * sysfs can't be invoked yet. Therefore, we use a subclass
820 * here to avoid lockdep complaining.
821 */
Pingfan Liu55f25032018-07-31 16:51:32 +0800822 mutex_lock_nested(&system_transition_mutex, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200823
824 if (swsusp_resume_device)
825 goto Check_image;
826
827 if (!strlen(resume_file)) {
828 error = -ENOENT;
829 goto Unlock;
830 }
831
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200832 pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200833
Barry Songf126f732011-10-10 23:38:41 +0200834 if (resume_delay) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100835 pr_info("Waiting %dsec before reading resume device ...\n",
Barry Songf126f732011-10-10 23:38:41 +0200836 resume_delay);
837 ssleep(resume_delay);
838 }
839
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200840 /* Check if the device is there */
841 swsusp_resume_device = name_to_dev_t(resume_file);
Minho Ban2df83fa2012-05-14 21:45:31 +0200842
843 /*
844 * name_to_dev_t is ineffective to verify parition if resume_file is in
845 * integer format. (e.g. major:minor)
846 */
847 if (isdigit(resume_file[0]) && resume_wait) {
848 int partno;
849 while (!get_gendisk(swsusp_resume_device, &partno))
850 msleep(10);
851 }
852
Pavel Machek3efa1472005-07-07 17:56:43 -0700853 if (!swsusp_resume_device) {
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100854 /*
855 * Some device discovery might still be in progress; we need
856 * to wait for this to finish.
857 */
858 wait_for_device_probe();
Barry Song6f8d7022011-10-06 20:34:46 +0200859
860 if (resume_wait) {
861 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
862 msleep(10);
863 async_synchronize_full();
864 }
865
Pavel Machek3efa1472005-07-07 17:56:43 -0700866 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200867 if (!swsusp_resume_device) {
868 error = -ENODEV;
869 goto Unlock;
870 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700871 }
872
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200873 Check_image:
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200874 pm_pr_dbg("Hibernation image partition %d:%d present\n",
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200875 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200877 pm_pr_dbg("Looking for hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800878 error = swsusp_check();
879 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700880 goto Unlock;
881
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700882 /* The snapshot device should not be opened while we're running */
883 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
884 error = -EBUSY;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200885 swsusp_close(FMODE_READ);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700886 goto Unlock;
887 }
888
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200889 pr_info("resume from hibernation\n");
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100890 pm_prepare_console();
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700891 error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
892 if (error) {
893 nr_calls--;
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200894 goto Close_Finish;
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700895 }
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700896
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800897 pm_pr_dbg("Preparing processes for hibernation restore.\n");
Tejun Heo03afed82011-11-21 12:32:24 -0800898 error = freeze_processes();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200899 if (error)
900 goto Close_Finish;
Chen Yufe12c002016-07-22 10:30:47 +0800901 error = load_image_and_restore();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200902 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700903 Finish:
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700904 __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100905 pm_restore_console();
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800906 pr_info("resume failed (%d)\n", error);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700907 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700908 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700909 Unlock:
Pingfan Liu55f25032018-07-31 16:51:32 +0800910 mutex_unlock(&system_transition_mutex);
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200911 pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700912 return error;
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200913 Close_Finish:
Jiri Slaby76b57e62009-10-07 22:37:35 +0200914 swsusp_close(FMODE_READ);
915 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Russ Dilld3c345d2013-10-24 14:25:26 +0100918late_initcall_sync(software_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700921static const char * const hibernation_modes[] = {
922 [HIBERNATION_PLATFORM] = "platform",
923 [HIBERNATION_SHUTDOWN] = "shutdown",
924 [HIBERNATION_REBOOT] = "reboot",
Bojan Smojver62c552c2012-06-16 00:09:58 +0200925#ifdef CONFIG_SUSPEND
926 [HIBERNATION_SUSPEND] = "suspend",
927#endif
Chen Yufe12c002016-07-22 10:30:47 +0800928 [HIBERNATION_TEST_RESUME] = "test_resume",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929};
930
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200931/*
932 * /sys/power/disk - Control hibernation mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200934 * Hibernation can be handled in several ways. There are a few different ways
935 * to put the system into the sleep state: using the platform driver (e.g. ACPI
936 * or other hibernation_ops), powering it off or rebooting it (for testing
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100937 * mostly).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200939 * The sysfs file /sys/power/disk provides an interface for selecting the
940 * hibernation mode to use. Reading from this file causes the available modes
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100941 * to be printed. There are 3 modes that can be supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * 'platform'
944 * 'shutdown'
945 * 'reboot'
946 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200947 * If a platform hibernation driver is in use, 'platform' will be supported
948 * and will be used by default. Otherwise, 'shutdown' will be used by default.
949 * The selected option (i.e. the one corresponding to the current value of
950 * hibernation_mode) is enclosed by a square bracket.
951 *
952 * To select a given hibernation mode it is necessary to write the mode's
953 * string representation (as returned by reading from /sys/power/disk) back
954 * into /sys/power/disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 */
956
Kay Sievers386f2752007-11-02 13:47:53 +0100957static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
958 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700960 int i;
961 char *start = buf;
962
Kees Cooka6e15a32014-06-13 13:30:35 -0700963 if (!hibernation_available())
964 return sprintf(buf, "[disabled]\n");
965
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700966 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
967 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700968 continue;
969 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700970 case HIBERNATION_SHUTDOWN:
971 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +0200972#ifdef CONFIG_SUSPEND
973 case HIBERNATION_SUSPEND:
974#endif
Chen Yufe12c002016-07-22 10:30:47 +0800975 case HIBERNATION_TEST_RESUME:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700976 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700977 case HIBERNATION_PLATFORM:
978 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700979 break;
980 /* not a valid mode, continue with loop */
981 continue;
982 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700983 if (i == hibernation_mode)
984 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700985 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700986 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700987 }
988 buf += sprintf(buf, "\n");
989 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
Kay Sievers386f2752007-11-02 13:47:53 +0100992static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
993 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 int error = 0;
996 int i;
997 int len;
998 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700999 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Kees Cooka6e15a32014-06-13 13:30:35 -07001001 if (!hibernation_available())
1002 return -EPERM;
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 p = memchr(buf, '\n', n);
1005 len = p ? p - buf : n;
1006
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001007 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001008 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -07001009 if (len == strlen(hibernation_modes[i])
1010 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 mode = i;
1012 break;
1013 }
1014 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001015 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001016 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001017 case HIBERNATION_SHUTDOWN:
1018 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +02001019#ifdef CONFIG_SUSPEND
1020 case HIBERNATION_SUSPEND:
1021#endif
Chen Yufe12c002016-07-22 10:30:47 +08001022 case HIBERNATION_TEST_RESUME:
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001023 hibernation_mode = mode;
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001024 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001025 case HIBERNATION_PLATFORM:
1026 if (hibernation_ops)
1027 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 else
1029 error = -EINVAL;
1030 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001031 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 error = -EINVAL;
1033
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001034 if (!error)
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +02001035 pm_pr_dbg("Hibernation mode set to '%s'\n",
1036 hibernation_modes[mode]);
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001037 unlock_system_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return error ? error : n;
1039}
1040
1041power_attr(disk);
1042
Kay Sievers386f2752007-11-02 13:47:53 +01001043static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1044 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
1047 MINOR(swsusp_resume_device));
1048}
1049
Kay Sievers386f2752007-11-02 13:47:53 +01001050static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1051 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 dev_t res;
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001054 int len = n;
1055 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001057 if (len && buf[len-1] == '\n')
1058 len--;
1059 name = kstrndup(buf, len, GFP_KERNEL);
1060 if (!name)
1061 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001063 res = name_to_dev_t(name);
1064 kfree(name);
1065 if (!res)
1066 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001068 lock_system_sleep();
Andrew Mortona5762192006-01-06 00:09:50 -08001069 swsusp_resume_device = res;
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001070 unlock_system_sleep();
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -08001071 pm_pr_dbg("Configured hibernation resume from disk to %u\n",
1072 swsusp_resume_device);
Andrew Mortona5762192006-01-06 00:09:50 -08001073 noresume = 0;
1074 software_resume();
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001075 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
1078power_attr(resume);
1079
Mario Limonciello35506462018-03-28 12:01:09 -05001080static ssize_t resume_offset_show(struct kobject *kobj,
1081 struct kobj_attribute *attr, char *buf)
1082{
1083 return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
1084}
1085
1086static ssize_t resume_offset_store(struct kobject *kobj,
1087 struct kobj_attribute *attr, const char *buf,
1088 size_t n)
1089{
1090 unsigned long long offset;
1091 int rc;
1092
1093 rc = kstrtoull(buf, 0, &offset);
1094 if (rc)
1095 return rc;
1096 swsusp_resume_block = offset;
1097
1098 return n;
1099}
1100
1101power_attr(resume_offset);
1102
Kay Sievers386f2752007-11-02 13:47:53 +01001103static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1104 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001105{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001106 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001107}
1108
Kay Sievers386f2752007-11-02 13:47:53 +01001109static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1110 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001111{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001112 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001113
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001114 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001115 image_size = size;
1116 return n;
1117 }
1118
1119 return -EINVAL;
1120}
1121
1122power_attr(image_size);
1123
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001124static ssize_t reserved_size_show(struct kobject *kobj,
1125 struct kobj_attribute *attr, char *buf)
1126{
1127 return sprintf(buf, "%lu\n", reserved_size);
1128}
1129
1130static ssize_t reserved_size_store(struct kobject *kobj,
1131 struct kobj_attribute *attr,
1132 const char *buf, size_t n)
1133{
1134 unsigned long size;
1135
1136 if (sscanf(buf, "%lu", &size) == 1) {
1137 reserved_size = size;
1138 return n;
1139 }
1140
1141 return -EINVAL;
1142}
1143
1144power_attr(reserved_size);
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146static struct attribute * g[] = {
1147 &disk_attr.attr,
Mario Limonciello35506462018-03-28 12:01:09 -05001148 &resume_offset_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001150 &image_size_attr.attr,
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001151 &reserved_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 NULL,
1153};
1154
1155
Arvind Yadav59494fe2017-06-29 16:58:40 +05301156static const struct attribute_group attr_group = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 .attrs = g,
1158};
1159
1160
1161static int __init pm_disk_init(void)
1162{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -08001163 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
1166core_initcall(pm_disk_init);
1167
1168
1169static int __init resume_setup(char *str)
1170{
1171 if (noresume)
1172 return 1;
1173
1174 strncpy( resume_file, str, 255 );
1175 return 1;
1176}
1177
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001178static int __init resume_offset_setup(char *str)
1179{
1180 unsigned long long offset;
1181
1182 if (noresume)
1183 return 1;
1184
1185 if (sscanf(str, "%llu", &offset) == 1)
1186 swsusp_resume_block = offset;
1187
1188 return 1;
1189}
1190
Bojan Smojverf996fc92010-09-09 23:06:23 +02001191static int __init hibernate_setup(char *str)
1192{
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001193 if (!strncmp(str, "noresume", 8)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001194 noresume = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001195 } else if (!strncmp(str, "nocompress", 10)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001196 nocompress = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001197 } else if (!strncmp(str, "no", 2)) {
Kees Cooka6e15a32014-06-13 13:30:35 -07001198 noresume = 1;
1199 nohibernate = 1;
Laura Abbott0f5bf6d2017-02-06 16:31:58 -08001200 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
Rafael J. Wysocki4c0b6c12016-07-10 02:12:10 +02001201 && !strncmp(str, "protect_image", 13)) {
1202 enable_restore_image_protection();
Kees Cooka6e15a32014-06-13 13:30:35 -07001203 }
Bojan Smojverf996fc92010-09-09 23:06:23 +02001204 return 1;
1205}
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207static int __init noresume_setup(char *str)
1208{
1209 noresume = 1;
1210 return 1;
1211}
1212
Barry Song6f8d7022011-10-06 20:34:46 +02001213static int __init resumewait_setup(char *str)
1214{
1215 resume_wait = 1;
1216 return 1;
1217}
1218
Barry Songf126f732011-10-10 23:38:41 +02001219static int __init resumedelay_setup(char *str)
1220{
Dan Carpenterf6514be2014-05-14 19:08:46 +03001221 int rc = kstrtouint(str, 0, &resume_delay);
Fabian Frederick317cf7e2014-05-09 23:32:08 +02001222
1223 if (rc)
1224 return rc;
Barry Songf126f732011-10-10 23:38:41 +02001225 return 1;
1226}
1227
Kees Cooka6e15a32014-06-13 13:30:35 -07001228static int __init nohibernate_setup(char *str)
1229{
1230 noresume = 1;
1231 nohibernate = 1;
1232 return 1;
1233}
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001236__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237__setup("resume=", resume_setup);
Bojan Smojverf996fc92010-09-09 23:06:23 +02001238__setup("hibernate=", hibernate_setup);
Barry Song6f8d7022011-10-06 20:34:46 +02001239__setup("resumewait", resumewait_setup);
Barry Songf126f732011-10-10 23:38:41 +02001240__setup("resumedelay=", resumedelay_setup);
Kees Cooka6e15a32014-06-13 13:30:35 -07001241__setup("nohibernate", nohibernate_setup);