blob: 1dee70815f3cd6a3320271bf6b1fa6550399832d [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. 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);
Minho Ban2df83fa2012-05-14 21:45:31 +0200949
950 /*
951 * name_to_dev_t is ineffective to verify parition if resume_file is in
952 * integer format. (e.g. major:minor)
953 */
954 if (isdigit(resume_file[0]) && resume_wait) {
955 int partno;
956 while (!get_gendisk(swsusp_resume_device, &partno))
957 msleep(10);
958 }
959
Pavel Machek3efa1472005-07-07 17:56:43 -0700960 if (!swsusp_resume_device) {
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100961 /*
962 * Some device discovery might still be in progress; we need
963 * to wait for this to finish.
964 */
965 wait_for_device_probe();
Barry Song6f8d7022011-10-06 20:34:46 +0200966
967 if (resume_wait) {
968 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
969 msleep(10);
970 async_synchronize_full();
971 }
972
Pavel Machek3efa1472005-07-07 17:56:43 -0700973 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200974 if (!swsusp_resume_device) {
975 error = -ENODEV;
976 goto Unlock;
977 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700978 }
979
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200980 Check_image:
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200981 pm_pr_dbg("Hibernation image partition %d:%d present\n",
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200982 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200984 pm_pr_dbg("Looking for hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800985 error = swsusp_check();
986 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700987 goto Unlock;
988
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700989 /* The snapshot device should not be opened while we're running */
Domenico Andreoliab7e9b02020-05-07 09:19:52 +0200990 if (!hibernate_acquire()) {
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700991 error = -EBUSY;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200992 swsusp_close(FMODE_READ);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700993 goto Unlock;
994 }
995
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200996 pr_info("resume from hibernation\n");
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100997 pm_prepare_console();
Peter Zijlstra70d93292020-08-18 15:57:36 +0200998 error = pm_notifier_call_chain_robust(PM_RESTORE_PREPARE, PM_POST_RESTORE);
999 if (error)
1000 goto Restore;
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -07001001
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -08001002 pm_pr_dbg("Preparing processes for hibernation restore.\n");
Tejun Heo03afed82011-11-21 12:32:24 -08001003 error = freeze_processes();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +02001004 if (error)
1005 goto Close_Finish;
Dexuan Cui2351f8d2020-04-23 20:40:16 -07001006
1007 error = freeze_kernel_threads();
1008 if (error) {
1009 thaw_processes();
1010 goto Close_Finish;
1011 }
1012
Chen Yufe12c002016-07-22 10:30:47 +08001013 error = load_image_and_restore();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +02001014 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -07001015 Finish:
Peter Zijlstra70d93292020-08-18 15:57:36 +02001016 pm_notifier_call_chain(PM_POST_RESTORE);
1017 Restore:
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +01001018 pm_restore_console();
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -08001019 pr_info("resume failed (%d)\n", error);
Domenico Andreoliab7e9b02020-05-07 09:19:52 +02001020 hibernate_release();
Shaohua Lidd5d6662005-09-03 15:57:04 -07001021 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -07001022 Unlock:
Pingfan Liu55f25032018-07-31 16:51:32 +08001023 mutex_unlock(&system_transition_mutex);
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +02001024 pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -07001025 return error;
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +02001026 Close_Finish:
Jiri Slaby76b57e62009-10-07 22:37:35 +02001027 swsusp_close(FMODE_READ);
1028 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
Russ Dilld3c345d2013-10-24 14:25:26 +01001031late_initcall_sync(software_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001034static const char * const hibernation_modes[] = {
1035 [HIBERNATION_PLATFORM] = "platform",
1036 [HIBERNATION_SHUTDOWN] = "shutdown",
1037 [HIBERNATION_REBOOT] = "reboot",
Bojan Smojver62c552c2012-06-16 00:09:58 +02001038#ifdef CONFIG_SUSPEND
1039 [HIBERNATION_SUSPEND] = "suspend",
1040#endif
Chen Yufe12c002016-07-22 10:30:47 +08001041 [HIBERNATION_TEST_RESUME] = "test_resume",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042};
1043
Rafael J. Wysockif42a9812011-05-24 23:36:06 +02001044/*
1045 * /sys/power/disk - Control hibernation mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +02001047 * Hibernation can be handled in several ways. There are a few different ways
1048 * to put the system into the sleep state: using the platform driver (e.g. ACPI
1049 * or other hibernation_ops), powering it off or rebooting it (for testing
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +01001050 * mostly).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +02001052 * The sysfs file /sys/power/disk provides an interface for selecting the
1053 * hibernation mode to use. Reading from this file causes the available modes
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +01001054 * to be printed. There are 3 modes that can be supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 * 'platform'
1057 * 'shutdown'
1058 * 'reboot'
1059 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +02001060 * If a platform hibernation driver is in use, 'platform' will be supported
1061 * and will be used by default. Otherwise, 'shutdown' will be used by default.
1062 * The selected option (i.e. the one corresponding to the current value of
1063 * hibernation_mode) is enclosed by a square bracket.
1064 *
1065 * To select a given hibernation mode it is necessary to write the mode's
1066 * string representation (as returned by reading from /sys/power/disk) back
1067 * into /sys/power/disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 */
1069
Kay Sievers386f2752007-11-02 13:47:53 +01001070static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
1071 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001073 int i;
1074 char *start = buf;
1075
Kees Cooka6e15a32014-06-13 13:30:35 -07001076 if (!hibernation_available())
1077 return sprintf(buf, "[disabled]\n");
1078
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001079 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
1080 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001081 continue;
1082 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001083 case HIBERNATION_SHUTDOWN:
1084 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +02001085#ifdef CONFIG_SUSPEND
1086 case HIBERNATION_SUSPEND:
1087#endif
Chen Yufe12c002016-07-22 10:30:47 +08001088 case HIBERNATION_TEST_RESUME:
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001089 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001090 case HIBERNATION_PLATFORM:
1091 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001092 break;
1093 /* not a valid mode, continue with loop */
1094 continue;
1095 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001096 if (i == hibernation_mode)
1097 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001098 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001099 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001100 }
1101 buf += sprintf(buf, "\n");
1102 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
Kay Sievers386f2752007-11-02 13:47:53 +01001105static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
1106 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
1108 int error = 0;
1109 int i;
1110 int len;
1111 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001112 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Kees Cooka6e15a32014-06-13 13:30:35 -07001114 if (!hibernation_available())
1115 return -EPERM;
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 p = memchr(buf, '\n', n);
1118 len = p ? p - buf : n;
1119
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001120 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001121 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -07001122 if (len == strlen(hibernation_modes[i])
1123 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 mode = i;
1125 break;
1126 }
1127 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001128 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001129 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001130 case HIBERNATION_SHUTDOWN:
1131 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +02001132#ifdef CONFIG_SUSPEND
1133 case HIBERNATION_SUSPEND:
1134#endif
Chen Yufe12c002016-07-22 10:30:47 +08001135 case HIBERNATION_TEST_RESUME:
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001136 hibernation_mode = mode;
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001137 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001138 case HIBERNATION_PLATFORM:
1139 if (hibernation_ops)
1140 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 else
1142 error = -EINVAL;
1143 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001144 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 error = -EINVAL;
1146
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001147 if (!error)
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +02001148 pm_pr_dbg("Hibernation mode set to '%s'\n",
1149 hibernation_modes[mode]);
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001150 unlock_system_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return error ? error : n;
1152}
1153
1154power_attr(disk);
1155
Kay Sievers386f2752007-11-02 13:47:53 +01001156static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1157 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Xiang Chen6ada7ba2020-06-22 17:09:13 +08001159 return sprintf(buf, "%d:%d\n", MAJOR(swsusp_resume_device),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 MINOR(swsusp_resume_device));
1161}
1162
Kay Sievers386f2752007-11-02 13:47:53 +01001163static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1164 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 dev_t res;
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001167 int len = n;
1168 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001170 if (len && buf[len-1] == '\n')
1171 len--;
1172 name = kstrndup(buf, len, GFP_KERNEL);
1173 if (!name)
1174 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001176 res = name_to_dev_t(name);
1177 kfree(name);
1178 if (!res)
1179 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001181 lock_system_sleep();
Andrew Mortona5762192006-01-06 00:09:50 -08001182 swsusp_resume_device = res;
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001183 unlock_system_sleep();
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -08001184 pm_pr_dbg("Configured hibernation resume from disk to %u\n",
1185 swsusp_resume_device);
Andrew Mortona5762192006-01-06 00:09:50 -08001186 noresume = 0;
1187 software_resume();
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001188 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191power_attr(resume);
1192
Mario Limonciello35506462018-03-28 12:01:09 -05001193static ssize_t resume_offset_show(struct kobject *kobj,
1194 struct kobj_attribute *attr, char *buf)
1195{
1196 return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
1197}
1198
1199static ssize_t resume_offset_store(struct kobject *kobj,
1200 struct kobj_attribute *attr, const char *buf,
1201 size_t n)
1202{
1203 unsigned long long offset;
1204 int rc;
1205
1206 rc = kstrtoull(buf, 0, &offset);
1207 if (rc)
1208 return rc;
1209 swsusp_resume_block = offset;
1210
1211 return n;
1212}
1213
1214power_attr(resume_offset);
1215
Kay Sievers386f2752007-11-02 13:47:53 +01001216static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1217 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001218{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001219 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001220}
1221
Kay Sievers386f2752007-11-02 13:47:53 +01001222static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1223 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001224{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001225 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001226
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001227 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001228 image_size = size;
1229 return n;
1230 }
1231
1232 return -EINVAL;
1233}
1234
1235power_attr(image_size);
1236
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001237static ssize_t reserved_size_show(struct kobject *kobj,
1238 struct kobj_attribute *attr, char *buf)
1239{
1240 return sprintf(buf, "%lu\n", reserved_size);
1241}
1242
1243static ssize_t reserved_size_store(struct kobject *kobj,
1244 struct kobj_attribute *attr,
1245 const char *buf, size_t n)
1246{
1247 unsigned long size;
1248
1249 if (sscanf(buf, "%lu", &size) == 1) {
1250 reserved_size = size;
1251 return n;
1252 }
1253
1254 return -EINVAL;
1255}
1256
1257power_attr(reserved_size);
1258
Xiang Chen6ada7ba2020-06-22 17:09:13 +08001259static struct attribute *g[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 &disk_attr.attr,
Mario Limonciello35506462018-03-28 12:01:09 -05001261 &resume_offset_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001263 &image_size_attr.attr,
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001264 &reserved_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 NULL,
1266};
1267
1268
Arvind Yadav59494fe2017-06-29 16:58:40 +05301269static const struct attribute_group attr_group = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 .attrs = g,
1271};
1272
1273
1274static int __init pm_disk_init(void)
1275{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -08001276 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
1279core_initcall(pm_disk_init);
1280
1281
1282static int __init resume_setup(char *str)
1283{
1284 if (noresume)
1285 return 1;
1286
Xiang Chen6ada7ba2020-06-22 17:09:13 +08001287 strncpy(resume_file, str, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return 1;
1289}
1290
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001291static int __init resume_offset_setup(char *str)
1292{
1293 unsigned long long offset;
1294
1295 if (noresume)
1296 return 1;
1297
1298 if (sscanf(str, "%llu", &offset) == 1)
1299 swsusp_resume_block = offset;
1300
1301 return 1;
1302}
1303
Bojan Smojverf996fc92010-09-09 23:06:23 +02001304static int __init hibernate_setup(char *str)
1305{
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001306 if (!strncmp(str, "noresume", 8)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001307 noresume = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001308 } else if (!strncmp(str, "nocompress", 10)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001309 nocompress = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001310 } else if (!strncmp(str, "no", 2)) {
Kees Cooka6e15a32014-06-13 13:30:35 -07001311 noresume = 1;
1312 nohibernate = 1;
Laura Abbott0f5bf6d2017-02-06 16:31:58 -08001313 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
Rafael J. Wysocki4c0b6c12016-07-10 02:12:10 +02001314 && !strncmp(str, "protect_image", 13)) {
1315 enable_restore_image_protection();
Kees Cooka6e15a32014-06-13 13:30:35 -07001316 }
Bojan Smojverf996fc92010-09-09 23:06:23 +02001317 return 1;
1318}
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320static int __init noresume_setup(char *str)
1321{
1322 noresume = 1;
1323 return 1;
1324}
1325
Barry Song6f8d7022011-10-06 20:34:46 +02001326static int __init resumewait_setup(char *str)
1327{
1328 resume_wait = 1;
1329 return 1;
1330}
1331
Barry Songf126f732011-10-10 23:38:41 +02001332static int __init resumedelay_setup(char *str)
1333{
Dan Carpenterf6514be2014-05-14 19:08:46 +03001334 int rc = kstrtouint(str, 0, &resume_delay);
Fabian Frederick317cf7e2014-05-09 23:32:08 +02001335
1336 if (rc)
1337 return rc;
Barry Songf126f732011-10-10 23:38:41 +02001338 return 1;
1339}
1340
Kees Cooka6e15a32014-06-13 13:30:35 -07001341static int __init nohibernate_setup(char *str)
1342{
1343 noresume = 1;
1344 nohibernate = 1;
1345 return 1;
1346}
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001349__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350__setup("resume=", resume_setup);
Bojan Smojverf996fc92010-09-09 23:06:23 +02001351__setup("hibernate=", hibernate_setup);
Barry Song6f8d7022011-10-06 20:34:46 +02001352__setup("resumewait", resumewait_setup);
Barry Songf126f732011-10-10 23:38:41 +02001353__setup("resumedelay=", resumedelay_setup);
Kees Cooka6e15a32014-06-13 13:30:35 -07001354__setup("nohibernate", nohibernate_setup);