Linux 2.6.19-rc6
[cris-mirror.git] / kernel / power / disk.c
blobb1fb7866b0b31d65ccea5946aa22e9c2881e0322
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>
24 #include "power.h"
27 static int noresume = 0;
28 char resume_file[256] = CONFIG_PM_STD_PARTITION;
29 dev_t swsusp_resume_device;
31 /**
32 * power_down - Shut machine down for hibernate.
33 * @mode: Suspend-to-disk mode
35 * Use the platform driver, if configured so, and return gracefully if it
36 * fails.
37 * Otherwise, try to power off and reboot. If they fail, halt the machine,
38 * there ain't no turning back.
41 static void power_down(suspend_disk_method_t mode)
43 int error = 0;
45 switch(mode) {
46 case PM_DISK_PLATFORM:
47 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
48 error = pm_ops->enter(PM_SUSPEND_DISK);
49 break;
50 case PM_DISK_SHUTDOWN:
51 kernel_power_off();
52 break;
53 case PM_DISK_REBOOT:
54 kernel_restart(NULL);
55 break;
57 kernel_halt();
58 /* Valid image is on the disk, if we continue we risk serious data corruption
59 after resume. */
60 printk(KERN_CRIT "Please power me down manually\n");
61 while(1);
64 static inline void platform_finish(void)
66 if (pm_disk_mode == PM_DISK_PLATFORM) {
67 if (pm_ops && pm_ops->finish)
68 pm_ops->finish(PM_SUSPEND_DISK);
72 static int prepare_processes(void)
74 int error = 0;
76 pm_prepare_console();
78 error = disable_nonboot_cpus();
79 if (error)
80 goto enable_cpus;
82 if (freeze_processes()) {
83 error = -EBUSY;
84 goto thaw;
87 if (pm_disk_mode == PM_DISK_TESTPROC) {
88 printk("swsusp debug: Waiting for 5 seconds.\n");
89 mdelay(5000);
90 goto thaw;
93 /* Free memory before shutting down devices. */
94 if (!(error = swsusp_shrink_memory()))
95 return 0;
96 thaw:
97 thaw_processes();
98 enable_cpus:
99 enable_nonboot_cpus();
100 pm_restore_console();
101 return error;
104 static void unprepare_processes(void)
106 platform_finish();
107 thaw_processes();
108 enable_nonboot_cpus();
109 pm_restore_console();
113 * pm_suspend_disk - The granpappy of hibernation power management.
115 * If we're going through the firmware, then get it over with quickly.
117 * If not, then call swsusp to do its thing, then figure out how
118 * to power down the system.
121 int pm_suspend_disk(void)
123 int error;
125 error = prepare_processes();
126 if (error)
127 return error;
129 if (pm_disk_mode == PM_DISK_TESTPROC)
130 goto Thaw;
132 suspend_console();
133 error = device_suspend(PMSG_FREEZE);
134 if (error) {
135 resume_console();
136 printk("Some devices failed to suspend\n");
137 goto Thaw;
140 if (pm_disk_mode == PM_DISK_TEST) {
141 printk("swsusp debug: Waiting for 5 seconds.\n");
142 mdelay(5000);
143 goto Done;
146 pr_debug("PM: snapshotting memory.\n");
147 in_suspend = 1;
148 if ((error = swsusp_suspend()))
149 goto Done;
151 if (in_suspend) {
152 device_resume();
153 resume_console();
154 pr_debug("PM: writing image.\n");
155 error = swsusp_write();
156 if (!error)
157 power_down(pm_disk_mode);
158 else {
159 swsusp_free();
160 goto Thaw;
162 } else {
163 pr_debug("PM: Image restored successfully.\n");
166 swsusp_free();
167 Done:
168 device_resume();
169 resume_console();
170 Thaw:
171 unprepare_processes();
172 return error;
177 * software_resume - Resume from a saved image.
179 * Called as a late_initcall (so all devices are discovered and
180 * initialized), we call swsusp to see if we have a saved image or not.
181 * If so, we quiesce devices, the restore the saved image. We will
182 * return above (in pm_suspend_disk() ) if everything goes well.
183 * Otherwise, we fail gracefully and return to the normally
184 * scheduled program.
188 static int software_resume(void)
190 int error;
192 down(&pm_sem);
193 if (!swsusp_resume_device) {
194 if (!strlen(resume_file)) {
195 up(&pm_sem);
196 return -ENOENT;
198 swsusp_resume_device = name_to_dev_t(resume_file);
199 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
200 } else {
201 pr_debug("swsusp: Resume From Partition %d:%d\n",
202 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
205 if (noresume) {
207 * FIXME: If noresume is specified, we need to find the partition
208 * and reset it back to normal swap space.
210 up(&pm_sem);
211 return 0;
214 pr_debug("PM: Checking swsusp image.\n");
216 if ((error = swsusp_check()))
217 goto Done;
219 pr_debug("PM: Preparing processes for restore.\n");
221 if ((error = prepare_processes())) {
222 swsusp_close();
223 goto Done;
226 pr_debug("PM: Reading swsusp image.\n");
228 if ((error = swsusp_read())) {
229 swsusp_free();
230 goto Thaw;
233 pr_debug("PM: Preparing devices for restore.\n");
235 suspend_console();
236 if ((error = device_suspend(PMSG_PRETHAW))) {
237 resume_console();
238 printk("Some devices failed to suspend\n");
239 swsusp_free();
240 goto Thaw;
243 mb();
245 pr_debug("PM: Restoring saved image.\n");
246 swsusp_resume();
247 pr_debug("PM: Restore failed, recovering.n");
248 device_resume();
249 resume_console();
250 Thaw:
251 unprepare_processes();
252 Done:
253 /* For success case, the suspend path will release the lock */
254 up(&pm_sem);
255 pr_debug("PM: Resume from disk failed.\n");
256 return 0;
259 late_initcall(software_resume);
262 static const char * const pm_disk_modes[] = {
263 [PM_DISK_FIRMWARE] = "firmware",
264 [PM_DISK_PLATFORM] = "platform",
265 [PM_DISK_SHUTDOWN] = "shutdown",
266 [PM_DISK_REBOOT] = "reboot",
267 [PM_DISK_TEST] = "test",
268 [PM_DISK_TESTPROC] = "testproc",
272 * disk - Control suspend-to-disk mode
274 * Suspend-to-disk can be handled in several ways. The greatest
275 * distinction is who writes memory to disk - the firmware or the OS.
276 * If the firmware does it, we assume that it also handles suspending
277 * the system.
278 * If the OS does it, then we have three options for putting the system
279 * to sleep - using the platform driver (e.g. ACPI or other PM registers),
280 * powering off the system or rebooting the system (for testing).
282 * The system will support either 'firmware' or 'platform', and that is
283 * known a priori (and encoded in pm_ops). But, the user may choose
284 * 'shutdown' or 'reboot' as alternatives.
286 * show() will display what the mode is currently set to.
287 * store() will accept one of
289 * 'firmware'
290 * 'platform'
291 * 'shutdown'
292 * 'reboot'
294 * It will only change to 'firmware' or 'platform' if the system
295 * supports it (as determined from pm_ops->pm_disk_mode).
298 static ssize_t disk_show(struct subsystem * subsys, char * buf)
300 return sprintf(buf, "%s\n", pm_disk_modes[pm_disk_mode]);
304 static ssize_t disk_store(struct subsystem * s, const char * buf, size_t n)
306 int error = 0;
307 int i;
308 int len;
309 char *p;
310 suspend_disk_method_t mode = 0;
312 p = memchr(buf, '\n', n);
313 len = p ? p - buf : n;
315 down(&pm_sem);
316 for (i = PM_DISK_FIRMWARE; i < PM_DISK_MAX; i++) {
317 if (!strncmp(buf, pm_disk_modes[i], len)) {
318 mode = i;
319 break;
322 if (mode) {
323 if (mode == PM_DISK_SHUTDOWN || mode == PM_DISK_REBOOT ||
324 mode == PM_DISK_TEST || mode == PM_DISK_TESTPROC) {
325 pm_disk_mode = mode;
326 } else {
327 if (pm_ops && pm_ops->enter &&
328 (mode == pm_ops->pm_disk_mode))
329 pm_disk_mode = mode;
330 else
331 error = -EINVAL;
333 } else {
334 error = -EINVAL;
337 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
338 pm_disk_modes[mode]);
339 up(&pm_sem);
340 return error ? error : n;
343 power_attr(disk);
345 static ssize_t resume_show(struct subsystem * subsys, char *buf)
347 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
348 MINOR(swsusp_resume_device));
351 static ssize_t resume_store(struct subsystem *subsys, const char *buf, size_t n)
353 unsigned int maj, min;
354 dev_t res;
355 int ret = -EINVAL;
357 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
358 goto out;
360 res = MKDEV(maj,min);
361 if (maj != MAJOR(res) || min != MINOR(res))
362 goto out;
364 down(&pm_sem);
365 swsusp_resume_device = res;
366 up(&pm_sem);
367 printk("Attempting manual resume\n");
368 noresume = 0;
369 software_resume();
370 ret = n;
371 out:
372 return ret;
375 power_attr(resume);
377 static ssize_t image_size_show(struct subsystem * subsys, char *buf)
379 return sprintf(buf, "%lu\n", image_size);
382 static ssize_t image_size_store(struct subsystem * subsys, const char * buf, size_t n)
384 unsigned long size;
386 if (sscanf(buf, "%lu", &size) == 1) {
387 image_size = size;
388 return n;
391 return -EINVAL;
394 power_attr(image_size);
396 static struct attribute * g[] = {
397 &disk_attr.attr,
398 &resume_attr.attr,
399 &image_size_attr.attr,
400 NULL,
404 static struct attribute_group attr_group = {
405 .attrs = g,
409 static int __init pm_disk_init(void)
411 return sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
414 core_initcall(pm_disk_init);
417 static int __init resume_setup(char *str)
419 if (noresume)
420 return 1;
422 strncpy( resume_file, str, 255 );
423 return 1;
426 static int __init noresume_setup(char *str)
428 noresume = 1;
429 return 1;
432 __setup("noresume", noresume_setup);
433 __setup("resume=", resume_setup);