blob: 0866b163c6bb928716e75c876761040817dc4c24 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/power/disk.c - Suspend-to-disk support.
3 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 * Copyright (c) 2004 Pavel Machek <pavel@suse.cz>
7 *
8 * This file is released under the GPLv2.
9 *
10 */
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>
17#include <linux/delay.h>
18#include <linux/fs.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070019#include <linux/mount.h>
Eric W. Biederman88d10bb2005-09-22 21:43:46 -070020#include <linux/pm.h>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070021#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070022#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080023#include <linux/freezer.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "power.h"
26
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static int noresume = 0;
29char resume_file[256] = CONFIG_PM_STD_PARTITION;
30dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080031sector_t swsusp_resume_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070033enum {
34 HIBERNATION_INVALID,
35 HIBERNATION_PLATFORM,
36 HIBERNATION_TEST,
37 HIBERNATION_TESTPROC,
38 HIBERNATION_SHUTDOWN,
39 HIBERNATION_REBOOT,
40 /* keep last */
41 __HIBERNATION_AFTER_LAST
42};
43#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
44#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
45
46static int hibernation_mode = HIBERNATION_SHUTDOWN;
47
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070048static struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070049
50/**
51 * hibernation_set_ops - set the global hibernate operations
52 * @ops: the hibernation operations to use in subsequent hibernation transitions
53 */
54
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070055void hibernation_set_ops(struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070056{
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -070057 if (ops && !(ops->start && ops->pre_snapshot && ops->finish
58 && ops->prepare && ops->enter && ops->pre_restore
59 && ops->restore_cleanup)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070060 WARN_ON(1);
61 return;
62 }
63 mutex_lock(&pm_mutex);
64 hibernation_ops = ops;
65 if (ops)
66 hibernation_mode = HIBERNATION_PLATFORM;
67 else if (hibernation_mode == HIBERNATION_PLATFORM)
68 hibernation_mode = HIBERNATION_SHUTDOWN;
69
70 mutex_unlock(&pm_mutex);
71}
72
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +010073#ifdef CONFIG_PM_DEBUG
74static void hibernation_debug_sleep(void)
75{
76 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
77 mdelay(5000);
78}
79
80static int hibernation_testmode(int mode)
81{
82 if (hibernation_mode == mode) {
83 hibernation_debug_sleep();
84 return 1;
85 }
86 return 0;
87}
88
89static int hibernation_test(int level)
90{
91 if (pm_test_level == level) {
92 hibernation_debug_sleep();
93 return 1;
94 }
95 return 0;
96}
97#else /* !CONFIG_PM_DEBUG */
98static int hibernation_testmode(int mode) { return 0; }
99static int hibernation_test(int level) { return 0; }
100#endif /* !CONFIG_PM_DEBUG */
101
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700102/**
103 * platform_start - tell the platform driver that we're starting
104 * hibernation
105 */
106
107static int platform_start(int platform_mode)
108{
109 return (platform_mode && hibernation_ops) ?
110 hibernation_ops->start() : 0;
111}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/**
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700114 * platform_pre_snapshot - prepare the machine for hibernation using the
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800115 * platform driver if so configured and return an error code if it fails
116 */
117
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700118static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800119{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700120 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700121 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800122}
123
124/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700125 * platform_leave - prepare the machine for switching to the normal mode
126 * of operation using the platform driver (called with interrupts disabled)
127 */
128
129static void platform_leave(int platform_mode)
130{
131 if (platform_mode && hibernation_ops)
132 hibernation_ops->leave();
133}
134
135/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700136 * platform_finish - switch the machine to the normal mode of operation
137 * using the platform driver (must be called after platform_prepare())
138 */
139
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700140static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700141{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700142 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700143 hibernation_ops->finish();
144}
145
146/**
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700147 * platform_pre_restore - prepare the platform for the restoration from a
148 * hibernation image. If the restore fails after this function has been
149 * called, platform_restore_cleanup() must be called.
150 */
151
152static int platform_pre_restore(int platform_mode)
153{
154 return (platform_mode && hibernation_ops) ?
155 hibernation_ops->pre_restore() : 0;
156}
157
158/**
159 * platform_restore_cleanup - switch the platform to the normal mode of
160 * operation after a failing restore. If platform_pre_restore() has been
161 * called before the failing restore, this function must be called too,
162 * regardless of the result of platform_pre_restore().
163 */
164
165static void platform_restore_cleanup(int platform_mode)
166{
167 if (platform_mode && hibernation_ops)
168 hibernation_ops->restore_cleanup();
169}
170
171/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700172 * create_image - freeze devices that need to be frozen with interrupts
173 * off, create the hibernation image and thaw those devices. Control
174 * reappears in this routine after a restore.
175 */
176
177int create_image(int platform_mode)
178{
179 int error;
180
181 error = arch_prepare_suspend();
182 if (error)
183 return error;
184
185 local_irq_disable();
186 /* At this point, device_suspend() has been called, but *not*
187 * device_power_down(). We *must* call device_power_down() now.
188 * Otherwise, drivers for some devices (e.g. interrupt controllers)
189 * become desynchronized with the actual state of the hardware
190 * at resume time, and evil weirdness ensues.
191 */
192 error = device_power_down(PMSG_FREEZE);
193 if (error) {
194 printk(KERN_ERR "Some devices failed to power down, "
195 KERN_ERR "aborting suspend\n");
196 goto Enable_irqs;
197 }
198
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100199 if (hibernation_test(TEST_CORE))
200 goto Power_up;
201
202 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700203 save_processor_state();
204 error = swsusp_arch_suspend();
205 if (error)
206 printk(KERN_ERR "Error %d while creating the image\n", error);
207 /* Restore control flow magically appears here */
208 restore_processor_state();
209 if (!in_suspend)
210 platform_leave(platform_mode);
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100211 Power_up:
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700212 /* NOTE: device_power_up() is just a resume() for devices
213 * that suspended with irqs off ... no overall powerup.
214 */
215 device_power_up();
216 Enable_irqs:
217 local_irq_enable();
218 return error;
219}
220
221/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700222 * hibernation_snapshot - quiesce devices and create the hibernation
223 * snapshot image.
224 * @platform_mode - if set, use the platform driver, if available, to
225 * prepare the platform frimware for the power transition.
226 *
227 * Must be called with pm_mutex held
228 */
229
230int hibernation_snapshot(int platform_mode)
231{
232 int error;
233
234 /* Free memory before shutting down devices. */
235 error = swsusp_shrink_memory();
236 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700237 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700238
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700239 error = platform_start(platform_mode);
240 if (error)
241 return error;
242
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700243 suspend_console();
244 error = device_suspend(PMSG_FREEZE);
245 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700246 goto Resume_console;
247
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100248 if (hibernation_test(TEST_DEVICES))
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700249 goto Resume_devices;
250
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100251 error = platform_pre_snapshot(platform_mode);
252 if (error || hibernation_test(TEST_PLATFORM))
253 goto Finish;
254
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700255 error = disable_nonboot_cpus();
256 if (!error) {
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100257 if (hibernation_test(TEST_CPUS))
258 goto Enable_cpus;
259
260 if (hibernation_testmode(HIBERNATION_TEST))
261 goto Enable_cpus;
262
263 error = create_image(platform_mode);
264 /* Control returns here after successful restore */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700265 }
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100266 Enable_cpus:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700267 enable_nonboot_cpus();
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100268 Finish:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700269 platform_finish(platform_mode);
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100270 Resume_devices:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700271 device_resume();
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700272 Resume_console:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700273 resume_console();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700274 return error;
275}
276
277/**
278 * hibernation_restore - quiesce devices and restore the hibernation
279 * snapshot image. If successful, control returns in hibernation_snaphot()
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700280 * @platform_mode - if set, use the platform driver, if available, to
281 * prepare the platform frimware for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700282 *
283 * Must be called with pm_mutex held
284 */
285
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700286int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700287{
288 int error;
289
290 pm_prepare_console();
291 suspend_console();
292 error = device_suspend(PMSG_PRETHAW);
293 if (error)
294 goto Finish;
295
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700296 error = platform_pre_restore(platform_mode);
297 if (!error) {
298 error = disable_nonboot_cpus();
299 if (!error)
300 error = swsusp_resume();
301 enable_nonboot_cpus();
302 }
303 platform_restore_cleanup(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700304 device_resume();
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700305 Finish:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700306 resume_console();
307 pm_restore_console();
308 return error;
309}
310
311/**
312 * hibernation_platform_enter - enter the hibernation state using the
313 * platform driver (if available)
314 */
315
316int hibernation_platform_enter(void)
317{
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700318 int error;
319
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700320 if (!hibernation_ops)
321 return -ENOSYS;
322
323 /*
324 * We have cancelled the power transition by running
325 * hibernation_ops->finish() before saving the image, so we should let
326 * the firmware know that we're going to enter the sleep state after all
327 */
328 error = hibernation_ops->start();
329 if (error)
330 return error;
331
332 suspend_console();
333 error = device_suspend(PMSG_SUSPEND);
334 if (error)
335 goto Resume_console;
336
337 error = hibernation_ops->prepare();
338 if (error)
339 goto Resume_devices;
340
341 error = disable_nonboot_cpus();
342 if (error)
343 goto Finish;
344
345 local_irq_disable();
346 error = device_power_down(PMSG_SUSPEND);
347 if (!error) {
348 hibernation_ops->enter();
349 /* We should never get here */
350 while (1);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700351 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700352 local_irq_enable();
353
354 /*
355 * We don't need to reenable the nonboot CPUs or resume consoles, since
356 * the system is going to be halted anyway.
357 */
358 Finish:
359 hibernation_ops->finish();
360 Resume_devices:
361 device_resume();
362 Resume_console:
363 resume_console();
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700364 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700365}
366
367/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700368 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 *
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700370 * Use the platform driver, if configured so; otherwise try
371 * to power off or reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 */
373
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700374static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700376 switch (hibernation_mode) {
377 case HIBERNATION_TEST:
378 case HIBERNATION_TESTPROC:
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700379 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700380 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600381 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700383 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700384 hibernation_platform_enter();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700385 case HIBERNATION_SHUTDOWN:
386 kernel_power_off();
387 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600389 kernel_halt();
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700390 /*
391 * Valid image is on the disk, if we continue we risk serious data
392 * corruption after resume.
393 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 printk(KERN_CRIT "Please power me down manually\n");
395 while(1);
396}
397
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800398static void unprepare_processes(void)
399{
400 thaw_processes();
401 pm_restore_console();
402}
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404static int prepare_processes(void)
405{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800406 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 pm_prepare_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (freeze_processes()) {
410 error = -EBUSY;
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800411 unprepare_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700413 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700417 * hibernate - The granpappy of the built-in hibernation management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
419
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700420int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 int error;
423
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700424 mutex_lock(&pm_mutex);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700425 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700426 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
427 error = -EBUSY;
428 goto Unlock;
429 }
430
431 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
432 if (error)
433 goto Exit;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700434
435 /* Allocate memory management structures */
436 error = create_basic_memory_bitmaps();
437 if (error)
438 goto Exit;
439
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700440 printk("Syncing filesystems ... ");
441 sys_sync();
442 printk("done.\n");
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700445 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700446 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100448 if (hibernation_test(TEST_FREEZER))
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800449 goto Thaw;
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100450
451 if (hibernation_testmode(HIBERNATION_TESTPROC))
452 goto Thaw;
453
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700454 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
455 if (in_suspend && !error) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700456 unsigned int flags = 0;
457
458 if (hibernation_mode == HIBERNATION_PLATFORM)
459 flags |= SF_PLATFORM_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700461 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700462 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (!error)
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700464 power_down();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800465 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700467 swsusp_free();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800468 }
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800469 Thaw:
Pavel Machek99dc7d62005-09-03 15:57:05 -0700470 unprepare_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700471 Finish:
472 free_basic_memory_bitmaps();
473 Exit:
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700474 pm_notifier_call_chain(PM_POST_HIBERNATION);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700475 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700476 Unlock:
477 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return error;
479}
480
481
482/**
483 * software_resume - Resume from a saved image.
484 *
485 * Called as a late_initcall (so all devices are discovered and
486 * initialized), we call swsusp to see if we have a saved image or not.
487 * If so, we quiesce devices, the restore the saved image. We will
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700488 * return above (in hibernate() ) if everything goes well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 * Otherwise, we fail gracefully and return to the normally
490 * scheduled program.
491 *
492 */
493
494static int software_resume(void)
495{
496 int error;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700497 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Johannes Berg60a0d232007-11-14 17:00:16 -0800499 /*
500 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
501 * is configured into the kernel. Since the regular hibernate
502 * trigger path is via sysfs which takes a buffer mutex before
503 * calling hibernate functions (which take pm_mutex) this can
504 * cause lockdep to complain about a possible ABBA deadlock
505 * which cannot happen since we're in the boot code here and
506 * sysfs can't be invoked yet. Therefore, we use a subclass
507 * here to avoid lockdep complaining.
508 */
509 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
Pavel Machek3efa1472005-07-07 17:56:43 -0700510 if (!swsusp_resume_device) {
Shaohua Lidd5d6662005-09-03 15:57:04 -0700511 if (!strlen(resume_file)) {
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800512 mutex_unlock(&pm_mutex);
Pavel Machek3efa1472005-07-07 17:56:43 -0700513 return -ENOENT;
Shaohua Lidd5d6662005-09-03 15:57:04 -0700514 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700515 swsusp_resume_device = name_to_dev_t(resume_file);
516 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
517 } else {
518 pr_debug("swsusp: Resume From Partition %d:%d\n",
519 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
520 }
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (noresume) {
523 /**
524 * FIXME: If noresume is specified, we need to find the partition
525 * and reset it back to normal swap space.
526 */
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800527 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return 0;
529 }
530
531 pr_debug("PM: Checking swsusp image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800532 error = swsusp_check();
533 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700534 goto Unlock;
535
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700536 /* The snapshot device should not be opened while we're running */
537 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
538 error = -EBUSY;
539 goto Unlock;
540 }
541
Alan Sternc3e94d82007-11-19 23:38:25 +0100542 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
543 if (error)
544 goto Finish;
545
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700546 error = create_basic_memory_bitmaps();
547 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700548 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 pr_debug("PM: Preparing processes for restore.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800551 error = prepare_processes();
552 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 swsusp_close();
Li Shaohua5a72e042005-06-25 14:55:06 -0700554 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556
557 pr_debug("PM: Reading swsusp image.\n");
558
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700559 error = swsusp_read(&flags);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800560 if (!error)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700561 hibernation_restore(flags & SF_PLATFORM_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800563 printk(KERN_ERR "PM: Restore failed, recovering.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700564 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 unprepare_processes();
566 Done:
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700567 free_basic_memory_bitmaps();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700568 Finish:
Alan Sternc3e94d82007-11-19 23:38:25 +0100569 pm_notifier_call_chain(PM_POST_RESTORE);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700570 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700571 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700572 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800573 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 pr_debug("PM: Resume from disk failed.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700575 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578late_initcall(software_resume);
579
580
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700581static const char * const hibernation_modes[] = {
582 [HIBERNATION_PLATFORM] = "platform",
583 [HIBERNATION_SHUTDOWN] = "shutdown",
584 [HIBERNATION_REBOOT] = "reboot",
585 [HIBERNATION_TEST] = "test",
586 [HIBERNATION_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587};
588
589/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700590 * disk - Control hibernation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700592 * Suspend-to-disk can be handled in several ways. We have a few options
593 * for putting the system to sleep - using the platform driver (e.g. ACPI
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700594 * or other hibernation_ops), powering off the system or rebooting the
595 * system (for testing) as well as the two test modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700597 * The system can support 'platform', and that is known a priori (and
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700598 * encoded by the presence of hibernation_ops). However, the user may
599 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
600 * test modes, 'test' or 'testproc'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *
602 * show() will display what the mode is currently set to.
603 * store() will accept one of
604 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * 'platform'
606 * 'shutdown'
607 * 'reboot'
Johannes Berg11d77d02007-04-30 15:09:53 -0700608 * 'test'
609 * 'testproc'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700611 * It will only change to 'platform' if the system
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700612 * supports it (as determined by having hibernation_ops).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
614
Kay Sievers386f2752007-11-02 13:47:53 +0100615static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
616 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700618 int i;
619 char *start = buf;
620
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700621 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
622 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700623 continue;
624 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700625 case HIBERNATION_SHUTDOWN:
626 case HIBERNATION_REBOOT:
627 case HIBERNATION_TEST:
628 case HIBERNATION_TESTPROC:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700629 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700630 case HIBERNATION_PLATFORM:
631 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700632 break;
633 /* not a valid mode, continue with loop */
634 continue;
635 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700636 if (i == hibernation_mode)
637 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700638 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700639 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700640 }
641 buf += sprintf(buf, "\n");
642 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645
Kay Sievers386f2752007-11-02 13:47:53 +0100646static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
647 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 int error = 0;
650 int i;
651 int len;
652 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700653 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 p = memchr(buf, '\n', n);
656 len = p ? p - buf : n;
657
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800658 mutex_lock(&pm_mutex);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700659 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700660 if (len == strlen(hibernation_modes[i])
661 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 mode = i;
663 break;
664 }
665 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700666 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700667 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700668 case HIBERNATION_SHUTDOWN:
669 case HIBERNATION_REBOOT:
670 case HIBERNATION_TEST:
671 case HIBERNATION_TESTPROC:
672 hibernation_mode = mode;
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700673 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700674 case HIBERNATION_PLATFORM:
675 if (hibernation_ops)
676 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 else
678 error = -EINVAL;
679 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700680 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 error = -EINVAL;
682
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700683 if (!error)
684 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
685 hibernation_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800686 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return error ? error : n;
688}
689
690power_attr(disk);
691
Kay Sievers386f2752007-11-02 13:47:53 +0100692static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
693 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
696 MINOR(swsusp_resume_device));
697}
698
Kay Sievers386f2752007-11-02 13:47:53 +0100699static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
700 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800704 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Andrew Mortona5762192006-01-06 00:09:50 -0800706 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
707 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Andrew Mortona5762192006-01-06 00:09:50 -0800709 res = MKDEV(maj,min);
710 if (maj != MAJOR(res) || min != MINOR(res))
711 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800713 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800714 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800715 mutex_unlock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800716 printk("Attempting manual resume\n");
717 noresume = 0;
718 software_resume();
719 ret = n;
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800720 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800721 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
724power_attr(resume);
725
Kay Sievers386f2752007-11-02 13:47:53 +0100726static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
727 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800728{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800729 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800730}
731
Kay Sievers386f2752007-11-02 13:47:53 +0100732static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
733 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800734{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800735 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800736
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800737 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800738 image_size = size;
739 return n;
740 }
741
742 return -EINVAL;
743}
744
745power_attr(image_size);
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747static struct attribute * g[] = {
748 &disk_attr.attr,
749 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800750 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 NULL,
752};
753
754
755static struct attribute_group attr_group = {
756 .attrs = g,
757};
758
759
760static int __init pm_disk_init(void)
761{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800762 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765core_initcall(pm_disk_init);
766
767
768static int __init resume_setup(char *str)
769{
770 if (noresume)
771 return 1;
772
773 strncpy( resume_file, str, 255 );
774 return 1;
775}
776
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800777static int __init resume_offset_setup(char *str)
778{
779 unsigned long long offset;
780
781 if (noresume)
782 return 1;
783
784 if (sscanf(str, "%llu", &offset) == 1)
785 swsusp_resume_block = offset;
786
787 return 1;
788}
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790static int __init noresume_setup(char *str)
791{
792 noresume = 1;
793 return 1;
794}
795
796__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800797__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798__setup("resume=", resume_setup);