]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/power/disk.c
PM: Make suspend_ops static
[net-next-2.6.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{
b1457bcc
RW
208 int error;
209
7777fab9
RW
210 if (hibernation_ops) {
211 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
b1457bcc
RW
212 /*
213 * We have cancelled the power transition by running
214 * hibernation_ops->finish() before saving the image, so we
215 * should let the firmware know that we're going to enter the
216 * sleep state after all
217 */
218 error = hibernation_ops->prepare();
58b3b71d 219 sysdev_shutdown();
b1457bcc
RW
220 if (!error)
221 error = hibernation_ops->enter();
7777fab9 222 } else {
b1457bcc 223 error = -ENOSYS;
7777fab9 224 }
b1457bcc 225 return error;
7777fab9
RW
226}
227
1da177e4 228/**
a3d25c27 229 * power_down - Shut the machine down for hibernation.
1da177e4 230 *
fe0c935a
JB
231 * Use the platform driver, if configured so; otherwise try
232 * to power off or reboot.
1da177e4
LT
233 */
234
fe0c935a 235static void power_down(void)
1da177e4 236{
a3d25c27
RW
237 switch (hibernation_mode) {
238 case HIBERNATION_TEST:
239 case HIBERNATION_TESTPROC:
fe0c935a 240 break;
a3d25c27 241 case HIBERNATION_SHUTDOWN:
fdde86ac 242 kernel_power_off();
1da177e4 243 break;
a3d25c27 244 case HIBERNATION_REBOOT:
fdde86ac 245 kernel_restart(NULL);
1da177e4 246 break;
a3d25c27 247 case HIBERNATION_PLATFORM:
7777fab9 248 hibernation_platform_enter();
1da177e4 249 }
fdde86ac 250 kernel_halt();
fe0c935a
JB
251 /*
252 * Valid image is on the disk, if we continue we risk serious data
253 * corruption after resume.
254 */
1da177e4
LT
255 printk(KERN_CRIT "Please power me down manually\n");
256 while(1);
257}
258
ed746e3b
RW
259static void unprepare_processes(void)
260{
261 thaw_processes();
262 pm_restore_console();
263}
264
1da177e4
LT
265static int prepare_processes(void)
266{
b918f6e6 267 int error = 0;
1da177e4
LT
268
269 pm_prepare_console();
1da177e4
LT
270 if (freeze_processes()) {
271 error = -EBUSY;
ed746e3b 272 unprepare_processes();
1da177e4 273 }
5a72e04d 274 return error;
1da177e4
LT
275}
276
1da177e4 277/**
a3d25c27 278 * hibernate - The granpappy of the built-in hibernation management
1da177e4
LT
279 */
280
a3d25c27 281int hibernate(void)
1da177e4
LT
282{
283 int error;
284
b10d9117 285 mutex_lock(&pm_mutex);
0709db60 286 /* The snapshot device should not be opened while we're running */
b10d9117
RW
287 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
288 error = -EBUSY;
289 goto Unlock;
290 }
291
292 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
293 if (error)
294 goto Exit;
0709db60
RW
295
296 /* Allocate memory management structures */
297 error = create_basic_memory_bitmaps();
298 if (error)
299 goto Exit;
300
1da177e4 301 error = prepare_processes();
5a72e04d 302 if (error)
0709db60 303 goto Finish;
1da177e4 304
a3d25c27 305 if (hibernation_mode == HIBERNATION_TESTPROC) {
ed746e3b
RW
306 printk("swsusp debug: Waiting for 5 seconds.\n");
307 mdelay(5000);
308 goto Thaw;
309 }
7777fab9
RW
310 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
311 if (in_suspend && !error) {
a634cc10
RW
312 unsigned int flags = 0;
313
314 if (hibernation_mode == HIBERNATION_PLATFORM)
315 flags |= SF_PLATFORM_MODE;
1da177e4 316 pr_debug("PM: writing image.\n");
a634cc10 317 error = swsusp_write(flags);
7777fab9 318 swsusp_free();
1da177e4 319 if (!error)
fe0c935a 320 power_down();
b918f6e6 321 } else {
1da177e4 322 pr_debug("PM: Image restored successfully.\n");
7777fab9 323 swsusp_free();
b918f6e6 324 }
b918f6e6 325 Thaw:
99dc7d63 326 unprepare_processes();
0709db60
RW
327 Finish:
328 free_basic_memory_bitmaps();
329 Exit:
b10d9117 330 pm_notifier_call_chain(PM_POST_HIBERNATION);
0709db60 331 atomic_inc(&snapshot_device_available);
b10d9117
RW
332 Unlock:
333 mutex_unlock(&pm_mutex);
1da177e4
LT
334 return error;
335}
336
337
338/**
339 * software_resume - Resume from a saved image.
340 *
341 * Called as a late_initcall (so all devices are discovered and
342 * initialized), we call swsusp to see if we have a saved image or not.
343 * If so, we quiesce devices, the restore the saved image. We will
a3d25c27 344 * return above (in hibernate() ) if everything goes well.
1da177e4
LT
345 * Otherwise, we fail gracefully and return to the normally
346 * scheduled program.
347 *
348 */
349
350static int software_resume(void)
351{
352 int error;
a634cc10 353 unsigned int flags;
1da177e4 354
a6d70980 355 mutex_lock(&pm_mutex);
3efa147a 356 if (!swsusp_resume_device) {
dd5d666b 357 if (!strlen(resume_file)) {
a6d70980 358 mutex_unlock(&pm_mutex);
3efa147a 359 return -ENOENT;
dd5d666b 360 }
3efa147a
PM
361 swsusp_resume_device = name_to_dev_t(resume_file);
362 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
363 } else {
364 pr_debug("swsusp: Resume From Partition %d:%d\n",
365 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
366 }
367
1da177e4
LT
368 if (noresume) {
369 /**
370 * FIXME: If noresume is specified, we need to find the partition
371 * and reset it back to normal swap space.
372 */
a6d70980 373 mutex_unlock(&pm_mutex);
1da177e4
LT
374 return 0;
375 }
376
377 pr_debug("PM: Checking swsusp image.\n");
ed746e3b
RW
378 error = swsusp_check();
379 if (error)
74dfd666 380 goto Unlock;
1da177e4 381
0709db60
RW
382 /* The snapshot device should not be opened while we're running */
383 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
384 error = -EBUSY;
385 goto Unlock;
386 }
387
74dfd666
RW
388 error = create_basic_memory_bitmaps();
389 if (error)
0709db60 390 goto Finish;
1da177e4 391
74dfd666 392 pr_debug("PM: Preparing processes for restore.\n");
ed746e3b
RW
393 error = prepare_processes();
394 if (error) {
1da177e4 395 swsusp_close();
5a72e04d 396 goto Done;
1da177e4
LT
397 }
398
399 pr_debug("PM: Reading swsusp image.\n");
400
a634cc10 401 error = swsusp_read(&flags);
ed746e3b 402 if (!error)
a634cc10 403 hibernation_restore(flags & SF_PLATFORM_MODE);
1da177e4 404
ed746e3b 405 printk(KERN_ERR "PM: Restore failed, recovering.\n");
7777fab9 406 swsusp_free();
1da177e4
LT
407 unprepare_processes();
408 Done:
74dfd666 409 free_basic_memory_bitmaps();
0709db60
RW
410 Finish:
411 atomic_inc(&snapshot_device_available);
dd5d666b 412 /* For success case, the suspend path will release the lock */
74dfd666 413 Unlock:
a6d70980 414 mutex_unlock(&pm_mutex);
1da177e4 415 pr_debug("PM: Resume from disk failed.\n");
7777fab9 416 return error;
1da177e4
LT
417}
418
419late_initcall(software_resume);
420
421
a3d25c27
RW
422static const char * const hibernation_modes[] = {
423 [HIBERNATION_PLATFORM] = "platform",
424 [HIBERNATION_SHUTDOWN] = "shutdown",
425 [HIBERNATION_REBOOT] = "reboot",
426 [HIBERNATION_TEST] = "test",
427 [HIBERNATION_TESTPROC] = "testproc",
1da177e4
LT
428};
429
430/**
a3d25c27 431 * disk - Control hibernation mode
1da177e4 432 *
11d77d0c
JB
433 * Suspend-to-disk can be handled in several ways. We have a few options
434 * for putting the system to sleep - using the platform driver (e.g. ACPI
a3d25c27
RW
435 * or other hibernation_ops), powering off the system or rebooting the
436 * system (for testing) as well as the two test modes.
1da177e4 437 *
11d77d0c 438 * The system can support 'platform', and that is known a priori (and
a3d25c27
RW
439 * encoded by the presence of hibernation_ops). However, the user may
440 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
441 * test modes, 'test' or 'testproc'.
1da177e4
LT
442 *
443 * show() will display what the mode is currently set to.
444 * store() will accept one of
445 *
1da177e4
LT
446 * 'platform'
447 * 'shutdown'
448 * 'reboot'
11d77d0c
JB
449 * 'test'
450 * 'testproc'
1da177e4 451 *
11d77d0c 452 * It will only change to 'platform' if the system
a3d25c27 453 * supports it (as determined by having hibernation_ops).
1da177e4
LT
454 */
455
823bccfc 456static ssize_t disk_show(struct kset *kset, char *buf)
1da177e4 457{
f0ced9b2
JB
458 int i;
459 char *start = buf;
460
a3d25c27
RW
461 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
462 if (!hibernation_modes[i])
f0ced9b2
JB
463 continue;
464 switch (i) {
a3d25c27
RW
465 case HIBERNATION_SHUTDOWN:
466 case HIBERNATION_REBOOT:
467 case HIBERNATION_TEST:
468 case HIBERNATION_TESTPROC:
f0ced9b2 469 break;
a3d25c27
RW
470 case HIBERNATION_PLATFORM:
471 if (hibernation_ops)
f0ced9b2
JB
472 break;
473 /* not a valid mode, continue with loop */
474 continue;
475 }
a3d25c27
RW
476 if (i == hibernation_mode)
477 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
f0ced9b2 478 else
a3d25c27 479 buf += sprintf(buf, "%s ", hibernation_modes[i]);
f0ced9b2
JB
480 }
481 buf += sprintf(buf, "\n");
482 return buf-start;
1da177e4
LT
483}
484
485
823bccfc 486static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
1da177e4
LT
487{
488 int error = 0;
489 int i;
490 int len;
491 char *p;
a3d25c27 492 int mode = HIBERNATION_INVALID;
1da177e4
LT
493
494 p = memchr(buf, '\n', n);
495 len = p ? p - buf : n;
496
a6d70980 497 mutex_lock(&pm_mutex);
a3d25c27 498 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
8d98a690
RW
499 if (len == strlen(hibernation_modes[i])
500 && !strncmp(buf, hibernation_modes[i], len)) {
1da177e4
LT
501 mode = i;
502 break;
503 }
504 }
a3d25c27 505 if (mode != HIBERNATION_INVALID) {
fe0c935a 506 switch (mode) {
a3d25c27
RW
507 case HIBERNATION_SHUTDOWN:
508 case HIBERNATION_REBOOT:
509 case HIBERNATION_TEST:
510 case HIBERNATION_TESTPROC:
511 hibernation_mode = mode;
fe0c935a 512 break;
a3d25c27
RW
513 case HIBERNATION_PLATFORM:
514 if (hibernation_ops)
515 hibernation_mode = mode;
1da177e4
LT
516 else
517 error = -EINVAL;
518 }
a3d25c27 519 } else
1da177e4
LT
520 error = -EINVAL;
521
a3d25c27
RW
522 if (!error)
523 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
524 hibernation_modes[mode]);
a6d70980 525 mutex_unlock(&pm_mutex);
1da177e4
LT
526 return error ? error : n;
527}
528
529power_attr(disk);
530
823bccfc 531static ssize_t resume_show(struct kset *kset, char *buf)
1da177e4
LT
532{
533 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
534 MINOR(swsusp_resume_device));
535}
536
823bccfc 537static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
1da177e4 538{
1da177e4 539 unsigned int maj, min;
1da177e4 540 dev_t res;
a576219a 541 int ret = -EINVAL;
1da177e4 542
a576219a
AM
543 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
544 goto out;
1da177e4 545
a576219a
AM
546 res = MKDEV(maj,min);
547 if (maj != MAJOR(res) || min != MINOR(res))
548 goto out;
1da177e4 549
a6d70980 550 mutex_lock(&pm_mutex);
a576219a 551 swsusp_resume_device = res;
a6d70980 552 mutex_unlock(&pm_mutex);
a576219a
AM
553 printk("Attempting manual resume\n");
554 noresume = 0;
555 software_resume();
556 ret = n;
59a49335 557 out:
a576219a 558 return ret;
1da177e4
LT
559}
560
561power_attr(resume);
562
823bccfc 563static ssize_t image_size_show(struct kset *kset, char *buf)
ca0aec0f 564{
853609b6 565 return sprintf(buf, "%lu\n", image_size);
ca0aec0f
RW
566}
567
823bccfc 568static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
ca0aec0f 569{
853609b6 570 unsigned long size;
ca0aec0f 571
853609b6 572 if (sscanf(buf, "%lu", &size) == 1) {
ca0aec0f
RW
573 image_size = size;
574 return n;
575 }
576
577 return -EINVAL;
578}
579
580power_attr(image_size);
581
1da177e4
LT
582static struct attribute * g[] = {
583 &disk_attr.attr,
584 &resume_attr.attr,
ca0aec0f 585 &image_size_attr.attr,
1da177e4
LT
586 NULL,
587};
588
589
590static struct attribute_group attr_group = {
591 .attrs = g,
592};
593
594
595static int __init pm_disk_init(void)
596{
823bccfc 597 return sysfs_create_group(&power_subsys.kobj, &attr_group);
1da177e4
LT
598}
599
600core_initcall(pm_disk_init);
601
602
603static int __init resume_setup(char *str)
604{
605 if (noresume)
606 return 1;
607
608 strncpy( resume_file, str, 255 );
609 return 1;
610}
611
9a154d9d
RW
612static int __init resume_offset_setup(char *str)
613{
614 unsigned long long offset;
615
616 if (noresume)
617 return 1;
618
619 if (sscanf(str, "%llu", &offset) == 1)
620 swsusp_resume_block = offset;
621
622 return 1;
623}
624
1da177e4
LT
625static int __init noresume_setup(char *str)
626{
627 noresume = 1;
628 return 1;
629}
630
631__setup("noresume", noresume_setup);
9a154d9d 632__setup("resume_offset=", resume_offset_setup);
1da177e4 633__setup("resume=", resume_setup);