blob: bbfe472d7524245c0edb87f99c2af7f676ff24f2 [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
6 * Copyright (c) 2004 Pavel Machek <pavel@suse.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>
Rafael J. Wysockic7510852009-04-12 20:06:56 +020025#include <scsi/scsi_scan.h>
Magnus Damma8af7892009-03-31 15:23:37 -070026#include <asm/suspend.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "power.h"
29
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int noresume = 0;
Adrian Bunk47a460d2008-02-04 22:30:06 -080032static char resume_file[256] = CONFIG_PM_STD_PARTITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080034sector_t swsusp_resume_block;
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +010035int in_suspend __nosavedata = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070037enum {
38 HIBERNATION_INVALID,
39 HIBERNATION_PLATFORM,
40 HIBERNATION_TEST,
41 HIBERNATION_TESTPROC,
42 HIBERNATION_SHUTDOWN,
43 HIBERNATION_REBOOT,
44 /* keep last */
45 __HIBERNATION_AFTER_LAST
46};
47#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
48#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
49
50static int hibernation_mode = HIBERNATION_SHUTDOWN;
51
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070052static struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070053
54/**
55 * hibernation_set_ops - set the global hibernate operations
56 * @ops: the hibernation operations to use in subsequent hibernation transitions
57 */
58
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070059void hibernation_set_ops(struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070060{
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +010061 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
62 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -070063 && ops->restore_cleanup)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070064 WARN_ON(1);
65 return;
66 }
67 mutex_lock(&pm_mutex);
68 hibernation_ops = ops;
69 if (ops)
70 hibernation_mode = HIBERNATION_PLATFORM;
71 else if (hibernation_mode == HIBERNATION_PLATFORM)
72 hibernation_mode = HIBERNATION_SHUTDOWN;
73
74 mutex_unlock(&pm_mutex);
75}
76
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +010077static bool entering_platform_hibernation;
78
79bool system_entering_hibernation(void)
80{
81 return entering_platform_hibernation;
82}
83EXPORT_SYMBOL(system_entering_hibernation);
84
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +010085#ifdef CONFIG_PM_DEBUG
86static void hibernation_debug_sleep(void)
87{
88 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
89 mdelay(5000);
90}
91
92static int hibernation_testmode(int mode)
93{
94 if (hibernation_mode == mode) {
95 hibernation_debug_sleep();
96 return 1;
97 }
98 return 0;
99}
100
101static int hibernation_test(int level)
102{
103 if (pm_test_level == level) {
104 hibernation_debug_sleep();
105 return 1;
106 }
107 return 0;
108}
109#else /* !CONFIG_PM_DEBUG */
110static int hibernation_testmode(int mode) { return 0; }
111static int hibernation_test(int level) { return 0; }
112#endif /* !CONFIG_PM_DEBUG */
113
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700114/**
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100115 * platform_begin - tell the platform driver that we're starting
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700116 * hibernation
117 */
118
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100119static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700120{
121 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100122 hibernation_ops->begin() : 0;
123}
124
125/**
126 * platform_end - tell the platform driver that we've entered the
127 * working state
128 */
129
130static void platform_end(int platform_mode)
131{
132 if (platform_mode && hibernation_ops)
133 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700134}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/**
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700137 * platform_pre_snapshot - prepare the machine for hibernation using the
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800138 * platform driver if so configured and return an error code if it fails
139 */
140
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700141static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800142{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700143 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700144 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800145}
146
147/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700148 * platform_leave - prepare the machine for switching to the normal mode
149 * of operation using the platform driver (called with interrupts disabled)
150 */
151
152static void platform_leave(int platform_mode)
153{
154 if (platform_mode && hibernation_ops)
155 hibernation_ops->leave();
156}
157
158/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700159 * platform_finish - switch the machine to the normal mode of operation
160 * using the platform driver (must be called after platform_prepare())
161 */
162
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700163static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700164{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700165 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700166 hibernation_ops->finish();
167}
168
169/**
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700170 * platform_pre_restore - prepare the platform for the restoration from a
171 * hibernation image. If the restore fails after this function has been
172 * called, platform_restore_cleanup() must be called.
173 */
174
175static int platform_pre_restore(int platform_mode)
176{
177 return (platform_mode && hibernation_ops) ?
178 hibernation_ops->pre_restore() : 0;
179}
180
181/**
182 * platform_restore_cleanup - switch the platform to the normal mode of
183 * operation after a failing restore. If platform_pre_restore() has been
184 * called before the failing restore, this function must be called too,
185 * regardless of the result of platform_pre_restore().
186 */
187
188static void platform_restore_cleanup(int platform_mode)
189{
190 if (platform_mode && hibernation_ops)
191 hibernation_ops->restore_cleanup();
192}
193
194/**
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200195 * platform_recover - recover the platform from a failure to suspend
196 * devices.
197 */
198
199static void platform_recover(int platform_mode)
200{
201 if (platform_mode && hibernation_ops && hibernation_ops->recover)
202 hibernation_ops->recover();
203}
204
205/**
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100206 * swsusp_show_speed - print the time elapsed between two events.
207 * @start: Starting event.
208 * @stop: Final event.
209 * @nr_pages - number of pages processed between @start and @stop
210 * @msg - introductory message to print
211 */
212
213void swsusp_show_speed(struct timeval *start, struct timeval *stop,
214 unsigned nr_pages, char *msg)
215{
216 s64 elapsed_centisecs64;
217 int centisecs;
218 int k;
219 int kps;
220
221 elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
222 do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
223 centisecs = elapsed_centisecs64;
224 if (centisecs == 0)
225 centisecs = 1; /* avoid div-by-zero */
226 k = nr_pages * (PAGE_SIZE / 1024);
227 kps = (k * 100) / centisecs;
228 printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
229 msg, k,
230 centisecs / 100, centisecs % 100,
231 kps / 1000, (kps % 1000) / 10);
232}
233
234/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700235 * create_image - freeze devices that need to be frozen with interrupts
236 * off, create the hibernation image and thaw those devices. Control
237 * reappears in this routine after a restore.
238 */
239
Adrian Bunk47a460d2008-02-04 22:30:06 -0800240static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700241{
242 int error;
243
244 error = arch_prepare_suspend();
245 if (error)
246 return error;
247
Alan Sternd1616302009-05-24 22:05:42 +0200248 /* At this point, dpm_suspend_start() has been called, but *not*
249 * dpm_suspend_noirq(). We *must* call dpm_suspend_noirq() now.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700250 * Otherwise, drivers for some devices (e.g. interrupt controllers)
251 * become desynchronized with the actual state of the hardware
252 * at resume time, and evil weirdness ensues.
253 */
Alan Sternd1616302009-05-24 22:05:42 +0200254 error = dpm_suspend_noirq(PMSG_FREEZE);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700255 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100256 printk(KERN_ERR "PM: Some devices failed to power down, "
257 "aborting hibernation\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200258 return error;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700259 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100260
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100261 error = platform_pre_snapshot(platform_mode);
262 if (error || hibernation_test(TEST_PLATFORM))
263 goto Platform_finish;
264
265 error = disable_nonboot_cpus();
266 if (error || hibernation_test(TEST_CPUS)
267 || hibernation_testmode(HIBERNATION_TEST))
268 goto Enable_cpus;
269
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100270 local_irq_disable();
271
Bjorn Helgaas44840792009-05-15 23:30:50 +0200272 error = sysdev_suspend(PMSG_FREEZE);
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100273 if (error) {
Bjorn Helgaas44840792009-05-15 23:30:50 +0200274 printk(KERN_ERR "PM: Some system devices failed to power down, "
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100275 "aborting hibernation\n");
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100276 goto Enable_irqs;
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100277 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700278
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100279 if (hibernation_test(TEST_CORE))
280 goto Power_up;
281
282 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700283 save_processor_state();
284 error = swsusp_arch_suspend();
285 if (error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100286 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
287 error);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700288 /* Restore control flow magically appears here */
289 restore_processor_state();
290 if (!in_suspend)
291 platform_leave(platform_mode);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100292
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100293 Power_up:
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100294 sysdev_resume();
Alan Sternd1616302009-05-24 22:05:42 +0200295 /* NOTE: dpm_resume_noirq() is just a resume() for devices
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700296 * that suspended with irqs off ... no overall powerup.
297 */
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100298
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100299 Enable_irqs:
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100300 local_irq_enable();
301
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100302 Enable_cpus:
303 enable_nonboot_cpus();
304
305 Platform_finish:
306 platform_finish(platform_mode);
307
Alan Sternd1616302009-05-24 22:05:42 +0200308 dpm_resume_noirq(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200309 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100310
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700311 return error;
312}
313
314/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700315 * hibernation_snapshot - quiesce devices and create the hibernation
316 * snapshot image.
317 * @platform_mode - if set, use the platform driver, if available, to
Nick Andrew877d0312009-01-26 11:06:57 +0100318 * prepare the platform firmware for the power transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700319 *
320 * Must be called with pm_mutex held
321 */
322
323int hibernation_snapshot(int platform_mode)
324{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100325 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700326
Zhang Rui3fe03132008-10-26 20:50:26 +0100327 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700328 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700329 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700330
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200331 /* Preallocate image memory before shutting down devices. */
332 error = hibernate_preallocate_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700333 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100334 goto Close;
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700335
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700336 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +0200337 error = dpm_suspend_start(PMSG_FREEZE);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700338 if (error)
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200339 goto Recover_platform;
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700340
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100341 if (hibernation_test(TEST_DEVICES))
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200342 goto Recover_platform;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700343
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100344 error = create_image(platform_mode);
345 /* Control returns here after successful restore */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100346
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100347 Resume_devices:
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200348 /* We may need to release the preallocated image pages here. */
349 if (error || !in_suspend)
350 swsusp_free();
351
Alan Sternd1616302009-05-24 22:05:42 +0200352 dpm_resume_end(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200353 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700354 resume_console();
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100355 Close:
356 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700357 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200358
359 Recover_platform:
360 platform_recover(platform_mode);
361 goto Resume_devices;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700362}
363
364/**
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100365 * resume_target_kernel - prepare devices that need to be suspended with
366 * interrupts off, restore the contents of highmem that have not been
367 * restored yet from the image and run the low level code that will restore
368 * the remaining contents of memory and switch to the just restored target
369 * kernel.
370 */
371
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100372static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100373{
374 int error;
375
Alan Sternd1616302009-05-24 22:05:42 +0200376 error = dpm_suspend_noirq(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100377 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100378 printk(KERN_ERR "PM: Some devices failed to power down, "
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100379 "aborting resume\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200380 return error;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100381 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100382
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100383 error = platform_pre_restore(platform_mode);
384 if (error)
385 goto Cleanup;
386
387 error = disable_nonboot_cpus();
388 if (error)
389 goto Enable_cpus;
390
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100391 local_irq_disable();
392
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100393 error = sysdev_suspend(PMSG_QUIESCE);
394 if (error)
395 goto Enable_irqs;
396
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100397 /* We'll ignore saved state, but this gets preempt count (etc) right */
398 save_processor_state();
399 error = restore_highmem();
400 if (!error) {
401 error = swsusp_arch_resume();
402 /*
403 * The code below is only ever reached in case of a failure.
404 * Otherwise execution continues at place where
405 * swsusp_arch_suspend() was called
406 */
407 BUG_ON(!error);
408 /* This call to restore_highmem() undos the previous one */
409 restore_highmem();
410 }
411 /*
412 * The only reason why swsusp_arch_resume() can fail is memory being
413 * very tight, so we have to free it as soon as we can to avoid
414 * subsequent failures
415 */
416 swsusp_free();
417 restore_processor_state();
418 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100419
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100420 sysdev_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100421
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100422 Enable_irqs:
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100423 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100424
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100425 Enable_cpus:
426 enable_nonboot_cpus();
427
428 Cleanup:
429 platform_restore_cleanup(platform_mode);
430
Alan Sternd1616302009-05-24 22:05:42 +0200431 dpm_resume_noirq(PMSG_RECOVER);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100432
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100433 return error;
434}
435
436/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700437 * hibernation_restore - quiesce devices and restore the hibernation
438 * snapshot image. If successful, control returns in hibernation_snaphot()
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700439 * @platform_mode - if set, use the platform driver, if available, to
Nick Andrew877d0312009-01-26 11:06:57 +0100440 * prepare the platform firmware for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700441 *
442 * Must be called with pm_mutex held
443 */
444
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700445int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700446{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100447 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700448
449 pm_prepare_console();
450 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +0200451 error = dpm_suspend_start(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700452 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100453 error = resume_target_kernel(platform_mode);
Alan Sternd1616302009-05-24 22:05:42 +0200454 dpm_resume_end(PMSG_RECOVER);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700455 }
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700456 resume_console();
457 pm_restore_console();
458 return error;
459}
460
461/**
462 * hibernation_platform_enter - enter the hibernation state using the
463 * platform driver (if available)
464 */
465
466int hibernation_platform_enter(void)
467{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100468 int error;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700469
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700470 if (!hibernation_ops)
471 return -ENOSYS;
472
473 /*
474 * We have cancelled the power transition by running
475 * hibernation_ops->finish() before saving the image, so we should let
476 * the firmware know that we're going to enter the sleep state after all
477 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100478 error = hibernation_ops->begin();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700479 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100480 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700481
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100482 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700483 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +0200484 error = dpm_suspend_start(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200485 if (error) {
486 if (hibernation_ops->recover)
487 hibernation_ops->recover();
488 goto Resume_devices;
489 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700490
Alan Sternd1616302009-05-24 22:05:42 +0200491 error = dpm_suspend_noirq(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100492 if (error)
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200493 goto Resume_devices;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100494
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100495 error = hibernation_ops->prepare();
496 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200497 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100498
499 error = disable_nonboot_cpus();
500 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200501 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100502
503 local_irq_disable();
504 sysdev_suspend(PMSG_HIBERNATE);
505 hibernation_ops->enter();
506 /* We should never get here */
507 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700508
509 /*
510 * We don't need to reenable the nonboot CPUs or resume consoles, since
511 * the system is going to be halted anyway.
512 */
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200513 Platform_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700514 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100515
Alan Sternd1616302009-05-24 22:05:42 +0200516 dpm_suspend_noirq(PMSG_RESTORE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100517
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700518 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100519 entering_platform_hibernation = false;
Alan Sternd1616302009-05-24 22:05:42 +0200520 dpm_resume_end(PMSG_RESTORE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700521 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100522
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100523 Close:
524 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100525
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700526 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700527}
528
529/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700530 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 *
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700532 * Use the platform driver, if configured so; otherwise try
533 * to power off or reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 */
535
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700536static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700538 switch (hibernation_mode) {
539 case HIBERNATION_TEST:
540 case HIBERNATION_TESTPROC:
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700541 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700542 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600543 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700545 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700546 hibernation_platform_enter();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700547 case HIBERNATION_SHUTDOWN:
548 kernel_power_off();
549 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600551 kernel_halt();
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700552 /*
553 * Valid image is on the disk, if we continue we risk serious data
554 * corruption after resume.
555 */
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100556 printk(KERN_CRIT "PM: Please power down manually\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 while(1);
558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560static int prepare_processes(void)
561{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800562 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (freeze_processes()) {
565 error = -EBUSY;
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100566 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700568 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700572 * hibernate - The granpappy of the built-in hibernation management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 */
574
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700575int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 int error;
578
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700579 mutex_lock(&pm_mutex);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700580 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700581 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
582 error = -EBUSY;
583 goto Unlock;
584 }
585
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100586 pm_prepare_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700587 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
588 if (error)
589 goto Exit;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700590
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700591 error = usermodehelper_disable();
592 if (error)
593 goto Exit;
594
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700595 /* Allocate memory management structures */
596 error = create_basic_memory_bitmaps();
597 if (error)
598 goto Exit;
599
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100600 printk(KERN_INFO "PM: Syncing filesystems ... ");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700601 sys_sync();
602 printk("done.\n");
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700605 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700606 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100608 if (hibernation_test(TEST_FREEZER))
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800609 goto Thaw;
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100610
611 if (hibernation_testmode(HIBERNATION_TESTPROC))
612 goto Thaw;
613
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700614 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200615 if (error)
616 goto Thaw;
617
618 if (in_suspend) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700619 unsigned int flags = 0;
620
621 if (hibernation_mode == HIBERNATION_PLATFORM)
622 flags |= SF_PLATFORM_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700624 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700625 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (!error)
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700627 power_down();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800628 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800630 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200631
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800632 Thaw:
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100633 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700634 Finish:
635 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700636 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700637 Exit:
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700638 pm_notifier_call_chain(PM_POST_HIBERNATION);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100639 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700640 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700641 Unlock:
642 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return error;
644}
645
646
647/**
648 * software_resume - Resume from a saved image.
649 *
650 * Called as a late_initcall (so all devices are discovered and
651 * initialized), we call swsusp to see if we have a saved image or not.
652 * If so, we quiesce devices, the restore the saved image. We will
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700653 * return above (in hibernate() ) if everything goes well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 * Otherwise, we fail gracefully and return to the normally
655 * scheduled program.
656 *
657 */
658
659static int software_resume(void)
660{
661 int error;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700662 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Johannes Berg60a0d232007-11-14 17:00:16 -0800664 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100665 * If the user said "noresume".. bail out early.
666 */
667 if (noresume)
668 return 0;
669
670 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800671 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
672 * is configured into the kernel. Since the regular hibernate
673 * trigger path is via sysfs which takes a buffer mutex before
674 * calling hibernate functions (which take pm_mutex) this can
675 * cause lockdep to complain about a possible ABBA deadlock
676 * which cannot happen since we're in the boot code here and
677 * sysfs can't be invoked yet. Therefore, we use a subclass
678 * here to avoid lockdep complaining.
679 */
680 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200681
682 if (swsusp_resume_device)
683 goto Check_image;
684
685 if (!strlen(resume_file)) {
686 error = -ENOENT;
687 goto Unlock;
688 }
689
690 pr_debug("PM: Checking image partition %s\n", resume_file);
691
692 /* Check if the device is there */
693 swsusp_resume_device = name_to_dev_t(resume_file);
Pavel Machek3efa1472005-07-07 17:56:43 -0700694 if (!swsusp_resume_device) {
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100695 /*
696 * Some device discovery might still be in progress; we need
697 * to wait for this to finish.
698 */
699 wait_for_device_probe();
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200700 /*
701 * We can't depend on SCSI devices being available after loading
702 * one of their modules until scsi_complete_async_scans() is
703 * called and the resume device usually is a SCSI one.
704 */
705 scsi_complete_async_scans();
706
Pavel Machek3efa1472005-07-07 17:56:43 -0700707 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200708 if (!swsusp_resume_device) {
709 error = -ENODEV;
710 goto Unlock;
711 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700712 }
713
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200714 Check_image:
715 pr_debug("PM: Resume from partition %d:%d\n",
716 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100718 pr_debug("PM: Checking hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800719 error = swsusp_check();
720 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700721 goto Unlock;
722
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700723 /* The snapshot device should not be opened while we're running */
724 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
725 error = -EBUSY;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200726 swsusp_close(FMODE_READ);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700727 goto Unlock;
728 }
729
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100730 pm_prepare_console();
Alan Sternc3e94d82007-11-19 23:38:25 +0100731 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
732 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200733 goto close_finish;
Alan Sternc3e94d82007-11-19 23:38:25 +0100734
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700735 error = usermodehelper_disable();
736 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200737 goto close_finish;
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700738
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700739 error = create_basic_memory_bitmaps();
740 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200741 goto close_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 pr_debug("PM: Preparing processes for restore.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800744 error = prepare_processes();
745 if (error) {
Al Viroc2dd0da2007-10-08 13:21:10 -0400746 swsusp_close(FMODE_READ);
Li Shaohua5a72e042005-06-25 14:55:06 -0700747 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100750 pr_debug("PM: Reading hibernation image.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700752 error = swsusp_read(&flags);
Jiri Slaby76b57e62009-10-07 22:37:35 +0200753 swsusp_close(FMODE_READ);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800754 if (!error)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700755 hibernation_restore(flags & SF_PLATFORM_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800757 printk(KERN_ERR "PM: Restore failed, recovering.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700758 swsusp_free();
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100759 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 Done:
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700761 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700762 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700763 Finish:
Alan Sternc3e94d82007-11-19 23:38:25 +0100764 pm_notifier_call_chain(PM_POST_RESTORE);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100765 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700766 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700767 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700768 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800769 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 pr_debug("PM: Resume from disk failed.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700771 return error;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200772close_finish:
773 swsusp_close(FMODE_READ);
774 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777late_initcall(software_resume);
778
779
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700780static const char * const hibernation_modes[] = {
781 [HIBERNATION_PLATFORM] = "platform",
782 [HIBERNATION_SHUTDOWN] = "shutdown",
783 [HIBERNATION_REBOOT] = "reboot",
784 [HIBERNATION_TEST] = "test",
785 [HIBERNATION_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786};
787
788/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700789 * disk - Control hibernation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700791 * Suspend-to-disk can be handled in several ways. We have a few options
792 * for putting the system to sleep - using the platform driver (e.g. ACPI
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700793 * or other hibernation_ops), powering off the system or rebooting the
794 * system (for testing) as well as the two test modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700796 * The system can support 'platform', and that is known a priori (and
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700797 * encoded by the presence of hibernation_ops). However, the user may
798 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
799 * test modes, 'test' or 'testproc'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 *
801 * show() will display what the mode is currently set to.
802 * store() will accept one of
803 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * 'platform'
805 * 'shutdown'
806 * 'reboot'
Johannes Berg11d77d02007-04-30 15:09:53 -0700807 * 'test'
808 * 'testproc'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700810 * It will only change to 'platform' if the system
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700811 * supports it (as determined by having hibernation_ops).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
813
Kay Sievers386f2752007-11-02 13:47:53 +0100814static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
815 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700817 int i;
818 char *start = buf;
819
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700820 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
821 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700822 continue;
823 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700824 case HIBERNATION_SHUTDOWN:
825 case HIBERNATION_REBOOT:
826 case HIBERNATION_TEST:
827 case HIBERNATION_TESTPROC:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700828 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700829 case HIBERNATION_PLATFORM:
830 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700831 break;
832 /* not a valid mode, continue with loop */
833 continue;
834 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700835 if (i == hibernation_mode)
836 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700837 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700838 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700839 }
840 buf += sprintf(buf, "\n");
841 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
844
Kay Sievers386f2752007-11-02 13:47:53 +0100845static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
846 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 int error = 0;
849 int i;
850 int len;
851 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700852 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 p = memchr(buf, '\n', n);
855 len = p ? p - buf : n;
856
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800857 mutex_lock(&pm_mutex);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700858 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700859 if (len == strlen(hibernation_modes[i])
860 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 mode = i;
862 break;
863 }
864 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700865 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700866 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700867 case HIBERNATION_SHUTDOWN:
868 case HIBERNATION_REBOOT:
869 case HIBERNATION_TEST:
870 case HIBERNATION_TESTPROC:
871 hibernation_mode = mode;
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700872 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700873 case HIBERNATION_PLATFORM:
874 if (hibernation_ops)
875 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 else
877 error = -EINVAL;
878 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700879 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 error = -EINVAL;
881
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700882 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100883 pr_debug("PM: Hibernation mode set to '%s'\n",
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700884 hibernation_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800885 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return error ? error : n;
887}
888
889power_attr(disk);
890
Kay Sievers386f2752007-11-02 13:47:53 +0100891static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
892 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
895 MINOR(swsusp_resume_device));
896}
897
Kay Sievers386f2752007-11-02 13:47:53 +0100898static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
899 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800903 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Andrew Mortona5762192006-01-06 00:09:50 -0800905 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
906 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Andrew Mortona5762192006-01-06 00:09:50 -0800908 res = MKDEV(maj,min);
909 if (maj != MAJOR(res) || min != MINOR(res))
910 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800912 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800913 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800914 mutex_unlock(&pm_mutex);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100915 printk(KERN_INFO "PM: Starting manual resume from disk\n");
Andrew Mortona5762192006-01-06 00:09:50 -0800916 noresume = 0;
917 software_resume();
918 ret = n;
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800919 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800920 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
923power_attr(resume);
924
Kay Sievers386f2752007-11-02 13:47:53 +0100925static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
926 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800927{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800928 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800929}
930
Kay Sievers386f2752007-11-02 13:47:53 +0100931static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
932 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800933{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800934 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800935
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800936 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800937 image_size = size;
938 return n;
939 }
940
941 return -EINVAL;
942}
943
944power_attr(image_size);
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946static struct attribute * g[] = {
947 &disk_attr.attr,
948 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800949 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 NULL,
951};
952
953
954static struct attribute_group attr_group = {
955 .attrs = g,
956};
957
958
959static int __init pm_disk_init(void)
960{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800961 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
964core_initcall(pm_disk_init);
965
966
967static int __init resume_setup(char *str)
968{
969 if (noresume)
970 return 1;
971
972 strncpy( resume_file, str, 255 );
973 return 1;
974}
975
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800976static int __init resume_offset_setup(char *str)
977{
978 unsigned long long offset;
979
980 if (noresume)
981 return 1;
982
983 if (sscanf(str, "%llu", &offset) == 1)
984 swsusp_resume_block = offset;
985
986 return 1;
987}
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989static int __init noresume_setup(char *str)
990{
991 noresume = 1;
992 return 1;
993}
994
995__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800996__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997__setup("resume=", resume_setup);