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.
14 #include <linux/suspend.h>
15 #include <linux/syscalls.h>
16 #include <linux/reboot.h>
17 #include <linux/string.h>
18 #include <linux/delay.h>
23 extern u32 pm_disk_mode;
24 extern struct pm_ops * pm_ops;
26 extern int pmdisk_save(void);
27 extern int pmdisk_write(void);
28 extern int pmdisk_read(void);
29 extern int pmdisk_restore(void);
30 extern int pmdisk_free(void);
34 * power_down - Shut machine down for hibernate.
35 * @mode: Suspend-to-disk mode
37 * Use the platform driver, if configured so, and return gracefully if it
39 * Otherwise, try to power off and reboot. If they fail, halt the machine,
40 * there ain't no turning back.
43 static int power_down(u32 mode)
48 local_irq_save(flags);
49 device_power_down(PM_SUSPEND_DISK);
51 case PM_DISK_PLATFORM:
52 error = pm_ops->enter(PM_SUSPEND_DISK);
54 case PM_DISK_SHUTDOWN:
55 printk("Powering off system\n");
59 machine_restart(NULL);
64 local_irq_restore(flags);
69 static int in_suspend __nosavedata = 0;
73 * free_some_memory - Try to free as much memory as possible
75 * ... but do not OOM-kill anyone
77 * Notice: all userland should be stopped at this point, or
78 * livelock is possible.
81 static void free_some_memory(void)
83 printk("Freeing memory: ");
84 while (shrink_all_memory(10000))
90 static inline void platform_finish(void)
92 if (pm_disk_mode == PM_DISK_PLATFORM) {
93 if (pm_ops && pm_ops->finish)
94 pm_ops->finish(PM_SUSPEND_DISK);
98 static void finish(void)
103 pm_restore_console();
107 static int prepare(void)
111 pm_prepare_console();
114 if (freeze_processes()) {
119 if (pm_disk_mode == PM_DISK_PLATFORM) {
120 if (pm_ops && pm_ops->prepare) {
121 if ((error = pm_ops->prepare(PM_SUSPEND_DISK)))
126 /* Free memory before shutting down devices. */
129 if ((error = device_suspend(PM_SUSPEND_DISK)))
137 pm_restore_console();
143 * pm_suspend_disk - The granpappy of power management.
145 * If we're going through the firmware, then get it over with quickly.
147 * If not, then call pmdis to do it's thing, then figure out how
148 * to power down the system.
151 int pm_suspend_disk(void)
155 if ((error = prepare()))
158 pr_debug("PM: Attempting to suspend to disk.\n");
159 if (pm_disk_mode == PM_DISK_FIRMWARE)
160 return pm_ops->enter(PM_SUSPEND_DISK);
162 pr_debug("PM: snapshotting memory.\n");
164 if ((error = pmdisk_save()))
168 pr_debug("PM: writing image.\n");
171 * FIXME: Leftover from swsusp. Are they necessary?
176 error = pmdisk_write();
178 error = power_down(pm_disk_mode);
179 pr_debug("PM: Power down failed.\n");
182 pr_debug("PM: Image restored successfully.\n");
191 * pm_resume - Resume from a saved image.
193 * Called as a late_initcall (so all devices are discovered and
194 * initialized), we call pmdisk to see if we have a saved image or not.
195 * If so, we quiesce devices, the restore the saved image. We will
196 * return above (in pm_suspend_disk() ) if everything goes well.
197 * Otherwise, we fail gracefully and return to the normally
202 static int pm_resume(void)
206 pr_debug("PM: Reading pmdisk image.\n");
208 if ((error = pmdisk_read()))
211 pr_debug("PM: Preparing system for restore.\n");
213 if ((error = prepare()))
219 /* FIXME: The following (comment and mdelay()) are from swsusp.
220 * Are they really necessary?
222 * We do not want some readahead with DMA to corrupt our memory, right?
223 * Do it with disabled interrupts for best effect. That way, if some
224 * driver scheduled DMA, we have good chance for DMA to finish ;-).
226 pr_debug("PM: Waiting for DMAs to settle down.\n");
229 pr_debug("PM: Restoring saved image.\n");
231 pr_debug("PM: Restore failed, recovering.n");
236 pr_debug("PM: Resume from disk failed.\n");
240 late_initcall(pm_resume);
243 static char * pm_disk_modes[] = {
244 [PM_DISK_FIRMWARE] = "firmware",
245 [PM_DISK_PLATFORM] = "platform",
246 [PM_DISK_SHUTDOWN] = "shutdown",
247 [PM_DISK_REBOOT] = "reboot",
251 * disk - Control suspend-to-disk mode
253 * Suspend-to-disk can be handled in several ways. The greatest
254 * distinction is who writes memory to disk - the firmware or the OS.
255 * If the firmware does it, we assume that it also handles suspending
257 * If the OS does it, then we have three options for putting the system
258 * to sleep - using the platform driver (e.g. ACPI or other PM registers),
259 * powering off the system or rebooting the system (for testing).
261 * The system will support either 'firmware' or 'platform', and that is
262 * known a priori (and encoded in pm_ops). But, the user may choose
263 * 'shutdown' or 'reboot' as alternatives.
265 * show() will display what the mode is currently set to.
266 * store() will accept one of
273 * It will only change to 'firmware' or 'platform' if the system
274 * supports it (as determined from pm_ops->pm_disk_mode).
277 static ssize_t disk_show(struct subsystem * subsys, char * buf)
279 return sprintf(buf,"%s\n",pm_disk_modes[pm_disk_mode]);
283 static ssize_t disk_store(struct subsystem * s, const char * buf, size_t n)
291 p = memchr(buf, '\n', n);
292 len = p ? p - buf : n;
295 for (i = PM_DISK_FIRMWARE; i < PM_DISK_MAX; i++) {
296 if (!strncmp(buf, pm_disk_modes[i], len)) {
302 if (mode == PM_DISK_SHUTDOWN || mode == PM_DISK_REBOOT)
305 if (pm_ops && pm_ops->enter &&
306 (mode == pm_ops->pm_disk_mode))
314 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
315 pm_disk_modes[mode]);
317 return error ? error : n;
322 static struct attribute * g[] = {
328 static struct attribute_group attr_group = {
333 static int __init pm_disk_init(void)
335 return sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
338 core_initcall(pm_disk_init);