swsusp: do not use page flags
[linux-2.6/mini2440.git] / kernel / power / disk.c
blob403bc3722fee914f13d82a912739c83b1f508895
1 /*
2 * kernel/power/disk.c - Suspend-to-disk support.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 * Copyright (c) 2004 Pavel Machek <pavel@suse.cz>
8 * This file is released under the GPLv2.
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>
19 #include <linux/mount.h>
20 #include <linux/pm.h>
21 #include <linux/console.h>
22 #include <linux/cpu.h>
23 #include <linux/freezer.h>
25 #include "power.h"
28 static int noresume = 0;
29 char resume_file[256] = CONFIG_PM_STD_PARTITION;
30 dev_t swsusp_resume_device;
31 sector_t swsusp_resume_block;
33 /**
34 * platform_prepare - prepare the machine for hibernation using the
35 * platform driver if so configured and return an error code if it fails
38 static inline int platform_prepare(void)
40 int error = 0;
42 switch (pm_disk_mode) {
43 case PM_DISK_TEST:
44 case PM_DISK_TESTPROC:
45 case PM_DISK_SHUTDOWN:
46 case PM_DISK_REBOOT:
47 break;
48 default:
49 if (pm_ops && pm_ops->prepare)
50 error = pm_ops->prepare(PM_SUSPEND_DISK);
52 return error;
55 /**
56 * power_down - Shut machine down for hibernate.
58 * Use the platform driver, if configured so; otherwise try
59 * to power off or reboot.
62 static void power_down(void)
64 switch (pm_disk_mode) {
65 case PM_DISK_TEST:
66 case PM_DISK_TESTPROC:
67 break;
68 case PM_DISK_SHUTDOWN:
69 kernel_power_off();
70 break;
71 case PM_DISK_REBOOT:
72 kernel_restart(NULL);
73 break;
74 default:
75 if (pm_ops && pm_ops->enter) {
76 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
77 pm_ops->enter(PM_SUSPEND_DISK);
78 break;
81 kernel_halt();
83 * Valid image is on the disk, if we continue we risk serious data
84 * corruption after resume.
86 printk(KERN_CRIT "Please power me down manually\n");
87 while(1);
90 static inline void platform_finish(void)
92 switch (pm_disk_mode) {
93 case PM_DISK_TEST:
94 case PM_DISK_TESTPROC:
95 case PM_DISK_SHUTDOWN:
96 case PM_DISK_REBOOT:
97 break;
98 default:
99 if (pm_ops && pm_ops->finish)
100 pm_ops->finish(PM_SUSPEND_DISK);
104 static void unprepare_processes(void)
106 thaw_processes();
107 pm_restore_console();
110 static int prepare_processes(void)
112 int error = 0;
114 pm_prepare_console();
115 if (freeze_processes()) {
116 error = -EBUSY;
117 unprepare_processes();
119 return error;
123 * pm_suspend_disk - The granpappy of hibernation power management.
125 * If not, then call swsusp to do its thing, then figure out how
126 * to power down the system.
129 int pm_suspend_disk(void)
131 int error;
133 error = prepare_processes();
134 if (error)
135 return error;
137 if (pm_disk_mode == PM_DISK_TESTPROC) {
138 printk("swsusp debug: Waiting for 5 seconds.\n");
139 mdelay(5000);
140 goto Thaw;
142 /* Allocate memory management structures */
143 error = create_basic_memory_bitmaps();
144 if (error)
145 goto Thaw;
147 /* Free memory before shutting down devices. */
148 error = swsusp_shrink_memory();
149 if (error)
150 goto Finish;
152 error = platform_prepare();
153 if (error)
154 goto Finish;
156 suspend_console();
157 error = device_suspend(PMSG_FREEZE);
158 if (error) {
159 printk(KERN_ERR "PM: Some devices failed to suspend\n");
160 goto Resume_devices;
162 error = disable_nonboot_cpus();
163 if (error)
164 goto Enable_cpus;
166 if (pm_disk_mode == PM_DISK_TEST) {
167 printk("swsusp debug: Waiting for 5 seconds.\n");
168 mdelay(5000);
169 goto Enable_cpus;
172 pr_debug("PM: snapshotting memory.\n");
173 in_suspend = 1;
174 error = swsusp_suspend();
175 if (error)
176 goto Enable_cpus;
178 if (in_suspend) {
179 enable_nonboot_cpus();
180 platform_finish();
181 device_resume();
182 resume_console();
183 pr_debug("PM: writing image.\n");
184 error = swsusp_write();
185 if (!error)
186 power_down();
187 else {
188 swsusp_free();
189 goto Finish;
191 } else {
192 pr_debug("PM: Image restored successfully.\n");
195 swsusp_free();
196 Enable_cpus:
197 enable_nonboot_cpus();
198 Resume_devices:
199 platform_finish();
200 device_resume();
201 resume_console();
202 Finish:
203 free_basic_memory_bitmaps();
204 Thaw:
205 unprepare_processes();
206 return error;
211 * software_resume - Resume from a saved image.
213 * Called as a late_initcall (so all devices are discovered and
214 * initialized), we call swsusp to see if we have a saved image or not.
215 * If so, we quiesce devices, the restore the saved image. We will
216 * return above (in pm_suspend_disk() ) if everything goes well.
217 * Otherwise, we fail gracefully and return to the normally
218 * scheduled program.
222 static int software_resume(void)
224 int error;
226 mutex_lock(&pm_mutex);
227 if (!swsusp_resume_device) {
228 if (!strlen(resume_file)) {
229 mutex_unlock(&pm_mutex);
230 return -ENOENT;
232 swsusp_resume_device = name_to_dev_t(resume_file);
233 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
234 } else {
235 pr_debug("swsusp: Resume From Partition %d:%d\n",
236 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
239 if (noresume) {
241 * FIXME: If noresume is specified, we need to find the partition
242 * and reset it back to normal swap space.
244 mutex_unlock(&pm_mutex);
245 return 0;
248 pr_debug("PM: Checking swsusp image.\n");
249 error = swsusp_check();
250 if (error)
251 goto Unlock;
253 error = create_basic_memory_bitmaps();
254 if (error)
255 goto Unlock;
257 pr_debug("PM: Preparing processes for restore.\n");
258 error = prepare_processes();
259 if (error) {
260 swsusp_close();
261 goto Done;
264 pr_debug("PM: Reading swsusp image.\n");
266 error = swsusp_read();
267 if (error) {
268 swsusp_free();
269 goto Thaw;
272 pr_debug("PM: Preparing devices for restore.\n");
274 suspend_console();
275 error = device_suspend(PMSG_PRETHAW);
276 if (error)
277 goto Free;
279 error = disable_nonboot_cpus();
280 if (!error)
281 swsusp_resume();
283 enable_nonboot_cpus();
284 Free:
285 swsusp_free();
286 device_resume();
287 resume_console();
288 Thaw:
289 printk(KERN_ERR "PM: Restore failed, recovering.\n");
290 unprepare_processes();
291 Done:
292 free_basic_memory_bitmaps();
293 /* For success case, the suspend path will release the lock */
294 Unlock:
295 mutex_unlock(&pm_mutex);
296 pr_debug("PM: Resume from disk failed.\n");
297 return 0;
300 late_initcall(software_resume);
303 static const char * const pm_disk_modes[] = {
304 [PM_DISK_PLATFORM] = "platform",
305 [PM_DISK_SHUTDOWN] = "shutdown",
306 [PM_DISK_REBOOT] = "reboot",
307 [PM_DISK_TEST] = "test",
308 [PM_DISK_TESTPROC] = "testproc",
312 * disk - Control suspend-to-disk mode
314 * Suspend-to-disk can be handled in several ways. We have a few options
315 * for putting the system to sleep - using the platform driver (e.g. ACPI
316 * or other pm_ops), powering off the system or rebooting the system
317 * (for testing) as well as the two test modes.
319 * The system can support 'platform', and that is known a priori (and
320 * encoded in pm_ops). However, the user may choose 'shutdown' or 'reboot'
321 * as alternatives, as well as the test modes 'test' and 'testproc'.
323 * show() will display what the mode is currently set to.
324 * store() will accept one of
326 * 'platform'
327 * 'shutdown'
328 * 'reboot'
329 * 'test'
330 * 'testproc'
332 * It will only change to 'platform' if the system
333 * supports it (as determined from pm_ops->pm_disk_mode).
336 static ssize_t disk_show(struct kset *kset, char *buf)
338 return sprintf(buf, "%s\n", pm_disk_modes[pm_disk_mode]);
342 static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
344 int error = 0;
345 int i;
346 int len;
347 char *p;
348 suspend_disk_method_t mode = 0;
350 p = memchr(buf, '\n', n);
351 len = p ? p - buf : n;
353 mutex_lock(&pm_mutex);
354 for (i = PM_DISK_PLATFORM; i < PM_DISK_MAX; i++) {
355 if (!strncmp(buf, pm_disk_modes[i], len)) {
356 mode = i;
357 break;
360 if (mode) {
361 switch (mode) {
362 case PM_DISK_SHUTDOWN:
363 case PM_DISK_REBOOT:
364 case PM_DISK_TEST:
365 case PM_DISK_TESTPROC:
366 pm_disk_mode = mode;
367 break;
368 default:
369 if (pm_ops && pm_ops->enter &&
370 (mode == pm_ops->pm_disk_mode))
371 pm_disk_mode = mode;
372 else
373 error = -EINVAL;
375 } else {
376 error = -EINVAL;
379 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
380 pm_disk_modes[mode]);
381 mutex_unlock(&pm_mutex);
382 return error ? error : n;
385 power_attr(disk);
387 static ssize_t resume_show(struct kset *kset, char *buf)
389 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
390 MINOR(swsusp_resume_device));
393 static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
395 unsigned int maj, min;
396 dev_t res;
397 int ret = -EINVAL;
399 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
400 goto out;
402 res = MKDEV(maj,min);
403 if (maj != MAJOR(res) || min != MINOR(res))
404 goto out;
406 mutex_lock(&pm_mutex);
407 swsusp_resume_device = res;
408 mutex_unlock(&pm_mutex);
409 printk("Attempting manual resume\n");
410 noresume = 0;
411 software_resume();
412 ret = n;
413 out:
414 return ret;
417 power_attr(resume);
419 static ssize_t image_size_show(struct kset *kset, char *buf)
421 return sprintf(buf, "%lu\n", image_size);
424 static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
426 unsigned long size;
428 if (sscanf(buf, "%lu", &size) == 1) {
429 image_size = size;
430 return n;
433 return -EINVAL;
436 power_attr(image_size);
438 static struct attribute * g[] = {
439 &disk_attr.attr,
440 &resume_attr.attr,
441 &image_size_attr.attr,
442 NULL,
446 static struct attribute_group attr_group = {
447 .attrs = g,
451 static int __init pm_disk_init(void)
453 return sysfs_create_group(&power_subsys.kobj, &attr_group);
456 core_initcall(pm_disk_init);
459 static int __init resume_setup(char *str)
461 if (noresume)
462 return 1;
464 strncpy( resume_file, str, 255 );
465 return 1;
468 static int __init resume_offset_setup(char *str)
470 unsigned long long offset;
472 if (noresume)
473 return 1;
475 if (sscanf(str, "%llu", &offset) == 1)
476 swsusp_resume_block = offset;
478 return 1;
481 static int __init noresume_setup(char *str)
483 noresume = 1;
484 return 1;
487 __setup("noresume", noresume_setup);
488 __setup("resume_offset=", resume_offset_setup);
489 __setup("resume=", resume_setup);