blob: b138b431e27135aa7b2716335288069ea00beeee [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. Wysocki74f270a2007-10-18 03:04:42 -070073/**
74 * platform_start - tell the platform driver that we're starting
75 * hibernation
76 */
77
78static int platform_start(int platform_mode)
79{
80 return (platform_mode && hibernation_ops) ?
81 hibernation_ops->start() : 0;
82}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/**
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -070085 * platform_pre_snapshot - prepare the machine for hibernation using the
Stefan Seyfried8a05aac2006-12-06 20:34:21 -080086 * platform driver if so configured and return an error code if it fails
87 */
88
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -070089static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -080090{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -070091 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -070092 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -080093}
94
95/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -070096 * platform_leave - prepare the machine for switching to the normal mode
97 * of operation using the platform driver (called with interrupts disabled)
98 */
99
100static void platform_leave(int platform_mode)
101{
102 if (platform_mode && hibernation_ops)
103 hibernation_ops->leave();
104}
105
106/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700107 * platform_finish - switch the machine to the normal mode of operation
108 * using the platform driver (must be called after platform_prepare())
109 */
110
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700111static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700112{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700113 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700114 hibernation_ops->finish();
115}
116
117/**
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700118 * platform_pre_restore - prepare the platform for the restoration from a
119 * hibernation image. If the restore fails after this function has been
120 * called, platform_restore_cleanup() must be called.
121 */
122
123static int platform_pre_restore(int platform_mode)
124{
125 return (platform_mode && hibernation_ops) ?
126 hibernation_ops->pre_restore() : 0;
127}
128
129/**
130 * platform_restore_cleanup - switch the platform to the normal mode of
131 * operation after a failing restore. If platform_pre_restore() has been
132 * called before the failing restore, this function must be called too,
133 * regardless of the result of platform_pre_restore().
134 */
135
136static void platform_restore_cleanup(int platform_mode)
137{
138 if (platform_mode && hibernation_ops)
139 hibernation_ops->restore_cleanup();
140}
141
142/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700143 * create_image - freeze devices that need to be frozen with interrupts
144 * off, create the hibernation image and thaw those devices. Control
145 * reappears in this routine after a restore.
146 */
147
148int create_image(int platform_mode)
149{
150 int error;
151
152 error = arch_prepare_suspend();
153 if (error)
154 return error;
155
156 local_irq_disable();
157 /* At this point, device_suspend() has been called, but *not*
158 * device_power_down(). We *must* call device_power_down() now.
159 * Otherwise, drivers for some devices (e.g. interrupt controllers)
160 * become desynchronized with the actual state of the hardware
161 * at resume time, and evil weirdness ensues.
162 */
163 error = device_power_down(PMSG_FREEZE);
164 if (error) {
165 printk(KERN_ERR "Some devices failed to power down, "
166 KERN_ERR "aborting suspend\n");
167 goto Enable_irqs;
168 }
169
170 save_processor_state();
171 error = swsusp_arch_suspend();
172 if (error)
173 printk(KERN_ERR "Error %d while creating the image\n", error);
174 /* Restore control flow magically appears here */
175 restore_processor_state();
176 if (!in_suspend)
177 platform_leave(platform_mode);
178 /* NOTE: device_power_up() is just a resume() for devices
179 * that suspended with irqs off ... no overall powerup.
180 */
181 device_power_up();
182 Enable_irqs:
183 local_irq_enable();
184 return error;
185}
186
187/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700188 * hibernation_snapshot - quiesce devices and create the hibernation
189 * snapshot image.
190 * @platform_mode - if set, use the platform driver, if available, to
191 * prepare the platform frimware for the power transition.
192 *
193 * Must be called with pm_mutex held
194 */
195
196int hibernation_snapshot(int platform_mode)
197{
198 int error;
199
200 /* Free memory before shutting down devices. */
201 error = swsusp_shrink_memory();
202 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700203 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700204
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700205 error = platform_start(platform_mode);
206 if (error)
207 return error;
208
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700209 suspend_console();
210 error = device_suspend(PMSG_FREEZE);
211 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700212 goto Resume_console;
213
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700214 error = platform_pre_snapshot(platform_mode);
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700215 if (error)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700216 goto Resume_devices;
217
218 error = disable_nonboot_cpus();
219 if (!error) {
220 if (hibernation_mode != HIBERNATION_TEST) {
221 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700222 error = create_image(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700223 /* Control returns here after successful restore */
224 } else {
225 printk("swsusp debug: Waiting for 5 seconds.\n");
226 mdelay(5000);
227 }
228 }
229 enable_nonboot_cpus();
230 Resume_devices:
231 platform_finish(platform_mode);
232 device_resume();
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700233 Resume_console:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700234 resume_console();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700235 return error;
236}
237
238/**
239 * hibernation_restore - quiesce devices and restore the hibernation
240 * snapshot image. If successful, control returns in hibernation_snaphot()
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700241 * @platform_mode - if set, use the platform driver, if available, to
242 * prepare the platform frimware for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700243 *
244 * Must be called with pm_mutex held
245 */
246
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700247int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700248{
249 int error;
250
251 pm_prepare_console();
252 suspend_console();
253 error = device_suspend(PMSG_PRETHAW);
254 if (error)
255 goto Finish;
256
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700257 error = platform_pre_restore(platform_mode);
258 if (!error) {
259 error = disable_nonboot_cpus();
260 if (!error)
261 error = swsusp_resume();
262 enable_nonboot_cpus();
263 }
264 platform_restore_cleanup(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700265 device_resume();
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700266 Finish:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700267 resume_console();
268 pm_restore_console();
269 return error;
270}
271
272/**
273 * hibernation_platform_enter - enter the hibernation state using the
274 * platform driver (if available)
275 */
276
277int hibernation_platform_enter(void)
278{
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700279 int error;
280
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700281 if (!hibernation_ops)
282 return -ENOSYS;
283
284 /*
285 * We have cancelled the power transition by running
286 * hibernation_ops->finish() before saving the image, so we should let
287 * the firmware know that we're going to enter the sleep state after all
288 */
289 error = hibernation_ops->start();
290 if (error)
291 return error;
292
293 suspend_console();
294 error = device_suspend(PMSG_SUSPEND);
295 if (error)
296 goto Resume_console;
297
298 error = hibernation_ops->prepare();
299 if (error)
300 goto Resume_devices;
301
302 error = disable_nonboot_cpus();
303 if (error)
304 goto Finish;
305
306 local_irq_disable();
307 error = device_power_down(PMSG_SUSPEND);
308 if (!error) {
309 hibernation_ops->enter();
310 /* We should never get here */
311 while (1);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700312 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700313 local_irq_enable();
314
315 /*
316 * We don't need to reenable the nonboot CPUs or resume consoles, since
317 * the system is going to be halted anyway.
318 */
319 Finish:
320 hibernation_ops->finish();
321 Resume_devices:
322 device_resume();
323 Resume_console:
324 resume_console();
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700325 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700326}
327
328/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700329 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 *
Johannes Bergfe0c9352007-04-30 15:09:51 -0700331 * Use the platform driver, if configured so; otherwise try
332 * to power off or reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 */
334
Johannes Bergfe0c9352007-04-30 15:09:51 -0700335static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700337 switch (hibernation_mode) {
338 case HIBERNATION_TEST:
339 case HIBERNATION_TESTPROC:
Johannes Bergfe0c9352007-04-30 15:09:51 -0700340 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700341 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600342 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700344 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700345 hibernation_platform_enter();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700346 case HIBERNATION_SHUTDOWN:
347 kernel_power_off();
348 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600350 kernel_halt();
Johannes Bergfe0c9352007-04-30 15:09:51 -0700351 /*
352 * Valid image is on the disk, if we continue we risk serious data
353 * corruption after resume.
354 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 printk(KERN_CRIT "Please power me down manually\n");
356 while(1);
357}
358
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800359static void unprepare_processes(void)
360{
361 thaw_processes();
362 pm_restore_console();
363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365static int prepare_processes(void)
366{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800367 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 pm_prepare_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (freeze_processes()) {
371 error = -EBUSY;
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800372 unprepare_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700374 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700378 * hibernate - The granpappy of the built-in hibernation management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 */
380
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700381int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 int error;
384
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700385 mutex_lock(&pm_mutex);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700386 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700387 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
388 error = -EBUSY;
389 goto Unlock;
390 }
391
392 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
393 if (error)
394 goto Exit;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700395
396 /* Allocate memory management structures */
397 error = create_basic_memory_bitmaps();
398 if (error)
399 goto Exit;
400
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700401 printk("Syncing filesystems ... ");
402 sys_sync();
403 printk("done.\n");
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700406 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700407 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700409 if (hibernation_mode == HIBERNATION_TESTPROC) {
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800410 printk("swsusp debug: Waiting for 5 seconds.\n");
411 mdelay(5000);
412 goto Thaw;
413 }
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700414 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
415 if (in_suspend && !error) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700416 unsigned int flags = 0;
417
418 if (hibernation_mode == HIBERNATION_PLATFORM)
419 flags |= SF_PLATFORM_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700421 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700422 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (!error)
Johannes Bergfe0c9352007-04-30 15:09:51 -0700424 power_down();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800425 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700427 swsusp_free();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800428 }
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800429 Thaw:
Pavel Machek99dc7d62005-09-03 15:57:05 -0700430 unprepare_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700431 Finish:
432 free_basic_memory_bitmaps();
433 Exit:
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700434 pm_notifier_call_chain(PM_POST_HIBERNATION);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700435 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700436 Unlock:
437 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return error;
439}
440
441
442/**
443 * software_resume - Resume from a saved image.
444 *
445 * Called as a late_initcall (so all devices are discovered and
446 * initialized), we call swsusp to see if we have a saved image or not.
447 * If so, we quiesce devices, the restore the saved image. We will
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700448 * return above (in hibernate() ) if everything goes well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * Otherwise, we fail gracefully and return to the normally
450 * scheduled program.
451 *
452 */
453
454static int software_resume(void)
455{
456 int error;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700457 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Johannes Berg60a0d232007-11-14 17:00:16 -0800459 /*
460 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
461 * is configured into the kernel. Since the regular hibernate
462 * trigger path is via sysfs which takes a buffer mutex before
463 * calling hibernate functions (which take pm_mutex) this can
464 * cause lockdep to complain about a possible ABBA deadlock
465 * which cannot happen since we're in the boot code here and
466 * sysfs can't be invoked yet. Therefore, we use a subclass
467 * here to avoid lockdep complaining.
468 */
469 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
Pavel Machek3efa1472005-07-07 17:56:43 -0700470 if (!swsusp_resume_device) {
Shaohua Lidd5d6662005-09-03 15:57:04 -0700471 if (!strlen(resume_file)) {
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800472 mutex_unlock(&pm_mutex);
Pavel Machek3efa1472005-07-07 17:56:43 -0700473 return -ENOENT;
Shaohua Lidd5d6662005-09-03 15:57:04 -0700474 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700475 swsusp_resume_device = name_to_dev_t(resume_file);
476 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
477 } else {
478 pr_debug("swsusp: Resume From Partition %d:%d\n",
479 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
480 }
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (noresume) {
483 /**
484 * FIXME: If noresume is specified, we need to find the partition
485 * and reset it back to normal swap space.
486 */
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800487 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return 0;
489 }
490
491 pr_debug("PM: Checking swsusp image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800492 error = swsusp_check();
493 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700494 goto Unlock;
495
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700496 /* The snapshot device should not be opened while we're running */
497 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
498 error = -EBUSY;
499 goto Unlock;
500 }
501
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700502 error = create_basic_memory_bitmaps();
503 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700504 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 pr_debug("PM: Preparing processes for restore.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800507 error = prepare_processes();
508 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 swsusp_close();
Li Shaohua5a72e042005-06-25 14:55:06 -0700510 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512
513 pr_debug("PM: Reading swsusp image.\n");
514
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700515 error = swsusp_read(&flags);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800516 if (!error)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700517 hibernation_restore(flags & SF_PLATFORM_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800519 printk(KERN_ERR "PM: Restore failed, recovering.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700520 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 unprepare_processes();
522 Done:
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700523 free_basic_memory_bitmaps();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700524 Finish:
525 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700526 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700527 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800528 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 pr_debug("PM: Resume from disk failed.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700530 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533late_initcall(software_resume);
534
535
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700536static const char * const hibernation_modes[] = {
537 [HIBERNATION_PLATFORM] = "platform",
538 [HIBERNATION_SHUTDOWN] = "shutdown",
539 [HIBERNATION_REBOOT] = "reboot",
540 [HIBERNATION_TEST] = "test",
541 [HIBERNATION_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542};
543
544/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700545 * disk - Control hibernation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700547 * Suspend-to-disk can be handled in several ways. We have a few options
548 * for putting the system to sleep - using the platform driver (e.g. ACPI
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700549 * or other hibernation_ops), powering off the system or rebooting the
550 * system (for testing) as well as the two test modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700552 * The system can support 'platform', and that is known a priori (and
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700553 * encoded by the presence of hibernation_ops). However, the user may
554 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
555 * test modes, 'test' or 'testproc'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 *
557 * show() will display what the mode is currently set to.
558 * store() will accept one of
559 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 * 'platform'
561 * 'shutdown'
562 * 'reboot'
Johannes Berg11d77d02007-04-30 15:09:53 -0700563 * 'test'
564 * 'testproc'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700566 * It will only change to 'platform' if the system
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700567 * supports it (as determined by having hibernation_ops).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
569
Kay Sievers386f2752007-11-02 13:47:53 +0100570static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
571 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700573 int i;
574 char *start = buf;
575
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700576 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
577 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700578 continue;
579 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700580 case HIBERNATION_SHUTDOWN:
581 case HIBERNATION_REBOOT:
582 case HIBERNATION_TEST:
583 case HIBERNATION_TESTPROC:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700584 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700585 case HIBERNATION_PLATFORM:
586 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700587 break;
588 /* not a valid mode, continue with loop */
589 continue;
590 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700591 if (i == hibernation_mode)
592 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700593 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700594 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700595 }
596 buf += sprintf(buf, "\n");
597 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
600
Kay Sievers386f2752007-11-02 13:47:53 +0100601static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
602 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 int error = 0;
605 int i;
606 int len;
607 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700608 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 p = memchr(buf, '\n', n);
611 len = p ? p - buf : n;
612
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800613 mutex_lock(&pm_mutex);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700614 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700615 if (len == strlen(hibernation_modes[i])
616 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 mode = i;
618 break;
619 }
620 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700621 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c9352007-04-30 15:09:51 -0700622 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700623 case HIBERNATION_SHUTDOWN:
624 case HIBERNATION_REBOOT:
625 case HIBERNATION_TEST:
626 case HIBERNATION_TESTPROC:
627 hibernation_mode = mode;
Johannes Bergfe0c9352007-04-30 15:09:51 -0700628 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700629 case HIBERNATION_PLATFORM:
630 if (hibernation_ops)
631 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 else
633 error = -EINVAL;
634 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700635 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 error = -EINVAL;
637
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700638 if (!error)
639 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
640 hibernation_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800641 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return error ? error : n;
643}
644
645power_attr(disk);
646
Kay Sievers386f2752007-11-02 13:47:53 +0100647static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
648 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
651 MINOR(swsusp_resume_device));
652}
653
Kay Sievers386f2752007-11-02 13:47:53 +0100654static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
655 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800659 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Andrew Mortona5762192006-01-06 00:09:50 -0800661 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
662 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Andrew Mortona5762192006-01-06 00:09:50 -0800664 res = MKDEV(maj,min);
665 if (maj != MAJOR(res) || min != MINOR(res))
666 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800668 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800669 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800670 mutex_unlock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800671 printk("Attempting manual resume\n");
672 noresume = 0;
673 software_resume();
674 ret = n;
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800675 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800676 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679power_attr(resume);
680
Kay Sievers386f2752007-11-02 13:47:53 +0100681static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
682 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800683{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800684 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800685}
686
Kay Sievers386f2752007-11-02 13:47:53 +0100687static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
688 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800689{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800690 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800691
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800692 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800693 image_size = size;
694 return n;
695 }
696
697 return -EINVAL;
698}
699
700power_attr(image_size);
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702static struct attribute * g[] = {
703 &disk_attr.attr,
704 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800705 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 NULL,
707};
708
709
710static struct attribute_group attr_group = {
711 .attrs = g,
712};
713
714
715static int __init pm_disk_init(void)
716{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800717 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
720core_initcall(pm_disk_init);
721
722
723static int __init resume_setup(char *str)
724{
725 if (noresume)
726 return 1;
727
728 strncpy( resume_file, str, 255 );
729 return 1;
730}
731
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800732static int __init resume_offset_setup(char *str)
733{
734 unsigned long long offset;
735
736 if (noresume)
737 return 1;
738
739 if (sscanf(str, "%llu", &offset) == 1)
740 swsusp_resume_block = offset;
741
742 return 1;
743}
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745static int __init noresume_setup(char *str)
746{
747 noresume = 1;
748 return 1;
749}
750
751__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800752__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753__setup("resume=", resume_setup);