blob: 2b4a946a6ff5cfcae69fd9e03ee1d5cc0728736e [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
Christoph Hellwigcf056a42023-05-31 14:55:24 +020014#include <linux/blkdev.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040015#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/suspend.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/reboot.h>
18#include <linux/string.h>
19#include <linux/device.h>
Barry Song6f8d7022011-10-06 20:34:46 +020020#include <linux/async.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/delay.h>
22#include <linux/fs.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070023#include <linux/mount.h>
Eric W. Biederman88d10bb2005-09-22 21:43:46 -070024#include <linux/pm.h>
Ingo Molnar38b8d202017-02-08 18:51:31 +010025#include <linux/nmi.h>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070026#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070027#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080028#include <linux/freezer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/gfp.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010030#include <linux/syscore_ops.h>
Minho Ban2df83fa2012-05-14 21:45:31 +020031#include <linux/ctype.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>
Mike Rapoport9a436f82021-07-07 18:08:07 -070034#include <linux/secretmem.h>
Todd E Brandtbb3632c2014-06-06 05:40:17 -070035#include <trace/events/power.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "power.h"
38
39
Barry Songd231ff12011-10-11 23:29:18 -070040static int nocompress;
41static int noresume;
Kees Cooka6e15a32014-06-13 13:30:35 -070042static int nohibernate;
Barry Songd231ff12011-10-11 23:29:18 -070043static int resume_wait;
Dan Carpenterf6514be2014-05-14 19:08:46 +030044static unsigned int resume_delay;
Adrian Bunk47a460d2008-02-04 22:30:06 -080045static char resume_file[256] = CONFIG_PM_STD_PARTITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080047sector_t swsusp_resume_block;
Andi Kleend6efc2f2013-08-05 15:02:49 -070048__visible int in_suspend __nosavedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070050enum {
51 HIBERNATION_INVALID,
52 HIBERNATION_PLATFORM,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070053 HIBERNATION_SHUTDOWN,
54 HIBERNATION_REBOOT,
Bojan Smojver62c552c2012-06-16 00:09:58 +020055#ifdef CONFIG_SUSPEND
56 HIBERNATION_SUSPEND,
57#endif
Chen Yufe12c002016-07-22 10:30:47 +080058 HIBERNATION_TEST_RESUME,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070059 /* keep last */
60 __HIBERNATION_AFTER_LAST
61};
62#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
63#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
64
65static int hibernation_mode = HIBERNATION_SHUTDOWN;
66
Srivatsa S. Bhat97819a22011-12-01 22:33:10 +010067bool freezer_test_done;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +010068
Lionel Debroux073ef1f2010-11-09 21:48:49 +010069static const struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070070
Domenico Andreoliab7e9b02020-05-07 09:19:52 +020071static atomic_t hibernate_atomic = ATOMIC_INIT(1);
72
73bool hibernate_acquire(void)
74{
75 return atomic_add_unless(&hibernate_atomic, -1, 0);
76}
77
78void hibernate_release(void)
79{
80 atomic_inc(&hibernate_atomic);
81}
82
Kees Cooka6e15a32014-06-13 13:30:35 -070083bool hibernation_available(void)
84{
Mike Rapoport9a436f82021-07-07 18:08:07 -070085 return nohibernate == 0 &&
86 !security_locked_down(LOCKDOWN_HIBERNATION) &&
Dan Williams9ea4dcf2022-04-22 15:58:11 -070087 !secretmem_active() && !cxl_mem_active();
Kees Cooka6e15a32014-06-13 13:30:35 -070088}
89
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070090/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +020091 * hibernation_set_ops - Set the global hibernate operations.
92 * @ops: Hibernation operations to use in subsequent hibernation transitions.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070093 */
Lionel Debroux073ef1f2010-11-09 21:48:49 +010094void hibernation_set_ops(const struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070095{
Peter Zijlstra5950e5d2022-08-22 13:18:17 +020096 unsigned int sleep_flags;
97
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +010098 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
99 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
MyungJoo Ham5729c632010-11-26 23:07:48 +0100100 && ops->restore_cleanup && ops->leave)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700101 WARN_ON(1);
102 return;
103 }
Peter Zijlstra5950e5d2022-08-22 13:18:17 +0200104
105 sleep_flags = lock_system_sleep();
106
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700107 hibernation_ops = ops;
108 if (ops)
109 hibernation_mode = HIBERNATION_PLATFORM;
110 else if (hibernation_mode == HIBERNATION_PLATFORM)
111 hibernation_mode = HIBERNATION_SHUTDOWN;
112
Peter Zijlstra5950e5d2022-08-22 13:18:17 +0200113 unlock_system_sleep(sleep_flags);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700114}
Leonardo Potenzae0c78552013-11-19 12:27:41 +0000115EXPORT_SYMBOL_GPL(hibernation_set_ops);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700116
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100117static bool entering_platform_hibernation;
118
119bool system_entering_hibernation(void)
120{
121 return entering_platform_hibernation;
122}
123EXPORT_SYMBOL(system_entering_hibernation);
124
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100125#ifdef CONFIG_PM_DEBUG
126static void hibernation_debug_sleep(void)
127{
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800128 pr_info("debug: Waiting for 5 seconds.\n");
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100129 mdelay(5000);
130}
131
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100132static int hibernation_test(int level)
133{
134 if (pm_test_level == level) {
135 hibernation_debug_sleep();
136 return 1;
137 }
138 return 0;
139}
140#else /* !CONFIG_PM_DEBUG */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100141static int hibernation_test(int level) { return 0; }
142#endif /* !CONFIG_PM_DEBUG */
143
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700144/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200145 * platform_begin - Call platform to start hibernation.
146 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700147 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100148static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700149{
150 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockibb186902019-05-16 12:43:19 +0200151 hibernation_ops->begin(PMSG_FREEZE) : 0;
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100152}
153
154/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200155 * platform_end - Call platform to finish transition to the working state.
156 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100157 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100158static void platform_end(int platform_mode)
159{
160 if (platform_mode && hibernation_ops)
161 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700162}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200165 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
166 * @platform_mode: Whether or not to use the platform driver.
167 *
168 * Use the platform driver to prepare the system for creating a hibernate image,
169 * if so configured, and return an error code if that fails.
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800170 */
171
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700172static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800173{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700174 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700175 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800176}
177
178/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200179 * platform_leave - Call platform to prepare a transition to the working state.
180 * @platform_mode: Whether or not to use the platform driver.
181 *
182 * Use the platform driver prepare to prepare the machine for switching to the
183 * normal mode of operation.
184 *
185 * This routine is called on one CPU with interrupts disabled.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700186 */
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700187static void platform_leave(int platform_mode)
188{
189 if (platform_mode && hibernation_ops)
190 hibernation_ops->leave();
191}
192
193/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200194 * platform_finish - Call platform to switch the system to the working state.
195 * @platform_mode: Whether or not to use the platform driver.
196 *
197 * Use the platform driver to switch the machine to the normal mode of
198 * operation.
199 *
200 * This routine must be called after platform_prepare().
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700201 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700202static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700203{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700204 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700205 hibernation_ops->finish();
206}
207
208/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200209 * platform_pre_restore - Prepare for hibernate image restoration.
210 * @platform_mode: Whether or not to use the platform driver.
211 *
212 * Use the platform driver to prepare the system for resume from a hibernation
213 * image.
214 *
215 * If the restore fails after this function has been called,
216 * platform_restore_cleanup() must be called.
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700217 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700218static int platform_pre_restore(int platform_mode)
219{
220 return (platform_mode && hibernation_ops) ?
221 hibernation_ops->pre_restore() : 0;
222}
223
224/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200225 * platform_restore_cleanup - Switch to the working state after failing restore.
226 * @platform_mode: Whether or not to use the platform driver.
227 *
228 * Use the platform driver to switch the system to the normal mode of operation
229 * after a failing restore.
230 *
231 * If platform_pre_restore() has been called before the failing restore, this
232 * function must be called too, regardless of the result of
233 * platform_pre_restore().
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700234 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700235static void platform_restore_cleanup(int platform_mode)
236{
237 if (platform_mode && hibernation_ops)
238 hibernation_ops->restore_cleanup();
239}
240
241/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200242 * platform_recover - Recover from a failure to suspend devices.
243 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200244 */
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200245static void platform_recover(int platform_mode)
246{
247 if (platform_mode && hibernation_ops && hibernation_ops->recover)
248 hibernation_ops->recover();
249}
250
251/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200252 * swsusp_show_speed - Print time elapsed between two events during hibernation.
253 * @start: Starting event.
254 * @stop: Final event.
255 * @nr_pages: Number of memory pages processed between @start and @stop.
256 * @msg: Additional diagnostic message to print.
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100257 */
Tina Ruchandanidb597602014-10-30 11:04:53 -0700258void swsusp_show_speed(ktime_t start, ktime_t stop,
259 unsigned nr_pages, char *msg)
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100260{
Tina Ruchandanidb597602014-10-30 11:04:53 -0700261 ktime_t diff;
Chen Gang4881f602014-04-25 08:44:59 +0800262 u64 elapsed_centisecs64;
263 unsigned int centisecs;
264 unsigned int k;
265 unsigned int kps;
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100266
Tina Ruchandanidb597602014-10-30 11:04:53 -0700267 diff = ktime_sub(stop, start);
268 elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC);
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100269 centisecs = elapsed_centisecs64;
270 if (centisecs == 0)
271 centisecs = 1; /* avoid div-by-zero */
272 k = nr_pages * (PAGE_SIZE / 1024);
273 kps = (k * 100) / centisecs;
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100274 pr_info("%s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
275 msg, k, centisecs / 100, centisecs % 100, kps / 1000,
276 (kps % 1000) / 10);
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100277}
278
Jiri Kosinaec527c32019-05-30 00:09:39 +0200279__weak int arch_resume_nosmt(void)
280{
281 return 0;
282}
283
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100284/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200285 * create_image - Create a hibernation image.
286 * @platform_mode: Whether or not to use the platform driver.
287 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100288 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
289 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200290 *
291 * Control reappears in this routine after the subsequent restore.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700292 */
Adrian Bunk47a460d2008-02-04 22:30:06 -0800293static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700294{
295 int error;
296
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100297 error = dpm_suspend_end(PMSG_FREEZE);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700298 if (error) {
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800299 pr_err("Some devices failed to power down, aborting\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200300 return error;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700301 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100302
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100303 error = platform_pre_snapshot(platform_mode);
304 if (error || hibernation_test(TEST_PLATFORM))
305 goto Platform_finish;
306
Rafael J. Wysocki23f62d72021-10-22 18:07:47 +0200307 error = pm_sleep_disable_secondary_cpus();
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100308 if (error || hibernation_test(TEST_CPUS))
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100309 goto Enable_cpus;
310
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100311 local_irq_disable();
312
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200313 system_state = SYSTEM_SUSPEND;
314
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200315 error = syscore_suspend();
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100316 if (error) {
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800317 pr_err("Some system devices failed to power down, aborting\n");
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100318 goto Enable_irqs;
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100319 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700320
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100321 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100322 goto Power_up;
323
324 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700325 save_processor_state();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700326 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700327 error = swsusp_arch_suspend();
Thomas Garnier62822e22016-08-11 14:49:29 -0700328 /* Restore control flow magically appears here */
329 restore_processor_state();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700330 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700331 if (error)
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800332 pr_err("Error %d creating image\n", error);
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100333
Anisse Astier1ad14102016-09-09 10:43:32 +0200334 if (!in_suspend) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200335 events_check_enabled = false;
Vlastimil Babka03b6c9a2020-12-14 19:13:38 -0800336 clear_or_poison_free_pages();
Anisse Astier1ad14102016-09-09 10:43:32 +0200337 }
Bjørn Mork362e77d2013-12-04 16:06:58 +0100338
339 platform_leave(platform_mode);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100340
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100341 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100342 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100343
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100344 Enable_irqs:
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200345 system_state = SYSTEM_RUNNING;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100346 local_irq_enable();
347
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100348 Enable_cpus:
Rafael J. Wysocki23f62d72021-10-22 18:07:47 +0200349 pm_sleep_enable_secondary_cpus();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100350
Jiri Kosinaec527c32019-05-30 00:09:39 +0200351 /* Allow architectures to do nosmt-specific post-resume dances */
352 if (!in_suspend)
353 error = arch_resume_nosmt();
354
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100355 Platform_finish:
356 platform_finish(platform_mode);
357
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100358 dpm_resume_start(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200359 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100360
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700361 return error;
362}
363
364/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200365 * hibernation_snapshot - Quiesce devices and create a hibernation image.
366 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700367 *
Pingfan Liu55f25032018-07-31 16:51:32 +0800368 * This routine must be called with system_transition_mutex held.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700369 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700370int hibernation_snapshot(int platform_mode)
371{
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100372 pm_message_t msg;
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100373 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700374
Lukas Wunner27614272016-03-23 00:11:20 +0100375 pm_suspend_clear_flags();
Zhang Rui3fe03132008-10-26 20:50:26 +0100376 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700377 if (error)
Rafael J. Wysockid074ee02010-07-07 23:43:35 +0200378 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700379
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200380 /* Preallocate image memory before shutting down devices. */
381 error = hibernate_preallocate_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700382 if (error)
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200383 goto Close;
384
385 error = freeze_kernel_threads();
386 if (error)
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100387 goto Cleanup;
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200388
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100389 if (hibernation_test(TEST_FREEZER)) {
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100390
391 /*
392 * Indicate to the caller that we are returning due to a
393 * successful freezer test.
394 */
395 freezer_test_done = true;
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100396 goto Thaw;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100397 }
398
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200399 error = dpm_prepare(PMSG_FREEZE);
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100400 if (error) {
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100401 dpm_complete(PMSG_RECOVER);
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100402 goto Thaw;
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100403 }
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700404
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700405 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100406 pm_restrict_gfp_mask();
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100407
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200408 error = dpm_suspend(PMSG_FREEZE);
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700409
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100410 if (error || hibernation_test(TEST_DEVICES))
411 platform_recover(platform_mode);
412 else
413 error = create_image(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700414
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100415 /*
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100416 * In the case that we call create_image() above, the control
417 * returns here (1) after the image has been created or the
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100418 * image creation has failed and (2) after a successful restore.
419 */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100420
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200421 /* We may need to release the preallocated image pages here. */
422 if (error || !in_suspend)
423 swsusp_free();
424
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200425 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
426 dpm_resume(msg);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100427
428 if (error || !in_suspend)
429 pm_restore_gfp_mask();
430
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700431 resume_console();
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200432 dpm_complete(msg);
433
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100434 Close:
435 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700436 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200437
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100438 Thaw:
439 thaw_kernel_threads();
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100440 Cleanup:
441 swsusp_free();
442 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700443}
444
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200445int __weak hibernate_resume_nonboot_cpu_disable(void)
446{
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000447 return suspend_disable_secondary_cpus();
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200448}
449
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700450/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200451 * resume_target_kernel - Restore system state from a hibernation image.
452 * @platform_mode: Whether or not to use the platform driver.
453 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100454 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
455 * contents of highmem that have not been restored yet from the image and run
456 * the low-level code that will restore the remaining contents of memory and
457 * switch to the just restored target kernel.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100458 */
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100459static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100460{
461 int error;
462
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100463 error = dpm_suspend_end(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100464 if (error) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100465 pr_err("Some devices failed to power down, aborting resume\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200466 return error;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100467 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100468
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100469 error = platform_pre_restore(platform_mode);
470 if (error)
471 goto Cleanup;
472
Rafael J. Wysocki23f62d72021-10-22 18:07:47 +0200473 cpuidle_pause();
474
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200475 error = hibernate_resume_nonboot_cpu_disable();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100476 if (error)
477 goto Enable_cpus;
478
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100479 local_irq_disable();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200480 system_state = SYSTEM_SUSPEND;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100481
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200482 error = syscore_suspend();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100483 if (error)
484 goto Enable_irqs;
485
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100486 save_processor_state();
487 error = restore_highmem();
488 if (!error) {
489 error = swsusp_arch_resume();
490 /*
491 * The code below is only ever reached in case of a failure.
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200492 * Otherwise, execution continues at the place where
493 * swsusp_arch_suspend() was called.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100494 */
495 BUG_ON(!error);
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200496 /*
497 * This call to restore_highmem() reverts the changes made by
498 * the previous one.
499 */
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100500 restore_highmem();
501 }
502 /*
503 * The only reason why swsusp_arch_resume() can fail is memory being
504 * very tight, so we have to free it as soon as we can to avoid
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200505 * subsequent failures.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100506 */
507 swsusp_free();
508 restore_processor_state();
509 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100510
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100511 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100512
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100513 Enable_irqs:
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200514 system_state = SYSTEM_RUNNING;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100515 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100516
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100517 Enable_cpus:
Rafael J. Wysocki23f62d72021-10-22 18:07:47 +0200518 pm_sleep_enable_secondary_cpus();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100519
520 Cleanup:
521 platform_restore_cleanup(platform_mode);
522
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100523 dpm_resume_start(PMSG_RECOVER);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100524
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100525 return error;
526}
527
528/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200529 * hibernation_restore - Quiesce devices and restore from a hibernation image.
530 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700531 *
Pingfan Liu55f25032018-07-31 16:51:32 +0800532 * This routine must be called with system_transition_mutex held. If it is
533 * successful, control reappears in the restored target kernel in
534 * hibernation_snapshot().
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700535 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700536int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700537{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100538 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700539
540 pm_prepare_console();
541 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100542 pm_restrict_gfp_mask();
Alan Sternd1616302009-05-24 22:05:42 +0200543 error = dpm_suspend_start(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700544 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100545 error = resume_target_kernel(platform_mode);
Imre Deak94fb8232014-10-24 20:29:10 +0300546 /*
547 * The above should either succeed and jump to the new kernel,
548 * or return with an error. Otherwise things are just
549 * undefined, so let's be paranoid.
550 */
551 BUG_ON(!error);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700552 }
Imre Deak94fb8232014-10-24 20:29:10 +0300553 dpm_resume_end(PMSG_RECOVER);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100554 pm_restore_gfp_mask();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700555 resume_console();
556 pm_restore_console();
557 return error;
558}
559
560/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200561 * hibernation_platform_enter - Power off the system using the platform driver.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700562 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700563int hibernation_platform_enter(void)
564{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100565 int error;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700566
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700567 if (!hibernation_ops)
568 return -ENOSYS;
569
570 /*
571 * We have cancelled the power transition by running
572 * hibernation_ops->finish() before saving the image, so we should let
573 * the firmware know that we're going to enter the sleep state after all
574 */
Rafael J. Wysockibb186902019-05-16 12:43:19 +0200575 error = hibernation_ops->begin(PMSG_HIBERNATE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700576 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100577 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700578
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100579 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700580 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +0200581 error = dpm_suspend_start(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200582 if (error) {
583 if (hibernation_ops->recover)
584 hibernation_ops->recover();
585 goto Resume_devices;
586 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700587
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100588 error = dpm_suspend_end(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100589 if (error)
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200590 goto Resume_devices;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100591
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100592 error = hibernation_ops->prepare();
593 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200594 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100595
Rafael J. Wysocki23f62d72021-10-22 18:07:47 +0200596 error = pm_sleep_disable_secondary_cpus();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100597 if (error)
Vitaly Kuznetsov8c506602015-06-24 16:02:06 +0200598 goto Enable_cpus;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100599
600 local_irq_disable();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200601 system_state = SYSTEM_SUSPEND;
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100602 syscore_suspend();
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100603 if (pm_wakeup_pending()) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200604 error = -EAGAIN;
605 goto Power_up;
606 }
607
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100608 hibernation_ops->enter();
609 /* We should never get here */
610 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700611
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200612 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100613 syscore_resume();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200614 system_state = SYSTEM_RUNNING;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200615 local_irq_enable();
Vitaly Kuznetsov8c506602015-06-24 16:02:06 +0200616
617 Enable_cpus:
Rafael J. Wysocki23f62d72021-10-22 18:07:47 +0200618 pm_sleep_enable_secondary_cpus();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200619
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200620 Platform_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700621 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100622
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100623 dpm_resume_start(PMSG_RESTORE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100624
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700625 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100626 entering_platform_hibernation = false;
Alan Sternd1616302009-05-24 22:05:42 +0200627 dpm_resume_end(PMSG_RESTORE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700628 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100629
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100630 Close:
631 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100632
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700633 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700634}
635
636/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200637 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200639 * Use the platform driver, if configured, to put the system into the sleep
640 * state corresponding to hibernation, or try to power it off or reboot,
641 * depending on the value of hibernation_mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 */
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700643static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Bojan Smojver62c552c2012-06-16 00:09:58 +0200645#ifdef CONFIG_SUSPEND
646 int error;
Rafael J. Wysocki81d45bd2017-02-24 00:25:28 +0100647
648 if (hibernation_mode == HIBERNATION_SUSPEND) {
Mario Limonciello85850af42022-10-12 22:50:17 -0500649 error = suspend_devices_and_enter(mem_sleep_current);
Rafael J. Wysocki81d45bd2017-02-24 00:25:28 +0100650 if (error) {
651 hibernation_mode = hibernation_ops ?
652 HIBERNATION_PLATFORM :
653 HIBERNATION_SHUTDOWN;
654 } else {
655 /* Restore swap signature. */
656 error = swsusp_unmark();
657 if (error)
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100658 pr_err("Swap will be unusable! Try swapon -a.\n");
Rafael J. Wysocki81d45bd2017-02-24 00:25:28 +0100659
660 return;
661 }
662 }
Bojan Smojver62c552c2012-06-16 00:09:58 +0200663#endif
664
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700665 switch (hibernation_mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700666 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600667 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700669 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700670 hibernation_platform_enter();
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500671 fallthrough;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700672 case HIBERNATION_SHUTDOWN:
Dmitry Osipenko20277322022-06-17 15:24:02 +0300673 if (kernel_can_power_off())
Sebastian Capella2c730782014-04-21 17:30:46 -0700674 kernel_power_off();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700675 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600677 kernel_halt();
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700678 /*
679 * Valid image is on the disk, if we continue we risk serious data
680 * corruption after resume.
681 */
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100682 pr_crit("Power down manually\n");
Sebastian Capella2c730782014-04-21 17:30:46 -0700683 while (1)
684 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Christoph Hellwigd6545e62023-05-31 14:55:14 +0200687static int load_image_and_restore(bool snapshot_test)
Chen Yufe12c002016-07-22 10:30:47 +0800688{
689 int error;
690 unsigned int flags;
691
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200692 pm_pr_dbg("Loading hibernation image.\n");
Chen Yufe12c002016-07-22 10:30:47 +0800693
694 lock_device_hotplug();
695 error = create_basic_memory_bitmaps();
Ye Bin3f51aa92022-02-09 19:29:51 +0800696 if (error) {
Christoph Hellwig2736e8e2023-06-08 13:02:43 +0200697 swsusp_close(snapshot_test);
Chen Yufe12c002016-07-22 10:30:47 +0800698 goto Unlock;
Ye Bin3f51aa92022-02-09 19:29:51 +0800699 }
Chen Yufe12c002016-07-22 10:30:47 +0800700
701 error = swsusp_read(&flags);
Christoph Hellwig2736e8e2023-06-08 13:02:43 +0200702 swsusp_close(snapshot_test);
Chen Yufe12c002016-07-22 10:30:47 +0800703 if (!error)
Dexuan Cui3704a6a2020-03-31 08:55:25 -0700704 error = hibernation_restore(flags & SF_PLATFORM_MODE);
Chen Yufe12c002016-07-22 10:30:47 +0800705
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800706 pr_err("Failed to load image, recovering.\n");
Chen Yufe12c002016-07-22 10:30:47 +0800707 swsusp_free();
708 free_basic_memory_bitmaps();
709 Unlock:
710 unlock_device_hotplug();
711
712 return error;
713}
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200716 * hibernate - Carry out system hibernation, including saving the image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700718int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Christoph Hellwigd6545e62023-05-31 14:55:14 +0200720 bool snapshot_test = false;
Peter Zijlstra5950e5d2022-08-22 13:18:17 +0200721 unsigned int sleep_flags;
Peter Zijlstra70d93292020-08-18 15:57:36 +0200722 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Kees Cooka6e15a32014-06-13 13:30:35 -0700724 if (!hibernation_available()) {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200725 pm_pr_dbg("Hibernation not available.\n");
Kees Cooka6e15a32014-06-13 13:30:35 -0700726 return -EPERM;
727 }
728
Peter Zijlstra5950e5d2022-08-22 13:18:17 +0200729 sleep_flags = lock_system_sleep();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700730 /* The snapshot device should not be opened while we're running */
Domenico Andreoliab7e9b02020-05-07 09:19:52 +0200731 if (!hibernate_acquire()) {
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700732 error = -EBUSY;
733 goto Unlock;
734 }
735
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200736 pr_info("hibernation entry\n");
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100737 pm_prepare_console();
Peter Zijlstra70d93292020-08-18 15:57:36 +0200738 error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION);
739 if (error)
740 goto Restore;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700741
Harry Panb5dee312019-02-25 20:36:41 +0800742 ksys_sync_helper();
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700743
Tejun Heo03afed82011-11-21 12:32:24 -0800744 error = freeze_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700745 if (error)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200746 goto Exit;
747
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200748 lock_device_hotplug();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200749 /* Allocate memory management structures */
750 error = create_basic_memory_bitmaps();
751 if (error)
752 goto Thaw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700754 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100755 if (error || freezer_test_done)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200756 goto Free_bitmaps;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200757
758 if (in_suspend) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700759 unsigned int flags = 0;
760
761 if (hibernation_mode == HIBERNATION_PLATFORM)
762 flags |= SF_PLATFORM_MODE;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200763 if (nocompress)
764 flags |= SF_NOCOMPRESS_MODE;
Bojan Smojver081a9d02011-10-13 23:58:07 +0200765 else
766 flags |= SF_CRC32_MODE;
767
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800768 pm_pr_dbg("Writing hibernation image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700769 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700770 swsusp_free();
Chen Yufe12c002016-07-22 10:30:47 +0800771 if (!error) {
772 if (hibernation_mode == HIBERNATION_TEST_RESUME)
773 snapshot_test = true;
774 else
775 power_down();
776 }
MyungJoo Ham5262a472010-11-26 23:07:56 +0100777 in_suspend = 0;
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100778 pm_restore_gfp_mask();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800779 } else {
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800780 pm_pr_dbg("Hibernation image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800781 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200782
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200783 Free_bitmaps:
784 free_basic_memory_bitmaps();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800785 Thaw:
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200786 unlock_device_hotplug();
Chen Yufe12c002016-07-22 10:30:47 +0800787 if (snapshot_test) {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200788 pm_pr_dbg("Checking hibernation image\n");
Christoph Hellwigd6545e62023-05-31 14:55:14 +0200789 error = swsusp_check(snapshot_test);
Chen Yufe12c002016-07-22 10:30:47 +0800790 if (!error)
Christoph Hellwigd6545e62023-05-31 14:55:14 +0200791 error = load_image_and_restore(snapshot_test);
Chen Yufe12c002016-07-22 10:30:47 +0800792 }
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100793 thaw_processes();
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100794
795 /* Don't bother checking whether freezer_test_done is true */
796 freezer_test_done = false;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700797 Exit:
Peter Zijlstra70d93292020-08-18 15:57:36 +0200798 pm_notifier_call_chain(PM_POST_HIBERNATION);
799 Restore:
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100800 pm_restore_console();
Domenico Andreoliab7e9b02020-05-07 09:19:52 +0200801 hibernate_release();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700802 Unlock:
Peter Zijlstra5950e5d2022-08-22 13:18:17 +0200803 unlock_system_sleep(sleep_flags);
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200804 pr_info("hibernation exit\n");
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return error;
807}
808
Dan Williams48001ea2020-07-20 15:08:18 -0700809/**
810 * hibernate_quiet_exec - Execute a function with all devices frozen.
811 * @func: Function to execute.
812 * @data: Data pointer to pass to @func.
813 *
814 * Return the @func return value or an error code if it cannot be executed.
815 */
816int hibernate_quiet_exec(int (*func)(void *data), void *data)
817{
Peter Zijlstra5950e5d2022-08-22 13:18:17 +0200818 unsigned int sleep_flags;
Peter Zijlstra70d93292020-08-18 15:57:36 +0200819 int error;
Dan Williams48001ea2020-07-20 15:08:18 -0700820
Peter Zijlstra5950e5d2022-08-22 13:18:17 +0200821 sleep_flags = lock_system_sleep();
Dan Williams48001ea2020-07-20 15:08:18 -0700822
823 if (!hibernate_acquire()) {
824 error = -EBUSY;
825 goto unlock;
826 }
827
828 pm_prepare_console();
829
Peter Zijlstra70d93292020-08-18 15:57:36 +0200830 error = pm_notifier_call_chain_robust(PM_HIBERNATION_PREPARE, PM_POST_HIBERNATION);
831 if (error)
832 goto restore;
Dan Williams48001ea2020-07-20 15:08:18 -0700833
834 error = freeze_processes();
835 if (error)
836 goto exit;
837
838 lock_device_hotplug();
839
840 pm_suspend_clear_flags();
841
842 error = platform_begin(true);
843 if (error)
844 goto thaw;
845
846 error = freeze_kernel_threads();
847 if (error)
848 goto thaw;
849
850 error = dpm_prepare(PMSG_FREEZE);
851 if (error)
852 goto dpm_complete;
853
854 suspend_console();
855
856 error = dpm_suspend(PMSG_FREEZE);
857 if (error)
858 goto dpm_resume;
859
860 error = dpm_suspend_end(PMSG_FREEZE);
861 if (error)
862 goto dpm_resume;
863
864 error = platform_pre_snapshot(true);
865 if (error)
866 goto skip;
867
868 error = func(data);
869
870skip:
871 platform_finish(true);
872
873 dpm_resume_start(PMSG_THAW);
874
875dpm_resume:
876 dpm_resume(PMSG_THAW);
877
878 resume_console();
879
880dpm_complete:
881 dpm_complete(PMSG_THAW);
882
883 thaw_kernel_threads();
884
885thaw:
886 platform_end(true);
887
888 unlock_device_hotplug();
889
890 thaw_processes();
891
892exit:
Peter Zijlstra70d93292020-08-18 15:57:36 +0200893 pm_notifier_call_chain(PM_POST_HIBERNATION);
Dan Williams48001ea2020-07-20 15:08:18 -0700894
Peter Zijlstra70d93292020-08-18 15:57:36 +0200895restore:
Dan Williams48001ea2020-07-20 15:08:18 -0700896 pm_restore_console();
897
898 hibernate_release();
899
900unlock:
Peter Zijlstra5950e5d2022-08-22 13:18:17 +0200901 unlock_system_sleep(sleep_flags);
Dan Williams48001ea2020-07-20 15:08:18 -0700902
903 return error;
904}
905EXPORT_SYMBOL_GPL(hibernate_quiet_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Christoph Hellwigcc89c632023-05-31 14:55:15 +0200907static int __init find_resume_device(void)
Christoph Hellwig02b42d52023-05-31 14:55:13 +0200908{
909 if (!strlen(resume_file))
910 return -ENOENT;
911
912 pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
913
914 if (resume_delay) {
915 pr_info("Waiting %dsec before reading resume device ...\n",
916 resume_delay);
917 ssleep(resume_delay);
918 }
919
920 /* Check if the device is there */
Christoph Hellwigcf056a42023-05-31 14:55:24 +0200921 if (!early_lookup_bdev(resume_file, &swsusp_resume_device))
Christoph Hellwig02b42d52023-05-31 14:55:13 +0200922 return 0;
923
924 /*
925 * Some device discovery might still be in progress; we need to wait for
926 * this to finish.
927 */
928 wait_for_device_probe();
929 if (resume_wait) {
Christoph Hellwigcf056a42023-05-31 14:55:24 +0200930 while (early_lookup_bdev(resume_file, &swsusp_resume_device))
Christoph Hellwig02b42d52023-05-31 14:55:13 +0200931 msleep(10);
932 async_synchronize_full();
933 }
934
Christoph Hellwigcf056a42023-05-31 14:55:24 +0200935 return early_lookup_bdev(resume_file, &swsusp_resume_device);
Christoph Hellwig02b42d52023-05-31 14:55:13 +0200936}
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938static int software_resume(void)
939{
Peter Zijlstra70d93292020-08-18 15:57:36 +0200940 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200942 pm_pr_dbg("Hibernation image partition %d:%d present\n",
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200943 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200945 pm_pr_dbg("Looking for hibernation image.\n");
Christoph Hellwigcc89c632023-05-31 14:55:15 +0200946
947 mutex_lock(&system_transition_mutex);
Christoph Hellwigd6545e62023-05-31 14:55:14 +0200948 error = swsusp_check(false);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800949 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700950 goto Unlock;
951
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700952 /* The snapshot device should not be opened while we're running */
Domenico Andreoliab7e9b02020-05-07 09:19:52 +0200953 if (!hibernate_acquire()) {
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700954 error = -EBUSY;
Christoph Hellwig2736e8e2023-06-08 13:02:43 +0200955 swsusp_close(false);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700956 goto Unlock;
957 }
958
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200959 pr_info("resume from hibernation\n");
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100960 pm_prepare_console();
Peter Zijlstra70d93292020-08-18 15:57:36 +0200961 error = pm_notifier_call_chain_robust(PM_RESTORE_PREPARE, PM_POST_RESTORE);
962 if (error)
963 goto Restore;
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700964
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800965 pm_pr_dbg("Preparing processes for hibernation restore.\n");
Tejun Heo03afed82011-11-21 12:32:24 -0800966 error = freeze_processes();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200967 if (error)
968 goto Close_Finish;
Dexuan Cui2351f8d2020-04-23 20:40:16 -0700969
970 error = freeze_kernel_threads();
971 if (error) {
972 thaw_processes();
973 goto Close_Finish;
974 }
975
Christoph Hellwigd6545e62023-05-31 14:55:14 +0200976 error = load_image_and_restore(false);
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200977 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700978 Finish:
Peter Zijlstra70d93292020-08-18 15:57:36 +0200979 pm_notifier_call_chain(PM_POST_RESTORE);
980 Restore:
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100981 pm_restore_console();
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -0800982 pr_info("resume failed (%d)\n", error);
Domenico Andreoliab7e9b02020-05-07 09:19:52 +0200983 hibernate_release();
Shaohua Lidd5d6662005-09-03 15:57:04 -0700984 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700985 Unlock:
Pingfan Liu55f25032018-07-31 16:51:32 +0800986 mutex_unlock(&system_transition_mutex);
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200987 pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700988 return error;
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200989 Close_Finish:
Christoph Hellwig2736e8e2023-06-08 13:02:43 +0200990 swsusp_close(false);
Jiri Slaby76b57e62009-10-07 22:37:35 +0200991 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Christoph Hellwigcc89c632023-05-31 14:55:15 +0200994/**
995 * software_resume_initcall - Resume from a saved hibernation image.
996 *
997 * This routine is called as a late initcall, when all devices have been
998 * discovered and initialized already.
999 *
1000 * The image reading code is called to see if there is a hibernation image
1001 * available for reading. If that is the case, devices are quiesced and the
1002 * contents of memory is restored from the saved image.
1003 *
1004 * If this is successful, control reappears in the restored target kernel in
1005 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
1006 * attempts to recover gracefully and make the kernel return to the normal mode
1007 * of operation.
1008 */
1009static int __init software_resume_initcall(void)
1010{
1011 /*
1012 * If the user said "noresume".. bail out early.
1013 */
1014 if (noresume || !hibernation_available())
1015 return 0;
1016
1017 if (!swsusp_resume_device) {
1018 int error = find_resume_device();
1019
1020 if (error)
1021 return error;
1022 }
1023
1024 return software_resume();
1025}
1026late_initcall_sync(software_resume_initcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001029static const char * const hibernation_modes[] = {
1030 [HIBERNATION_PLATFORM] = "platform",
1031 [HIBERNATION_SHUTDOWN] = "shutdown",
1032 [HIBERNATION_REBOOT] = "reboot",
Bojan Smojver62c552c2012-06-16 00:09:58 +02001033#ifdef CONFIG_SUSPEND
1034 [HIBERNATION_SUSPEND] = "suspend",
1035#endif
Chen Yufe12c002016-07-22 10:30:47 +08001036 [HIBERNATION_TEST_RESUME] = "test_resume",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037};
1038
Rafael J. Wysockif42a9812011-05-24 23:36:06 +02001039/*
1040 * /sys/power/disk - Control hibernation mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +02001042 * Hibernation can be handled in several ways. There are a few different ways
1043 * to put the system into the sleep state: using the platform driver (e.g. ACPI
1044 * or other hibernation_ops), powering it off or rebooting it (for testing
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +01001045 * mostly).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +02001047 * The sysfs file /sys/power/disk provides an interface for selecting the
1048 * hibernation mode to use. Reading from this file causes the available modes
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +01001049 * to be printed. There are 3 modes that can be supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 * 'platform'
1052 * 'shutdown'
1053 * 'reboot'
1054 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +02001055 * If a platform hibernation driver is in use, 'platform' will be supported
1056 * and will be used by default. Otherwise, 'shutdown' will be used by default.
1057 * The selected option (i.e. the one corresponding to the current value of
1058 * hibernation_mode) is enclosed by a square bracket.
1059 *
1060 * To select a given hibernation mode it is necessary to write the mode's
1061 * string representation (as returned by reading from /sys/power/disk) back
1062 * into /sys/power/disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 */
1064
Kay Sievers386f2752007-11-02 13:47:53 +01001065static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
1066 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001068 int i;
1069 char *start = buf;
1070
Kees Cooka6e15a32014-06-13 13:30:35 -07001071 if (!hibernation_available())
1072 return sprintf(buf, "[disabled]\n");
1073
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001074 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
1075 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001076 continue;
1077 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001078 case HIBERNATION_SHUTDOWN:
1079 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +02001080#ifdef CONFIG_SUSPEND
1081 case HIBERNATION_SUSPEND:
1082#endif
Chen Yufe12c002016-07-22 10:30:47 +08001083 case HIBERNATION_TEST_RESUME:
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001084 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001085 case HIBERNATION_PLATFORM:
1086 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001087 break;
1088 /* not a valid mode, continue with loop */
1089 continue;
1090 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001091 if (i == hibernation_mode)
1092 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001093 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001094 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -07001095 }
1096 buf += sprintf(buf, "\n");
1097 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
Kay Sievers386f2752007-11-02 13:47:53 +01001100static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
1101 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Peter Zijlstra5950e5d2022-08-22 13:18:17 +02001103 int mode = HIBERNATION_INVALID;
1104 unsigned int sleep_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 int len;
1107 char *p;
Peter Zijlstra5950e5d2022-08-22 13:18:17 +02001108 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Kees Cooka6e15a32014-06-13 13:30:35 -07001110 if (!hibernation_available())
1111 return -EPERM;
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 p = memchr(buf, '\n', n);
1114 len = p ? p - buf : n;
1115
Peter Zijlstra5950e5d2022-08-22 13:18:17 +02001116 sleep_flags = lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001117 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -07001118 if (len == strlen(hibernation_modes[i])
1119 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 mode = i;
1121 break;
1122 }
1123 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001124 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001125 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001126 case HIBERNATION_SHUTDOWN:
1127 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +02001128#ifdef CONFIG_SUSPEND
1129 case HIBERNATION_SUSPEND:
1130#endif
Chen Yufe12c002016-07-22 10:30:47 +08001131 case HIBERNATION_TEST_RESUME:
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001132 hibernation_mode = mode;
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001133 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001134 case HIBERNATION_PLATFORM:
1135 if (hibernation_ops)
1136 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 else
1138 error = -EINVAL;
1139 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001140 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 error = -EINVAL;
1142
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001143 if (!error)
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +02001144 pm_pr_dbg("Hibernation mode set to '%s'\n",
1145 hibernation_modes[mode]);
Peter Zijlstra5950e5d2022-08-22 13:18:17 +02001146 unlock_system_sleep(sleep_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return error ? error : n;
1148}
1149
1150power_attr(disk);
1151
Kay Sievers386f2752007-11-02 13:47:53 +01001152static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1153 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Xiang Chen6ada7ba2020-06-22 17:09:13 +08001155 return sprintf(buf, "%d:%d\n", MAJOR(swsusp_resume_device),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 MINOR(swsusp_resume_device));
1157}
1158
Kay Sievers386f2752007-11-02 13:47:53 +01001159static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1160 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Peter Zijlstra5950e5d2022-08-22 13:18:17 +02001162 unsigned int sleep_flags;
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001163 int len = n;
1164 char *name;
Christoph Hellwigcf056a42023-05-31 14:55:24 +02001165 dev_t dev;
1166 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Christoph Hellwigcc89c632023-05-31 14:55:15 +02001168 if (!hibernation_available())
Vlastimil Babkadf2f7cd2023-08-07 10:33:57 +02001169 return n;
Christoph Hellwigcc89c632023-05-31 14:55:15 +02001170
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001171 if (len && buf[len-1] == '\n')
1172 len--;
1173 name = kstrndup(buf, len, GFP_KERNEL);
1174 if (!name)
1175 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Christoph Hellwig1e8c8132023-05-31 14:55:32 +02001177 error = lookup_bdev(name, &dev);
1178 if (error) {
1179 unsigned maj, min, offset;
1180 char *p, dummy;
1181
Azat Khuzhinc9e4bf62023-07-11 13:48:12 +02001182 error = 0;
Christoph Hellwig1e8c8132023-05-31 14:55:32 +02001183 if (sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2 ||
1184 sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset,
1185 &dummy) == 3) {
1186 dev = MKDEV(maj, min);
1187 if (maj != MAJOR(dev) || min != MINOR(dev))
1188 error = -EINVAL;
1189 } else {
1190 dev = new_decode_dev(simple_strtoul(name, &p, 16));
1191 if (*p)
1192 error = -EINVAL;
1193 }
1194 }
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001195 kfree(name);
Christoph Hellwigcf056a42023-05-31 14:55:24 +02001196 if (error)
1197 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Peter Zijlstra5950e5d2022-08-22 13:18:17 +02001199 sleep_flags = lock_system_sleep();
Christoph Hellwigcf056a42023-05-31 14:55:24 +02001200 swsusp_resume_device = dev;
Peter Zijlstra5950e5d2022-08-22 13:18:17 +02001201 unlock_system_sleep(sleep_flags);
1202
Luigi Semenzato7a7b99bf2020-01-02 15:19:40 -08001203 pm_pr_dbg("Configured hibernation resume from disk to %u\n",
1204 swsusp_resume_device);
Andrew Mortona5762192006-01-06 00:09:50 -08001205 noresume = 0;
1206 software_resume();
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001207 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210power_attr(resume);
1211
Mario Limonciello35506462018-03-28 12:01:09 -05001212static ssize_t resume_offset_show(struct kobject *kobj,
1213 struct kobj_attribute *attr, char *buf)
1214{
1215 return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
1216}
1217
1218static ssize_t resume_offset_store(struct kobject *kobj,
1219 struct kobj_attribute *attr, const char *buf,
1220 size_t n)
1221{
1222 unsigned long long offset;
1223 int rc;
1224
1225 rc = kstrtoull(buf, 0, &offset);
1226 if (rc)
1227 return rc;
1228 swsusp_resume_block = offset;
1229
1230 return n;
1231}
1232
1233power_attr(resume_offset);
1234
Kay Sievers386f2752007-11-02 13:47:53 +01001235static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1236 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001237{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001238 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001239}
1240
Kay Sievers386f2752007-11-02 13:47:53 +01001241static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1242 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001243{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001244 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001245
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001246 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001247 image_size = size;
1248 return n;
1249 }
1250
1251 return -EINVAL;
1252}
1253
1254power_attr(image_size);
1255
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001256static ssize_t reserved_size_show(struct kobject *kobj,
1257 struct kobj_attribute *attr, char *buf)
1258{
1259 return sprintf(buf, "%lu\n", reserved_size);
1260}
1261
1262static ssize_t reserved_size_store(struct kobject *kobj,
1263 struct kobj_attribute *attr,
1264 const char *buf, size_t n)
1265{
1266 unsigned long size;
1267
1268 if (sscanf(buf, "%lu", &size) == 1) {
1269 reserved_size = size;
1270 return n;
1271 }
1272
1273 return -EINVAL;
1274}
1275
1276power_attr(reserved_size);
1277
Xiang Chen6ada7ba2020-06-22 17:09:13 +08001278static struct attribute *g[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 &disk_attr.attr,
Mario Limonciello35506462018-03-28 12:01:09 -05001280 &resume_offset_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001282 &image_size_attr.attr,
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001283 &reserved_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 NULL,
1285};
1286
1287
Arvind Yadav59494fe2017-06-29 16:58:40 +05301288static const struct attribute_group attr_group = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 .attrs = g,
1290};
1291
1292
1293static int __init pm_disk_init(void)
1294{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -08001295 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
1298core_initcall(pm_disk_init);
1299
1300
1301static int __init resume_setup(char *str)
1302{
1303 if (noresume)
1304 return 1;
1305
Xiang Chen6ada7ba2020-06-22 17:09:13 +08001306 strncpy(resume_file, str, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return 1;
1308}
1309
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001310static int __init resume_offset_setup(char *str)
1311{
1312 unsigned long long offset;
1313
1314 if (noresume)
1315 return 1;
1316
1317 if (sscanf(str, "%llu", &offset) == 1)
1318 swsusp_resume_block = offset;
1319
1320 return 1;
1321}
1322
Bojan Smojverf996fc92010-09-09 23:06:23 +02001323static int __init hibernate_setup(char *str)
1324{
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001325 if (!strncmp(str, "noresume", 8)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001326 noresume = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001327 } else if (!strncmp(str, "nocompress", 10)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001328 nocompress = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001329 } else if (!strncmp(str, "no", 2)) {
Kees Cooka6e15a32014-06-13 13:30:35 -07001330 noresume = 1;
1331 nohibernate = 1;
Laura Abbott0f5bf6d2017-02-06 16:31:58 -08001332 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
Rafael J. Wysocki4c0b6c12016-07-10 02:12:10 +02001333 && !strncmp(str, "protect_image", 13)) {
1334 enable_restore_image_protection();
Kees Cooka6e15a32014-06-13 13:30:35 -07001335 }
Bojan Smojverf996fc92010-09-09 23:06:23 +02001336 return 1;
1337}
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339static int __init noresume_setup(char *str)
1340{
1341 noresume = 1;
1342 return 1;
1343}
1344
Barry Song6f8d7022011-10-06 20:34:46 +02001345static int __init resumewait_setup(char *str)
1346{
1347 resume_wait = 1;
1348 return 1;
1349}
1350
Barry Songf126f732011-10-10 23:38:41 +02001351static int __init resumedelay_setup(char *str)
1352{
Dan Carpenterf6514be2014-05-14 19:08:46 +03001353 int rc = kstrtouint(str, 0, &resume_delay);
Fabian Frederick317cf7e2014-05-09 23:32:08 +02001354
1355 if (rc)
Randy Dunlapba7ffcd2022-02-28 14:05:32 -08001356 pr_warn("resumedelay: bad option string '%s'\n", str);
Barry Songf126f732011-10-10 23:38:41 +02001357 return 1;
1358}
1359
Kees Cooka6e15a32014-06-13 13:30:35 -07001360static int __init nohibernate_setup(char *str)
1361{
1362 noresume = 1;
1363 nohibernate = 1;
1364 return 1;
1365}
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001368__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369__setup("resume=", resume_setup);
Bojan Smojverf996fc92010-09-09 23:06:23 +02001370__setup("hibernate=", hibernate_setup);
Barry Song6f8d7022011-10-06 20:34:46 +02001371__setup("resumewait", resumewait_setup);
Barry Songf126f732011-10-10 23:38:41 +02001372__setup("resumedelay=", resumedelay_setup);
Kees Cooka6e15a32014-06-13 13:30:35 -07001373__setup("nohibernate", nohibernate_setup);