blob: da0b419141779960541d743b42b7269e77cc66bf [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;
Vlastimil Babka03b6c9a2020-12-14 19:13:38 -0800329 clear_or_poison_free_pages();
Anisse Astier1ad14102016-09-09 10:43:32 +0200330 }
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. Silvadf561f662020-08-23 17:36:59 -0500662 fallthrough;
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{
Chen Yufe12c002016-07-22 10:30:47 +0800709 bool snapshot_test = false;
Peter Zijlstra70d93292020-08-18 15:57:36 +0200710 int error;
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();
Peter Zijlstra70d93292020-08-18 15:57:36 +0200726 error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION);
727 if (error)
728 goto Restore;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700729
Harry Panb5dee312019-02-25 20:36:41 +0800730 ksys_sync_helper();
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700731
Tejun Heo03afed82011-11-21 12:32:24 -0800732 error = freeze_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700733 if (error)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200734 goto Exit;
735
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200736 lock_device_hotplug();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200737 /* Allocate memory management structures */
738 error = create_basic_memory_bitmaps();
739 if (error)
740 goto Thaw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700742 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100743 if (error || freezer_test_done)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200744 goto Free_bitmaps;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200745
746 if (in_suspend) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700747 unsigned int flags = 0;
748
749 if (hibernation_mode == HIBERNATION_PLATFORM)
750 flags |= SF_PLATFORM_MODE;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200751 if (nocompress)
752 flags |= SF_NOCOMPRESS_MODE;
Bojan Smojver081a9d02011-10-13 23:58:07 +0200753 else
754 flags |= SF_CRC32_MODE;
755
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800756 pm_pr_dbg("Writing hibernation image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700757 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700758 swsusp_free();
Chen Yufe12c002016-07-22 10:30:47 +0800759 if (!error) {
760 if (hibernation_mode == HIBERNATION_TEST_RESUME)
761 snapshot_test = true;
762 else
763 power_down();
764 }
MyungJoo Ham5262a472010-11-26 23:07:56 +0100765 in_suspend = 0;
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100766 pm_restore_gfp_mask();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800767 } else {
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800768 pm_pr_dbg("Hibernation image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800769 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200770
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200771 Free_bitmaps:
772 free_basic_memory_bitmaps();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800773 Thaw:
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200774 unlock_device_hotplug();
Chen Yufe12c002016-07-22 10:30:47 +0800775 if (snapshot_test) {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200776 pm_pr_dbg("Checking hibernation image\n");
Chen Yufe12c002016-07-22 10:30:47 +0800777 error = swsusp_check();
778 if (!error)
779 error = load_image_and_restore();
780 }
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100781 thaw_processes();
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100782
783 /* Don't bother checking whether freezer_test_done is true */
784 freezer_test_done = false;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700785 Exit:
Peter Zijlstra70d93292020-08-18 15:57:36 +0200786 pm_notifier_call_chain(PM_POST_HIBERNATION);
787 Restore:
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100788 pm_restore_console();
Domenico Andreoliab7e9b02020-05-07 09:19:52 +0200789 hibernate_release();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700790 Unlock:
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100791 unlock_system_sleep();
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200792 pr_info("hibernation exit\n");
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return error;
795}
796
Dan Williams48001ea2020-07-20 15:08:18 -0700797/**
798 * hibernate_quiet_exec - Execute a function with all devices frozen.
799 * @func: Function to execute.
800 * @data: Data pointer to pass to @func.
801 *
802 * Return the @func return value or an error code if it cannot be executed.
803 */
804int hibernate_quiet_exec(int (*func)(void *data), void *data)
805{
Peter Zijlstra70d93292020-08-18 15:57:36 +0200806 int error;
Dan Williams48001ea2020-07-20 15:08:18 -0700807
808 lock_system_sleep();
809
810 if (!hibernate_acquire()) {
811 error = -EBUSY;
812 goto unlock;
813 }
814
815 pm_prepare_console();
816
Peter Zijlstra70d93292020-08-18 15:57:36 +0200817 error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION);
818 if (error)
819 goto restore;
Dan Williams48001ea2020-07-20 15:08:18 -0700820
821 error = freeze_processes();
822 if (error)
823 goto exit;
824
825 lock_device_hotplug();
826
827 pm_suspend_clear_flags();
828
829 error = platform_begin(true);
830 if (error)
831 goto thaw;
832
833 error = freeze_kernel_threads();
834 if (error)
835 goto thaw;
836
837 error = dpm_prepare(PMSG_FREEZE);
838 if (error)
839 goto dpm_complete;
840
841 suspend_console();
842
843 error = dpm_suspend(PMSG_FREEZE);
844 if (error)
845 goto dpm_resume;
846
847 error = dpm_suspend_end(PMSG_FREEZE);
848 if (error)
849 goto dpm_resume;
850
851 error = platform_pre_snapshot(true);
852 if (error)
853 goto skip;
854
855 error = func(data);
856
857skip:
858 platform_finish(true);
859
860 dpm_resume_start(PMSG_THAW);
861
862dpm_resume:
863 dpm_resume(PMSG_THAW);
864
865 resume_console();
866
867dpm_complete:
868 dpm_complete(PMSG_THAW);
869
870 thaw_kernel_threads();
871
872thaw:
873 platform_end(true);
874
875 unlock_device_hotplug();
876
877 thaw_processes();
878
879exit:
Peter Zijlstra70d93292020-08-18 15:57:36 +0200880 pm_notifier_call_chain(PM_POST_HIBERNATION);
Dan Williams48001ea2020-07-20 15:08:18 -0700881
Peter Zijlstra70d93292020-08-18 15:57:36 +0200882restore:
Dan Williams48001ea2020-07-20 15:08:18 -0700883 pm_restore_console();
884
885 hibernate_release();
886
887unlock:
888 unlock_system_sleep();
889
890 return error;
891}
892EXPORT_SYMBOL_GPL(hibernate_quiet_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200895 * software_resume - Resume from a saved hibernation image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200897 * This routine is called as a late initcall, when all devices have been
898 * discovered and initialized already.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200900 * The image reading code is called to see if there is a hibernation image
901 * available for reading. If that is the case, devices are quiesced and the
902 * contents of memory is restored from the saved image.
903 *
904 * If this is successful, control reappears in the restored target kernel in
Geliang Tangd439e642015-09-29 20:36:58 -0700905 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200906 * attempts to recover gracefully and make the kernel return to the normal mode
907 * of operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909static int software_resume(void)
910{
Peter Zijlstra70d93292020-08-18 15:57:36 +0200911 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Johannes Berg60a0d232007-11-14 17:00:16 -0800913 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100914 * If the user said "noresume".. bail out early.
915 */
Kees Cooka6e15a32014-06-13 13:30:35 -0700916 if (noresume || !hibernation_available())
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100917 return 0;
918
919 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800920 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
921 * is configured into the kernel. Since the regular hibernate
922 * trigger path is via sysfs which takes a buffer mutex before
Pingfan Liu55f25032018-07-31 16:51:32 +0800923 * calling hibernate functions (which take system_transition_mutex)
924 * this can cause lockdep to complain about a possible ABBA deadlock
Johannes Berg60a0d232007-11-14 17:00:16 -0800925 * which cannot happen since we're in the boot code here and
926 * sysfs can't be invoked yet. Therefore, we use a subclass
927 * here to avoid lockdep complaining.
928 */
Pingfan Liu55f25032018-07-31 16:51:32 +0800929 mutex_lock_nested(&system_transition_mutex, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200930
931 if (swsusp_resume_device)
932 goto Check_image;
933
934 if (!strlen(resume_file)) {
935 error = -ENOENT;
936 goto Unlock;
937 }
938
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200939 pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200940
Barry Songf126f732011-10-10 23:38:41 +0200941 if (resume_delay) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100942 pr_info("Waiting %dsec before reading resume device ...\n",
Barry Songf126f732011-10-10 23:38:41 +0200943 resume_delay);
944 ssleep(resume_delay);
945 }
946
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200947 /* Check if the device is there */
948 swsusp_resume_device = name_to_dev_t(resume_file);
Pavel Machek3efa1472005-07-07 17:56:43 -0700949 if (!swsusp_resume_device) {
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100950 /*
951 * Some device discovery might still be in progress; we need
952 * to wait for this to finish.
953 */
954 wait_for_device_probe();
Barry Song6f8d7022011-10-06 20:34:46 +0200955
956 if (resume_wait) {
957 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
958 msleep(10);
959 async_synchronize_full();
960 }
961
Pavel Machek3efa1472005-07-07 17:56:43 -0700962 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200963 if (!swsusp_resume_device) {
964 error = -ENODEV;
965 goto Unlock;
966 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700967 }
968
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200969 Check_image:
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200970 pm_pr_dbg("Hibernation image partition %d:%d present\n",
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200971 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200973 pm_pr_dbg("Looking for hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800974 error = swsusp_check();
975 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700976 goto Unlock;
977
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700978 /* The snapshot device should not be opened while we're running */
Domenico Andreoliab7e9b02020-05-07 09:19:52 +0200979 if (!hibernate_acquire()) {
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700980 error = -EBUSY;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200981 swsusp_close(FMODE_READ);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700982 goto Unlock;
983 }
984
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200985 pr_info("resume from hibernation\n");
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100986 pm_prepare_console();
Peter Zijlstra70d93292020-08-18 15:57:36 +0200987 error = pm_notifier_call_chain_robust(PM_RESTORE_PREPARE, PM_POST_RESTORE);
988 if (error)
989 goto Restore;
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700990
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800991 pm_pr_dbg("Preparing processes for hibernation restore.\n");
Tejun Heo03afed82011-11-21 12:32:24 -0800992 error = freeze_processes();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200993 if (error)
994 goto Close_Finish;
Dexuan Cui2351f8d2020-04-23 20:40:16 -0700995
996 error = freeze_kernel_threads();
997 if (error) {
998 thaw_processes();
999 goto Close_Finish;
1000 }
1001
Chen Yufe12c002016-07-22 10:30:47 +08001002 error = load_image_and_restore();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +02001003 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -07001004 Finish:
Peter Zijlstra70d93292020-08-18 15:57:36 +02001005 pm_notifier_call_chain(PM_POST_RESTORE);
1006 Restore:
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +01001007 pm_restore_console();
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -08001008 pr_info("resume failed (%d)\n", error);
Domenico Andreoliab7e9b02020-05-07 09:19:52 +02001009 hibernate_release();
Shaohua Lidd5d6662005-09-03 15:57:04 -07001010 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -07001011 Unlock:
Pingfan Liu55f25032018-07-31 16:51:32 +08001012 mutex_unlock(&system_transition_mutex);
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +02001013 pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -07001014 return error;
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +02001015 Close_Finish:
Jiri Slaby76b57e62009-10-07 22:37:35 +02001016 swsusp_close(FMODE_READ);
1017 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Russ Dilld3c345d2013-10-24 14:25:26 +01001020late_initcall_sync(software_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001023static const char * const hibernation_modes[] = {
1024 [HIBERNATION_PLATFORM] = "platform",
1025 [HIBERNATION_SHUTDOWN] = "shutdown",
1026 [HIBERNATION_REBOOT] = "reboot",
Bojan Smojver62c552c2012-06-16 00:09:58 +02001027#ifdef CONFIG_SUSPEND
1028 [HIBERNATION_SUSPEND] = "suspend",
1029#endif
Chen Yufe12c002016-07-22 10:30:47 +08001030 [HIBERNATION_TEST_RESUME] = "test_resume",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031};
1032
Rafael J. Wysockif42a9812011-05-24 23:36:06 +02001033/*
1034 * /sys/power/disk - Control hibernation mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +02001036 * Hibernation can be handled in several ways. There are a few different ways
1037 * to put the system into the sleep state: using the platform driver (e.g. ACPI
1038 * or other hibernation_ops), powering it off or rebooting it (for testing
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +01001039 * mostly).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +02001041 * The sysfs file /sys/power/disk provides an interface for selecting the
1042 * hibernation mode to use. Reading from this file causes the available modes
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +01001043 * to be printed. There are 3 modes that can be supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 * 'platform'
1046 * 'shutdown'
1047 * 'reboot'
1048 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +02001049 * If a platform hibernation driver is in use, 'platform' will be supported
1050 * and will be used by default. Otherwise, 'shutdown' will be used by default.
1051 * The selected option (i.e. the one corresponding to the current value of
1052 * hibernation_mode) is enclosed by a square bracket.
1053 *
1054 * To select a given hibernation mode it is necessary to write the mode's
1055 * string representation (as returned by reading from /sys/power/disk) back
1056 * into /sys/power/disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 */
1058
Kay Sievers386f2752007-11-02 13:47:53 +01001059static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
1060 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001062 int i;
1063 char *start = buf;
1064
Kees Cooka6e15a32014-06-13 13:30:35 -07001065 if (!hibernation_available())
1066 return sprintf(buf, "[disabled]\n");
1067
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001068 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
1069 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001070 continue;
1071 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001072 case HIBERNATION_SHUTDOWN:
1073 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +02001074#ifdef CONFIG_SUSPEND
1075 case HIBERNATION_SUSPEND:
1076#endif
Chen Yufe12c002016-07-22 10:30:47 +08001077 case HIBERNATION_TEST_RESUME:
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001078 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001079 case HIBERNATION_PLATFORM:
1080 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001081 break;
1082 /* not a valid mode, continue with loop */
1083 continue;
1084 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001085 if (i == hibernation_mode)
1086 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001087 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001088 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001089 }
1090 buf += sprintf(buf, "\n");
1091 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
Kay Sievers386f2752007-11-02 13:47:53 +01001094static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
1095 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 int error = 0;
1098 int i;
1099 int len;
1100 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001101 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Kees Cooka6e15a32014-06-13 13:30:35 -07001103 if (!hibernation_available())
1104 return -EPERM;
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 p = memchr(buf, '\n', n);
1107 len = p ? p - buf : n;
1108
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001109 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001110 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -07001111 if (len == strlen(hibernation_modes[i])
1112 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 mode = i;
1114 break;
1115 }
1116 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001117 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001118 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001119 case HIBERNATION_SHUTDOWN:
1120 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +02001121#ifdef CONFIG_SUSPEND
1122 case HIBERNATION_SUSPEND:
1123#endif
Chen Yufe12c002016-07-22 10:30:47 +08001124 case HIBERNATION_TEST_RESUME:
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001125 hibernation_mode = mode;
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001126 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001127 case HIBERNATION_PLATFORM:
1128 if (hibernation_ops)
1129 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 else
1131 error = -EINVAL;
1132 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001133 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 error = -EINVAL;
1135
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001136 if (!error)
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +02001137 pm_pr_dbg("Hibernation mode set to '%s'\n",
1138 hibernation_modes[mode]);
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001139 unlock_system_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return error ? error : n;
1141}
1142
1143power_attr(disk);
1144
Kay Sievers386f2752007-11-02 13:47:53 +01001145static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1146 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Xiang Chen6ada7ba2020-06-22 17:09:13 +08001148 return sprintf(buf, "%d:%d\n", MAJOR(swsusp_resume_device),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 MINOR(swsusp_resume_device));
1150}
1151
Kay Sievers386f2752007-11-02 13:47:53 +01001152static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1153 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 dev_t res;
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001156 int len = n;
1157 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001159 if (len && buf[len-1] == '\n')
1160 len--;
1161 name = kstrndup(buf, len, GFP_KERNEL);
1162 if (!name)
1163 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001165 res = name_to_dev_t(name);
1166 kfree(name);
1167 if (!res)
1168 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001170 lock_system_sleep();
Andrew Mortona5762192006-01-06 00:09:50 -08001171 swsusp_resume_device = res;
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001172 unlock_system_sleep();
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -08001173 pm_pr_dbg("Configured hibernation resume from disk to %u\n",
1174 swsusp_resume_device);
Andrew Mortona5762192006-01-06 00:09:50 -08001175 noresume = 0;
1176 software_resume();
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001177 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
1180power_attr(resume);
1181
Mario Limonciello35506462018-03-28 12:01:09 -05001182static ssize_t resume_offset_show(struct kobject *kobj,
1183 struct kobj_attribute *attr, char *buf)
1184{
1185 return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
1186}
1187
1188static ssize_t resume_offset_store(struct kobject *kobj,
1189 struct kobj_attribute *attr, const char *buf,
1190 size_t n)
1191{
1192 unsigned long long offset;
1193 int rc;
1194
1195 rc = kstrtoull(buf, 0, &offset);
1196 if (rc)
1197 return rc;
1198 swsusp_resume_block = offset;
1199
1200 return n;
1201}
1202
1203power_attr(resume_offset);
1204
Kay Sievers386f2752007-11-02 13:47:53 +01001205static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1206 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001207{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001208 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001209}
1210
Kay Sievers386f2752007-11-02 13:47:53 +01001211static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1212 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001213{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001214 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001215
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001216 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001217 image_size = size;
1218 return n;
1219 }
1220
1221 return -EINVAL;
1222}
1223
1224power_attr(image_size);
1225
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001226static ssize_t reserved_size_show(struct kobject *kobj,
1227 struct kobj_attribute *attr, char *buf)
1228{
1229 return sprintf(buf, "%lu\n", reserved_size);
1230}
1231
1232static ssize_t reserved_size_store(struct kobject *kobj,
1233 struct kobj_attribute *attr,
1234 const char *buf, size_t n)
1235{
1236 unsigned long size;
1237
1238 if (sscanf(buf, "%lu", &size) == 1) {
1239 reserved_size = size;
1240 return n;
1241 }
1242
1243 return -EINVAL;
1244}
1245
1246power_attr(reserved_size);
1247
Xiang Chen6ada7ba2020-06-22 17:09:13 +08001248static struct attribute *g[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 &disk_attr.attr,
Mario Limonciello35506462018-03-28 12:01:09 -05001250 &resume_offset_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001252 &image_size_attr.attr,
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001253 &reserved_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 NULL,
1255};
1256
1257
Arvind Yadav59494fe2017-06-29 16:58:40 +05301258static const struct attribute_group attr_group = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 .attrs = g,
1260};
1261
1262
1263static int __init pm_disk_init(void)
1264{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -08001265 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
1268core_initcall(pm_disk_init);
1269
1270
1271static int __init resume_setup(char *str)
1272{
1273 if (noresume)
1274 return 1;
1275
Xiang Chen6ada7ba2020-06-22 17:09:13 +08001276 strncpy(resume_file, str, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return 1;
1278}
1279
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001280static int __init resume_offset_setup(char *str)
1281{
1282 unsigned long long offset;
1283
1284 if (noresume)
1285 return 1;
1286
1287 if (sscanf(str, "%llu", &offset) == 1)
1288 swsusp_resume_block = offset;
1289
1290 return 1;
1291}
1292
Bojan Smojverf996fc92010-09-09 23:06:23 +02001293static int __init hibernate_setup(char *str)
1294{
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001295 if (!strncmp(str, "noresume", 8)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001296 noresume = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001297 } else if (!strncmp(str, "nocompress", 10)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001298 nocompress = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001299 } else if (!strncmp(str, "no", 2)) {
Kees Cooka6e15a32014-06-13 13:30:35 -07001300 noresume = 1;
1301 nohibernate = 1;
Laura Abbott0f5bf6d2017-02-06 16:31:58 -08001302 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
Rafael J. Wysocki4c0b6c12016-07-10 02:12:10 +02001303 && !strncmp(str, "protect_image", 13)) {
1304 enable_restore_image_protection();
Kees Cooka6e15a32014-06-13 13:30:35 -07001305 }
Bojan Smojverf996fc92010-09-09 23:06:23 +02001306 return 1;
1307}
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309static int __init noresume_setup(char *str)
1310{
1311 noresume = 1;
1312 return 1;
1313}
1314
Barry Song6f8d7022011-10-06 20:34:46 +02001315static int __init resumewait_setup(char *str)
1316{
1317 resume_wait = 1;
1318 return 1;
1319}
1320
Barry Songf126f732011-10-10 23:38:41 +02001321static int __init resumedelay_setup(char *str)
1322{
Dan Carpenterf6514be2014-05-14 19:08:46 +03001323 int rc = kstrtouint(str, 0, &resume_delay);
Fabian Frederick317cf7e2014-05-09 23:32:08 +02001324
1325 if (rc)
1326 return rc;
Barry Songf126f732011-10-10 23:38:41 +02001327 return 1;
1328}
1329
Kees Cooka6e15a32014-06-13 13:30:35 -07001330static int __init nohibernate_setup(char *str)
1331{
1332 noresume = 1;
1333 nohibernate = 1;
1334 return 1;
1335}
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001338__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339__setup("resume=", resume_setup);
Bojan Smojverf996fc92010-09-09 23:06:23 +02001340__setup("hibernate=", hibernate_setup);
Barry Song6f8d7022011-10-06 20:34:46 +02001341__setup("resumewait", resumewait_setup);
Barry Songf126f732011-10-10 23:38:41 +02001342__setup("resumedelay=", resumedelay_setup);
Kees Cooka6e15a32014-06-13 13:30:35 -07001343__setup("nohibernate", nohibernate_setup);