blob: 02ec716a492713877caa67ff51b2d83a5d495b3e [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
Domenico Andreoliab7e9b02020-05-07 09:19:52 +020070static atomic_t hibernate_atomic = ATOMIC_INIT(1);
71
72bool hibernate_acquire(void)
73{
74 return atomic_add_unless(&hibernate_atomic, -1, 0);
75}
76
77void hibernate_release(void)
78{
79 atomic_inc(&hibernate_atomic);
80}
81
Kees Cooka6e15a32014-06-13 13:30:35 -070082bool hibernation_available(void)
83{
Josh Boyer38bd94b2019-08-19 17:17:46 -070084 return nohibernate == 0 && !security_locked_down(LOCKDOWN_HIBERNATION);
Kees Cooka6e15a32014-06-13 13:30:35 -070085}
86
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070087/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +020088 * hibernation_set_ops - Set the global hibernate operations.
89 * @ops: Hibernation operations to use in subsequent hibernation transitions.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070090 */
Lionel Debroux073ef1f2010-11-09 21:48:49 +010091void hibernation_set_ops(const struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070092{
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +010093 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
94 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
MyungJoo Ham5729c632010-11-26 23:07:48 +010095 && ops->restore_cleanup && ops->leave)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070096 WARN_ON(1);
97 return;
98 }
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010099 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700100 hibernation_ops = ops;
101 if (ops)
102 hibernation_mode = HIBERNATION_PLATFORM;
103 else if (hibernation_mode == HIBERNATION_PLATFORM)
104 hibernation_mode = HIBERNATION_SHUTDOWN;
105
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100106 unlock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700107}
Leonardo Potenzae0c78552013-11-19 12:27:41 +0000108EXPORT_SYMBOL_GPL(hibernation_set_ops);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700109
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100110static bool entering_platform_hibernation;
111
112bool system_entering_hibernation(void)
113{
114 return entering_platform_hibernation;
115}
116EXPORT_SYMBOL(system_entering_hibernation);
117
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100118#ifdef CONFIG_PM_DEBUG
119static void hibernation_debug_sleep(void)
120{
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800121 pr_info("debug: Waiting for 5 seconds.\n");
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100122 mdelay(5000);
123}
124
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100125static int hibernation_test(int level)
126{
127 if (pm_test_level == level) {
128 hibernation_debug_sleep();
129 return 1;
130 }
131 return 0;
132}
133#else /* !CONFIG_PM_DEBUG */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100134static int hibernation_test(int level) { return 0; }
135#endif /* !CONFIG_PM_DEBUG */
136
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700137/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200138 * platform_begin - Call platform to start hibernation.
139 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700140 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100141static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700142{
143 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockibb186902019-05-16 12:43:19 +0200144 hibernation_ops->begin(PMSG_FREEZE) : 0;
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100145}
146
147/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200148 * platform_end - Call platform to finish transition to the working state.
149 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100150 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100151static void platform_end(int platform_mode)
152{
153 if (platform_mode && hibernation_ops)
154 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700155}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200158 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
159 * @platform_mode: Whether or not to use the platform driver.
160 *
161 * Use the platform driver to prepare the system for creating a hibernate image,
162 * if so configured, and return an error code if that fails.
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800163 */
164
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700165static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800166{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700167 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700168 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800169}
170
171/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200172 * platform_leave - Call platform to prepare a transition to the working state.
173 * @platform_mode: Whether or not to use the platform driver.
174 *
175 * Use the platform driver prepare to prepare the machine for switching to the
176 * normal mode of operation.
177 *
178 * This routine is called on one CPU with interrupts disabled.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700179 */
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700180static void platform_leave(int platform_mode)
181{
182 if (platform_mode && hibernation_ops)
183 hibernation_ops->leave();
184}
185
186/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200187 * platform_finish - Call platform to switch the system to the working state.
188 * @platform_mode: Whether or not to use the platform driver.
189 *
190 * Use the platform driver to switch the machine to the normal mode of
191 * operation.
192 *
193 * This routine must be called after platform_prepare().
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700194 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700195static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700196{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700197 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700198 hibernation_ops->finish();
199}
200
201/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200202 * platform_pre_restore - Prepare for hibernate image restoration.
203 * @platform_mode: Whether or not to use the platform driver.
204 *
205 * Use the platform driver to prepare the system for resume from a hibernation
206 * image.
207 *
208 * If the restore fails after this function has been called,
209 * platform_restore_cleanup() must be called.
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700210 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700211static int platform_pre_restore(int platform_mode)
212{
213 return (platform_mode && hibernation_ops) ?
214 hibernation_ops->pre_restore() : 0;
215}
216
217/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200218 * platform_restore_cleanup - Switch to the working state after failing restore.
219 * @platform_mode: Whether or not to use the platform driver.
220 *
221 * Use the platform driver to switch the system to the normal mode of operation
222 * after a failing restore.
223 *
224 * If platform_pre_restore() has been called before the failing restore, this
225 * function must be called too, regardless of the result of
226 * platform_pre_restore().
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700227 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700228static void platform_restore_cleanup(int platform_mode)
229{
230 if (platform_mode && hibernation_ops)
231 hibernation_ops->restore_cleanup();
232}
233
234/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200235 * platform_recover - Recover from a failure to suspend devices.
236 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200237 */
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200238static void platform_recover(int platform_mode)
239{
240 if (platform_mode && hibernation_ops && hibernation_ops->recover)
241 hibernation_ops->recover();
242}
243
244/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200245 * swsusp_show_speed - Print time elapsed between two events during hibernation.
246 * @start: Starting event.
247 * @stop: Final event.
248 * @nr_pages: Number of memory pages processed between @start and @stop.
249 * @msg: Additional diagnostic message to print.
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100250 */
Tina Ruchandanidb597602014-10-30 11:04:53 -0700251void swsusp_show_speed(ktime_t start, ktime_t stop,
252 unsigned nr_pages, char *msg)
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100253{
Tina Ruchandanidb597602014-10-30 11:04:53 -0700254 ktime_t diff;
Chen Gang4881f602014-04-25 08:44:59 +0800255 u64 elapsed_centisecs64;
256 unsigned int centisecs;
257 unsigned int k;
258 unsigned int kps;
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100259
Tina Ruchandanidb597602014-10-30 11:04:53 -0700260 diff = ktime_sub(stop, start);
261 elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC);
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100262 centisecs = elapsed_centisecs64;
263 if (centisecs == 0)
264 centisecs = 1; /* avoid div-by-zero */
265 k = nr_pages * (PAGE_SIZE / 1024);
266 kps = (k * 100) / centisecs;
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100267 pr_info("%s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
268 msg, k, centisecs / 100, centisecs % 100, kps / 1000,
269 (kps % 1000) / 10);
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100270}
271
Jiri Kosinaec527c32019-05-30 00:09:39 +0200272__weak int arch_resume_nosmt(void)
273{
274 return 0;
275}
276
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100277/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200278 * create_image - Create a hibernation image.
279 * @platform_mode: Whether or not to use the platform driver.
280 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100281 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
282 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200283 *
284 * Control reappears in this routine after the subsequent restore.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700285 */
Adrian Bunk47a460d2008-02-04 22:30:06 -0800286static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700287{
288 int error;
289
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100290 error = dpm_suspend_end(PMSG_FREEZE);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700291 if (error) {
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800292 pr_err("Some devices failed to power down, aborting\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200293 return error;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700294 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100295
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100296 error = platform_pre_snapshot(platform_mode);
297 if (error || hibernation_test(TEST_PLATFORM))
298 goto Platform_finish;
299
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000300 error = suspend_disable_secondary_cpus();
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100301 if (error || hibernation_test(TEST_CPUS))
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100302 goto Enable_cpus;
303
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100304 local_irq_disable();
305
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200306 system_state = SYSTEM_SUSPEND;
307
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200308 error = syscore_suspend();
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100309 if (error) {
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800310 pr_err("Some system devices failed to power down, aborting\n");
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100311 goto Enable_irqs;
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100312 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700313
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100314 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100315 goto Power_up;
316
317 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700318 save_processor_state();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700319 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700320 error = swsusp_arch_suspend();
Thomas Garnier62822e22016-08-11 14:49:29 -0700321 /* Restore control flow magically appears here */
322 restore_processor_state();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700323 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700324 if (error)
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800325 pr_err("Error %d creating image\n", error);
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100326
Anisse Astier1ad14102016-09-09 10:43:32 +0200327 if (!in_suspend) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200328 events_check_enabled = false;
Anisse Astier1ad14102016-09-09 10:43:32 +0200329 clear_free_pages();
330 }
Bjørn Mork362e77d2013-12-04 16:06:58 +0100331
332 platform_leave(platform_mode);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100333
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100334 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100335 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100336
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100337 Enable_irqs:
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200338 system_state = SYSTEM_RUNNING;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100339 local_irq_enable();
340
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100341 Enable_cpus:
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000342 suspend_enable_secondary_cpus();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100343
Jiri Kosinaec527c32019-05-30 00:09:39 +0200344 /* Allow architectures to do nosmt-specific post-resume dances */
345 if (!in_suspend)
346 error = arch_resume_nosmt();
347
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100348 Platform_finish:
349 platform_finish(platform_mode);
350
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100351 dpm_resume_start(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200352 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100353
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700354 return error;
355}
356
357/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200358 * hibernation_snapshot - Quiesce devices and create a hibernation image.
359 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700360 *
Pingfan Liu55f25032018-07-31 16:51:32 +0800361 * This routine must be called with system_transition_mutex held.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700362 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700363int hibernation_snapshot(int platform_mode)
364{
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100365 pm_message_t msg;
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100366 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700367
Lukas Wunner27614272016-03-23 00:11:20 +0100368 pm_suspend_clear_flags();
Zhang Rui3fe03132008-10-26 20:50:26 +0100369 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700370 if (error)
Rafael J. Wysockid074ee02010-07-07 23:43:35 +0200371 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700372
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200373 /* Preallocate image memory before shutting down devices. */
374 error = hibernate_preallocate_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700375 if (error)
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200376 goto Close;
377
378 error = freeze_kernel_threads();
379 if (error)
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100380 goto Cleanup;
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200381
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100382 if (hibernation_test(TEST_FREEZER)) {
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100383
384 /*
385 * Indicate to the caller that we are returning due to a
386 * successful freezer test.
387 */
388 freezer_test_done = true;
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100389 goto Thaw;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100390 }
391
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200392 error = dpm_prepare(PMSG_FREEZE);
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100393 if (error) {
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100394 dpm_complete(PMSG_RECOVER);
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100395 goto Thaw;
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100396 }
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700397
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700398 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100399 pm_restrict_gfp_mask();
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100400
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200401 error = dpm_suspend(PMSG_FREEZE);
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700402
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100403 if (error || hibernation_test(TEST_DEVICES))
404 platform_recover(platform_mode);
405 else
406 error = create_image(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700407
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100408 /*
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100409 * In the case that we call create_image() above, the control
410 * returns here (1) after the image has been created or the
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100411 * image creation has failed and (2) after a successful restore.
412 */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100413
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200414 /* We may need to release the preallocated image pages here. */
415 if (error || !in_suspend)
416 swsusp_free();
417
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200418 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
419 dpm_resume(msg);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100420
421 if (error || !in_suspend)
422 pm_restore_gfp_mask();
423
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700424 resume_console();
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200425 dpm_complete(msg);
426
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100427 Close:
428 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700429 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200430
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100431 Thaw:
432 thaw_kernel_threads();
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100433 Cleanup:
434 swsusp_free();
435 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700436}
437
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200438int __weak hibernate_resume_nonboot_cpu_disable(void)
439{
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000440 return suspend_disable_secondary_cpus();
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200441}
442
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700443/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200444 * resume_target_kernel - Restore system state from a hibernation image.
445 * @platform_mode: Whether or not to use the platform driver.
446 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100447 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
448 * contents of highmem that have not been restored yet from the image and run
449 * the low-level code that will restore the remaining contents of memory and
450 * switch to the just restored target kernel.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100451 */
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100452static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100453{
454 int error;
455
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100456 error = dpm_suspend_end(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100457 if (error) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100458 pr_err("Some devices failed to power down, aborting resume\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200459 return error;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100460 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100461
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100462 error = platform_pre_restore(platform_mode);
463 if (error)
464 goto Cleanup;
465
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200466 error = hibernate_resume_nonboot_cpu_disable();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100467 if (error)
468 goto Enable_cpus;
469
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100470 local_irq_disable();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200471 system_state = SYSTEM_SUSPEND;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100472
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200473 error = syscore_suspend();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100474 if (error)
475 goto Enable_irqs;
476
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100477 save_processor_state();
478 error = restore_highmem();
479 if (!error) {
480 error = swsusp_arch_resume();
481 /*
482 * The code below is only ever reached in case of a failure.
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200483 * Otherwise, execution continues at the place where
484 * swsusp_arch_suspend() was called.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100485 */
486 BUG_ON(!error);
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200487 /*
488 * This call to restore_highmem() reverts the changes made by
489 * the previous one.
490 */
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100491 restore_highmem();
492 }
493 /*
494 * The only reason why swsusp_arch_resume() can fail is memory being
495 * very tight, so we have to free it as soon as we can to avoid
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200496 * subsequent failures.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100497 */
498 swsusp_free();
499 restore_processor_state();
500 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100501
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100502 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100503
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100504 Enable_irqs:
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200505 system_state = SYSTEM_RUNNING;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100506 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100507
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100508 Enable_cpus:
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000509 suspend_enable_secondary_cpus();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100510
511 Cleanup:
512 platform_restore_cleanup(platform_mode);
513
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100514 dpm_resume_start(PMSG_RECOVER);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100515
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100516 return error;
517}
518
519/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200520 * hibernation_restore - Quiesce devices and restore from a hibernation image.
521 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700522 *
Pingfan Liu55f25032018-07-31 16:51:32 +0800523 * This routine must be called with system_transition_mutex held. If it is
524 * successful, control reappears in the restored target kernel in
525 * hibernation_snapshot().
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700526 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700527int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700528{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100529 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700530
531 pm_prepare_console();
532 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100533 pm_restrict_gfp_mask();
Alan Sternd1616302009-05-24 22:05:42 +0200534 error = dpm_suspend_start(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700535 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100536 error = resume_target_kernel(platform_mode);
Imre Deak94fb8232014-10-24 20:29:10 +0300537 /*
538 * The above should either succeed and jump to the new kernel,
539 * or return with an error. Otherwise things are just
540 * undefined, so let's be paranoid.
541 */
542 BUG_ON(!error);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700543 }
Imre Deak94fb8232014-10-24 20:29:10 +0300544 dpm_resume_end(PMSG_RECOVER);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100545 pm_restore_gfp_mask();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700546 resume_console();
547 pm_restore_console();
548 return error;
549}
550
551/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200552 * hibernation_platform_enter - Power off the system using the platform driver.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700553 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700554int hibernation_platform_enter(void)
555{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100556 int error;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700557
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700558 if (!hibernation_ops)
559 return -ENOSYS;
560
561 /*
562 * We have cancelled the power transition by running
563 * hibernation_ops->finish() before saving the image, so we should let
564 * the firmware know that we're going to enter the sleep state after all
565 */
Rafael J. Wysockibb186902019-05-16 12:43:19 +0200566 error = hibernation_ops->begin(PMSG_HIBERNATE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700567 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100568 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700569
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100570 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700571 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +0200572 error = dpm_suspend_start(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200573 if (error) {
574 if (hibernation_ops->recover)
575 hibernation_ops->recover();
576 goto Resume_devices;
577 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700578
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100579 error = dpm_suspend_end(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100580 if (error)
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200581 goto Resume_devices;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100582
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100583 error = hibernation_ops->prepare();
584 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200585 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100586
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000587 error = suspend_disable_secondary_cpus();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100588 if (error)
Vitaly Kuznetsov8c506602015-06-24 16:02:06 +0200589 goto Enable_cpus;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100590
591 local_irq_disable();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200592 system_state = SYSTEM_SUSPEND;
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100593 syscore_suspend();
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100594 if (pm_wakeup_pending()) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200595 error = -EAGAIN;
596 goto Power_up;
597 }
598
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100599 hibernation_ops->enter();
600 /* We should never get here */
601 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700602
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200603 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100604 syscore_resume();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200605 system_state = SYSTEM_RUNNING;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200606 local_irq_enable();
Vitaly Kuznetsov8c506602015-06-24 16:02:06 +0200607
608 Enable_cpus:
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000609 suspend_enable_secondary_cpus();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200610
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200611 Platform_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700612 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100613
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100614 dpm_resume_start(PMSG_RESTORE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100615
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700616 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100617 entering_platform_hibernation = false;
Alan Sternd1616302009-05-24 22:05:42 +0200618 dpm_resume_end(PMSG_RESTORE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700619 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100620
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100621 Close:
622 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100623
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700624 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700625}
626
627/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200628 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200630 * Use the platform driver, if configured, to put the system into the sleep
631 * state corresponding to hibernation, or try to power it off or reboot,
632 * depending on the value of hibernation_mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700634static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Bojan Smojver62c552c2012-06-16 00:09:58 +0200636#ifdef CONFIG_SUSPEND
637 int error;
Rafael J. Wysocki81d45bd2017-02-24 00:25:28 +0100638
639 if (hibernation_mode == HIBERNATION_SUSPEND) {
640 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
641 if (error) {
642 hibernation_mode = hibernation_ops ?
643 HIBERNATION_PLATFORM :
644 HIBERNATION_SHUTDOWN;
645 } else {
646 /* Restore swap signature. */
647 error = swsusp_unmark();
648 if (error)
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100649 pr_err("Swap will be unusable! Try swapon -a.\n");
Rafael J. Wysocki81d45bd2017-02-24 00:25:28 +0100650
651 return;
652 }
653 }
Bojan Smojver62c552c2012-06-16 00:09:58 +0200654#endif
655
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700656 switch (hibernation_mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700657 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600658 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700660 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700661 hibernation_platform_enter();
Gustavo A. R. Silva82837ad2018-08-04 21:12:00 -0500662 /* Fall through */
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700663 case HIBERNATION_SHUTDOWN:
Sebastian Capella2c730782014-04-21 17:30:46 -0700664 if (pm_power_off)
665 kernel_power_off();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700666 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600668 kernel_halt();
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700669 /*
670 * Valid image is on the disk, if we continue we risk serious data
671 * corruption after resume.
672 */
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100673 pr_crit("Power down manually\n");
Sebastian Capella2c730782014-04-21 17:30:46 -0700674 while (1)
675 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Chen Yufe12c002016-07-22 10:30:47 +0800678static int load_image_and_restore(void)
679{
680 int error;
681 unsigned int flags;
682
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200683 pm_pr_dbg("Loading hibernation image.\n");
Chen Yufe12c002016-07-22 10:30:47 +0800684
685 lock_device_hotplug();
686 error = create_basic_memory_bitmaps();
687 if (error)
688 goto Unlock;
689
690 error = swsusp_read(&flags);
691 swsusp_close(FMODE_READ);
692 if (!error)
Dexuan Cui3704a6a2020-03-31 08:55:25 -0700693 error = hibernation_restore(flags & SF_PLATFORM_MODE);
Chen Yufe12c002016-07-22 10:30:47 +0800694
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800695 pr_err("Failed to load image, recovering.\n");
Chen Yufe12c002016-07-22 10:30:47 +0800696 swsusp_free();
697 free_basic_memory_bitmaps();
698 Unlock:
699 unlock_device_hotplug();
700
701 return error;
702}
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200705 * hibernate - Carry out system hibernation, including saving the image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700707int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700709 int error, nr_calls = 0;
Chen Yufe12c002016-07-22 10:30:47 +0800710 bool snapshot_test = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Kees Cooka6e15a32014-06-13 13:30:35 -0700712 if (!hibernation_available()) {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200713 pm_pr_dbg("Hibernation not available.\n");
Kees Cooka6e15a32014-06-13 13:30:35 -0700714 return -EPERM;
715 }
716
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100717 lock_system_sleep();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700718 /* The snapshot device should not be opened while we're running */
Domenico Andreoliab7e9b02020-05-07 09:19:52 +0200719 if (!hibernate_acquire()) {
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700720 error = -EBUSY;
721 goto Unlock;
722 }
723
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200724 pr_info("hibernation entry\n");
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100725 pm_prepare_console();
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700726 error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
727 if (error) {
728 nr_calls--;
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700729 goto Exit;
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700730 }
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700731
Harry Panb5dee312019-02-25 20:36:41 +0800732 ksys_sync_helper();
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700733
Tejun Heo03afed82011-11-21 12:32:24 -0800734 error = freeze_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700735 if (error)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200736 goto Exit;
737
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200738 lock_device_hotplug();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200739 /* Allocate memory management structures */
740 error = create_basic_memory_bitmaps();
741 if (error)
742 goto Thaw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700744 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100745 if (error || freezer_test_done)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200746 goto Free_bitmaps;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200747
748 if (in_suspend) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700749 unsigned int flags = 0;
750
751 if (hibernation_mode == HIBERNATION_PLATFORM)
752 flags |= SF_PLATFORM_MODE;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200753 if (nocompress)
754 flags |= SF_NOCOMPRESS_MODE;
Bojan Smojver081a9d02011-10-13 23:58:07 +0200755 else
756 flags |= SF_CRC32_MODE;
757
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800758 pm_pr_dbg("Writing hibernation image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700759 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700760 swsusp_free();
Chen Yufe12c002016-07-22 10:30:47 +0800761 if (!error) {
762 if (hibernation_mode == HIBERNATION_TEST_RESUME)
763 snapshot_test = true;
764 else
765 power_down();
766 }
MyungJoo Ham5262a472010-11-26 23:07:56 +0100767 in_suspend = 0;
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100768 pm_restore_gfp_mask();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800769 } else {
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800770 pm_pr_dbg("Hibernation image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800771 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200772
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200773 Free_bitmaps:
774 free_basic_memory_bitmaps();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800775 Thaw:
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200776 unlock_device_hotplug();
Chen Yufe12c002016-07-22 10:30:47 +0800777 if (snapshot_test) {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200778 pm_pr_dbg("Checking hibernation image\n");
Chen Yufe12c002016-07-22 10:30:47 +0800779 error = swsusp_check();
780 if (!error)
781 error = load_image_and_restore();
782 }
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100783 thaw_processes();
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100784
785 /* Don't bother checking whether freezer_test_done is true */
786 freezer_test_done = false;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700787 Exit:
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700788 __pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100789 pm_restore_console();
Domenico Andreoliab7e9b02020-05-07 09:19:52 +0200790 hibernate_release();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700791 Unlock:
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100792 unlock_system_sleep();
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200793 pr_info("hibernation exit\n");
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return error;
796}
797
798
799/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200800 * software_resume - Resume from a saved hibernation image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200802 * This routine is called as a late initcall, when all devices have been
803 * discovered and initialized already.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200805 * The image reading code is called to see if there is a hibernation image
806 * available for reading. If that is the case, devices are quiesced and the
807 * contents of memory is restored from the saved image.
808 *
809 * If this is successful, control reappears in the restored target kernel in
Geliang Tangd439e642015-09-29 20:36:58 -0700810 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200811 * attempts to recover gracefully and make the kernel return to the normal mode
812 * of operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814static int software_resume(void)
815{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700816 int error, nr_calls = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Johannes Berg60a0d232007-11-14 17:00:16 -0800818 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100819 * If the user said "noresume".. bail out early.
820 */
Kees Cooka6e15a32014-06-13 13:30:35 -0700821 if (noresume || !hibernation_available())
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100822 return 0;
823
824 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800825 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
826 * is configured into the kernel. Since the regular hibernate
827 * trigger path is via sysfs which takes a buffer mutex before
Pingfan Liu55f25032018-07-31 16:51:32 +0800828 * calling hibernate functions (which take system_transition_mutex)
829 * this can cause lockdep to complain about a possible ABBA deadlock
Johannes Berg60a0d232007-11-14 17:00:16 -0800830 * which cannot happen since we're in the boot code here and
831 * sysfs can't be invoked yet. Therefore, we use a subclass
832 * here to avoid lockdep complaining.
833 */
Pingfan Liu55f25032018-07-31 16:51:32 +0800834 mutex_lock_nested(&system_transition_mutex, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200835
836 if (swsusp_resume_device)
837 goto Check_image;
838
839 if (!strlen(resume_file)) {
840 error = -ENOENT;
841 goto Unlock;
842 }
843
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200844 pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200845
Barry Songf126f732011-10-10 23:38:41 +0200846 if (resume_delay) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100847 pr_info("Waiting %dsec before reading resume device ...\n",
Barry Songf126f732011-10-10 23:38:41 +0200848 resume_delay);
849 ssleep(resume_delay);
850 }
851
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200852 /* Check if the device is there */
853 swsusp_resume_device = name_to_dev_t(resume_file);
Minho Ban2df83fa2012-05-14 21:45:31 +0200854
855 /*
856 * name_to_dev_t is ineffective to verify parition if resume_file is in
857 * integer format. (e.g. major:minor)
858 */
859 if (isdigit(resume_file[0]) && resume_wait) {
860 int partno;
861 while (!get_gendisk(swsusp_resume_device, &partno))
862 msleep(10);
863 }
864
Pavel Machek3efa1472005-07-07 17:56:43 -0700865 if (!swsusp_resume_device) {
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100866 /*
867 * Some device discovery might still be in progress; we need
868 * to wait for this to finish.
869 */
870 wait_for_device_probe();
Barry Song6f8d7022011-10-06 20:34:46 +0200871
872 if (resume_wait) {
873 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
874 msleep(10);
875 async_synchronize_full();
876 }
877
Pavel Machek3efa1472005-07-07 17:56:43 -0700878 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200879 if (!swsusp_resume_device) {
880 error = -ENODEV;
881 goto Unlock;
882 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700883 }
884
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200885 Check_image:
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200886 pm_pr_dbg("Hibernation image partition %d:%d present\n",
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200887 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200889 pm_pr_dbg("Looking for hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800890 error = swsusp_check();
891 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700892 goto Unlock;
893
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700894 /* The snapshot device should not be opened while we're running */
Domenico Andreoliab7e9b02020-05-07 09:19:52 +0200895 if (!hibernate_acquire()) {
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700896 error = -EBUSY;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200897 swsusp_close(FMODE_READ);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700898 goto Unlock;
899 }
900
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200901 pr_info("resume from hibernation\n");
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100902 pm_prepare_console();
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700903 error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
904 if (error) {
905 nr_calls--;
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200906 goto Close_Finish;
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700907 }
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700908
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800909 pm_pr_dbg("Preparing processes for hibernation restore.\n");
Tejun Heo03afed82011-11-21 12:32:24 -0800910 error = freeze_processes();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200911 if (error)
912 goto Close_Finish;
Dexuan Cui2351f8d2020-04-23 20:40:16 -0700913
914 error = freeze_kernel_threads();
915 if (error) {
916 thaw_processes();
917 goto Close_Finish;
918 }
919
Chen Yufe12c002016-07-22 10:30:47 +0800920 error = load_image_and_restore();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200921 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700922 Finish:
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700923 __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100924 pm_restore_console();
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800925 pr_info("resume failed (%d)\n", error);
Domenico Andreoliab7e9b02020-05-07 09:19:52 +0200926 hibernate_release();
Shaohua Lidd5d6662005-09-03 15:57:04 -0700927 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700928 Unlock:
Pingfan Liu55f25032018-07-31 16:51:32 +0800929 mutex_unlock(&system_transition_mutex);
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200930 pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700931 return error;
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200932 Close_Finish:
Jiri Slaby76b57e62009-10-07 22:37:35 +0200933 swsusp_close(FMODE_READ);
934 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Russ Dilld3c345d2013-10-24 14:25:26 +0100937late_initcall_sync(software_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700940static const char * const hibernation_modes[] = {
941 [HIBERNATION_PLATFORM] = "platform",
942 [HIBERNATION_SHUTDOWN] = "shutdown",
943 [HIBERNATION_REBOOT] = "reboot",
Bojan Smojver62c552c2012-06-16 00:09:58 +0200944#ifdef CONFIG_SUSPEND
945 [HIBERNATION_SUSPEND] = "suspend",
946#endif
Chen Yufe12c002016-07-22 10:30:47 +0800947 [HIBERNATION_TEST_RESUME] = "test_resume",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948};
949
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200950/*
951 * /sys/power/disk - Control hibernation mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200953 * Hibernation can be handled in several ways. There are a few different ways
954 * to put the system into the sleep state: using the platform driver (e.g. ACPI
955 * or other hibernation_ops), powering it off or rebooting it (for testing
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100956 * mostly).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200958 * The sysfs file /sys/power/disk provides an interface for selecting the
959 * hibernation mode to use. Reading from this file causes the available modes
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100960 * to be printed. There are 3 modes that can be supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 * 'platform'
963 * 'shutdown'
964 * 'reboot'
965 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200966 * If a platform hibernation driver is in use, 'platform' will be supported
967 * and will be used by default. Otherwise, 'shutdown' will be used by default.
968 * The selected option (i.e. the one corresponding to the current value of
969 * hibernation_mode) is enclosed by a square bracket.
970 *
971 * To select a given hibernation mode it is necessary to write the mode's
972 * string representation (as returned by reading from /sys/power/disk) back
973 * into /sys/power/disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 */
975
Kay Sievers386f2752007-11-02 13:47:53 +0100976static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
977 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700979 int i;
980 char *start = buf;
981
Kees Cooka6e15a32014-06-13 13:30:35 -0700982 if (!hibernation_available())
983 return sprintf(buf, "[disabled]\n");
984
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700985 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
986 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700987 continue;
988 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700989 case HIBERNATION_SHUTDOWN:
990 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +0200991#ifdef CONFIG_SUSPEND
992 case HIBERNATION_SUSPEND:
993#endif
Chen Yufe12c002016-07-22 10:30:47 +0800994 case HIBERNATION_TEST_RESUME:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700995 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700996 case HIBERNATION_PLATFORM:
997 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700998 break;
999 /* not a valid mode, continue with loop */
1000 continue;
1001 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001002 if (i == hibernation_mode)
1003 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001004 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001005 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001006 }
1007 buf += sprintf(buf, "\n");
1008 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Kay Sievers386f2752007-11-02 13:47:53 +01001011static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
1012 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 int error = 0;
1015 int i;
1016 int len;
1017 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001018 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Kees Cooka6e15a32014-06-13 13:30:35 -07001020 if (!hibernation_available())
1021 return -EPERM;
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 p = memchr(buf, '\n', n);
1024 len = p ? p - buf : n;
1025
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001026 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001027 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -07001028 if (len == strlen(hibernation_modes[i])
1029 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 mode = i;
1031 break;
1032 }
1033 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001034 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001035 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001036 case HIBERNATION_SHUTDOWN:
1037 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +02001038#ifdef CONFIG_SUSPEND
1039 case HIBERNATION_SUSPEND:
1040#endif
Chen Yufe12c002016-07-22 10:30:47 +08001041 case HIBERNATION_TEST_RESUME:
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001042 hibernation_mode = mode;
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001043 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001044 case HIBERNATION_PLATFORM:
1045 if (hibernation_ops)
1046 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 else
1048 error = -EINVAL;
1049 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001050 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 error = -EINVAL;
1052
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001053 if (!error)
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +02001054 pm_pr_dbg("Hibernation mode set to '%s'\n",
1055 hibernation_modes[mode]);
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001056 unlock_system_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return error ? error : n;
1058}
1059
1060power_attr(disk);
1061
Kay Sievers386f2752007-11-02 13:47:53 +01001062static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1063 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
1066 MINOR(swsusp_resume_device));
1067}
1068
Kay Sievers386f2752007-11-02 13:47:53 +01001069static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1070 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 dev_t res;
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001073 int len = n;
1074 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001076 if (len && buf[len-1] == '\n')
1077 len--;
1078 name = kstrndup(buf, len, GFP_KERNEL);
1079 if (!name)
1080 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001082 res = name_to_dev_t(name);
1083 kfree(name);
1084 if (!res)
1085 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001087 lock_system_sleep();
Andrew Mortona5762192006-01-06 00:09:50 -08001088 swsusp_resume_device = res;
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001089 unlock_system_sleep();
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -08001090 pm_pr_dbg("Configured hibernation resume from disk to %u\n",
1091 swsusp_resume_device);
Andrew Mortona5762192006-01-06 00:09:50 -08001092 noresume = 0;
1093 software_resume();
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001094 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
1097power_attr(resume);
1098
Mario Limonciello35506462018-03-28 12:01:09 -05001099static ssize_t resume_offset_show(struct kobject *kobj,
1100 struct kobj_attribute *attr, char *buf)
1101{
1102 return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
1103}
1104
1105static ssize_t resume_offset_store(struct kobject *kobj,
1106 struct kobj_attribute *attr, const char *buf,
1107 size_t n)
1108{
1109 unsigned long long offset;
1110 int rc;
1111
1112 rc = kstrtoull(buf, 0, &offset);
1113 if (rc)
1114 return rc;
1115 swsusp_resume_block = offset;
1116
1117 return n;
1118}
1119
1120power_attr(resume_offset);
1121
Kay Sievers386f2752007-11-02 13:47:53 +01001122static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1123 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001124{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001125 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001126}
1127
Kay Sievers386f2752007-11-02 13:47:53 +01001128static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1129 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001130{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001131 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001132
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001133 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001134 image_size = size;
1135 return n;
1136 }
1137
1138 return -EINVAL;
1139}
1140
1141power_attr(image_size);
1142
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001143static ssize_t reserved_size_show(struct kobject *kobj,
1144 struct kobj_attribute *attr, char *buf)
1145{
1146 return sprintf(buf, "%lu\n", reserved_size);
1147}
1148
1149static ssize_t reserved_size_store(struct kobject *kobj,
1150 struct kobj_attribute *attr,
1151 const char *buf, size_t n)
1152{
1153 unsigned long size;
1154
1155 if (sscanf(buf, "%lu", &size) == 1) {
1156 reserved_size = size;
1157 return n;
1158 }
1159
1160 return -EINVAL;
1161}
1162
1163power_attr(reserved_size);
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165static struct attribute * g[] = {
1166 &disk_attr.attr,
Mario Limonciello35506462018-03-28 12:01:09 -05001167 &resume_offset_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001169 &image_size_attr.attr,
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001170 &reserved_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 NULL,
1172};
1173
1174
Arvind Yadav59494fe2017-06-29 16:58:40 +05301175static const struct attribute_group attr_group = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 .attrs = g,
1177};
1178
1179
1180static int __init pm_disk_init(void)
1181{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -08001182 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
1185core_initcall(pm_disk_init);
1186
1187
1188static int __init resume_setup(char *str)
1189{
1190 if (noresume)
1191 return 1;
1192
1193 strncpy( resume_file, str, 255 );
1194 return 1;
1195}
1196
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001197static int __init resume_offset_setup(char *str)
1198{
1199 unsigned long long offset;
1200
1201 if (noresume)
1202 return 1;
1203
1204 if (sscanf(str, "%llu", &offset) == 1)
1205 swsusp_resume_block = offset;
1206
1207 return 1;
1208}
1209
Bojan Smojverf996fc92010-09-09 23:06:23 +02001210static int __init hibernate_setup(char *str)
1211{
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001212 if (!strncmp(str, "noresume", 8)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001213 noresume = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001214 } else if (!strncmp(str, "nocompress", 10)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001215 nocompress = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001216 } else if (!strncmp(str, "no", 2)) {
Kees Cooka6e15a32014-06-13 13:30:35 -07001217 noresume = 1;
1218 nohibernate = 1;
Laura Abbott0f5bf6d2017-02-06 16:31:58 -08001219 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
Rafael J. Wysocki4c0b6c12016-07-10 02:12:10 +02001220 && !strncmp(str, "protect_image", 13)) {
1221 enable_restore_image_protection();
Kees Cooka6e15a32014-06-13 13:30:35 -07001222 }
Bojan Smojverf996fc92010-09-09 23:06:23 +02001223 return 1;
1224}
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226static int __init noresume_setup(char *str)
1227{
1228 noresume = 1;
1229 return 1;
1230}
1231
Barry Song6f8d7022011-10-06 20:34:46 +02001232static int __init resumewait_setup(char *str)
1233{
1234 resume_wait = 1;
1235 return 1;
1236}
1237
Barry Songf126f732011-10-10 23:38:41 +02001238static int __init resumedelay_setup(char *str)
1239{
Dan Carpenterf6514be2014-05-14 19:08:46 +03001240 int rc = kstrtouint(str, 0, &resume_delay);
Fabian Frederick317cf7e2014-05-09 23:32:08 +02001241
1242 if (rc)
1243 return rc;
Barry Songf126f732011-10-10 23:38:41 +02001244 return 1;
1245}
1246
Kees Cooka6e15a32014-06-13 13:30:35 -07001247static int __init nohibernate_setup(char *str)
1248{
1249 noresume = 1;
1250 nohibernate = 1;
1251 return 1;
1252}
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001255__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256__setup("resume=", resume_setup);
Bojan Smojverf996fc92010-09-09 23:06:23 +02001257__setup("hibernate=", hibernate_setup);
Barry Song6f8d7022011-10-06 20:34:46 +02001258__setup("resumewait", resumewait_setup);
Barry Songf126f732011-10-10 23:38:41 +02001259__setup("resumedelay=", resumedelay_setup);
Kees Cooka6e15a32014-06-13 13:30:35 -07001260__setup("nohibernate", nohibernate_setup);