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