initial commit with v2.6.9
[linux-2.6.9-moxart.git] / kernel / power / disk.c
blob312aa169c5665cd379783cca6400e1ec5c7dfac9
1 /*
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.
9 */
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>
17 #include <linux/fs.h>
18 #include "power.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;
34 /**
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
39 * fails.
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)
46 unsigned long flags;
47 int error = 0;
49 local_irq_save(flags);
50 switch(mode) {
51 case PM_DISK_PLATFORM:
52 device_power_down(PM_SUSPEND_DISK);
53 error = pm_ops->enter(PM_SUSPEND_DISK);
54 break;
55 case PM_DISK_SHUTDOWN:
56 printk("Powering off system\n");
57 device_shutdown();
58 machine_power_off();
59 break;
60 case PM_DISK_REBOOT:
61 device_shutdown();
62 machine_restart(NULL);
63 break;
65 machine_halt();
66 /* Valid image is on the disk, if we continue we risk serious data corruption
67 after resume. */
68 printk(KERN_CRIT "Please power me down manually\n");
69 while(1);
70 return 0;
74 static int in_suspend __nosavedata = 0;
77 /**
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))
90 printk(".");
91 printk("|\n");
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)
105 device_resume();
106 platform_finish();
107 enable_nonboot_cpus();
108 thaw_processes();
109 pm_restore_console();
113 static int prepare(void)
115 int error;
117 pm_prepare_console();
119 sys_sync();
120 if (freeze_processes()) {
121 error = -EBUSY;
122 goto Thaw;
125 if (pm_disk_mode == PM_DISK_PLATFORM) {
126 if (pm_ops && pm_ops->prepare) {
127 if ((error = pm_ops->prepare(PM_SUSPEND_DISK)))
128 goto Thaw;
132 /* Free memory before shutting down devices. */
133 free_some_memory();
135 disable_nonboot_cpus();
136 if ((error = device_suspend(PM_SUSPEND_DISK)))
137 goto Finish;
139 return 0;
140 Finish:
141 platform_finish();
142 Thaw:
143 enable_nonboot_cpus();
144 thaw_processes();
145 pm_restore_console();
146 return error;
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)
161 int error;
163 if ((error = prepare()))
164 return error;
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");
171 in_suspend = 1;
172 if ((error = swsusp_suspend()))
173 goto Done;
175 if (in_suspend) {
176 pr_debug("PM: writing image.\n");
179 * FIXME: Leftover from swsusp. Are they necessary?
181 mb();
182 barrier();
184 error = swsusp_write();
185 if (!error) {
186 error = power_down(pm_disk_mode);
187 pr_debug("PM: Power down failed.\n");
189 } else
190 pr_debug("PM: Image restored successfully.\n");
191 swsusp_free();
192 Done:
193 finish();
194 return error;
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
206 * scheduled program.
210 static int software_resume(void)
212 int error;
214 if (noresume) {
216 * FIXME: If noresume is specified, we need to find the partition
217 * and reset it back to normal swap space.
219 return 0;
222 pr_debug("PM: Reading pmdisk image.\n");
224 if ((error = swsusp_read()))
225 goto Done;
227 pr_debug("PM: Preparing system for restore.\n");
229 if ((error = prepare()))
230 goto Free;
232 barrier();
233 mb();
235 pr_debug("PM: Restoring saved image.\n");
236 swsusp_resume();
237 pr_debug("PM: Restore failed, recovering.n");
238 finish();
239 Free:
240 swsusp_free();
241 Done:
242 pr_debug("PM: Resume from disk failed.\n");
243 return 0;
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
262 * the system.
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
274 * 'firmware'
275 * 'platform'
276 * 'shutdown'
277 * 'reboot'
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)
291 int error = 0;
292 int i;
293 int len;
294 char *p;
295 u32 mode = 0;
297 p = memchr(buf, '\n', n);
298 len = p ? p - buf : n;
300 down(&pm_sem);
301 for (i = PM_DISK_FIRMWARE; i < PM_DISK_MAX; i++) {
302 if (!strncmp(buf, pm_disk_modes[i], len)) {
303 mode = i;
304 break;
307 if (mode) {
308 if (mode == PM_DISK_SHUTDOWN || mode == PM_DISK_REBOOT)
309 pm_disk_mode = mode;
310 else {
311 if (pm_ops && pm_ops->enter &&
312 (mode == pm_ops->pm_disk_mode))
313 pm_disk_mode = mode;
314 else
315 error = -EINVAL;
317 } else
318 error = -EINVAL;
320 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
321 pm_disk_modes[mode]);
322 up(&pm_sem);
323 return error ? error : n;
326 power_attr(disk);
328 static struct attribute * g[] = {
329 &disk_attr.attr,
330 NULL,
334 static struct attribute_group attr_group = {
335 .attrs = g,
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)
349 if (noresume)
350 return 1;
352 strncpy( resume_file, str, 255 );
353 return 1;
356 static int __init noresume_setup(char *str)
358 noresume = 1;
359 return 1;
362 __setup("noresume", noresume_setup);
363 __setup("resume=", resume_setup);