blob: 97522630b1b627dbe7e0a243d9994a7b070db6ed [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Rafael J. Wysocki8b759b82009-06-10 01:27:49 +02002 * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
Pavel Macheka2531292010-07-18 14:27:13 +02006 * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz>
Rafael J. Wysocki8b759b82009-06-10 01:27:49 +02007 * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
Bojan Smojver62c552c2012-06-16 00:09:58 +02008 * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This file is released under the GPLv2.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Rafael J. Wysocki2872de12017-02-24 00:26:15 +010013#define pr_fmt(fmt) "PM: " fmt
14
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>
32#include <linux/genhd.h>
Tina Ruchandanidb597602014-10-30 11:04:53 -070033#include <linux/ktime.h>
Todd E Brandtbb3632c2014-06-06 05:40:17 -070034#include <trace/events/power.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "power.h"
37
38
Barry Songd231ff12011-10-11 23:29:18 -070039static int nocompress;
40static int noresume;
Kees Cooka6e15a32014-06-13 13:30:35 -070041static int nohibernate;
Barry Songd231ff12011-10-11 23:29:18 -070042static int resume_wait;
Dan Carpenterf6514be2014-05-14 19:08:46 +030043static unsigned int resume_delay;
Adrian Bunk47a460d2008-02-04 22:30:06 -080044static char resume_file[256] = CONFIG_PM_STD_PARTITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080046sector_t swsusp_resume_block;
Andi Kleend6efc2f2013-08-05 15:02:49 -070047__visible int in_suspend __nosavedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070049enum {
50 HIBERNATION_INVALID,
51 HIBERNATION_PLATFORM,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070052 HIBERNATION_SHUTDOWN,
53 HIBERNATION_REBOOT,
Bojan Smojver62c552c2012-06-16 00:09:58 +020054#ifdef CONFIG_SUSPEND
55 HIBERNATION_SUSPEND,
56#endif
Chen Yufe12c002016-07-22 10:30:47 +080057 HIBERNATION_TEST_RESUME,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070058 /* keep last */
59 __HIBERNATION_AFTER_LAST
60};
61#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
62#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
63
64static int hibernation_mode = HIBERNATION_SHUTDOWN;
65
Srivatsa S. Bhat97819a22011-12-01 22:33:10 +010066bool freezer_test_done;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +010067
Lionel Debroux073ef1f2010-11-09 21:48:49 +010068static const struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070069
Kees Cooka6e15a32014-06-13 13:30:35 -070070bool hibernation_available(void)
71{
72 return (nohibernate == 0);
73}
74
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070075/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +020076 * hibernation_set_ops - Set the global hibernate operations.
77 * @ops: Hibernation operations to use in subsequent hibernation transitions.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070078 */
Lionel Debroux073ef1f2010-11-09 21:48:49 +010079void hibernation_set_ops(const struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070080{
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +010081 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
82 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
MyungJoo Ham5729c632010-11-26 23:07:48 +010083 && ops->restore_cleanup && ops->leave)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070084 WARN_ON(1);
85 return;
86 }
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010087 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070088 hibernation_ops = ops;
89 if (ops)
90 hibernation_mode = HIBERNATION_PLATFORM;
91 else if (hibernation_mode == HIBERNATION_PLATFORM)
92 hibernation_mode = HIBERNATION_SHUTDOWN;
93
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010094 unlock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070095}
Leonardo Potenzae0c78552013-11-19 12:27:41 +000096EXPORT_SYMBOL_GPL(hibernation_set_ops);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070097
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +010098static bool entering_platform_hibernation;
99
100bool system_entering_hibernation(void)
101{
102 return entering_platform_hibernation;
103}
104EXPORT_SYMBOL(system_entering_hibernation);
105
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100106#ifdef CONFIG_PM_DEBUG
107static void hibernation_debug_sleep(void)
108{
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100109 pr_info("hibernation debug: Waiting for 5 seconds.\n");
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100110 mdelay(5000);
111}
112
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100113static int hibernation_test(int level)
114{
115 if (pm_test_level == level) {
116 hibernation_debug_sleep();
117 return 1;
118 }
119 return 0;
120}
121#else /* !CONFIG_PM_DEBUG */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100122static int hibernation_test(int level) { return 0; }
123#endif /* !CONFIG_PM_DEBUG */
124
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700125/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200126 * platform_begin - Call platform to start hibernation.
127 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700128 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100129static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700130{
131 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockibb186902019-05-16 12:43:19 +0200132 hibernation_ops->begin(PMSG_FREEZE) : 0;
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100133}
134
135/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200136 * platform_end - Call platform to finish transition to the working state.
137 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100138 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100139static void platform_end(int platform_mode)
140{
141 if (platform_mode && hibernation_ops)
142 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700143}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200146 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
147 * @platform_mode: Whether or not to use the platform driver.
148 *
149 * Use the platform driver to prepare the system for creating a hibernate image,
150 * if so configured, and return an error code if that fails.
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800151 */
152
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700153static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800154{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700155 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700156 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800157}
158
159/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200160 * platform_leave - Call platform to prepare a transition to the working state.
161 * @platform_mode: Whether or not to use the platform driver.
162 *
163 * Use the platform driver prepare to prepare the machine for switching to the
164 * normal mode of operation.
165 *
166 * This routine is called on one CPU with interrupts disabled.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700167 */
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700168static void platform_leave(int platform_mode)
169{
170 if (platform_mode && hibernation_ops)
171 hibernation_ops->leave();
172}
173
174/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200175 * platform_finish - Call platform to switch the system to the working state.
176 * @platform_mode: Whether or not to use the platform driver.
177 *
178 * Use the platform driver to switch the machine to the normal mode of
179 * operation.
180 *
181 * This routine must be called after platform_prepare().
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700182 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700183static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700184{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700185 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700186 hibernation_ops->finish();
187}
188
189/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200190 * platform_pre_restore - Prepare for hibernate image restoration.
191 * @platform_mode: Whether or not to use the platform driver.
192 *
193 * Use the platform driver to prepare the system for resume from a hibernation
194 * image.
195 *
196 * If the restore fails after this function has been called,
197 * platform_restore_cleanup() must be called.
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700198 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700199static int platform_pre_restore(int platform_mode)
200{
201 return (platform_mode && hibernation_ops) ?
202 hibernation_ops->pre_restore() : 0;
203}
204
205/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200206 * platform_restore_cleanup - Switch to the working state after failing restore.
207 * @platform_mode: Whether or not to use the platform driver.
208 *
209 * Use the platform driver to switch the system to the normal mode of operation
210 * after a failing restore.
211 *
212 * If platform_pre_restore() has been called before the failing restore, this
213 * function must be called too, regardless of the result of
214 * platform_pre_restore().
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700215 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700216static void platform_restore_cleanup(int platform_mode)
217{
218 if (platform_mode && hibernation_ops)
219 hibernation_ops->restore_cleanup();
220}
221
222/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200223 * platform_recover - Recover from a failure to suspend devices.
224 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200225 */
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200226static void platform_recover(int platform_mode)
227{
228 if (platform_mode && hibernation_ops && hibernation_ops->recover)
229 hibernation_ops->recover();
230}
231
232/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200233 * swsusp_show_speed - Print time elapsed between two events during hibernation.
234 * @start: Starting event.
235 * @stop: Final event.
236 * @nr_pages: Number of memory pages processed between @start and @stop.
237 * @msg: Additional diagnostic message to print.
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100238 */
Tina Ruchandanidb597602014-10-30 11:04:53 -0700239void swsusp_show_speed(ktime_t start, ktime_t stop,
240 unsigned nr_pages, char *msg)
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100241{
Tina Ruchandanidb597602014-10-30 11:04:53 -0700242 ktime_t diff;
Chen Gang4881f602014-04-25 08:44:59 +0800243 u64 elapsed_centisecs64;
244 unsigned int centisecs;
245 unsigned int k;
246 unsigned int kps;
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100247
Tina Ruchandanidb597602014-10-30 11:04:53 -0700248 diff = ktime_sub(stop, start);
249 elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC);
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100250 centisecs = elapsed_centisecs64;
251 if (centisecs == 0)
252 centisecs = 1; /* avoid div-by-zero */
253 k = nr_pages * (PAGE_SIZE / 1024);
254 kps = (k * 100) / centisecs;
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100255 pr_info("%s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
256 msg, k, centisecs / 100, centisecs % 100, kps / 1000,
257 (kps % 1000) / 10);
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100258}
259
260/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200261 * create_image - Create a hibernation image.
262 * @platform_mode: Whether or not to use the platform driver.
263 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100264 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
265 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200266 *
267 * Control reappears in this routine after the subsequent restore.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700268 */
Adrian Bunk47a460d2008-02-04 22:30:06 -0800269static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700270{
271 int error;
272
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100273 error = dpm_suspend_end(PMSG_FREEZE);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700274 if (error) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100275 pr_err("Some devices failed to power down, aborting hibernation\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200276 return error;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700277 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100278
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100279 error = platform_pre_snapshot(platform_mode);
280 if (error || hibernation_test(TEST_PLATFORM))
281 goto Platform_finish;
282
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000283 error = suspend_disable_secondary_cpus();
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100284 if (error || hibernation_test(TEST_CPUS))
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100285 goto Enable_cpus;
286
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100287 local_irq_disable();
288
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200289 system_state = SYSTEM_SUSPEND;
290
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200291 error = syscore_suspend();
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100292 if (error) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100293 pr_err("Some system devices failed to power down, aborting hibernation\n");
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100294 goto Enable_irqs;
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100295 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700296
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100297 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100298 goto Power_up;
299
300 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700301 save_processor_state();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700302 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700303 error = swsusp_arch_suspend();
Thomas Garnier62822e22016-08-11 14:49:29 -0700304 /* Restore control flow magically appears here */
305 restore_processor_state();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700306 trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700307 if (error)
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100308 pr_err("Error %d creating hibernation image\n", error);
309
Anisse Astier1ad14102016-09-09 10:43:32 +0200310 if (!in_suspend) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200311 events_check_enabled = false;
Anisse Astier1ad14102016-09-09 10:43:32 +0200312 clear_free_pages();
313 }
Bjørn Mork362e77d2013-12-04 16:06:58 +0100314
315 platform_leave(platform_mode);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100316
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100317 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100318 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100319
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100320 Enable_irqs:
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200321 system_state = SYSTEM_RUNNING;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100322 local_irq_enable();
323
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100324 Enable_cpus:
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000325 suspend_enable_secondary_cpus();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100326
327 Platform_finish:
328 platform_finish(platform_mode);
329
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100330 dpm_resume_start(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200331 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100332
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700333 return error;
334}
335
336/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200337 * hibernation_snapshot - Quiesce devices and create a hibernation image.
338 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700339 *
Pingfan Liu55f25032018-07-31 16:51:32 +0800340 * This routine must be called with system_transition_mutex held.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700341 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700342int hibernation_snapshot(int platform_mode)
343{
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100344 pm_message_t msg;
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100345 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700346
Lukas Wunner27614272016-03-23 00:11:20 +0100347 pm_suspend_clear_flags();
Zhang Rui3fe03132008-10-26 20:50:26 +0100348 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700349 if (error)
Rafael J. Wysockid074ee02010-07-07 23:43:35 +0200350 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700351
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200352 /* Preallocate image memory before shutting down devices. */
353 error = hibernate_preallocate_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700354 if (error)
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200355 goto Close;
356
357 error = freeze_kernel_threads();
358 if (error)
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100359 goto Cleanup;
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200360
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100361 if (hibernation_test(TEST_FREEZER)) {
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100362
363 /*
364 * Indicate to the caller that we are returning due to a
365 * successful freezer test.
366 */
367 freezer_test_done = true;
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100368 goto Thaw;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100369 }
370
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200371 error = dpm_prepare(PMSG_FREEZE);
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100372 if (error) {
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100373 dpm_complete(PMSG_RECOVER);
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100374 goto Thaw;
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100375 }
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700376
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700377 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100378 pm_restrict_gfp_mask();
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100379
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200380 error = dpm_suspend(PMSG_FREEZE);
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700381
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100382 if (error || hibernation_test(TEST_DEVICES))
383 platform_recover(platform_mode);
384 else
385 error = create_image(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700386
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100387 /*
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100388 * In the case that we call create_image() above, the control
389 * returns here (1) after the image has been created or the
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100390 * image creation has failed and (2) after a successful restore.
391 */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100392
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200393 /* We may need to release the preallocated image pages here. */
394 if (error || !in_suspend)
395 swsusp_free();
396
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200397 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
398 dpm_resume(msg);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100399
400 if (error || !in_suspend)
401 pm_restore_gfp_mask();
402
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700403 resume_console();
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200404 dpm_complete(msg);
405
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100406 Close:
407 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700408 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200409
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100410 Thaw:
411 thaw_kernel_threads();
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100412 Cleanup:
413 swsusp_free();
414 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700415}
416
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200417int __weak hibernate_resume_nonboot_cpu_disable(void)
418{
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000419 return suspend_disable_secondary_cpus();
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200420}
421
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700422/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200423 * resume_target_kernel - Restore system state from a hibernation image.
424 * @platform_mode: Whether or not to use the platform driver.
425 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100426 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
427 * contents of highmem that have not been restored yet from the image and run
428 * the low-level code that will restore the remaining contents of memory and
429 * switch to the just restored target kernel.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100430 */
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100431static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100432{
433 int error;
434
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100435 error = dpm_suspend_end(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100436 if (error) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100437 pr_err("Some devices failed to power down, aborting resume\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200438 return error;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100439 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100440
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100441 error = platform_pre_restore(platform_mode);
442 if (error)
443 goto Cleanup;
444
Rafael J. Wysocki406f9922016-07-14 03:55:23 +0200445 error = hibernate_resume_nonboot_cpu_disable();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100446 if (error)
447 goto Enable_cpus;
448
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100449 local_irq_disable();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200450 system_state = SYSTEM_SUSPEND;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100451
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200452 error = syscore_suspend();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100453 if (error)
454 goto Enable_irqs;
455
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100456 save_processor_state();
457 error = restore_highmem();
458 if (!error) {
459 error = swsusp_arch_resume();
460 /*
461 * The code below is only ever reached in case of a failure.
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200462 * Otherwise, execution continues at the place where
463 * swsusp_arch_suspend() was called.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100464 */
465 BUG_ON(!error);
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200466 /*
467 * This call to restore_highmem() reverts the changes made by
468 * the previous one.
469 */
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100470 restore_highmem();
471 }
472 /*
473 * The only reason why swsusp_arch_resume() can fail is memory being
474 * very tight, so we have to free it as soon as we can to avoid
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200475 * subsequent failures.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100476 */
477 swsusp_free();
478 restore_processor_state();
479 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100480
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100481 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100482
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100483 Enable_irqs:
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200484 system_state = SYSTEM_RUNNING;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100485 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100486
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100487 Enable_cpus:
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000488 suspend_enable_secondary_cpus();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100489
490 Cleanup:
491 platform_restore_cleanup(platform_mode);
492
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100493 dpm_resume_start(PMSG_RECOVER);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100494
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100495 return error;
496}
497
498/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200499 * hibernation_restore - Quiesce devices and restore from a hibernation image.
500 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700501 *
Pingfan Liu55f25032018-07-31 16:51:32 +0800502 * This routine must be called with system_transition_mutex held. If it is
503 * successful, control reappears in the restored target kernel in
504 * hibernation_snapshot().
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700505 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700506int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700507{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100508 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700509
510 pm_prepare_console();
511 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100512 pm_restrict_gfp_mask();
Alan Sternd1616302009-05-24 22:05:42 +0200513 error = dpm_suspend_start(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700514 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100515 error = resume_target_kernel(platform_mode);
Imre Deak94fb8232014-10-24 20:29:10 +0300516 /*
517 * The above should either succeed and jump to the new kernel,
518 * or return with an error. Otherwise things are just
519 * undefined, so let's be paranoid.
520 */
521 BUG_ON(!error);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700522 }
Imre Deak94fb8232014-10-24 20:29:10 +0300523 dpm_resume_end(PMSG_RECOVER);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100524 pm_restore_gfp_mask();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700525 resume_console();
526 pm_restore_console();
527 return error;
528}
529
530/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200531 * hibernation_platform_enter - Power off the system using the platform driver.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700532 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700533int hibernation_platform_enter(void)
534{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100535 int error;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700536
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700537 if (!hibernation_ops)
538 return -ENOSYS;
539
540 /*
541 * We have cancelled the power transition by running
542 * hibernation_ops->finish() before saving the image, so we should let
543 * the firmware know that we're going to enter the sleep state after all
544 */
Rafael J. Wysockibb186902019-05-16 12:43:19 +0200545 error = hibernation_ops->begin(PMSG_HIBERNATE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700546 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100547 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700548
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100549 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700550 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +0200551 error = dpm_suspend_start(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200552 if (error) {
553 if (hibernation_ops->recover)
554 hibernation_ops->recover();
555 goto Resume_devices;
556 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700557
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100558 error = dpm_suspend_end(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100559 if (error)
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200560 goto Resume_devices;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100561
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100562 error = hibernation_ops->prepare();
563 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200564 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100565
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000566 error = suspend_disable_secondary_cpus();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100567 if (error)
Vitaly Kuznetsov8c506602015-06-24 16:02:06 +0200568 goto Enable_cpus;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100569
570 local_irq_disable();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200571 system_state = SYSTEM_SUSPEND;
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100572 syscore_suspend();
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100573 if (pm_wakeup_pending()) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200574 error = -EAGAIN;
575 goto Power_up;
576 }
577
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100578 hibernation_ops->enter();
579 /* We should never get here */
580 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700581
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200582 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100583 syscore_resume();
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200584 system_state = SYSTEM_RUNNING;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200585 local_irq_enable();
Vitaly Kuznetsov8c506602015-06-24 16:02:06 +0200586
587 Enable_cpus:
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000588 suspend_enable_secondary_cpus();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200589
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200590 Platform_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700591 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100592
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100593 dpm_resume_start(PMSG_RESTORE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100594
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700595 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100596 entering_platform_hibernation = false;
Alan Sternd1616302009-05-24 22:05:42 +0200597 dpm_resume_end(PMSG_RESTORE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700598 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100599
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100600 Close:
601 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100602
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700603 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700604}
605
606/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200607 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200609 * Use the platform driver, if configured, to put the system into the sleep
610 * state corresponding to hibernation, or try to power it off or reboot,
611 * depending on the value of hibernation_mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700613static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Bojan Smojver62c552c2012-06-16 00:09:58 +0200615#ifdef CONFIG_SUSPEND
616 int error;
Rafael J. Wysocki81d45bd2017-02-24 00:25:28 +0100617
618 if (hibernation_mode == HIBERNATION_SUSPEND) {
619 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
620 if (error) {
621 hibernation_mode = hibernation_ops ?
622 HIBERNATION_PLATFORM :
623 HIBERNATION_SHUTDOWN;
624 } else {
625 /* Restore swap signature. */
626 error = swsusp_unmark();
627 if (error)
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100628 pr_err("Swap will be unusable! Try swapon -a.\n");
Rafael J. Wysocki81d45bd2017-02-24 00:25:28 +0100629
630 return;
631 }
632 }
Bojan Smojver62c552c2012-06-16 00:09:58 +0200633#endif
634
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700635 switch (hibernation_mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700636 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600637 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700639 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700640 hibernation_platform_enter();
Gustavo A. R. Silva82837ad2018-08-04 21:12:00 -0500641 /* Fall through */
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700642 case HIBERNATION_SHUTDOWN:
Sebastian Capella2c730782014-04-21 17:30:46 -0700643 if (pm_power_off)
644 kernel_power_off();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700645 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600647 kernel_halt();
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700648 /*
649 * Valid image is on the disk, if we continue we risk serious data
650 * corruption after resume.
651 */
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100652 pr_crit("Power down manually\n");
Sebastian Capella2c730782014-04-21 17:30:46 -0700653 while (1)
654 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Chen Yufe12c002016-07-22 10:30:47 +0800657static int load_image_and_restore(void)
658{
659 int error;
660 unsigned int flags;
661
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200662 pm_pr_dbg("Loading hibernation image.\n");
Chen Yufe12c002016-07-22 10:30:47 +0800663
664 lock_device_hotplug();
665 error = create_basic_memory_bitmaps();
666 if (error)
667 goto Unlock;
668
669 error = swsusp_read(&flags);
670 swsusp_close(FMODE_READ);
671 if (!error)
672 hibernation_restore(flags & SF_PLATFORM_MODE);
673
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100674 pr_err("Failed to load hibernation image, recovering.\n");
Chen Yufe12c002016-07-22 10:30:47 +0800675 swsusp_free();
676 free_basic_memory_bitmaps();
677 Unlock:
678 unlock_device_hotplug();
679
680 return error;
681}
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200684 * hibernate - Carry out system hibernation, including saving the image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700686int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700688 int error, nr_calls = 0;
Chen Yufe12c002016-07-22 10:30:47 +0800689 bool snapshot_test = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Kees Cooka6e15a32014-06-13 13:30:35 -0700691 if (!hibernation_available()) {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200692 pm_pr_dbg("Hibernation not available.\n");
Kees Cooka6e15a32014-06-13 13:30:35 -0700693 return -EPERM;
694 }
695
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100696 lock_system_sleep();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700697 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700698 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
699 error = -EBUSY;
700 goto Unlock;
701 }
702
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200703 pr_info("hibernation entry\n");
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100704 pm_prepare_console();
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700705 error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
706 if (error) {
707 nr_calls--;
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700708 goto Exit;
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700709 }
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700710
Harry Panb5dee312019-02-25 20:36:41 +0800711 ksys_sync_helper();
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700712
Tejun Heo03afed82011-11-21 12:32:24 -0800713 error = freeze_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700714 if (error)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200715 goto Exit;
716
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200717 lock_device_hotplug();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200718 /* Allocate memory management structures */
719 error = create_basic_memory_bitmaps();
720 if (error)
721 goto Thaw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700723 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100724 if (error || freezer_test_done)
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200725 goto Free_bitmaps;
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200726
727 if (in_suspend) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700728 unsigned int flags = 0;
729
730 if (hibernation_mode == HIBERNATION_PLATFORM)
731 flags |= SF_PLATFORM_MODE;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200732 if (nocompress)
733 flags |= SF_NOCOMPRESS_MODE;
Bojan Smojver081a9d02011-10-13 23:58:07 +0200734 else
735 flags |= SF_CRC32_MODE;
736
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200737 pm_pr_dbg("Writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700738 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700739 swsusp_free();
Chen Yufe12c002016-07-22 10:30:47 +0800740 if (!error) {
741 if (hibernation_mode == HIBERNATION_TEST_RESUME)
742 snapshot_test = true;
743 else
744 power_down();
745 }
MyungJoo Ham5262a472010-11-26 23:07:56 +0100746 in_suspend = 0;
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100747 pm_restore_gfp_mask();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800748 } else {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200749 pm_pr_dbg("Image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800750 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200751
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200752 Free_bitmaps:
753 free_basic_memory_bitmaps();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800754 Thaw:
Rafael J. Wysocki942f4012013-08-30 14:19:46 +0200755 unlock_device_hotplug();
Chen Yufe12c002016-07-22 10:30:47 +0800756 if (snapshot_test) {
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200757 pm_pr_dbg("Checking hibernation image\n");
Chen Yufe12c002016-07-22 10:30:47 +0800758 error = swsusp_check();
759 if (!error)
760 error = load_image_and_restore();
761 }
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100762 thaw_processes();
Srivatsa S. Bhata556d5b52012-02-04 23:39:56 +0100763
764 /* Don't bother checking whether freezer_test_done is true */
765 freezer_test_done = false;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700766 Exit:
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700767 __pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100768 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700769 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700770 Unlock:
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100771 unlock_system_sleep();
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200772 pr_info("hibernation exit\n");
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return error;
775}
776
777
778/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200779 * software_resume - Resume from a saved hibernation image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200781 * This routine is called as a late initcall, when all devices have been
782 * discovered and initialized already.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200784 * The image reading code is called to see if there is a hibernation image
785 * available for reading. If that is the case, devices are quiesced and the
786 * contents of memory is restored from the saved image.
787 *
788 * If this is successful, control reappears in the restored target kernel in
Geliang Tangd439e642015-09-29 20:36:58 -0700789 * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200790 * attempts to recover gracefully and make the kernel return to the normal mode
791 * of operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793static int software_resume(void)
794{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700795 int error, nr_calls = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Johannes Berg60a0d232007-11-14 17:00:16 -0800797 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100798 * If the user said "noresume".. bail out early.
799 */
Kees Cooka6e15a32014-06-13 13:30:35 -0700800 if (noresume || !hibernation_available())
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100801 return 0;
802
803 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800804 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
805 * is configured into the kernel. Since the regular hibernate
806 * trigger path is via sysfs which takes a buffer mutex before
Pingfan Liu55f25032018-07-31 16:51:32 +0800807 * calling hibernate functions (which take system_transition_mutex)
808 * this can cause lockdep to complain about a possible ABBA deadlock
Johannes Berg60a0d232007-11-14 17:00:16 -0800809 * which cannot happen since we're in the boot code here and
810 * sysfs can't be invoked yet. Therefore, we use a subclass
811 * here to avoid lockdep complaining.
812 */
Pingfan Liu55f25032018-07-31 16:51:32 +0800813 mutex_lock_nested(&system_transition_mutex, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200814
815 if (swsusp_resume_device)
816 goto Check_image;
817
818 if (!strlen(resume_file)) {
819 error = -ENOENT;
820 goto Unlock;
821 }
822
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200823 pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200824
Barry Songf126f732011-10-10 23:38:41 +0200825 if (resume_delay) {
Rafael J. Wysocki2872de12017-02-24 00:26:15 +0100826 pr_info("Waiting %dsec before reading resume device ...\n",
Barry Songf126f732011-10-10 23:38:41 +0200827 resume_delay);
828 ssleep(resume_delay);
829 }
830
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200831 /* Check if the device is there */
832 swsusp_resume_device = name_to_dev_t(resume_file);
Minho Ban2df83fa2012-05-14 21:45:31 +0200833
834 /*
835 * name_to_dev_t is ineffective to verify parition if resume_file is in
836 * integer format. (e.g. major:minor)
837 */
838 if (isdigit(resume_file[0]) && resume_wait) {
839 int partno;
840 while (!get_gendisk(swsusp_resume_device, &partno))
841 msleep(10);
842 }
843
Pavel Machek3efa1472005-07-07 17:56:43 -0700844 if (!swsusp_resume_device) {
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100845 /*
846 * Some device discovery might still be in progress; we need
847 * to wait for this to finish.
848 */
849 wait_for_device_probe();
Barry Song6f8d7022011-10-06 20:34:46 +0200850
851 if (resume_wait) {
852 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
853 msleep(10);
854 async_synchronize_full();
855 }
856
Pavel Machek3efa1472005-07-07 17:56:43 -0700857 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200858 if (!swsusp_resume_device) {
859 error = -ENODEV;
860 goto Unlock;
861 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700862 }
863
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200864 Check_image:
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200865 pm_pr_dbg("Hibernation image partition %d:%d present\n",
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200866 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200868 pm_pr_dbg("Looking for hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800869 error = swsusp_check();
870 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700871 goto Unlock;
872
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700873 /* The snapshot device should not be opened while we're running */
874 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
875 error = -EBUSY;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200876 swsusp_close(FMODE_READ);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700877 goto Unlock;
878 }
879
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200880 pr_info("resume from hibernation\n");
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100881 pm_prepare_console();
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700882 error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
883 if (error) {
884 nr_calls--;
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200885 goto Close_Finish;
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700886 }
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700887
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200888 pm_pr_dbg("Preparing processes for restore.\n");
Tejun Heo03afed82011-11-21 12:32:24 -0800889 error = freeze_processes();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200890 if (error)
891 goto Close_Finish;
Chen Yufe12c002016-07-22 10:30:47 +0800892 error = load_image_and_restore();
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200893 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700894 Finish:
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700895 __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100896 pm_restore_console();
Rafael J. Wysocki8915aa202017-07-20 03:38:07 +0200897 pr_info("resume from hibernation failed (%d)\n", error);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700898 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700899 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700900 Unlock:
Pingfan Liu55f25032018-07-31 16:51:32 +0800901 mutex_unlock(&system_transition_mutex);
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200902 pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700903 return error;
Rafael J. Wysocki8fd37a42013-08-30 14:19:38 +0200904 Close_Finish:
Jiri Slaby76b57e62009-10-07 22:37:35 +0200905 swsusp_close(FMODE_READ);
906 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
Russ Dilld3c345d2013-10-24 14:25:26 +0100909late_initcall_sync(software_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700912static const char * const hibernation_modes[] = {
913 [HIBERNATION_PLATFORM] = "platform",
914 [HIBERNATION_SHUTDOWN] = "shutdown",
915 [HIBERNATION_REBOOT] = "reboot",
Bojan Smojver62c552c2012-06-16 00:09:58 +0200916#ifdef CONFIG_SUSPEND
917 [HIBERNATION_SUSPEND] = "suspend",
918#endif
Chen Yufe12c002016-07-22 10:30:47 +0800919 [HIBERNATION_TEST_RESUME] = "test_resume",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920};
921
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200922/*
923 * /sys/power/disk - Control hibernation mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200925 * Hibernation can be handled in several ways. There are a few different ways
926 * to put the system into the sleep state: using the platform driver (e.g. ACPI
927 * or other hibernation_ops), powering it off or rebooting it (for testing
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100928 * mostly).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200930 * The sysfs file /sys/power/disk provides an interface for selecting the
931 * hibernation mode to use. Reading from this file causes the available modes
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100932 * to be printed. There are 3 modes that can be supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 * 'platform'
935 * 'shutdown'
936 * 'reboot'
937 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200938 * If a platform hibernation driver is in use, 'platform' will be supported
939 * and will be used by default. Otherwise, 'shutdown' will be used by default.
940 * The selected option (i.e. the one corresponding to the current value of
941 * hibernation_mode) is enclosed by a square bracket.
942 *
943 * To select a given hibernation mode it is necessary to write the mode's
944 * string representation (as returned by reading from /sys/power/disk) back
945 * into /sys/power/disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 */
947
Kay Sievers386f2752007-11-02 13:47:53 +0100948static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
949 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700951 int i;
952 char *start = buf;
953
Kees Cooka6e15a32014-06-13 13:30:35 -0700954 if (!hibernation_available())
955 return sprintf(buf, "[disabled]\n");
956
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700957 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
958 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700959 continue;
960 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700961 case HIBERNATION_SHUTDOWN:
962 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +0200963#ifdef CONFIG_SUSPEND
964 case HIBERNATION_SUSPEND:
965#endif
Chen Yufe12c002016-07-22 10:30:47 +0800966 case HIBERNATION_TEST_RESUME:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700967 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700968 case HIBERNATION_PLATFORM:
969 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700970 break;
971 /* not a valid mode, continue with loop */
972 continue;
973 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700974 if (i == hibernation_mode)
975 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700976 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700977 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700978 }
979 buf += sprintf(buf, "\n");
980 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Kay Sievers386f2752007-11-02 13:47:53 +0100983static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
984 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 int error = 0;
987 int i;
988 int len;
989 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700990 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Kees Cooka6e15a32014-06-13 13:30:35 -0700992 if (!hibernation_available())
993 return -EPERM;
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 p = memchr(buf, '\n', n);
996 len = p ? p - buf : n;
997
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100998 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700999 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -07001000 if (len == strlen(hibernation_modes[i])
1001 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 mode = i;
1003 break;
1004 }
1005 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001006 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001007 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001008 case HIBERNATION_SHUTDOWN:
1009 case HIBERNATION_REBOOT:
Bojan Smojver62c552c2012-06-16 00:09:58 +02001010#ifdef CONFIG_SUSPEND
1011 case HIBERNATION_SUSPEND:
1012#endif
Chen Yufe12c002016-07-22 10:30:47 +08001013 case HIBERNATION_TEST_RESUME:
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001014 hibernation_mode = mode;
Johannes Bergfe0c935a2007-04-30 15:09:51 -07001015 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001016 case HIBERNATION_PLATFORM:
1017 if (hibernation_ops)
1018 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 else
1020 error = -EINVAL;
1021 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001022 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 error = -EINVAL;
1024
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -07001025 if (!error)
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +02001026 pm_pr_dbg("Hibernation mode set to '%s'\n",
1027 hibernation_modes[mode]);
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001028 unlock_system_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return error ? error : n;
1030}
1031
1032power_attr(disk);
1033
Kay Sievers386f2752007-11-02 13:47:53 +01001034static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
1035 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
1038 MINOR(swsusp_resume_device));
1039}
1040
Kay Sievers386f2752007-11-02 13:47:53 +01001041static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1042 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 dev_t res;
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001045 int len = n;
1046 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001048 if (len && buf[len-1] == '\n')
1049 len--;
1050 name = kstrndup(buf, len, GFP_KERNEL);
1051 if (!name)
1052 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001054 res = name_to_dev_t(name);
1055 kfree(name);
1056 if (!res)
1057 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001059 lock_system_sleep();
Andrew Mortona5762192006-01-06 00:09:50 -08001060 swsusp_resume_device = res;
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +01001061 unlock_system_sleep();
Mario Limonciello64846402018-03-28 12:01:10 -05001062 pm_pr_dbg("Configured resume from disk to %u\n", swsusp_resume_device);
Andrew Mortona5762192006-01-06 00:09:50 -08001063 noresume = 0;
1064 software_resume();
Sebastian Capella421a5fa2014-02-14 14:52:56 -08001065 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
1068power_attr(resume);
1069
Mario Limonciello35506462018-03-28 12:01:09 -05001070static ssize_t resume_offset_show(struct kobject *kobj,
1071 struct kobj_attribute *attr, char *buf)
1072{
1073 return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block);
1074}
1075
1076static ssize_t resume_offset_store(struct kobject *kobj,
1077 struct kobj_attribute *attr, const char *buf,
1078 size_t n)
1079{
1080 unsigned long long offset;
1081 int rc;
1082
1083 rc = kstrtoull(buf, 0, &offset);
1084 if (rc)
1085 return rc;
1086 swsusp_resume_block = offset;
1087
1088 return n;
1089}
1090
1091power_attr(resume_offset);
1092
Kay Sievers386f2752007-11-02 13:47:53 +01001093static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1094 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001095{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001096 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001097}
1098
Kay Sievers386f2752007-11-02 13:47:53 +01001099static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1100 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001101{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001102 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001103
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001104 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001105 image_size = size;
1106 return n;
1107 }
1108
1109 return -EINVAL;
1110}
1111
1112power_attr(image_size);
1113
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001114static ssize_t reserved_size_show(struct kobject *kobj,
1115 struct kobj_attribute *attr, char *buf)
1116{
1117 return sprintf(buf, "%lu\n", reserved_size);
1118}
1119
1120static ssize_t reserved_size_store(struct kobject *kobj,
1121 struct kobj_attribute *attr,
1122 const char *buf, size_t n)
1123{
1124 unsigned long size;
1125
1126 if (sscanf(buf, "%lu", &size) == 1) {
1127 reserved_size = size;
1128 return n;
1129 }
1130
1131 return -EINVAL;
1132}
1133
1134power_attr(reserved_size);
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136static struct attribute * g[] = {
1137 &disk_attr.attr,
Mario Limonciello35506462018-03-28 12:01:09 -05001138 &resume_offset_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001140 &image_size_attr.attr,
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001141 &reserved_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 NULL,
1143};
1144
1145
Arvind Yadav59494fe2017-06-29 16:58:40 +05301146static const struct attribute_group attr_group = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 .attrs = g,
1148};
1149
1150
1151static int __init pm_disk_init(void)
1152{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -08001153 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
1156core_initcall(pm_disk_init);
1157
1158
1159static int __init resume_setup(char *str)
1160{
1161 if (noresume)
1162 return 1;
1163
1164 strncpy( resume_file, str, 255 );
1165 return 1;
1166}
1167
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001168static int __init resume_offset_setup(char *str)
1169{
1170 unsigned long long offset;
1171
1172 if (noresume)
1173 return 1;
1174
1175 if (sscanf(str, "%llu", &offset) == 1)
1176 swsusp_resume_block = offset;
1177
1178 return 1;
1179}
1180
Bojan Smojverf996fc92010-09-09 23:06:23 +02001181static int __init hibernate_setup(char *str)
1182{
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001183 if (!strncmp(str, "noresume", 8)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001184 noresume = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001185 } else if (!strncmp(str, "nocompress", 10)) {
Bojan Smojverf996fc92010-09-09 23:06:23 +02001186 nocompress = 1;
Rafael J. Wysocki2f88e412016-07-06 02:40:56 +02001187 } else if (!strncmp(str, "no", 2)) {
Kees Cooka6e15a32014-06-13 13:30:35 -07001188 noresume = 1;
1189 nohibernate = 1;
Laura Abbott0f5bf6d2017-02-06 16:31:58 -08001190 } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)
Rafael J. Wysocki4c0b6c12016-07-10 02:12:10 +02001191 && !strncmp(str, "protect_image", 13)) {
1192 enable_restore_image_protection();
Kees Cooka6e15a32014-06-13 13:30:35 -07001193 }
Bojan Smojverf996fc92010-09-09 23:06:23 +02001194 return 1;
1195}
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197static int __init noresume_setup(char *str)
1198{
1199 noresume = 1;
1200 return 1;
1201}
1202
Barry Song6f8d7022011-10-06 20:34:46 +02001203static int __init resumewait_setup(char *str)
1204{
1205 resume_wait = 1;
1206 return 1;
1207}
1208
Barry Songf126f732011-10-10 23:38:41 +02001209static int __init resumedelay_setup(char *str)
1210{
Dan Carpenterf6514be2014-05-14 19:08:46 +03001211 int rc = kstrtouint(str, 0, &resume_delay);
Fabian Frederick317cf7e2014-05-09 23:32:08 +02001212
1213 if (rc)
1214 return rc;
Barry Songf126f732011-10-10 23:38:41 +02001215 return 1;
1216}
1217
Kees Cooka6e15a32014-06-13 13:30:35 -07001218static int __init nohibernate_setup(char *str)
1219{
1220 noresume = 1;
1221 nohibernate = 1;
1222 return 1;
1223}
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001226__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227__setup("resume=", resume_setup);
Bojan Smojverf996fc92010-09-09 23:06:23 +02001228__setup("hibernate=", hibernate_setup);
Barry Song6f8d7022011-10-06 20:34:46 +02001229__setup("resumewait", resumewait_setup);
Barry Songf126f732011-10-10 23:38:41 +02001230__setup("resumedelay=", resumedelay_setup);
Kees Cooka6e15a32014-06-13 13:30:35 -07001231__setup("nohibernate", nohibernate_setup);