2 * kernel/power/disk.c - Suspend-to-disk support.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
7 * This file is released under the GPLv2.
11 #include <linux/suspend.h>
12 #include <linux/syscalls.h>
13 #include <linux/reboot.h>
14 #include <linux/string.h>
15 #include <linux/device.h>
16 #include <linux/delay.h>
21 extern u32 pm_disk_mode;
22 extern struct pm_ops * pm_ops;
24 extern int swsusp_suspend(void);
25 extern int swsusp_write(void);
26 extern int swsusp_read(void);
27 extern int swsusp_resume(void);
28 extern int swsusp_free(void);
31 static int noresume = 0;
32 char resume_file[256] = CONFIG_PM_STD_PARTITION;
35 * power_down - Shut machine down for hibernate.
36 * @mode: Suspend-to-disk mode
38 * Use the platform driver, if configured so, and return gracefully if it
40 * Otherwise, try to power off and reboot. If they fail, halt the machine,
41 * there ain't no turning back.
44 static int power_down(u32 mode)
49 local_irq_save(flags);
51 case PM_DISK_PLATFORM:
52 device_power_down(PM_SUSPEND_DISK);
53 error = pm_ops->enter(PM_SUSPEND_DISK);
55 case PM_DISK_SHUTDOWN:
56 printk("Powering off system\n");
62 machine_restart(NULL);
66 /* Valid image is on the disk, if we continue we risk serious data corruption
68 printk(KERN_CRIT "Please power me down manually\n");
74 static int in_suspend __nosavedata = 0;
78 * free_some_memory - Try to free as much memory as possible
80 * ... but do not OOM-kill anyone
82 * Notice: all userland should be stopped at this point, or
83 * livelock is possible.
86 static void free_some_memory(void)
88 printk("Freeing memory: ");
89 while (shrink_all_memory(10000))
95 static inline void platform_finish(void)
97 if (pm_disk_mode == PM_DISK_PLATFORM) {
98 if (pm_ops && pm_ops->finish)
99 pm_ops->finish(PM_SUSPEND_DISK);
103 static void finish(void)
107 enable_nonboot_cpus();
109 pm_restore_console();
113 static int prepare(void)
117 pm_prepare_console();
120 if (freeze_processes()) {
125 if (pm_disk_mode == PM_DISK_PLATFORM) {
126 if (pm_ops && pm_ops->prepare) {
127 if ((error = pm_ops->prepare(PM_SUSPEND_DISK)))
132 /* Free memory before shutting down devices. */
135 disable_nonboot_cpus();
136 if ((error = device_suspend(PM_SUSPEND_DISK)))
143 enable_nonboot_cpus();
145 pm_restore_console();
151 * pm_suspend_disk - The granpappy of power management.
153 * If we're going through the firmware, then get it over with quickly.
155 * If not, then call pmdis to do it's thing, then figure out how
156 * to power down the system.
159 int pm_suspend_disk(void)
163 if ((error = prepare()))
166 pr_debug("PM: Attempting to suspend to disk.\n");
167 if (pm_disk_mode == PM_DISK_FIRMWARE)
168 return pm_ops->enter(PM_SUSPEND_DISK);
170 pr_debug("PM: snapshotting memory.\n");
172 if ((error = swsusp_suspend()))
176 pr_debug("PM: writing image.\n");
179 * FIXME: Leftover from swsusp. Are they necessary?
184 error = swsusp_write();
186 error = power_down(pm_disk_mode);
187 pr_debug("PM: Power down failed.\n");
190 pr_debug("PM: Image restored successfully.\n");
199 * software_resume - Resume from a saved image.
201 * Called as a late_initcall (so all devices are discovered and
202 * initialized), we call pmdisk to see if we have a saved image or not.
203 * If so, we quiesce devices, the restore the saved image. We will
204 * return above (in pm_suspend_disk() ) if everything goes well.
205 * Otherwise, we fail gracefully and return to the normally
210 static int software_resume(void)
216 * FIXME: If noresume is specified, we need to find the partition
217 * and reset it back to normal swap space.
222 pr_debug("PM: Reading pmdisk image.\n");
224 if ((error = swsusp_read()))
227 pr_debug("PM: Preparing system for restore.\n");
229 if ((error = prepare()))
235 pr_debug("PM: Restoring saved image.\n");
237 pr_debug("PM: Restore failed, recovering.n");
242 pr_debug("PM: Resume from disk failed.\n");
246 late_initcall(software_resume);
249 static char * pm_disk_modes[] = {
250 [PM_DISK_FIRMWARE] = "firmware",
251 [PM_DISK_PLATFORM] = "platform",
252 [PM_DISK_SHUTDOWN] = "shutdown",
253 [PM_DISK_REBOOT] = "reboot",
257 * disk - Control suspend-to-disk mode
259 * Suspend-to-disk can be handled in several ways. The greatest
260 * distinction is who writes memory to disk - the firmware or the OS.
261 * If the firmware does it, we assume that it also handles suspending
263 * If the OS does it, then we have three options for putting the system
264 * to sleep - using the platform driver (e.g. ACPI or other PM registers),
265 * powering off the system or rebooting the system (for testing).
267 * The system will support either 'firmware' or 'platform', and that is
268 * known a priori (and encoded in pm_ops). But, the user may choose
269 * 'shutdown' or 'reboot' as alternatives.
271 * show() will display what the mode is currently set to.
272 * store() will accept one of
279 * It will only change to 'firmware' or 'platform' if the system
280 * supports it (as determined from pm_ops->pm_disk_mode).
283 static ssize_t disk_show(struct subsystem * subsys, char * buf)
285 return sprintf(buf,"%s\n",pm_disk_modes[pm_disk_mode]);
289 static ssize_t disk_store(struct subsystem * s, const char * buf, size_t n)
297 p = memchr(buf, '\n', n);
298 len = p ? p - buf : n;
301 for (i = PM_DISK_FIRMWARE; i < PM_DISK_MAX; i++) {
302 if (!strncmp(buf, pm_disk_modes[i], len)) {
308 if (mode == PM_DISK_SHUTDOWN || mode == PM_DISK_REBOOT)
311 if (pm_ops && pm_ops->enter &&
312 (mode == pm_ops->pm_disk_mode))
320 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
321 pm_disk_modes[mode]);
323 return error ? error : n;
328 static struct attribute * g[] = {
334 static struct attribute_group attr_group = {
339 static int __init pm_disk_init(void)
341 return sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
344 core_initcall(pm_disk_init);
347 static int __init resume_setup(char *str)
352 strncpy( resume_file, str, 255 );
356 static int __init noresume_setup(char *str)
362 __setup("noresume", noresume_setup);
363 __setup("resume=", resume_setup);