blob: 870f72bc72aed308049e3862ee9f62a3cf9be4f4 [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
12#include <linux/suspend.h>
13#include <linux/syscalls.h>
14#include <linux/reboot.h>
15#include <linux/string.h>
16#include <linux/device.h>
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -070017#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/delay.h>
19#include <linux/fs.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070020#include <linux/mount.h>
Eric W. Biederman88d10bb2005-09-22 21:43:46 -070021#include <linux/pm.h>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070022#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070023#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080024#include <linux/freezer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/gfp.h>
Rafael J. Wysockic7510852009-04-12 20:06:56 +020026#include <scsi/scsi_scan.h>
Magnus Damma8af7892009-03-31 15:23:37 -070027#include <asm/suspend.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "power.h"
30
31
Bojan Smojverf996fc92010-09-09 23:06:23 +020032static int nocompress = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static int noresume = 0;
Adrian Bunk47a460d2008-02-04 22:30:06 -080034static char resume_file[256] = CONFIG_PM_STD_PARTITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080036sector_t swsusp_resume_block;
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +010037int in_suspend __nosavedata = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070039enum {
40 HIBERNATION_INVALID,
41 HIBERNATION_PLATFORM,
42 HIBERNATION_TEST,
43 HIBERNATION_TESTPROC,
44 HIBERNATION_SHUTDOWN,
45 HIBERNATION_REBOOT,
46 /* keep last */
47 __HIBERNATION_AFTER_LAST
48};
49#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
50#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
51
52static int hibernation_mode = HIBERNATION_SHUTDOWN;
53
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070054static struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070055
56/**
57 * hibernation_set_ops - set the global hibernate operations
58 * @ops: the hibernation operations to use in subsequent hibernation transitions
59 */
60
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070061void hibernation_set_ops(struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070062{
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +010063 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
64 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
MyungJoo Ham5729c632010-11-26 23:07:48 +010065 && ops->restore_cleanup && ops->leave)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070066 WARN_ON(1);
67 return;
68 }
69 mutex_lock(&pm_mutex);
70 hibernation_ops = ops;
71 if (ops)
72 hibernation_mode = HIBERNATION_PLATFORM;
73 else if (hibernation_mode == HIBERNATION_PLATFORM)
74 hibernation_mode = HIBERNATION_SHUTDOWN;
75
76 mutex_unlock(&pm_mutex);
77}
78
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +010079static bool entering_platform_hibernation;
80
81bool system_entering_hibernation(void)
82{
83 return entering_platform_hibernation;
84}
85EXPORT_SYMBOL(system_entering_hibernation);
86
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +010087#ifdef CONFIG_PM_DEBUG
88static void hibernation_debug_sleep(void)
89{
90 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
91 mdelay(5000);
92}
93
94static int hibernation_testmode(int mode)
95{
96 if (hibernation_mode == mode) {
97 hibernation_debug_sleep();
98 return 1;
99 }
100 return 0;
101}
102
103static int hibernation_test(int level)
104{
105 if (pm_test_level == level) {
106 hibernation_debug_sleep();
107 return 1;
108 }
109 return 0;
110}
111#else /* !CONFIG_PM_DEBUG */
112static int hibernation_testmode(int mode) { return 0; }
113static int hibernation_test(int level) { return 0; }
114#endif /* !CONFIG_PM_DEBUG */
115
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700116/**
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100117 * platform_begin - tell the platform driver that we're starting
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700118 * hibernation
119 */
120
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100121static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700122{
123 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100124 hibernation_ops->begin() : 0;
125}
126
127/**
128 * platform_end - tell the platform driver that we've entered the
129 * working state
130 */
131
132static void platform_end(int platform_mode)
133{
134 if (platform_mode && hibernation_ops)
135 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700136}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/**
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700139 * platform_pre_snapshot - prepare the machine for hibernation using the
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800140 * platform driver if so configured and return an error code if it fails
141 */
142
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700143static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800144{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700145 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700146 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800147}
148
149/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700150 * platform_leave - prepare the machine for switching to the normal mode
151 * of operation using the platform driver (called with interrupts disabled)
152 */
153
154static void platform_leave(int platform_mode)
155{
156 if (platform_mode && hibernation_ops)
157 hibernation_ops->leave();
158}
159
160/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700161 * platform_finish - switch the machine to the normal mode of operation
162 * using the platform driver (must be called after platform_prepare())
163 */
164
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700165static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700166{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700167 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700168 hibernation_ops->finish();
169}
170
171/**
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700172 * platform_pre_restore - prepare the platform for the restoration from a
173 * hibernation image. If the restore fails after this function has been
174 * called, platform_restore_cleanup() must be called.
175 */
176
177static int platform_pre_restore(int platform_mode)
178{
179 return (platform_mode && hibernation_ops) ?
180 hibernation_ops->pre_restore() : 0;
181}
182
183/**
184 * platform_restore_cleanup - switch the platform to the normal mode of
185 * operation after a failing restore. If platform_pre_restore() has been
186 * called before the failing restore, this function must be called too,
187 * regardless of the result of platform_pre_restore().
188 */
189
190static void platform_restore_cleanup(int platform_mode)
191{
192 if (platform_mode && hibernation_ops)
193 hibernation_ops->restore_cleanup();
194}
195
196/**
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200197 * platform_recover - recover the platform from a failure to suspend
198 * devices.
199 */
200
201static void platform_recover(int platform_mode)
202{
203 if (platform_mode && hibernation_ops && hibernation_ops->recover)
204 hibernation_ops->recover();
205}
206
207/**
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100208 * swsusp_show_speed - print the time elapsed between two events.
209 * @start: Starting event.
210 * @stop: Final event.
211 * @nr_pages - number of pages processed between @start and @stop
212 * @msg - introductory message to print
213 */
214
215void swsusp_show_speed(struct timeval *start, struct timeval *stop,
216 unsigned nr_pages, char *msg)
217{
218 s64 elapsed_centisecs64;
219 int centisecs;
220 int k;
221 int kps;
222
223 elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
224 do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
225 centisecs = elapsed_centisecs64;
226 if (centisecs == 0)
227 centisecs = 1; /* avoid div-by-zero */
228 k = nr_pages * (PAGE_SIZE / 1024);
229 kps = (k * 100) / centisecs;
230 printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
231 msg, k,
232 centisecs / 100, centisecs % 100,
233 kps / 1000, (kps % 1000) / 10);
234}
235
236/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700237 * create_image - freeze devices that need to be frozen with interrupts
238 * off, create the hibernation image and thaw those devices. Control
239 * reappears in this routine after a restore.
240 */
241
Adrian Bunk47a460d2008-02-04 22:30:06 -0800242static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700243{
244 int error;
245
246 error = arch_prepare_suspend();
247 if (error)
248 return error;
249
Alan Sternd1616302009-05-24 22:05:42 +0200250 /* At this point, dpm_suspend_start() has been called, but *not*
251 * dpm_suspend_noirq(). We *must* call dpm_suspend_noirq() now.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700252 * Otherwise, drivers for some devices (e.g. interrupt controllers)
253 * become desynchronized with the actual state of the hardware
254 * at resume time, and evil weirdness ensues.
255 */
Alan Sternd1616302009-05-24 22:05:42 +0200256 error = dpm_suspend_noirq(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();
268 if (error || hibernation_test(TEST_CPUS)
269 || hibernation_testmode(HIBERNATION_TEST))
270 goto Enable_cpus;
271
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100272 local_irq_disable();
273
Bjorn Helgaas44840792009-05-15 23:30:50 +0200274 error = sysdev_suspend(PMSG_FREEZE);
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100275 if (error) {
Bjorn Helgaas44840792009-05-15 23:30:50 +0200276 printk(KERN_ERR "PM: Some system devices failed to power down, "
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100277 "aborting hibernation\n");
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100278 goto Enable_irqs;
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100279 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700280
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100281 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100282 goto Power_up;
283
284 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700285 save_processor_state();
286 error = swsusp_arch_suspend();
287 if (error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100288 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
289 error);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700290 /* Restore control flow magically appears here */
291 restore_processor_state();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200292 if (!in_suspend) {
293 events_check_enabled = false;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700294 platform_leave(platform_mode);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200295 }
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100296
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100297 Power_up:
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100298 sysdev_resume();
Alan Sternd1616302009-05-24 22:05:42 +0200299 /* NOTE: dpm_resume_noirq() is just a resume() for devices
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700300 * that suspended with irqs off ... no overall powerup.
301 */
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100302
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100303 Enable_irqs:
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100304 local_irq_enable();
305
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100306 Enable_cpus:
307 enable_nonboot_cpus();
308
309 Platform_finish:
310 platform_finish(platform_mode);
311
Alan Sternd1616302009-05-24 22:05:42 +0200312 dpm_resume_noirq(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200313 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100314
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700315 return error;
316}
317
318/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700319 * hibernation_snapshot - quiesce devices and create the hibernation
320 * snapshot image.
321 * @platform_mode - if set, use the platform driver, if available, to
Nick Andrew877d0312009-01-26 11:06:57 +0100322 * prepare the platform firmware for the power transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700323 *
324 * Must be called with pm_mutex held
325 */
326
327int hibernation_snapshot(int platform_mode)
328{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100329 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700330
Zhang Rui3fe03132008-10-26 20:50:26 +0100331 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700332 if (error)
Rafael J. Wysockid074ee02010-07-07 23:43:35 +0200333 goto Close;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700334
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200335 /* Preallocate image memory before shutting down devices. */
336 error = hibernate_preallocate_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700337 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100338 goto Close;
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700339
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700340 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100341 pm_restrict_gfp_mask();
Alan Sternd1616302009-05-24 22:05:42 +0200342 error = dpm_suspend_start(PMSG_FREEZE);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700343 if (error)
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200344 goto Recover_platform;
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700345
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100346 if (hibernation_test(TEST_DEVICES))
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200347 goto Recover_platform;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700348
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100349 error = create_image(platform_mode);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100350 /*
351 * Control returns here (1) after the image has been created or the
352 * image creation has failed and (2) after a successful restore.
353 */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100354
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100355 Resume_devices:
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200356 /* We may need to release the preallocated image pages here. */
357 if (error || !in_suspend)
358 swsusp_free();
359
Alan Sternd1616302009-05-24 22:05:42 +0200360 dpm_resume_end(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200361 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100362
363 if (error || !in_suspend)
364 pm_restore_gfp_mask();
365
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700366 resume_console();
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100367 Close:
368 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700369 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200370
371 Recover_platform:
372 platform_recover(platform_mode);
373 goto Resume_devices;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700374}
375
376/**
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100377 * resume_target_kernel - prepare devices that need to be suspended with
378 * interrupts off, restore the contents of highmem that have not been
379 * restored yet from the image and run the low level code that will restore
380 * the remaining contents of memory and switch to the just restored target
381 * kernel.
382 */
383
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100384static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100385{
386 int error;
387
Alan Sternd1616302009-05-24 22:05:42 +0200388 error = dpm_suspend_noirq(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100389 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100390 printk(KERN_ERR "PM: Some devices failed to power down, "
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100391 "aborting resume\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200392 return error;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100393 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100394
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100395 error = platform_pre_restore(platform_mode);
396 if (error)
397 goto Cleanup;
398
399 error = disable_nonboot_cpus();
400 if (error)
401 goto Enable_cpus;
402
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100403 local_irq_disable();
404
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100405 error = sysdev_suspend(PMSG_QUIESCE);
406 if (error)
407 goto Enable_irqs;
408
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100409 /* We'll ignore saved state, but this gets preempt count (etc) right */
410 save_processor_state();
411 error = restore_highmem();
412 if (!error) {
413 error = swsusp_arch_resume();
414 /*
415 * The code below is only ever reached in case of a failure.
416 * Otherwise execution continues at place where
417 * swsusp_arch_suspend() was called
418 */
419 BUG_ON(!error);
420 /* This call to restore_highmem() undos the previous one */
421 restore_highmem();
422 }
423 /*
424 * The only reason why swsusp_arch_resume() can fail is memory being
425 * very tight, so we have to free it as soon as we can to avoid
426 * subsequent failures
427 */
428 swsusp_free();
429 restore_processor_state();
430 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100431
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100432 sysdev_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100433
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100434 Enable_irqs:
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100435 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100436
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100437 Enable_cpus:
438 enable_nonboot_cpus();
439
440 Cleanup:
441 platform_restore_cleanup(platform_mode);
442
Alan Sternd1616302009-05-24 22:05:42 +0200443 dpm_resume_noirq(PMSG_RECOVER);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100444
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100445 return error;
446}
447
448/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700449 * hibernation_restore - quiesce devices and restore the hibernation
450 * snapshot image. If successful, control returns in hibernation_snaphot()
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700451 * @platform_mode - if set, use the platform driver, if available, to
Nick Andrew877d0312009-01-26 11:06:57 +0100452 * prepare the platform firmware for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700453 *
454 * Must be called with pm_mutex held
455 */
456
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700457int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700458{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100459 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700460
461 pm_prepare_console();
462 suspend_console();
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100463 pm_restrict_gfp_mask();
Alan Sternd1616302009-05-24 22:05:42 +0200464 error = dpm_suspend_start(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700465 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100466 error = resume_target_kernel(platform_mode);
Alan Sternd1616302009-05-24 22:05:42 +0200467 dpm_resume_end(PMSG_RECOVER);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700468 }
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100469 pm_restore_gfp_mask();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700470 resume_console();
471 pm_restore_console();
472 return error;
473}
474
475/**
476 * hibernation_platform_enter - enter the hibernation state using the
477 * platform driver (if available)
478 */
479
480int hibernation_platform_enter(void)
481{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100482 int error;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700483
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700484 if (!hibernation_ops)
485 return -ENOSYS;
486
487 /*
488 * We have cancelled the power transition by running
489 * hibernation_ops->finish() before saving the image, so we should let
490 * the firmware know that we're going to enter the sleep state after all
491 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100492 error = hibernation_ops->begin();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700493 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100494 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700495
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100496 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700497 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +0200498 error = dpm_suspend_start(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200499 if (error) {
500 if (hibernation_ops->recover)
501 hibernation_ops->recover();
502 goto Resume_devices;
503 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700504
Alan Sternd1616302009-05-24 22:05:42 +0200505 error = dpm_suspend_noirq(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100506 if (error)
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200507 goto Resume_devices;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100508
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100509 error = hibernation_ops->prepare();
510 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200511 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100512
513 error = disable_nonboot_cpus();
514 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200515 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100516
517 local_irq_disable();
518 sysdev_suspend(PMSG_HIBERNATE);
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100519 if (pm_wakeup_pending()) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200520 error = -EAGAIN;
521 goto Power_up;
522 }
523
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100524 hibernation_ops->enter();
525 /* We should never get here */
526 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700527
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200528 Power_up:
529 sysdev_resume();
530 local_irq_enable();
531 enable_nonboot_cpus();
532
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200533 Platform_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700534 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100535
Rafael J. Wysockif6f71f12010-07-07 23:43:18 +0200536 dpm_resume_noirq(PMSG_RESTORE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100537
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700538 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100539 entering_platform_hibernation = false;
Alan Sternd1616302009-05-24 22:05:42 +0200540 dpm_resume_end(PMSG_RESTORE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700541 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100542
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100543 Close:
544 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100545
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700546 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700547}
548
549/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700550 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 *
Johannes Bergfe0c9352007-04-30 15:09:51 -0700552 * Use the platform driver, if configured so; otherwise try
553 * to power off or reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
555
Johannes Bergfe0c9352007-04-30 15:09:51 -0700556static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700558 switch (hibernation_mode) {
559 case HIBERNATION_TEST:
560 case HIBERNATION_TESTPROC:
Johannes Bergfe0c9352007-04-30 15:09:51 -0700561 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700562 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600563 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700565 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700566 hibernation_platform_enter();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700567 case HIBERNATION_SHUTDOWN:
568 kernel_power_off();
569 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600571 kernel_halt();
Johannes Bergfe0c9352007-04-30 15:09:51 -0700572 /*
573 * Valid image is on the disk, if we continue we risk serious data
574 * corruption after resume.
575 */
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100576 printk(KERN_CRIT "PM: Please power down manually\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 while(1);
578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580static int prepare_processes(void)
581{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800582 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (freeze_processes()) {
585 error = -EBUSY;
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100586 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700588 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700592 * hibernate - The granpappy of the built-in hibernation management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
594
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700595int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 int error;
598
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700599 mutex_lock(&pm_mutex);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700600 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700601 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
602 error = -EBUSY;
603 goto Unlock;
604 }
605
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100606 pm_prepare_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700607 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
608 if (error)
609 goto Exit;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700610
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700611 error = usermodehelper_disable();
612 if (error)
613 goto Exit;
614
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700615 /* Allocate memory management structures */
616 error = create_basic_memory_bitmaps();
617 if (error)
618 goto Exit;
619
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100620 printk(KERN_INFO "PM: Syncing filesystems ... ");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700621 sys_sync();
622 printk("done.\n");
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700625 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700626 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100628 if (hibernation_test(TEST_FREEZER))
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800629 goto Thaw;
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100630
631 if (hibernation_testmode(HIBERNATION_TESTPROC))
632 goto Thaw;
633
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700634 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200635 if (error)
636 goto Thaw;
637
638 if (in_suspend) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700639 unsigned int flags = 0;
640
641 if (hibernation_mode == HIBERNATION_PLATFORM)
642 flags |= SF_PLATFORM_MODE;
Bojan Smojverf996fc92010-09-09 23:06:23 +0200643 if (nocompress)
644 flags |= SF_NOCOMPRESS_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700646 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700647 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (!error)
Johannes Bergfe0c9352007-04-30 15:09:51 -0700649 power_down();
MyungJoo Ham5262a472010-11-26 23:07:56 +0100650 in_suspend = 0;
Rafael J. Wysockic9e664f2010-12-03 22:57:45 +0100651 pm_restore_gfp_mask();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800652 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800654 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200655
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800656 Thaw:
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100657 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700658 Finish:
659 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700660 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700661 Exit:
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700662 pm_notifier_call_chain(PM_POST_HIBERNATION);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100663 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700664 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700665 Unlock:
666 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return error;
668}
669
670
671/**
672 * software_resume - Resume from a saved image.
673 *
674 * Called as a late_initcall (so all devices are discovered and
675 * initialized), we call swsusp to see if we have a saved image or not.
676 * If so, we quiesce devices, the restore the saved image. We will
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700677 * return above (in hibernate() ) if everything goes well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 * Otherwise, we fail gracefully and return to the normally
679 * scheduled program.
680 *
681 */
682
683static int software_resume(void)
684{
685 int error;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700686 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Johannes Berg60a0d232007-11-14 17:00:16 -0800688 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100689 * If the user said "noresume".. bail out early.
690 */
691 if (noresume)
692 return 0;
693
694 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800695 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
696 * is configured into the kernel. Since the regular hibernate
697 * trigger path is via sysfs which takes a buffer mutex before
698 * calling hibernate functions (which take pm_mutex) this can
699 * cause lockdep to complain about a possible ABBA deadlock
700 * which cannot happen since we're in the boot code here and
701 * sysfs can't be invoked yet. Therefore, we use a subclass
702 * here to avoid lockdep complaining.
703 */
704 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200705
706 if (swsusp_resume_device)
707 goto Check_image;
708
709 if (!strlen(resume_file)) {
710 error = -ENOENT;
711 goto Unlock;
712 }
713
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200714 pr_debug("PM: Checking hibernation image partition %s\n", resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200715
716 /* Check if the device is there */
717 swsusp_resume_device = name_to_dev_t(resume_file);
Pavel Machek3efa1472005-07-07 17:56:43 -0700718 if (!swsusp_resume_device) {
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100719 /*
720 * Some device discovery might still be in progress; we need
721 * to wait for this to finish.
722 */
723 wait_for_device_probe();
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200724 /*
725 * We can't depend on SCSI devices being available after loading
726 * one of their modules until scsi_complete_async_scans() is
727 * called and the resume device usually is a SCSI one.
728 */
729 scsi_complete_async_scans();
730
Pavel Machek3efa1472005-07-07 17:56:43 -0700731 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200732 if (!swsusp_resume_device) {
733 error = -ENODEV;
734 goto Unlock;
735 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700736 }
737
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200738 Check_image:
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200739 pr_debug("PM: Hibernation image partition %d:%d present\n",
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200740 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200742 pr_debug("PM: Looking for hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800743 error = swsusp_check();
744 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700745 goto Unlock;
746
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700747 /* The snapshot device should not be opened while we're running */
748 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
749 error = -EBUSY;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200750 swsusp_close(FMODE_READ);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700751 goto Unlock;
752 }
753
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100754 pm_prepare_console();
Alan Sternc3e94d82007-11-19 23:38:25 +0100755 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
756 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200757 goto close_finish;
Alan Sternc3e94d82007-11-19 23:38:25 +0100758
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700759 error = usermodehelper_disable();
760 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200761 goto close_finish;
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700762
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700763 error = create_basic_memory_bitmaps();
764 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200765 goto close_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 pr_debug("PM: Preparing processes for restore.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800768 error = prepare_processes();
769 if (error) {
Al Viroc2dd0da2007-10-08 13:21:10 -0400770 swsusp_close(FMODE_READ);
Li Shaohua5a72e042005-06-25 14:55:06 -0700771 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200774 pr_debug("PM: Loading hibernation image.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700776 error = swsusp_read(&flags);
Jiri Slaby76b57e62009-10-07 22:37:35 +0200777 swsusp_close(FMODE_READ);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800778 if (!error)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700779 hibernation_restore(flags & SF_PLATFORM_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200781 printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700782 swsusp_free();
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100783 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 Done:
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700785 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700786 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700787 Finish:
Alan Sternc3e94d82007-11-19 23:38:25 +0100788 pm_notifier_call_chain(PM_POST_RESTORE);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100789 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700790 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700791 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700792 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800793 mutex_unlock(&pm_mutex);
Rafael J. Wysockid0941ea2010-09-28 23:31:22 +0200794 pr_debug("PM: Hibernation image not present or could not be loaded.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700795 return error;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200796close_finish:
797 swsusp_close(FMODE_READ);
798 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
801late_initcall(software_resume);
802
803
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700804static const char * const hibernation_modes[] = {
805 [HIBERNATION_PLATFORM] = "platform",
806 [HIBERNATION_SHUTDOWN] = "shutdown",
807 [HIBERNATION_REBOOT] = "reboot",
808 [HIBERNATION_TEST] = "test",
809 [HIBERNATION_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810};
811
812/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700813 * disk - Control hibernation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700815 * Suspend-to-disk can be handled in several ways. We have a few options
816 * for putting the system to sleep - using the platform driver (e.g. ACPI
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700817 * or other hibernation_ops), powering off the system or rebooting the
818 * system (for testing) as well as the two test modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700820 * The system can support 'platform', and that is known a priori (and
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700821 * encoded by the presence of hibernation_ops). However, the user may
822 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
823 * test modes, 'test' or 'testproc'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 *
825 * show() will display what the mode is currently set to.
826 * store() will accept one of
827 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 * 'platform'
829 * 'shutdown'
830 * 'reboot'
Johannes Berg11d77d02007-04-30 15:09:53 -0700831 * 'test'
832 * 'testproc'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700834 * It will only change to 'platform' if the system
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700835 * supports it (as determined by having hibernation_ops).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 */
837
Kay Sievers386f2752007-11-02 13:47:53 +0100838static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
839 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700841 int i;
842 char *start = buf;
843
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700844 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
845 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700846 continue;
847 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700848 case HIBERNATION_SHUTDOWN:
849 case HIBERNATION_REBOOT:
850 case HIBERNATION_TEST:
851 case HIBERNATION_TESTPROC:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700852 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700853 case HIBERNATION_PLATFORM:
854 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700855 break;
856 /* not a valid mode, continue with loop */
857 continue;
858 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700859 if (i == hibernation_mode)
860 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700861 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700862 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700863 }
864 buf += sprintf(buf, "\n");
865 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
868
Kay Sievers386f2752007-11-02 13:47:53 +0100869static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
870 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 int error = 0;
873 int i;
874 int len;
875 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700876 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 p = memchr(buf, '\n', n);
879 len = p ? p - buf : n;
880
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800881 mutex_lock(&pm_mutex);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700882 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700883 if (len == strlen(hibernation_modes[i])
884 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 mode = i;
886 break;
887 }
888 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700889 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c9352007-04-30 15:09:51 -0700890 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700891 case HIBERNATION_SHUTDOWN:
892 case HIBERNATION_REBOOT:
893 case HIBERNATION_TEST:
894 case HIBERNATION_TESTPROC:
895 hibernation_mode = mode;
Johannes Bergfe0c9352007-04-30 15:09:51 -0700896 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700897 case HIBERNATION_PLATFORM:
898 if (hibernation_ops)
899 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 else
901 error = -EINVAL;
902 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700903 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 error = -EINVAL;
905
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700906 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100907 pr_debug("PM: Hibernation mode set to '%s'\n",
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700908 hibernation_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800909 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return error ? error : n;
911}
912
913power_attr(disk);
914
Kay Sievers386f2752007-11-02 13:47:53 +0100915static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
916 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
919 MINOR(swsusp_resume_device));
920}
921
Kay Sievers386f2752007-11-02 13:47:53 +0100922static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
923 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800927 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Andrew Mortona5762192006-01-06 00:09:50 -0800929 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
930 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Andrew Mortona5762192006-01-06 00:09:50 -0800932 res = MKDEV(maj,min);
933 if (maj != MAJOR(res) || min != MINOR(res))
934 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800936 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800937 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800938 mutex_unlock(&pm_mutex);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100939 printk(KERN_INFO "PM: Starting manual resume from disk\n");
Andrew Mortona5762192006-01-06 00:09:50 -0800940 noresume = 0;
941 software_resume();
942 ret = n;
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800943 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800944 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947power_attr(resume);
948
Kay Sievers386f2752007-11-02 13:47:53 +0100949static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
950 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800951{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800952 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800953}
954
Kay Sievers386f2752007-11-02 13:47:53 +0100955static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
956 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800957{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800958 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800959
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800960 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800961 image_size = size;
962 return n;
963 }
964
965 return -EINVAL;
966}
967
968power_attr(image_size);
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970static struct attribute * g[] = {
971 &disk_attr.attr,
972 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800973 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 NULL,
975};
976
977
978static struct attribute_group attr_group = {
979 .attrs = g,
980};
981
982
983static int __init pm_disk_init(void)
984{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800985 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
988core_initcall(pm_disk_init);
989
990
991static int __init resume_setup(char *str)
992{
993 if (noresume)
994 return 1;
995
996 strncpy( resume_file, str, 255 );
997 return 1;
998}
999
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001000static int __init resume_offset_setup(char *str)
1001{
1002 unsigned long long offset;
1003
1004 if (noresume)
1005 return 1;
1006
1007 if (sscanf(str, "%llu", &offset) == 1)
1008 swsusp_resume_block = offset;
1009
1010 return 1;
1011}
1012
Bojan Smojverf996fc92010-09-09 23:06:23 +02001013static int __init hibernate_setup(char *str)
1014{
1015 if (!strncmp(str, "noresume", 8))
1016 noresume = 1;
1017 else if (!strncmp(str, "nocompress", 10))
1018 nocompress = 1;
1019 return 1;
1020}
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022static int __init noresume_setup(char *str)
1023{
1024 noresume = 1;
1025 return 1;
1026}
1027
1028__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001029__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030__setup("resume=", resume_setup);
Bojan Smojverf996fc92010-09-09 23:06:23 +02001031__setup("hibernate=", hibernate_setup);