swsusp: fix hibernation code ordering
[deliverable/linux.git] / kernel / power / disk.c
CommitLineData
1da177e4
LT
1/*
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>
d53d9f16 19#include <linux/mount.h>
88d10bba 20#include <linux/pm.h>
97c7801c 21#include <linux/console.h>
e3920fb4 22#include <linux/cpu.h>
7dfb7103 23#include <linux/freezer.h>
d53d9f16 24
1da177e4
LT
25#include "power.h"
26
27
1da177e4
LT
28static int noresume = 0;
29char resume_file[256] = CONFIG_PM_STD_PARTITION;
30dev_t swsusp_resume_device;
9a154d9d 31sector_t swsusp_resume_block;
1da177e4 32
a3d25c27
RW
33enum {
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
7777fab9 48static struct hibernation_ops *hibernation_ops;
a3d25c27
RW
49
50/**
51 * hibernation_set_ops - set the global hibernate operations
52 * @ops: the hibernation operations to use in subsequent hibernation transitions
53 */
54
55void hibernation_set_ops(struct hibernation_ops *ops)
56{
a634cc10
RW
57 if (ops && !(ops->prepare && ops->enter && ops->finish
58 && ops->pre_restore && ops->restore_cleanup)) {
a3d25c27
RW
59 WARN_ON(1);
60 return;
61 }
62 mutex_lock(&pm_mutex);
63 hibernation_ops = ops;
64 if (ops)
65 hibernation_mode = HIBERNATION_PLATFORM;
66 else if (hibernation_mode == HIBERNATION_PLATFORM)
67 hibernation_mode = HIBERNATION_SHUTDOWN;
68
69 mutex_unlock(&pm_mutex);
70}
71
72
8a05aac2
SS
73/**
74 * platform_prepare - prepare the machine for hibernation using the
75 * platform driver if so configured and return an error code if it fails
76 */
77
7777fab9 78static int platform_prepare(int platform_mode)
8a05aac2 79{
7777fab9 80 return (platform_mode && hibernation_ops) ?
a3d25c27
RW
81 hibernation_ops->prepare() : 0;
82}
8a05aac2 83
a3d25c27
RW
84/**
85 * platform_finish - switch the machine to the normal mode of operation
86 * using the platform driver (must be called after platform_prepare())
87 */
88
7777fab9 89static void platform_finish(int platform_mode)
a3d25c27 90{
7777fab9 91 if (platform_mode && hibernation_ops)
a3d25c27 92 hibernation_ops->finish();
8a05aac2
SS
93}
94
a634cc10
RW
95/**
96 * platform_pre_restore - prepare the platform for the restoration from a
97 * hibernation image. If the restore fails after this function has been
98 * called, platform_restore_cleanup() must be called.
99 */
100
101static int platform_pre_restore(int platform_mode)
102{
103 return (platform_mode && hibernation_ops) ?
104 hibernation_ops->pre_restore() : 0;
105}
106
107/**
108 * platform_restore_cleanup - switch the platform to the normal mode of
109 * operation after a failing restore. If platform_pre_restore() has been
110 * called before the failing restore, this function must be called too,
111 * regardless of the result of platform_pre_restore().
112 */
113
114static void platform_restore_cleanup(int platform_mode)
115{
116 if (platform_mode && hibernation_ops)
117 hibernation_ops->restore_cleanup();
118}
119
7777fab9
RW
120/**
121 * hibernation_snapshot - quiesce devices and create the hibernation
122 * snapshot image.
123 * @platform_mode - if set, use the platform driver, if available, to
124 * prepare the platform frimware for the power transition.
125 *
126 * Must be called with pm_mutex held
127 */
128
129int hibernation_snapshot(int platform_mode)
130{
131 int error;
132
133 /* Free memory before shutting down devices. */
134 error = swsusp_shrink_memory();
135 if (error)
10a1803d 136 return error;
7777fab9
RW
137
138 suspend_console();
139 error = device_suspend(PMSG_FREEZE);
10a1803d
RW
140 if (error)
141 goto Resume_console;
142
143 error = platform_prepare(platform_mode);
7777fab9
RW
144 if (error)
145 goto Resume_devices;
146
147 error = disable_nonboot_cpus();
148 if (!error) {
149 if (hibernation_mode != HIBERNATION_TEST) {
150 in_suspend = 1;
151 error = swsusp_suspend();
152 /* Control returns here after successful restore */
153 } else {
154 printk("swsusp debug: Waiting for 5 seconds.\n");
155 mdelay(5000);
156 }
157 }
158 enable_nonboot_cpus();
159 Resume_devices:
160 platform_finish(platform_mode);
161 device_resume();
10a1803d 162 Resume_console:
7777fab9 163 resume_console();
7777fab9
RW
164 return error;
165}
166
167/**
168 * hibernation_restore - quiesce devices and restore the hibernation
169 * snapshot image. If successful, control returns in hibernation_snaphot()
a634cc10
RW
170 * @platform_mode - if set, use the platform driver, if available, to
171 * prepare the platform frimware for the transition.
7777fab9
RW
172 *
173 * Must be called with pm_mutex held
174 */
175
a634cc10 176int hibernation_restore(int platform_mode)
7777fab9
RW
177{
178 int error;
179
180 pm_prepare_console();
181 suspend_console();
182 error = device_suspend(PMSG_PRETHAW);
183 if (error)
184 goto Finish;
185
a634cc10
RW
186 error = platform_pre_restore(platform_mode);
187 if (!error) {
188 error = disable_nonboot_cpus();
189 if (!error)
190 error = swsusp_resume();
191 enable_nonboot_cpus();
192 }
193 platform_restore_cleanup(platform_mode);
7777fab9 194 device_resume();
10a1803d 195 Finish:
7777fab9
RW
196 resume_console();
197 pm_restore_console();
198 return error;
199}
200
201/**
202 * hibernation_platform_enter - enter the hibernation state using the
203 * platform driver (if available)
204 */
205
206int hibernation_platform_enter(void)
207{
208 if (hibernation_ops) {
209 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
210 return hibernation_ops->enter();
211 } else {
212 return -ENOSYS;
213 }
214}
215
1da177e4 216/**
a3d25c27 217 * power_down - Shut the machine down for hibernation.
1da177e4 218 *
fe0c935a
JB
219 * Use the platform driver, if configured so; otherwise try
220 * to power off or reboot.
1da177e4
LT
221 */
222
fe0c935a 223static void power_down(void)
1da177e4 224{
a3d25c27
RW
225 switch (hibernation_mode) {
226 case HIBERNATION_TEST:
227 case HIBERNATION_TESTPROC:
fe0c935a 228 break;
a3d25c27 229 case HIBERNATION_SHUTDOWN:
fdde86ac 230 kernel_power_off();
1da177e4 231 break;
a3d25c27 232 case HIBERNATION_REBOOT:
fdde86ac 233 kernel_restart(NULL);
1da177e4 234 break;
a3d25c27 235 case HIBERNATION_PLATFORM:
7777fab9 236 hibernation_platform_enter();
1da177e4 237 }
fdde86ac 238 kernel_halt();
fe0c935a
JB
239 /*
240 * Valid image is on the disk, if we continue we risk serious data
241 * corruption after resume.
242 */
1da177e4
LT
243 printk(KERN_CRIT "Please power me down manually\n");
244 while(1);
245}
246
ed746e3b
RW
247static void unprepare_processes(void)
248{
249 thaw_processes();
250 pm_restore_console();
251}
252
1da177e4
LT
253static int prepare_processes(void)
254{
b918f6e6 255 int error = 0;
1da177e4
LT
256
257 pm_prepare_console();
1da177e4
LT
258 if (freeze_processes()) {
259 error = -EBUSY;
ed746e3b 260 unprepare_processes();
1da177e4 261 }
5a72e04d 262 return error;
1da177e4
LT
263}
264
1da177e4 265/**
a3d25c27 266 * hibernate - The granpappy of the built-in hibernation management
1da177e4
LT
267 */
268
a3d25c27 269int hibernate(void)
1da177e4
LT
270{
271 int error;
272
0709db60
RW
273 /* The snapshot device should not be opened while we're running */
274 if (!atomic_add_unless(&snapshot_device_available, -1, 0))
275 return -EBUSY;
276
277 /* Allocate memory management structures */
278 error = create_basic_memory_bitmaps();
279 if (error)
280 goto Exit;
281
1da177e4 282 error = prepare_processes();
5a72e04d 283 if (error)
0709db60 284 goto Finish;
1da177e4 285
a3d25c27
RW
286 mutex_lock(&pm_mutex);
287 if (hibernation_mode == HIBERNATION_TESTPROC) {
ed746e3b
RW
288 printk("swsusp debug: Waiting for 5 seconds.\n");
289 mdelay(5000);
290 goto Thaw;
291 }
7777fab9
RW
292 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
293 if (in_suspend && !error) {
a634cc10
RW
294 unsigned int flags = 0;
295
296 if (hibernation_mode == HIBERNATION_PLATFORM)
297 flags |= SF_PLATFORM_MODE;
1da177e4 298 pr_debug("PM: writing image.\n");
a634cc10 299 error = swsusp_write(flags);
7777fab9 300 swsusp_free();
1da177e4 301 if (!error)
fe0c935a 302 power_down();
b918f6e6 303 } else {
1da177e4 304 pr_debug("PM: Image restored successfully.\n");
7777fab9 305 swsusp_free();
b918f6e6 306 }
b918f6e6 307 Thaw:
a3d25c27 308 mutex_unlock(&pm_mutex);
99dc7d63 309 unprepare_processes();
0709db60
RW
310 Finish:
311 free_basic_memory_bitmaps();
312 Exit:
313 atomic_inc(&snapshot_device_available);
1da177e4
LT
314 return error;
315}
316
317
318/**
319 * software_resume - Resume from a saved image.
320 *
321 * Called as a late_initcall (so all devices are discovered and
322 * initialized), we call swsusp to see if we have a saved image or not.
323 * If so, we quiesce devices, the restore the saved image. We will
a3d25c27 324 * return above (in hibernate() ) if everything goes well.
1da177e4
LT
325 * Otherwise, we fail gracefully and return to the normally
326 * scheduled program.
327 *
328 */
329
330static int software_resume(void)
331{
332 int error;
a634cc10 333 unsigned int flags;
1da177e4 334
a6d70980 335 mutex_lock(&pm_mutex);
3efa147a 336 if (!swsusp_resume_device) {
dd5d666b 337 if (!strlen(resume_file)) {
a6d70980 338 mutex_unlock(&pm_mutex);
3efa147a 339 return -ENOENT;
dd5d666b 340 }
3efa147a
PM
341 swsusp_resume_device = name_to_dev_t(resume_file);
342 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
343 } else {
344 pr_debug("swsusp: Resume From Partition %d:%d\n",
345 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
346 }
347
1da177e4
LT
348 if (noresume) {
349 /**
350 * FIXME: If noresume is specified, we need to find the partition
351 * and reset it back to normal swap space.
352 */
a6d70980 353 mutex_unlock(&pm_mutex);
1da177e4
LT
354 return 0;
355 }
356
357 pr_debug("PM: Checking swsusp image.\n");
ed746e3b
RW
358 error = swsusp_check();
359 if (error)
74dfd666 360 goto Unlock;
1da177e4 361
0709db60
RW
362 /* The snapshot device should not be opened while we're running */
363 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
364 error = -EBUSY;
365 goto Unlock;
366 }
367
74dfd666
RW
368 error = create_basic_memory_bitmaps();
369 if (error)
0709db60 370 goto Finish;
1da177e4 371
74dfd666 372 pr_debug("PM: Preparing processes for restore.\n");
ed746e3b
RW
373 error = prepare_processes();
374 if (error) {
1da177e4 375 swsusp_close();
5a72e04d 376 goto Done;
1da177e4
LT
377 }
378
379 pr_debug("PM: Reading swsusp image.\n");
380
a634cc10 381 error = swsusp_read(&flags);
ed746e3b 382 if (!error)
a634cc10 383 hibernation_restore(flags & SF_PLATFORM_MODE);
1da177e4 384
ed746e3b 385 printk(KERN_ERR "PM: Restore failed, recovering.\n");
7777fab9 386 swsusp_free();
1da177e4
LT
387 unprepare_processes();
388 Done:
74dfd666 389 free_basic_memory_bitmaps();
0709db60
RW
390 Finish:
391 atomic_inc(&snapshot_device_available);
dd5d666b 392 /* For success case, the suspend path will release the lock */
74dfd666 393 Unlock:
a6d70980 394 mutex_unlock(&pm_mutex);
1da177e4 395 pr_debug("PM: Resume from disk failed.\n");
7777fab9 396 return error;
1da177e4
LT
397}
398
399late_initcall(software_resume);
400
401
a3d25c27
RW
402static const char * const hibernation_modes[] = {
403 [HIBERNATION_PLATFORM] = "platform",
404 [HIBERNATION_SHUTDOWN] = "shutdown",
405 [HIBERNATION_REBOOT] = "reboot",
406 [HIBERNATION_TEST] = "test",
407 [HIBERNATION_TESTPROC] = "testproc",
1da177e4
LT
408};
409
410/**
a3d25c27 411 * disk - Control hibernation mode
1da177e4 412 *
11d77d0c
JB
413 * Suspend-to-disk can be handled in several ways. We have a few options
414 * for putting the system to sleep - using the platform driver (e.g. ACPI
a3d25c27
RW
415 * or other hibernation_ops), powering off the system or rebooting the
416 * system (for testing) as well as the two test modes.
1da177e4 417 *
11d77d0c 418 * The system can support 'platform', and that is known a priori (and
a3d25c27
RW
419 * encoded by the presence of hibernation_ops). However, the user may
420 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
421 * test modes, 'test' or 'testproc'.
1da177e4
LT
422 *
423 * show() will display what the mode is currently set to.
424 * store() will accept one of
425 *
1da177e4
LT
426 * 'platform'
427 * 'shutdown'
428 * 'reboot'
11d77d0c
JB
429 * 'test'
430 * 'testproc'
1da177e4 431 *
11d77d0c 432 * It will only change to 'platform' if the system
a3d25c27 433 * supports it (as determined by having hibernation_ops).
1da177e4
LT
434 */
435
823bccfc 436static ssize_t disk_show(struct kset *kset, char *buf)
1da177e4 437{
f0ced9b2
JB
438 int i;
439 char *start = buf;
440
a3d25c27
RW
441 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
442 if (!hibernation_modes[i])
f0ced9b2
JB
443 continue;
444 switch (i) {
a3d25c27
RW
445 case HIBERNATION_SHUTDOWN:
446 case HIBERNATION_REBOOT:
447 case HIBERNATION_TEST:
448 case HIBERNATION_TESTPROC:
f0ced9b2 449 break;
a3d25c27
RW
450 case HIBERNATION_PLATFORM:
451 if (hibernation_ops)
f0ced9b2
JB
452 break;
453 /* not a valid mode, continue with loop */
454 continue;
455 }
a3d25c27
RW
456 if (i == hibernation_mode)
457 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
f0ced9b2 458 else
a3d25c27 459 buf += sprintf(buf, "%s ", hibernation_modes[i]);
f0ced9b2
JB
460 }
461 buf += sprintf(buf, "\n");
462 return buf-start;
1da177e4
LT
463}
464
465
823bccfc 466static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
1da177e4
LT
467{
468 int error = 0;
469 int i;
470 int len;
471 char *p;
a3d25c27 472 int mode = HIBERNATION_INVALID;
1da177e4
LT
473
474 p = memchr(buf, '\n', n);
475 len = p ? p - buf : n;
476
a6d70980 477 mutex_lock(&pm_mutex);
a3d25c27 478 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
8d98a690
RW
479 if (len == strlen(hibernation_modes[i])
480 && !strncmp(buf, hibernation_modes[i], len)) {
1da177e4
LT
481 mode = i;
482 break;
483 }
484 }
a3d25c27 485 if (mode != HIBERNATION_INVALID) {
fe0c935a 486 switch (mode) {
a3d25c27
RW
487 case HIBERNATION_SHUTDOWN:
488 case HIBERNATION_REBOOT:
489 case HIBERNATION_TEST:
490 case HIBERNATION_TESTPROC:
491 hibernation_mode = mode;
fe0c935a 492 break;
a3d25c27
RW
493 case HIBERNATION_PLATFORM:
494 if (hibernation_ops)
495 hibernation_mode = mode;
1da177e4
LT
496 else
497 error = -EINVAL;
498 }
a3d25c27 499 } else
1da177e4
LT
500 error = -EINVAL;
501
a3d25c27
RW
502 if (!error)
503 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
504 hibernation_modes[mode]);
a6d70980 505 mutex_unlock(&pm_mutex);
1da177e4
LT
506 return error ? error : n;
507}
508
509power_attr(disk);
510
823bccfc 511static ssize_t resume_show(struct kset *kset, char *buf)
1da177e4
LT
512{
513 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
514 MINOR(swsusp_resume_device));
515}
516
823bccfc 517static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
1da177e4 518{
1da177e4 519 unsigned int maj, min;
1da177e4 520 dev_t res;
a576219a 521 int ret = -EINVAL;
1da177e4 522
a576219a
AM
523 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
524 goto out;
1da177e4 525
a576219a
AM
526 res = MKDEV(maj,min);
527 if (maj != MAJOR(res) || min != MINOR(res))
528 goto out;
1da177e4 529
a6d70980 530 mutex_lock(&pm_mutex);
a576219a 531 swsusp_resume_device = res;
a6d70980 532 mutex_unlock(&pm_mutex);
a576219a
AM
533 printk("Attempting manual resume\n");
534 noresume = 0;
535 software_resume();
536 ret = n;
59a49335 537 out:
a576219a 538 return ret;
1da177e4
LT
539}
540
541power_attr(resume);
542
823bccfc 543static ssize_t image_size_show(struct kset *kset, char *buf)
ca0aec0f 544{
853609b6 545 return sprintf(buf, "%lu\n", image_size);
ca0aec0f
RW
546}
547
823bccfc 548static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
ca0aec0f 549{
853609b6 550 unsigned long size;
ca0aec0f 551
853609b6 552 if (sscanf(buf, "%lu", &size) == 1) {
ca0aec0f
RW
553 image_size = size;
554 return n;
555 }
556
557 return -EINVAL;
558}
559
560power_attr(image_size);
561
1da177e4
LT
562static struct attribute * g[] = {
563 &disk_attr.attr,
564 &resume_attr.attr,
ca0aec0f 565 &image_size_attr.attr,
1da177e4
LT
566 NULL,
567};
568
569
570static struct attribute_group attr_group = {
571 .attrs = g,
572};
573
574
575static int __init pm_disk_init(void)
576{
823bccfc 577 return sysfs_create_group(&power_subsys.kobj, &attr_group);
1da177e4
LT
578}
579
580core_initcall(pm_disk_init);
581
582
583static int __init resume_setup(char *str)
584{
585 if (noresume)
586 return 1;
587
588 strncpy( resume_file, str, 255 );
589 return 1;
590}
591
9a154d9d
RW
592static int __init resume_offset_setup(char *str)
593{
594 unsigned long long offset;
595
596 if (noresume)
597 return 1;
598
599 if (sscanf(str, "%llu", &offset) == 1)
600 swsusp_resume_block = offset;
601
602 return 1;
603}
604
1da177e4
LT
605static int __init noresume_setup(char *str)
606{
607 noresume = 1;
608 return 1;
609}
610
611__setup("noresume", noresume_setup);
9a154d9d 612__setup("resume_offset=", resume_offset_setup);
1da177e4 613__setup("resume=", resume_setup);
This page took 0.33515 seconds and 5 git commands to generate.