blob: e09dfbfeecee43ca7fff59c327f213f9605ef0f4 [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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040012#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/suspend.h>
14#include <linux/syscalls.h>
15#include <linux/reboot.h>
16#include <linux/string.h>
17#include <linux/device.h>
Barry Song6f8d7022011-10-06 20:34:46 +020018#include <linux/async.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/delay.h>
20#include <linux/fs.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070021#include <linux/mount.h>
Eric W. Biederman88d10bb2005-09-22 21:43:46 -070022#include <linux/pm.h>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070023#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070024#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080025#include <linux/freezer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/gfp.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010027#include <linux/syscore_ops.h>
Rafael J. Wysockic7510852009-04-12 20:06:56 +020028#include <scsi/scsi_scan.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "power.h"
31
32
Barry Songd231ff12011-10-11 23:29:18 -070033static int nocompress;
34static int noresume;
35static int resume_wait;
36static int resume_delay;
Adrian Bunk47a460d2008-02-04 22:30:06 -080037static char resume_file[256] = CONFIG_PM_STD_PARTITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080039sector_t swsusp_resume_block;
Barry Songd231ff12011-10-11 23:29:18 -070040int in_suspend __nosavedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070042enum {
43 HIBERNATION_INVALID,
44 HIBERNATION_PLATFORM,
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070045 HIBERNATION_SHUTDOWN,
46 HIBERNATION_REBOOT,
47 /* keep last */
48 __HIBERNATION_AFTER_LAST
49};
50#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
51#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
52
53static int hibernation_mode = HIBERNATION_SHUTDOWN;
54
Srivatsa S. Bhat97819a22011-12-01 22:33:10 +010055bool freezer_test_done;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +010056
Lionel Debroux073ef1f2010-11-09 21:48:49 +010057static const struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070058
59/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +020060 * hibernation_set_ops - Set the global hibernate operations.
61 * @ops: Hibernation operations to use in subsequent hibernation transitions.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070062 */
Lionel Debroux073ef1f2010-11-09 21:48:49 +010063void hibernation_set_ops(const struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070064{
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +010065 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
66 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
MyungJoo Ham5729c632010-11-26 23:07:48 +010067 && ops->restore_cleanup && ops->leave)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070068 WARN_ON(1);
69 return;
70 }
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010071 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070072 hibernation_ops = ops;
73 if (ops)
74 hibernation_mode = HIBERNATION_PLATFORM;
75 else if (hibernation_mode == HIBERNATION_PLATFORM)
76 hibernation_mode = HIBERNATION_SHUTDOWN;
77
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010078 unlock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070079}
80
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +010081static bool entering_platform_hibernation;
82
83bool system_entering_hibernation(void)
84{
85 return entering_platform_hibernation;
86}
87EXPORT_SYMBOL(system_entering_hibernation);
88
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +010089#ifdef CONFIG_PM_DEBUG
90static void hibernation_debug_sleep(void)
91{
92 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
93 mdelay(5000);
94}
95
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +010096static int hibernation_test(int level)
97{
98 if (pm_test_level == level) {
99 hibernation_debug_sleep();
100 return 1;
101 }
102 return 0;
103}
104#else /* !CONFIG_PM_DEBUG */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100105static int hibernation_test(int level) { return 0; }
106#endif /* !CONFIG_PM_DEBUG */
107
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700108/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200109 * platform_begin - Call platform to start hibernation.
110 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700111 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100112static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700113{
114 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100115 hibernation_ops->begin() : 0;
116}
117
118/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200119 * platform_end - Call platform to finish transition to the working state.
120 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100121 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100122static void platform_end(int platform_mode)
123{
124 if (platform_mode && hibernation_ops)
125 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700126}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200129 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
130 * @platform_mode: Whether or not to use the platform driver.
131 *
132 * Use the platform driver to prepare the system for creating a hibernate image,
133 * if so configured, and return an error code if that fails.
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800134 */
135
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700136static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800137{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700138 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700139 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800140}
141
142/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200143 * platform_leave - Call platform to prepare a transition to the working state.
144 * @platform_mode: Whether or not to use the platform driver.
145 *
146 * Use the platform driver prepare to prepare the machine for switching to the
147 * normal mode of operation.
148 *
149 * This routine is called on one CPU with interrupts disabled.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700150 */
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700151static void platform_leave(int platform_mode)
152{
153 if (platform_mode && hibernation_ops)
154 hibernation_ops->leave();
155}
156
157/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200158 * platform_finish - Call platform to switch the system to the working state.
159 * @platform_mode: Whether or not to use the platform driver.
160 *
161 * Use the platform driver to switch the machine to the normal mode of
162 * operation.
163 *
164 * This routine must be called after platform_prepare().
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700165 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700166static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700167{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700168 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700169 hibernation_ops->finish();
170}
171
172/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200173 * platform_pre_restore - Prepare for hibernate image restoration.
174 * @platform_mode: Whether or not to use the platform driver.
175 *
176 * Use the platform driver to prepare the system for resume from a hibernation
177 * image.
178 *
179 * If the restore fails after this function has been called,
180 * platform_restore_cleanup() must be called.
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700181 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700182static int platform_pre_restore(int platform_mode)
183{
184 return (platform_mode && hibernation_ops) ?
185 hibernation_ops->pre_restore() : 0;
186}
187
188/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200189 * platform_restore_cleanup - Switch to the working state after failing restore.
190 * @platform_mode: Whether or not to use the platform driver.
191 *
192 * Use the platform driver to switch the system to the normal mode of operation
193 * after a failing restore.
194 *
195 * If platform_pre_restore() has been called before the failing restore, this
196 * function must be called too, regardless of the result of
197 * platform_pre_restore().
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700198 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700199static void platform_restore_cleanup(int platform_mode)
200{
201 if (platform_mode && hibernation_ops)
202 hibernation_ops->restore_cleanup();
203}
204
205/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200206 * platform_recover - Recover from a failure to suspend devices.
207 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200208 */
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200209static void platform_recover(int platform_mode)
210{
211 if (platform_mode && hibernation_ops && hibernation_ops->recover)
212 hibernation_ops->recover();
213}
214
215/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200216 * swsusp_show_speed - Print time elapsed between two events during hibernation.
217 * @start: Starting event.
218 * @stop: Final event.
219 * @nr_pages: Number of memory pages processed between @start and @stop.
220 * @msg: Additional diagnostic message to print.
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100221 */
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100222void swsusp_show_speed(struct timeval *start, struct timeval *stop,
223 unsigned nr_pages, char *msg)
224{
225 s64 elapsed_centisecs64;
226 int centisecs;
227 int k;
228 int kps;
229
230 elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
231 do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
232 centisecs = elapsed_centisecs64;
233 if (centisecs == 0)
234 centisecs = 1; /* avoid div-by-zero */
235 k = nr_pages * (PAGE_SIZE / 1024);
236 kps = (k * 100) / centisecs;
237 printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
238 msg, k,
239 centisecs / 100, centisecs % 100,
240 kps / 1000, (kps % 1000) / 10);
241}
242
243/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200244 * create_image - Create a hibernation image.
245 * @platform_mode: Whether or not to use the platform driver.
246 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100247 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
248 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200249 *
250 * Control reappears in this routine after the subsequent restore.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700251 */
Adrian Bunk47a460d2008-02-04 22:30:06 -0800252static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700253{
254 int error;
255
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100256 error = dpm_suspend_end(PMSG_FREEZE);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700257 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100258 printk(KERN_ERR "PM: Some devices failed to power down, "
259 "aborting hibernation\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200260 return error;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700261 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100262
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100263 error = platform_pre_snapshot(platform_mode);
264 if (error || hibernation_test(TEST_PLATFORM))
265 goto Platform_finish;
266
267 error = disable_nonboot_cpus();
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100268 if (error || hibernation_test(TEST_CPUS))
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100269 goto Enable_cpus;
270
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100271 local_irq_disable();
272
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200273 error = syscore_suspend();
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100274 if (error) {
Bjorn Helgaas44840792009-05-15 23:30:50 +0200275 printk(KERN_ERR "PM: Some system devices failed to power down, "
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100276 "aborting hibernation\n");
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100277 goto Enable_irqs;
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100278 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700279
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100280 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100281 goto Power_up;
282
283 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700284 save_processor_state();
285 error = swsusp_arch_suspend();
286 if (error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100287 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
288 error);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700289 /* Restore control flow magically appears here */
290 restore_processor_state();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200291 if (!in_suspend) {
292 events_check_enabled = false;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700293 platform_leave(platform_mode);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200294 }
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100295
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100296 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100297 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100298
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100299 Enable_irqs:
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100300 local_irq_enable();
301
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100302 Enable_cpus:
303 enable_nonboot_cpus();
304
305 Platform_finish:
306 platform_finish(platform_mode);
307
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100308 dpm_resume_start(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200309 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100310
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700311 return error;
312}
313
314/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200315 * hibernation_snapshot - Quiesce devices and create a hibernation image.
316 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700317 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200318 * This routine must be called with pm_mutex held.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700319 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700320int hibernation_snapshot(int platform_mode)
321{
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100322 pm_message_t msg;
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100323 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700324
Zhang Rui3fe03132008-10-26 20:50:26 +0100325 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700326 if (error)
Rafael J. Wysockid074ee02010-07-07 23:43:35 +0200327 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700328
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200329 /* Preallocate image memory before shutting down devices. */
330 error = hibernate_preallocate_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700331 if (error)
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200332 goto Close;
333
334 error = freeze_kernel_threads();
335 if (error)
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100336 goto Cleanup;
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200337
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100338 if (hibernation_test(TEST_FREEZER)) {
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100339
340 /*
341 * Indicate to the caller that we are returning due to a
342 * successful freezer test.
343 */
344 freezer_test_done = true;
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100345 goto Thaw;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100346 }
347
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200348 error = dpm_prepare(PMSG_FREEZE);
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100349 if (error) {
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100350 dpm_complete(PMSG_RECOVER);
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100351 goto Thaw;
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100352 }
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700353
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700354 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100355 pm_restrict_gfp_mask();
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100356
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200357 error = dpm_suspend(PMSG_FREEZE);
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700358
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100359 if (error || hibernation_test(TEST_DEVICES))
360 platform_recover(platform_mode);
361 else
362 error = create_image(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700363
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100364 /*
Srivatsa S. Bhat953a2062011-11-22 23:20:31 +0100365 * In the case that we call create_image() above, the control
366 * returns here (1) after the image has been created or the
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100367 * image creation has failed and (2) after a successful restore.
368 */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100369
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200370 /* We may need to release the preallocated image pages here. */
371 if (error || !in_suspend)
372 swsusp_free();
373
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200374 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
375 dpm_resume(msg);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100376
377 if (error || !in_suspend)
378 pm_restore_gfp_mask();
379
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700380 resume_console();
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200381 dpm_complete(msg);
382
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100383 Close:
384 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700385 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200386
Srivatsa S. Bhat51d6ff72012-02-04 22:26:38 +0100387 Thaw:
388 thaw_kernel_threads();
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100389 Cleanup:
390 swsusp_free();
391 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700392}
393
394/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200395 * resume_target_kernel - Restore system state from a hibernation image.
396 * @platform_mode: Whether or not to use the platform driver.
397 *
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100398 * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
399 * contents of highmem that have not been restored yet from the image and run
400 * the low-level code that will restore the remaining contents of memory and
401 * switch to the just restored target kernel.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100402 */
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100403static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100404{
405 int error;
406
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100407 error = dpm_suspend_end(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100408 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100409 printk(KERN_ERR "PM: Some devices failed to power down, "
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100410 "aborting resume\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200411 return error;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100412 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100413
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100414 error = platform_pre_restore(platform_mode);
415 if (error)
416 goto Cleanup;
417
418 error = disable_nonboot_cpus();
419 if (error)
420 goto Enable_cpus;
421
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100422 local_irq_disable();
423
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200424 error = syscore_suspend();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100425 if (error)
426 goto Enable_irqs;
427
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100428 save_processor_state();
429 error = restore_highmem();
430 if (!error) {
431 error = swsusp_arch_resume();
432 /*
433 * The code below is only ever reached in case of a failure.
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200434 * Otherwise, execution continues at the place where
435 * swsusp_arch_suspend() was called.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100436 */
437 BUG_ON(!error);
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200438 /*
439 * This call to restore_highmem() reverts the changes made by
440 * the previous one.
441 */
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100442 restore_highmem();
443 }
444 /*
445 * The only reason why swsusp_arch_resume() can fail is memory being
446 * very tight, so we have to free it as soon as we can to avoid
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200447 * subsequent failures.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100448 */
449 swsusp_free();
450 restore_processor_state();
451 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100452
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100453 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100454
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100455 Enable_irqs:
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100456 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100457
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100458 Enable_cpus:
459 enable_nonboot_cpus();
460
461 Cleanup:
462 platform_restore_cleanup(platform_mode);
463
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100464 dpm_resume_start(PMSG_RECOVER);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100465
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100466 return error;
467}
468
469/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200470 * hibernation_restore - Quiesce devices and restore from a hibernation image.
471 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700472 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200473 * This routine must be called with pm_mutex held. If it is successful, control
Barry Song21e82802011-09-27 22:05:44 +0200474 * reappears in the restored target kernel in hibernation_snapshot().
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700475 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700476int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700477{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100478 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700479
480 pm_prepare_console();
481 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100482 pm_restrict_gfp_mask();
Alan Sternd1616302009-05-24 22:05:42 +0200483 error = dpm_suspend_start(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700484 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100485 error = resume_target_kernel(platform_mode);
Alan Sternd1616302009-05-24 22:05:42 +0200486 dpm_resume_end(PMSG_RECOVER);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700487 }
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100488 pm_restore_gfp_mask();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700489 resume_console();
490 pm_restore_console();
491 return error;
492}
493
494/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200495 * hibernation_platform_enter - Power off the system using the platform driver.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700496 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700497int hibernation_platform_enter(void)
498{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100499 int error;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700500
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700501 if (!hibernation_ops)
502 return -ENOSYS;
503
504 /*
505 * We have cancelled the power transition by running
506 * hibernation_ops->finish() before saving the image, so we should let
507 * the firmware know that we're going to enter the sleep state after all
508 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100509 error = hibernation_ops->begin();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700510 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100511 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700512
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100513 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700514 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +0200515 error = dpm_suspend_start(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200516 if (error) {
517 if (hibernation_ops->recover)
518 hibernation_ops->recover();
519 goto Resume_devices;
520 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700521
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100522 error = dpm_suspend_end(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100523 if (error)
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200524 goto Resume_devices;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100525
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100526 error = hibernation_ops->prepare();
527 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200528 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100529
530 error = disable_nonboot_cpus();
531 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200532 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100533
534 local_irq_disable();
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100535 syscore_suspend();
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100536 if (pm_wakeup_pending()) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200537 error = -EAGAIN;
538 goto Power_up;
539 }
540
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100541 hibernation_ops->enter();
542 /* We should never get here */
543 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700544
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200545 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100546 syscore_resume();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200547 local_irq_enable();
548 enable_nonboot_cpus();
549
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200550 Platform_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700551 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100552
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100553 dpm_resume_start(PMSG_RESTORE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100554
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700555 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100556 entering_platform_hibernation = false;
Alan Sternd1616302009-05-24 22:05:42 +0200557 dpm_resume_end(PMSG_RESTORE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700558 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100559
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100560 Close:
561 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100562
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700563 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700564}
565
566/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200567 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200569 * Use the platform driver, if configured, to put the system into the sleep
570 * state corresponding to hibernation, or try to power it off or reboot,
571 * depending on the value of hibernation_mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 */
Johannes Bergfe0c9352007-04-30 15:09:51 -0700573static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700575 switch (hibernation_mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700576 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600577 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700579 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700580 hibernation_platform_enter();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700581 case HIBERNATION_SHUTDOWN:
582 kernel_power_off();
583 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600585 kernel_halt();
Johannes Bergfe0c9352007-04-30 15:09:51 -0700586 /*
587 * Valid image is on the disk, if we continue we risk serious data
588 * corruption after resume.
589 */
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100590 printk(KERN_CRIT "PM: Please power down manually\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 while(1);
592}
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200595 * hibernate - Carry out system hibernation, including saving the image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700597int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 int error;
600
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100601 lock_system_sleep();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700602 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700603 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
604 error = -EBUSY;
605 goto Unlock;
606 }
607
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100608 pm_prepare_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700609 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
610 if (error)
611 goto Exit;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700612
613 /* Allocate memory management structures */
614 error = create_basic_memory_bitmaps();
615 if (error)
Rafael J. Wysocki7b5179a2012-03-28 23:30:14 +0200616 goto Exit;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700617
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100618 printk(KERN_INFO "PM: Syncing filesystems ... ");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700619 sys_sync();
620 printk("done.\n");
621
Tejun Heo03afed82011-11-21 12:32:24 -0800622 error = freeze_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700623 if (error)
Srivatsa S. Bhat05b48772012-02-17 23:39:51 +0100624 goto Free_bitmaps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700626 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
Srivatsa S. Bhata556d5b2012-02-04 23:39:56 +0100627 if (error || freezer_test_done)
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200628 goto Thaw;
629
630 if (in_suspend) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700631 unsigned int flags = 0;
632
633 if (hibernation_mode == HIBERNATION_PLATFORM)
634 flags |= SF_PLATFORM_MODE;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200635 if (nocompress)
636 flags |= SF_NOCOMPRESS_MODE;
Bojan Smojver081a9d02011-10-13 23:58:07 +0200637 else
638 flags |= SF_CRC32_MODE;
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700641 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700642 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (!error)
Johannes Bergfe0c9352007-04-30 15:09:51 -0700644 power_down();
MyungJoo Ham5262a472010-11-26 23:07:56 +0100645 in_suspend = 0;
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100646 pm_restore_gfp_mask();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800647 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800649 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200650
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800651 Thaw:
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100652 thaw_processes();
Srivatsa S. Bhata556d5b2012-02-04 23:39:56 +0100653
654 /* Don't bother checking whether freezer_test_done is true */
655 freezer_test_done = false;
656
Srivatsa S. Bhat05b48772012-02-17 23:39:51 +0100657 Free_bitmaps:
Rafael J. Wysocki7b5179a2012-03-28 23:30:14 +0200658 free_basic_memory_bitmaps();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700659 Exit:
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700660 pm_notifier_call_chain(PM_POST_HIBERNATION);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100661 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700662 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700663 Unlock:
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100664 unlock_system_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return error;
666}
667
668
669/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200670 * software_resume - Resume from a saved hibernation image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200672 * This routine is called as a late initcall, when all devices have been
673 * discovered and initialized already.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200675 * The image reading code is called to see if there is a hibernation image
676 * available for reading. If that is the case, devices are quiesced and the
677 * contents of memory is restored from the saved image.
678 *
679 * If this is successful, control reappears in the restored target kernel in
680 * hibernation_snaphot() which returns to hibernate(). Otherwise, the routine
681 * attempts to recover gracefully and make the kernel return to the normal mode
682 * of operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684static int software_resume(void)
685{
686 int error;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700687 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Johannes Berg60a0d232007-11-14 17:00:16 -0800689 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100690 * If the user said "noresume".. bail out early.
691 */
692 if (noresume)
693 return 0;
694
695 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800696 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
697 * is configured into the kernel. Since the regular hibernate
698 * trigger path is via sysfs which takes a buffer mutex before
699 * calling hibernate functions (which take pm_mutex) this can
700 * cause lockdep to complain about a possible ABBA deadlock
701 * which cannot happen since we're in the boot code here and
702 * sysfs can't be invoked yet. Therefore, we use a subclass
703 * here to avoid lockdep complaining.
704 */
705 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200706
707 if (swsusp_resume_device)
708 goto Check_image;
709
710 if (!strlen(resume_file)) {
711 error = -ENOENT;
712 goto Unlock;
713 }
714
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200715 pr_debug("PM: Checking hibernation image partition %s\n", resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200716
Barry Songf126f732011-10-10 23:38:41 +0200717 if (resume_delay) {
718 printk(KERN_INFO "Waiting %dsec before reading resume device...\n",
719 resume_delay);
720 ssleep(resume_delay);
721 }
722
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200723 /* Check if the device is there */
724 swsusp_resume_device = name_to_dev_t(resume_file);
Pavel Machek3efa1472005-07-07 17:56:43 -0700725 if (!swsusp_resume_device) {
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100726 /*
727 * Some device discovery might still be in progress; we need
728 * to wait for this to finish.
729 */
730 wait_for_device_probe();
Barry Song6f8d7022011-10-06 20:34:46 +0200731
732 if (resume_wait) {
733 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
734 msleep(10);
735 async_synchronize_full();
736 }
737
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200738 /*
739 * We can't depend on SCSI devices being available after loading
740 * one of their modules until scsi_complete_async_scans() is
741 * called and the resume device usually is a SCSI one.
742 */
743 scsi_complete_async_scans();
744
Pavel Machek3efa1472005-07-07 17:56:43 -0700745 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200746 if (!swsusp_resume_device) {
747 error = -ENODEV;
748 goto Unlock;
749 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700750 }
751
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200752 Check_image:
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200753 pr_debug("PM: Hibernation image partition %d:%d present\n",
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200754 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200756 pr_debug("PM: Looking for hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800757 error = swsusp_check();
758 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700759 goto Unlock;
760
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700761 /* The snapshot device should not be opened while we're running */
762 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
763 error = -EBUSY;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200764 swsusp_close(FMODE_READ);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700765 goto Unlock;
766 }
767
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100768 pm_prepare_console();
Alan Sternc3e94d82007-11-19 23:38:25 +0100769 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
770 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200771 goto close_finish;
Alan Sternc3e94d82007-11-19 23:38:25 +0100772
Rafael J. Wysocki7b5179a2012-03-28 23:30:14 +0200773 error = create_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700774 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200775 goto close_finish;
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 pr_debug("PM: Preparing processes for restore.\n");
Tejun Heo03afed82011-11-21 12:32:24 -0800778 error = freeze_processes();
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800779 if (error) {
Al Viroc2dd0da2007-10-08 13:21:10 -0400780 swsusp_close(FMODE_READ);
Li Shaohua5a72e042005-06-25 14:55:06 -0700781 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200784 pr_debug("PM: Loading hibernation image.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700786 error = swsusp_read(&flags);
Jiri Slaby76b57e62009-10-07 22:37:35 +0200787 swsusp_close(FMODE_READ);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800788 if (!error)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700789 hibernation_restore(flags & SF_PLATFORM_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200791 printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700792 swsusp_free();
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100793 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 Done:
Rafael J. Wysocki7b5179a2012-03-28 23:30:14 +0200795 free_basic_memory_bitmaps();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700796 Finish:
Alan Sternc3e94d82007-11-19 23:38:25 +0100797 pm_notifier_call_chain(PM_POST_RESTORE);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100798 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700799 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700800 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700801 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800802 mutex_unlock(&pm_mutex);
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200803 pr_debug("PM: Hibernation image not present or could not be loaded.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700804 return error;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200805close_finish:
806 swsusp_close(FMODE_READ);
807 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810late_initcall(software_resume);
811
812
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700813static const char * const hibernation_modes[] = {
814 [HIBERNATION_PLATFORM] = "platform",
815 [HIBERNATION_SHUTDOWN] = "shutdown",
816 [HIBERNATION_REBOOT] = "reboot",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817};
818
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200819/*
820 * /sys/power/disk - Control hibernation mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200822 * Hibernation can be handled in several ways. There are a few different ways
823 * to put the system into the sleep state: using the platform driver (e.g. ACPI
824 * or other hibernation_ops), powering it off or rebooting it (for testing
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100825 * mostly).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200827 * The sysfs file /sys/power/disk provides an interface for selecting the
828 * hibernation mode to use. Reading from this file causes the available modes
Srivatsa S. Bhat48580ab2011-12-01 22:33:20 +0100829 * to be printed. There are 3 modes that can be supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 * 'platform'
832 * 'shutdown'
833 * 'reboot'
834 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200835 * If a platform hibernation driver is in use, 'platform' will be supported
836 * and will be used by default. Otherwise, 'shutdown' will be used by default.
837 * The selected option (i.e. the one corresponding to the current value of
838 * hibernation_mode) is enclosed by a square bracket.
839 *
840 * To select a given hibernation mode it is necessary to write the mode's
841 * string representation (as returned by reading from /sys/power/disk) back
842 * into /sys/power/disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 */
844
Kay Sievers386f2752007-11-02 13:47:53 +0100845static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
846 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700848 int i;
849 char *start = buf;
850
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700851 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
852 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700853 continue;
854 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700855 case HIBERNATION_SHUTDOWN:
856 case HIBERNATION_REBOOT:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700857 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700858 case HIBERNATION_PLATFORM:
859 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700860 break;
861 /* not a valid mode, continue with loop */
862 continue;
863 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700864 if (i == hibernation_mode)
865 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700866 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700867 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700868 }
869 buf += sprintf(buf, "\n");
870 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
Kay Sievers386f2752007-11-02 13:47:53 +0100873static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
874 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 int error = 0;
877 int i;
878 int len;
879 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700880 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 p = memchr(buf, '\n', n);
883 len = p ? p - buf : n;
884
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100885 lock_system_sleep();
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700886 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700887 if (len == strlen(hibernation_modes[i])
888 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 mode = i;
890 break;
891 }
892 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700893 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c9352007-04-30 15:09:51 -0700894 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700895 case HIBERNATION_SHUTDOWN:
896 case HIBERNATION_REBOOT:
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700897 hibernation_mode = mode;
Johannes Bergfe0c9352007-04-30 15:09:51 -0700898 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700899 case HIBERNATION_PLATFORM:
900 if (hibernation_ops)
901 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 else
903 error = -EINVAL;
904 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700905 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 error = -EINVAL;
907
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700908 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100909 pr_debug("PM: Hibernation mode set to '%s'\n",
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700910 hibernation_modes[mode]);
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100911 unlock_system_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return error ? error : n;
913}
914
915power_attr(disk);
916
Kay Sievers386f2752007-11-02 13:47:53 +0100917static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
918 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
921 MINOR(swsusp_resume_device));
922}
923
Kay Sievers386f2752007-11-02 13:47:53 +0100924static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
925 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800929 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Andrew Mortona5762192006-01-06 00:09:50 -0800931 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
932 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Andrew Mortona5762192006-01-06 00:09:50 -0800934 res = MKDEV(maj,min);
935 if (maj != MAJOR(res) || min != MINOR(res))
936 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100938 lock_system_sleep();
Andrew Mortona5762192006-01-06 00:09:50 -0800939 swsusp_resume_device = res;
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100940 unlock_system_sleep();
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100941 printk(KERN_INFO "PM: Starting manual resume from disk\n");
Andrew Mortona5762192006-01-06 00:09:50 -0800942 noresume = 0;
943 software_resume();
944 ret = n;
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800945 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800946 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
949power_attr(resume);
950
Kay Sievers386f2752007-11-02 13:47:53 +0100951static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
952 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800953{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800954 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800955}
956
Kay Sievers386f2752007-11-02 13:47:53 +0100957static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
958 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800959{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800960 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800961
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800962 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800963 image_size = size;
964 return n;
965 }
966
967 return -EINVAL;
968}
969
970power_attr(image_size);
971
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +0200972static ssize_t reserved_size_show(struct kobject *kobj,
973 struct kobj_attribute *attr, char *buf)
974{
975 return sprintf(buf, "%lu\n", reserved_size);
976}
977
978static ssize_t reserved_size_store(struct kobject *kobj,
979 struct kobj_attribute *attr,
980 const char *buf, size_t n)
981{
982 unsigned long size;
983
984 if (sscanf(buf, "%lu", &size) == 1) {
985 reserved_size = size;
986 return n;
987 }
988
989 return -EINVAL;
990}
991
992power_attr(reserved_size);
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994static struct attribute * g[] = {
995 &disk_attr.attr,
996 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800997 &image_size_attr.attr,
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +0200998 &reserved_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 NULL,
1000};
1001
1002
1003static struct attribute_group attr_group = {
1004 .attrs = g,
1005};
1006
1007
1008static int __init pm_disk_init(void)
1009{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -08001010 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
1013core_initcall(pm_disk_init);
1014
1015
1016static int __init resume_setup(char *str)
1017{
1018 if (noresume)
1019 return 1;
1020
1021 strncpy( resume_file, str, 255 );
1022 return 1;
1023}
1024
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001025static int __init resume_offset_setup(char *str)
1026{
1027 unsigned long long offset;
1028
1029 if (noresume)
1030 return 1;
1031
1032 if (sscanf(str, "%llu", &offset) == 1)
1033 swsusp_resume_block = offset;
1034
1035 return 1;
1036}
1037
Bojan Smojverf996fc92010-09-09 23:06:23 +02001038static int __init hibernate_setup(char *str)
1039{
1040 if (!strncmp(str, "noresume", 8))
1041 noresume = 1;
1042 else if (!strncmp(str, "nocompress", 10))
1043 nocompress = 1;
1044 return 1;
1045}
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047static int __init noresume_setup(char *str)
1048{
1049 noresume = 1;
1050 return 1;
1051}
1052
Barry Song6f8d7022011-10-06 20:34:46 +02001053static int __init resumewait_setup(char *str)
1054{
1055 resume_wait = 1;
1056 return 1;
1057}
1058
Barry Songf126f732011-10-10 23:38:41 +02001059static int __init resumedelay_setup(char *str)
1060{
1061 resume_delay = simple_strtoul(str, NULL, 0);
1062 return 1;
1063}
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001066__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067__setup("resume=", resume_setup);
Bojan Smojverf996fc92010-09-09 23:06:23 +02001068__setup("hibernate=", hibernate_setup);
Barry Song6f8d7022011-10-06 20:34:46 +02001069__setup("resumewait", resumewait_setup);
Barry Songf126f732011-10-10 23:38:41 +02001070__setup("resumedelay=", resumedelay_setup);