blob: a6b0503574ee714b2cbe6c871741d298ad9ce5f7 [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>
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -070019#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h>
21#include <linux/fs.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070022#include <linux/mount.h>
Eric W. Biederman88d10bb2005-09-22 21:43:46 -070023#include <linux/pm.h>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070024#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070025#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080026#include <linux/freezer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/gfp.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010028#include <linux/syscore_ops.h>
Rafael J. Wysockic7510852009-04-12 20:06:56 +020029#include <scsi/scsi_scan.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "power.h"
32
33
Barry Songd231ff12011-10-11 23:29:18 -070034static int nocompress;
35static int noresume;
36static int resume_wait;
37static int resume_delay;
Adrian Bunk47a460d2008-02-04 22:30:06 -080038static char resume_file[256] = CONFIG_PM_STD_PARTITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080040sector_t swsusp_resume_block;
Barry Songd231ff12011-10-11 23:29:18 -070041int in_suspend __nosavedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070043enum {
44 HIBERNATION_INVALID,
45 HIBERNATION_PLATFORM,
46 HIBERNATION_TEST,
47 HIBERNATION_TESTPROC,
48 HIBERNATION_SHUTDOWN,
49 HIBERNATION_REBOOT,
50 /* keep last */
51 __HIBERNATION_AFTER_LAST
52};
53#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
54#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
55
56static int hibernation_mode = HIBERNATION_SHUTDOWN;
57
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +010058static bool freezer_test_done;
59
Lionel Debroux073ef1f2010-11-09 21:48:49 +010060static const struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070061
62/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +020063 * hibernation_set_ops - Set the global hibernate operations.
64 * @ops: Hibernation operations to use in subsequent hibernation transitions.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070065 */
Lionel Debroux073ef1f2010-11-09 21:48:49 +010066void hibernation_set_ops(const struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070067{
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +010068 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
69 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
MyungJoo Ham5729c632010-11-26 23:07:48 +010070 && ops->restore_cleanup && ops->leave)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070071 WARN_ON(1);
72 return;
73 }
74 mutex_lock(&pm_mutex);
75 hibernation_ops = ops;
76 if (ops)
77 hibernation_mode = HIBERNATION_PLATFORM;
78 else if (hibernation_mode == HIBERNATION_PLATFORM)
79 hibernation_mode = HIBERNATION_SHUTDOWN;
80
81 mutex_unlock(&pm_mutex);
82}
83
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +010084static bool entering_platform_hibernation;
85
86bool system_entering_hibernation(void)
87{
88 return entering_platform_hibernation;
89}
90EXPORT_SYMBOL(system_entering_hibernation);
91
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +010092#ifdef CONFIG_PM_DEBUG
93static void hibernation_debug_sleep(void)
94{
95 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
96 mdelay(5000);
97}
98
99static int hibernation_testmode(int mode)
100{
101 if (hibernation_mode == mode) {
102 hibernation_debug_sleep();
103 return 1;
104 }
105 return 0;
106}
107
108static int hibernation_test(int level)
109{
110 if (pm_test_level == level) {
111 hibernation_debug_sleep();
112 return 1;
113 }
114 return 0;
115}
116#else /* !CONFIG_PM_DEBUG */
117static int hibernation_testmode(int mode) { return 0; }
118static int hibernation_test(int level) { return 0; }
119#endif /* !CONFIG_PM_DEBUG */
120
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700121/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200122 * platform_begin - Call platform to start hibernation.
123 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700124 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100125static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700126{
127 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100128 hibernation_ops->begin() : 0;
129}
130
131/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200132 * platform_end - Call platform to finish transition to the working state.
133 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100134 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100135static void platform_end(int platform_mode)
136{
137 if (platform_mode && hibernation_ops)
138 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700139}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200142 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
143 * @platform_mode: Whether or not to use the platform driver.
144 *
145 * Use the platform driver to prepare the system for creating a hibernate image,
146 * if so configured, and return an error code if that fails.
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800147 */
148
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700149static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800150{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700151 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700152 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800153}
154
155/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200156 * platform_leave - Call platform to prepare a transition to the working state.
157 * @platform_mode: Whether or not to use the platform driver.
158 *
159 * Use the platform driver prepare to prepare the machine for switching to the
160 * normal mode of operation.
161 *
162 * This routine is called on one CPU with interrupts disabled.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700163 */
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700164static void platform_leave(int platform_mode)
165{
166 if (platform_mode && hibernation_ops)
167 hibernation_ops->leave();
168}
169
170/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200171 * platform_finish - Call platform to switch the system to the working state.
172 * @platform_mode: Whether or not to use the platform driver.
173 *
174 * Use the platform driver to switch the machine to the normal mode of
175 * operation.
176 *
177 * This routine must be called after platform_prepare().
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700178 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700179static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700180{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700181 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700182 hibernation_ops->finish();
183}
184
185/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200186 * platform_pre_restore - Prepare for hibernate image restoration.
187 * @platform_mode: Whether or not to use the platform driver.
188 *
189 * Use the platform driver to prepare the system for resume from a hibernation
190 * image.
191 *
192 * If the restore fails after this function has been called,
193 * platform_restore_cleanup() must be called.
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700194 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700195static int platform_pre_restore(int platform_mode)
196{
197 return (platform_mode && hibernation_ops) ?
198 hibernation_ops->pre_restore() : 0;
199}
200
201/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200202 * platform_restore_cleanup - Switch to the working state after failing restore.
203 * @platform_mode: Whether or not to use the platform driver.
204 *
205 * Use the platform driver to switch the system to the normal mode of operation
206 * after a failing restore.
207 *
208 * If platform_pre_restore() has been called before the failing restore, this
209 * function must be called too, regardless of the result of
210 * platform_pre_restore().
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700211 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700212static void platform_restore_cleanup(int platform_mode)
213{
214 if (platform_mode && hibernation_ops)
215 hibernation_ops->restore_cleanup();
216}
217
218/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200219 * platform_recover - Recover from a failure to suspend devices.
220 * @platform_mode: Whether or not to use the platform driver.
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200221 */
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200222static void platform_recover(int platform_mode)
223{
224 if (platform_mode && hibernation_ops && hibernation_ops->recover)
225 hibernation_ops->recover();
226}
227
228/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200229 * swsusp_show_speed - Print time elapsed between two events during hibernation.
230 * @start: Starting event.
231 * @stop: Final event.
232 * @nr_pages: Number of memory pages processed between @start and @stop.
233 * @msg: Additional diagnostic message to print.
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100234 */
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100235void swsusp_show_speed(struct timeval *start, struct timeval *stop,
236 unsigned nr_pages, char *msg)
237{
238 s64 elapsed_centisecs64;
239 int centisecs;
240 int k;
241 int kps;
242
243 elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
244 do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
245 centisecs = elapsed_centisecs64;
246 if (centisecs == 0)
247 centisecs = 1; /* avoid div-by-zero */
248 k = nr_pages * (PAGE_SIZE / 1024);
249 kps = (k * 100) / centisecs;
250 printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
251 msg, k,
252 centisecs / 100, centisecs % 100,
253 kps / 1000, (kps % 1000) / 10);
254}
255
256/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200257 * create_image - Create a hibernation image.
258 * @platform_mode: Whether or not to use the platform driver.
259 *
260 * Execute device drivers' .freeze_noirq() callbacks, create a hibernation image
261 * and execute the drivers' .thaw_noirq() callbacks.
262 *
263 * Control reappears in this routine after the subsequent restore.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700264 */
Adrian Bunk47a460d2008-02-04 22:30:06 -0800265static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700266{
267 int error;
268
Alan Sternd1616302009-05-24 22:05:42 +0200269 error = dpm_suspend_noirq(PMSG_FREEZE);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700270 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100271 printk(KERN_ERR "PM: Some devices failed to power down, "
272 "aborting hibernation\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200273 return error;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700274 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100275
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100276 error = platform_pre_snapshot(platform_mode);
277 if (error || hibernation_test(TEST_PLATFORM))
278 goto Platform_finish;
279
280 error = disable_nonboot_cpus();
281 if (error || hibernation_test(TEST_CPUS)
282 || hibernation_testmode(HIBERNATION_TEST))
283 goto Enable_cpus;
284
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100285 local_irq_disable();
286
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200287 error = syscore_suspend();
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100288 if (error) {
Bjorn Helgaas44840792009-05-15 23:30:50 +0200289 printk(KERN_ERR "PM: Some system devices failed to power down, "
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100290 "aborting hibernation\n");
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100291 goto Enable_irqs;
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100292 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700293
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100294 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100295 goto Power_up;
296
297 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700298 save_processor_state();
299 error = swsusp_arch_suspend();
300 if (error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100301 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
302 error);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700303 /* Restore control flow magically appears here */
304 restore_processor_state();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200305 if (!in_suspend) {
306 events_check_enabled = false;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700307 platform_leave(platform_mode);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200308 }
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100309
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100310 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100311 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100312
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100313 Enable_irqs:
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100314 local_irq_enable();
315
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100316 Enable_cpus:
317 enable_nonboot_cpus();
318
319 Platform_finish:
320 platform_finish(platform_mode);
321
Alan Sternd1616302009-05-24 22:05:42 +0200322 dpm_resume_noirq(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200323 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100324
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700325 return error;
326}
327
328/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200329 * hibernation_snapshot - Quiesce devices and create a hibernation image.
330 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700331 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200332 * This routine must be called with pm_mutex held.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700333 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700334int hibernation_snapshot(int platform_mode)
335{
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200336 pm_message_t msg = PMSG_RECOVER;
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100337 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700338
Zhang Rui3fe03132008-10-26 20:50:26 +0100339 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700340 if (error)
Rafael J. Wysockid074ee02010-07-07 23:43:35 +0200341 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700342
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200343 /* Preallocate image memory before shutting down devices. */
344 error = hibernate_preallocate_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700345 if (error)
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200346 goto Close;
347
348 error = freeze_kernel_threads();
349 if (error)
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100350 goto Cleanup;
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200351
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100352 if (hibernation_test(TEST_FREEZER) ||
353 hibernation_testmode(HIBERNATION_TESTPROC)) {
354
355 /*
356 * Indicate to the caller that we are returning due to a
357 * successful freezer test.
358 */
359 freezer_test_done = true;
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100360 goto Cleanup;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100361 }
362
Rafael J. Wysocki2aede852011-09-26 20:32:27 +0200363 error = dpm_prepare(PMSG_FREEZE);
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100364 if (error) {
365 dpm_complete(msg);
366 goto Cleanup;
367 }
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700368
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700369 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100370 pm_restrict_gfp_mask();
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200371 error = dpm_suspend(PMSG_FREEZE);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700372 if (error)
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200373 goto Recover_platform;
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700374
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100375 if (hibernation_test(TEST_DEVICES))
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200376 goto Recover_platform;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700377
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100378 error = create_image(platform_mode);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100379 /*
380 * Control returns here (1) after the image has been created or the
381 * image creation has failed and (2) after a successful restore.
382 */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100383
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100384 Resume_devices:
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200385 /* We may need to release the preallocated image pages here. */
386 if (error || !in_suspend)
387 swsusp_free();
388
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200389 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
390 dpm_resume(msg);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100391
392 if (error || !in_suspend)
393 pm_restore_gfp_mask();
394
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700395 resume_console();
Rafael J. Wysocki91e7c752011-05-17 23:26:00 +0200396 dpm_complete(msg);
397
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100398 Close:
399 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700400 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200401
402 Recover_platform:
403 platform_recover(platform_mode);
404 goto Resume_devices;
Rafael J. Wysockibb58dd52011-11-22 23:08:10 +0100405
406 Cleanup:
407 swsusp_free();
408 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700409}
410
411/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200412 * resume_target_kernel - Restore system state from a hibernation image.
413 * @platform_mode: Whether or not to use the platform driver.
414 *
415 * Execute device drivers' .freeze_noirq() callbacks, restore the contents of
416 * highmem that have not been restored yet from the image and run the low-level
417 * code that will restore the remaining contents of memory and switch to the
418 * just restored target kernel.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100419 */
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100420static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100421{
422 int error;
423
Alan Sternd1616302009-05-24 22:05:42 +0200424 error = dpm_suspend_noirq(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100425 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100426 printk(KERN_ERR "PM: Some devices failed to power down, "
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100427 "aborting resume\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200428 return error;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100429 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100430
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100431 error = platform_pre_restore(platform_mode);
432 if (error)
433 goto Cleanup;
434
435 error = disable_nonboot_cpus();
436 if (error)
437 goto Enable_cpus;
438
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100439 local_irq_disable();
440
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200441 error = syscore_suspend();
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100442 if (error)
443 goto Enable_irqs;
444
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100445 save_processor_state();
446 error = restore_highmem();
447 if (!error) {
448 error = swsusp_arch_resume();
449 /*
450 * The code below is only ever reached in case of a failure.
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200451 * Otherwise, execution continues at the place where
452 * swsusp_arch_suspend() was called.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100453 */
454 BUG_ON(!error);
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200455 /*
456 * This call to restore_highmem() reverts the changes made by
457 * the previous one.
458 */
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100459 restore_highmem();
460 }
461 /*
462 * The only reason why swsusp_arch_resume() can fail is memory being
463 * very tight, so we have to free it as soon as we can to avoid
Rafael J. Wysocki4e2d9492011-05-24 00:21:26 +0200464 * subsequent failures.
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100465 */
466 swsusp_free();
467 restore_processor_state();
468 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100469
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100470 syscore_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100471
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100472 Enable_irqs:
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100473 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100474
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100475 Enable_cpus:
476 enable_nonboot_cpus();
477
478 Cleanup:
479 platform_restore_cleanup(platform_mode);
480
Alan Sternd1616302009-05-24 22:05:42 +0200481 dpm_resume_noirq(PMSG_RECOVER);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100482
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100483 return error;
484}
485
486/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200487 * hibernation_restore - Quiesce devices and restore from a hibernation image.
488 * @platform_mode: If set, use platform driver to prepare for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700489 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200490 * This routine must be called with pm_mutex held. If it is successful, control
Barry Song21e82802011-09-27 22:05:44 +0200491 * reappears in the restored target kernel in hibernation_snapshot().
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700492 */
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700493int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700494{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100495 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700496
497 pm_prepare_console();
498 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100499 pm_restrict_gfp_mask();
Alan Sternd1616302009-05-24 22:05:42 +0200500 error = dpm_suspend_start(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700501 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100502 error = resume_target_kernel(platform_mode);
Alan Sternd1616302009-05-24 22:05:42 +0200503 dpm_resume_end(PMSG_RECOVER);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700504 }
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100505 pm_restore_gfp_mask();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700506 resume_console();
507 pm_restore_console();
508 return error;
509}
510
511/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200512 * hibernation_platform_enter - Power off the system using the platform driver.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700513 */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700514int hibernation_platform_enter(void)
515{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100516 int error;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700517
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700518 if (!hibernation_ops)
519 return -ENOSYS;
520
521 /*
522 * We have cancelled the power transition by running
523 * hibernation_ops->finish() before saving the image, so we should let
524 * the firmware know that we're going to enter the sleep state after all
525 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100526 error = hibernation_ops->begin();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700527 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100528 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700529
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100530 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700531 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +0200532 error = dpm_suspend_start(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200533 if (error) {
534 if (hibernation_ops->recover)
535 hibernation_ops->recover();
536 goto Resume_devices;
537 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700538
Alan Sternd1616302009-05-24 22:05:42 +0200539 error = dpm_suspend_noirq(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100540 if (error)
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200541 goto Resume_devices;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100542
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100543 error = hibernation_ops->prepare();
544 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200545 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100546
547 error = disable_nonboot_cpus();
548 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200549 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100550
551 local_irq_disable();
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100552 syscore_suspend();
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100553 if (pm_wakeup_pending()) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200554 error = -EAGAIN;
555 goto Power_up;
556 }
557
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100558 hibernation_ops->enter();
559 /* We should never get here */
560 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700561
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200562 Power_up:
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100563 syscore_resume();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200564 local_irq_enable();
565 enable_nonboot_cpus();
566
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200567 Platform_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700568 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100569
Rafael J. Wysockif6f71f12010-07-07 23:43:18 +0200570 dpm_resume_noirq(PMSG_RESTORE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100571
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700572 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100573 entering_platform_hibernation = false;
Alan Sternd1616302009-05-24 22:05:42 +0200574 dpm_resume_end(PMSG_RESTORE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700575 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100576
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100577 Close:
578 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100579
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700580 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700581}
582
583/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200584 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200586 * Use the platform driver, if configured, to put the system into the sleep
587 * state corresponding to hibernation, or try to power it off or reboot,
588 * depending on the value of hibernation_mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 */
Johannes Bergfe0c9352007-04-30 15:09:51 -0700590static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700592 switch (hibernation_mode) {
593 case HIBERNATION_TEST:
594 case HIBERNATION_TESTPROC:
Johannes Bergfe0c9352007-04-30 15:09:51 -0700595 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700596 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600597 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700599 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700600 hibernation_platform_enter();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700601 case HIBERNATION_SHUTDOWN:
602 kernel_power_off();
603 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600605 kernel_halt();
Johannes Bergfe0c9352007-04-30 15:09:51 -0700606 /*
607 * Valid image is on the disk, if we continue we risk serious data
608 * corruption after resume.
609 */
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100610 printk(KERN_CRIT "PM: Please power down manually\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 while(1);
612}
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614static int prepare_processes(void)
615{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800616 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (freeze_processes()) {
619 error = -EBUSY;
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100620 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700622 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200626 * hibernate - Carry out system hibernation, including saving the image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 */
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700628int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 int error;
631
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700632 mutex_lock(&pm_mutex);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700633 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700634 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
635 error = -EBUSY;
636 goto Unlock;
637 }
638
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100639 pm_prepare_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700640 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
641 if (error)
642 goto Exit;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700643
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700644 error = usermodehelper_disable();
645 if (error)
646 goto Exit;
647
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700648 /* Allocate memory management structures */
649 error = create_basic_memory_bitmaps();
650 if (error)
651 goto Exit;
652
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100653 printk(KERN_INFO "PM: Syncing filesystems ... ");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700654 sys_sync();
655 printk("done.\n");
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700658 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700659 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700661 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200662 if (error)
663 goto Thaw;
Srivatsa S. Bhataa9a7b12011-11-18 23:02:42 +0100664 if (freezer_test_done) {
665 freezer_test_done = false;
666 goto Thaw;
667 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200668
669 if (in_suspend) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700670 unsigned int flags = 0;
671
672 if (hibernation_mode == HIBERNATION_PLATFORM)
673 flags |= SF_PLATFORM_MODE;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200674 if (nocompress)
675 flags |= SF_NOCOMPRESS_MODE;
Bojan Smojver081a9d02011-10-13 23:58:07 +0200676 else
677 flags |= SF_CRC32_MODE;
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700680 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700681 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (!error)
Johannes Bergfe0c9352007-04-30 15:09:51 -0700683 power_down();
MyungJoo Ham5262a472010-11-26 23:07:56 +0100684 in_suspend = 0;
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100685 pm_restore_gfp_mask();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800686 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800688 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200689
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800690 Thaw:
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100691 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700692 Finish:
693 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700694 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700695 Exit:
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700696 pm_notifier_call_chain(PM_POST_HIBERNATION);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100697 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700698 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700699 Unlock:
700 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return error;
702}
703
704
705/**
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200706 * software_resume - Resume from a saved hibernation image.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200708 * This routine is called as a late initcall, when all devices have been
709 * discovered and initialized already.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200711 * The image reading code is called to see if there is a hibernation image
712 * available for reading. If that is the case, devices are quiesced and the
713 * contents of memory is restored from the saved image.
714 *
715 * If this is successful, control reappears in the restored target kernel in
716 * hibernation_snaphot() which returns to hibernate(). Otherwise, the routine
717 * attempts to recover gracefully and make the kernel return to the normal mode
718 * of operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720static int software_resume(void)
721{
722 int error;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700723 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Johannes Berg60a0d232007-11-14 17:00:16 -0800725 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100726 * If the user said "noresume".. bail out early.
727 */
728 if (noresume)
729 return 0;
730
731 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800732 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
733 * is configured into the kernel. Since the regular hibernate
734 * trigger path is via sysfs which takes a buffer mutex before
735 * calling hibernate functions (which take pm_mutex) this can
736 * cause lockdep to complain about a possible ABBA deadlock
737 * which cannot happen since we're in the boot code here and
738 * sysfs can't be invoked yet. Therefore, we use a subclass
739 * here to avoid lockdep complaining.
740 */
741 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200742
743 if (swsusp_resume_device)
744 goto Check_image;
745
746 if (!strlen(resume_file)) {
747 error = -ENOENT;
748 goto Unlock;
749 }
750
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200751 pr_debug("PM: Checking hibernation image partition %s\n", resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200752
Barry Songf126f732011-10-10 23:38:41 +0200753 if (resume_delay) {
754 printk(KERN_INFO "Waiting %dsec before reading resume device...\n",
755 resume_delay);
756 ssleep(resume_delay);
757 }
758
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200759 /* Check if the device is there */
760 swsusp_resume_device = name_to_dev_t(resume_file);
Pavel Machek3efa1472005-07-07 17:56:43 -0700761 if (!swsusp_resume_device) {
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100762 /*
763 * Some device discovery might still be in progress; we need
764 * to wait for this to finish.
765 */
766 wait_for_device_probe();
Barry Song6f8d7022011-10-06 20:34:46 +0200767
768 if (resume_wait) {
769 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
770 msleep(10);
771 async_synchronize_full();
772 }
773
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200774 /*
775 * We can't depend on SCSI devices being available after loading
776 * one of their modules until scsi_complete_async_scans() is
777 * called and the resume device usually is a SCSI one.
778 */
779 scsi_complete_async_scans();
780
Pavel Machek3efa1472005-07-07 17:56:43 -0700781 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200782 if (!swsusp_resume_device) {
783 error = -ENODEV;
784 goto Unlock;
785 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700786 }
787
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200788 Check_image:
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200789 pr_debug("PM: Hibernation image partition %d:%d present\n",
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200790 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200792 pr_debug("PM: Looking for hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800793 error = swsusp_check();
794 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700795 goto Unlock;
796
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700797 /* The snapshot device should not be opened while we're running */
798 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
799 error = -EBUSY;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200800 swsusp_close(FMODE_READ);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700801 goto Unlock;
802 }
803
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100804 pm_prepare_console();
Alan Sternc3e94d82007-11-19 23:38:25 +0100805 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
806 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200807 goto close_finish;
Alan Sternc3e94d82007-11-19 23:38:25 +0100808
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700809 error = usermodehelper_disable();
810 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200811 goto close_finish;
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700812
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700813 error = create_basic_memory_bitmaps();
814 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200815 goto close_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 pr_debug("PM: Preparing processes for restore.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800818 error = prepare_processes();
819 if (error) {
Al Viroc2dd0da2007-10-08 13:21:10 -0400820 swsusp_close(FMODE_READ);
Li Shaohua5a72e042005-06-25 14:55:06 -0700821 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200824 pr_debug("PM: Loading hibernation image.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700826 error = swsusp_read(&flags);
Jiri Slaby76b57e62009-10-07 22:37:35 +0200827 swsusp_close(FMODE_READ);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800828 if (!error)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700829 hibernation_restore(flags & SF_PLATFORM_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200831 printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700832 swsusp_free();
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100833 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 Done:
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700835 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700836 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700837 Finish:
Alan Sternc3e94d82007-11-19 23:38:25 +0100838 pm_notifier_call_chain(PM_POST_RESTORE);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100839 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700840 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700841 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700842 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800843 mutex_unlock(&pm_mutex);
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200844 pr_debug("PM: Hibernation image not present or could not be loaded.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700845 return error;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200846close_finish:
847 swsusp_close(FMODE_READ);
848 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
851late_initcall(software_resume);
852
853
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700854static const char * const hibernation_modes[] = {
855 [HIBERNATION_PLATFORM] = "platform",
856 [HIBERNATION_SHUTDOWN] = "shutdown",
857 [HIBERNATION_REBOOT] = "reboot",
858 [HIBERNATION_TEST] = "test",
859 [HIBERNATION_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860};
861
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200862/*
863 * /sys/power/disk - Control hibernation mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200865 * Hibernation can be handled in several ways. There are a few different ways
866 * to put the system into the sleep state: using the platform driver (e.g. ACPI
867 * or other hibernation_ops), powering it off or rebooting it (for testing
868 * mostly), or using one of the two available test modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200870 * The sysfs file /sys/power/disk provides an interface for selecting the
871 * hibernation mode to use. Reading from this file causes the available modes
872 * to be printed. There are 5 modes that can be supported:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 * 'platform'
875 * 'shutdown'
876 * 'reboot'
Johannes Berg11d77d02007-04-30 15:09:53 -0700877 * 'test'
878 * 'testproc'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 *
Rafael J. Wysockif42a9812011-05-24 23:36:06 +0200880 * If a platform hibernation driver is in use, 'platform' will be supported
881 * and will be used by default. Otherwise, 'shutdown' will be used by default.
882 * The selected option (i.e. the one corresponding to the current value of
883 * hibernation_mode) is enclosed by a square bracket.
884 *
885 * To select a given hibernation mode it is necessary to write the mode's
886 * string representation (as returned by reading from /sys/power/disk) back
887 * into /sys/power/disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 */
889
Kay Sievers386f2752007-11-02 13:47:53 +0100890static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
891 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700893 int i;
894 char *start = buf;
895
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700896 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
897 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700898 continue;
899 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700900 case HIBERNATION_SHUTDOWN:
901 case HIBERNATION_REBOOT:
902 case HIBERNATION_TEST:
903 case HIBERNATION_TESTPROC:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700904 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700905 case HIBERNATION_PLATFORM:
906 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700907 break;
908 /* not a valid mode, continue with loop */
909 continue;
910 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700911 if (i == hibernation_mode)
912 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700913 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700914 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700915 }
916 buf += sprintf(buf, "\n");
917 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Kay Sievers386f2752007-11-02 13:47:53 +0100920static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
921 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 int error = 0;
924 int i;
925 int len;
926 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700927 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 p = memchr(buf, '\n', n);
930 len = p ? p - buf : n;
931
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800932 mutex_lock(&pm_mutex);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700933 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700934 if (len == strlen(hibernation_modes[i])
935 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 mode = i;
937 break;
938 }
939 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700940 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c9352007-04-30 15:09:51 -0700941 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700942 case HIBERNATION_SHUTDOWN:
943 case HIBERNATION_REBOOT:
944 case HIBERNATION_TEST:
945 case HIBERNATION_TESTPROC:
946 hibernation_mode = mode;
Johannes Bergfe0c9352007-04-30 15:09:51 -0700947 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700948 case HIBERNATION_PLATFORM:
949 if (hibernation_ops)
950 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 else
952 error = -EINVAL;
953 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700954 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 error = -EINVAL;
956
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700957 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100958 pr_debug("PM: Hibernation mode set to '%s'\n",
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700959 hibernation_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800960 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return error ? error : n;
962}
963
964power_attr(disk);
965
Kay Sievers386f2752007-11-02 13:47:53 +0100966static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
967 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
970 MINOR(swsusp_resume_device));
971}
972
Kay Sievers386f2752007-11-02 13:47:53 +0100973static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
974 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800978 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Andrew Mortona5762192006-01-06 00:09:50 -0800980 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
981 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Andrew Mortona5762192006-01-06 00:09:50 -0800983 res = MKDEV(maj,min);
984 if (maj != MAJOR(res) || min != MINOR(res))
985 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800987 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800988 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800989 mutex_unlock(&pm_mutex);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100990 printk(KERN_INFO "PM: Starting manual resume from disk\n");
Andrew Mortona5762192006-01-06 00:09:50 -0800991 noresume = 0;
992 software_resume();
993 ret = n;
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800994 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800995 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
998power_attr(resume);
999
Kay Sievers386f2752007-11-02 13:47:53 +01001000static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1001 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001002{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001003 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001004}
1005
Kay Sievers386f2752007-11-02 13:47:53 +01001006static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1007 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001008{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001009 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001010
Rafael J. Wysocki853609b2006-02-01 03:05:07 -08001011 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001012 image_size = size;
1013 return n;
1014 }
1015
1016 return -EINVAL;
1017}
1018
1019power_attr(image_size);
1020
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001021static ssize_t reserved_size_show(struct kobject *kobj,
1022 struct kobj_attribute *attr, char *buf)
1023{
1024 return sprintf(buf, "%lu\n", reserved_size);
1025}
1026
1027static ssize_t reserved_size_store(struct kobject *kobj,
1028 struct kobj_attribute *attr,
1029 const char *buf, size_t n)
1030{
1031 unsigned long size;
1032
1033 if (sscanf(buf, "%lu", &size) == 1) {
1034 reserved_size = size;
1035 return n;
1036 }
1037
1038 return -EINVAL;
1039}
1040
1041power_attr(reserved_size);
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043static struct attribute * g[] = {
1044 &disk_attr.attr,
1045 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -08001046 &image_size_attr.attr,
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +02001047 &reserved_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 NULL,
1049};
1050
1051
1052static struct attribute_group attr_group = {
1053 .attrs = g,
1054};
1055
1056
1057static int __init pm_disk_init(void)
1058{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -08001059 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062core_initcall(pm_disk_init);
1063
1064
1065static int __init resume_setup(char *str)
1066{
1067 if (noresume)
1068 return 1;
1069
1070 strncpy( resume_file, str, 255 );
1071 return 1;
1072}
1073
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001074static int __init resume_offset_setup(char *str)
1075{
1076 unsigned long long offset;
1077
1078 if (noresume)
1079 return 1;
1080
1081 if (sscanf(str, "%llu", &offset) == 1)
1082 swsusp_resume_block = offset;
1083
1084 return 1;
1085}
1086
Bojan Smojverf996fc92010-09-09 23:06:23 +02001087static int __init hibernate_setup(char *str)
1088{
1089 if (!strncmp(str, "noresume", 8))
1090 noresume = 1;
1091 else if (!strncmp(str, "nocompress", 10))
1092 nocompress = 1;
1093 return 1;
1094}
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096static int __init noresume_setup(char *str)
1097{
1098 noresume = 1;
1099 return 1;
1100}
1101
Barry Song6f8d7022011-10-06 20:34:46 +02001102static int __init resumewait_setup(char *str)
1103{
1104 resume_wait = 1;
1105 return 1;
1106}
1107
Barry Songf126f732011-10-10 23:38:41 +02001108static int __init resumedelay_setup(char *str)
1109{
1110 resume_delay = simple_strtoul(str, NULL, 0);
1111 return 1;
1112}
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001115__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116__setup("resume=", resume_setup);
Bojan Smojverf996fc92010-09-09 23:06:23 +02001117__setup("hibernate=", hibernate_setup);
Barry Song6f8d7022011-10-06 20:34:46 +02001118__setup("resumewait", resumewait_setup);
Barry Songf126f732011-10-10 23:38:41 +02001119__setup("resumedelay=", resumedelay_setup);