get_unmapped_area handles MAP_FIXED on i386
[pv_ops_mirror.git] / kernel / power / disk.c
blob8df51c23bba4056c4864a5ebe69d473a603594dd
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 /* Free memory before shutting down devices. */
143 error = swsusp_shrink_memory();
144 if (error)
145 goto Thaw;
147 error = platform_prepare();
148 if (error)
149 goto Thaw;
151 suspend_console();
152 error = device_suspend(PMSG_FREEZE);
153 if (error) {
154 printk(KERN_ERR "PM: Some devices failed to suspend\n");
155 goto Resume_devices;
157 error = disable_nonboot_cpus();
158 if (error)
159 goto Enable_cpus;
161 if (pm_disk_mode == PM_DISK_TEST) {
162 printk("swsusp debug: Waiting for 5 seconds.\n");
163 mdelay(5000);
164 goto Enable_cpus;
167 pr_debug("PM: snapshotting memory.\n");
168 in_suspend = 1;
169 error = swsusp_suspend();
170 if (error)
171 goto Enable_cpus;
173 if (in_suspend) {
174 enable_nonboot_cpus();
175 platform_finish();
176 device_resume();
177 resume_console();
178 pr_debug("PM: writing image.\n");
179 error = swsusp_write();
180 if (!error)
181 power_down();
182 else {
183 swsusp_free();
184 goto Thaw;
186 } else {
187 pr_debug("PM: Image restored successfully.\n");
190 swsusp_free();
191 Enable_cpus:
192 enable_nonboot_cpus();
193 Resume_devices:
194 platform_finish();
195 device_resume();
196 resume_console();
197 Thaw:
198 unprepare_processes();
199 return error;
204 * software_resume - Resume from a saved image.
206 * Called as a late_initcall (so all devices are discovered and
207 * initialized), we call swsusp to see if we have a saved image or not.
208 * If so, we quiesce devices, the restore the saved image. We will
209 * return above (in pm_suspend_disk() ) if everything goes well.
210 * Otherwise, we fail gracefully and return to the normally
211 * scheduled program.
215 static int software_resume(void)
217 int error;
219 mutex_lock(&pm_mutex);
220 if (!swsusp_resume_device) {
221 if (!strlen(resume_file)) {
222 mutex_unlock(&pm_mutex);
223 return -ENOENT;
225 swsusp_resume_device = name_to_dev_t(resume_file);
226 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
227 } else {
228 pr_debug("swsusp: Resume From Partition %d:%d\n",
229 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
232 if (noresume) {
234 * FIXME: If noresume is specified, we need to find the partition
235 * and reset it back to normal swap space.
237 mutex_unlock(&pm_mutex);
238 return 0;
241 pr_debug("PM: Checking swsusp image.\n");
243 error = swsusp_check();
244 if (error)
245 goto Done;
247 pr_debug("PM: Preparing processes for restore.\n");
249 error = prepare_processes();
250 if (error) {
251 swsusp_close();
252 goto Done;
255 pr_debug("PM: Reading swsusp image.\n");
257 error = swsusp_read();
258 if (error) {
259 swsusp_free();
260 goto Thaw;
263 pr_debug("PM: Preparing devices for restore.\n");
265 suspend_console();
266 error = device_suspend(PMSG_PRETHAW);
267 if (error)
268 goto Free;
270 error = disable_nonboot_cpus();
271 if (!error)
272 swsusp_resume();
274 enable_nonboot_cpus();
275 Free:
276 swsusp_free();
277 device_resume();
278 resume_console();
279 Thaw:
280 printk(KERN_ERR "PM: Restore failed, recovering.\n");
281 unprepare_processes();
282 Done:
283 /* For success case, the suspend path will release the lock */
284 mutex_unlock(&pm_mutex);
285 pr_debug("PM: Resume from disk failed.\n");
286 return 0;
289 late_initcall(software_resume);
292 static const char * const pm_disk_modes[] = {
293 [PM_DISK_PLATFORM] = "platform",
294 [PM_DISK_SHUTDOWN] = "shutdown",
295 [PM_DISK_REBOOT] = "reboot",
296 [PM_DISK_TEST] = "test",
297 [PM_DISK_TESTPROC] = "testproc",
301 * disk - Control suspend-to-disk mode
303 * Suspend-to-disk can be handled in several ways. We have a few options
304 * for putting the system to sleep - using the platform driver (e.g. ACPI
305 * or other pm_ops), powering off the system or rebooting the system
306 * (for testing) as well as the two test modes.
308 * The system can support 'platform', and that is known a priori (and
309 * encoded in pm_ops). However, the user may choose 'shutdown' or 'reboot'
310 * as alternatives, as well as the test modes 'test' and 'testproc'.
312 * show() will display what the mode is currently set to.
313 * store() will accept one of
315 * 'platform'
316 * 'shutdown'
317 * 'reboot'
318 * 'test'
319 * 'testproc'
321 * It will only change to 'platform' if the system
322 * supports it (as determined from pm_ops->pm_disk_mode).
325 static ssize_t disk_show(struct kset *kset, char *buf)
327 return sprintf(buf, "%s\n", pm_disk_modes[pm_disk_mode]);
331 static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
333 int error = 0;
334 int i;
335 int len;
336 char *p;
337 suspend_disk_method_t mode = 0;
339 p = memchr(buf, '\n', n);
340 len = p ? p - buf : n;
342 mutex_lock(&pm_mutex);
343 for (i = PM_DISK_PLATFORM; i < PM_DISK_MAX; i++) {
344 if (!strncmp(buf, pm_disk_modes[i], len)) {
345 mode = i;
346 break;
349 if (mode) {
350 switch (mode) {
351 case PM_DISK_SHUTDOWN:
352 case PM_DISK_REBOOT:
353 case PM_DISK_TEST:
354 case PM_DISK_TESTPROC:
355 pm_disk_mode = mode;
356 break;
357 default:
358 if (pm_ops && pm_ops->enter &&
359 (mode == pm_ops->pm_disk_mode))
360 pm_disk_mode = mode;
361 else
362 error = -EINVAL;
364 } else {
365 error = -EINVAL;
368 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
369 pm_disk_modes[mode]);
370 mutex_unlock(&pm_mutex);
371 return error ? error : n;
374 power_attr(disk);
376 static ssize_t resume_show(struct kset *kset, char *buf)
378 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
379 MINOR(swsusp_resume_device));
382 static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
384 unsigned int maj, min;
385 dev_t res;
386 int ret = -EINVAL;
388 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
389 goto out;
391 res = MKDEV(maj,min);
392 if (maj != MAJOR(res) || min != MINOR(res))
393 goto out;
395 mutex_lock(&pm_mutex);
396 swsusp_resume_device = res;
397 mutex_unlock(&pm_mutex);
398 printk("Attempting manual resume\n");
399 noresume = 0;
400 software_resume();
401 ret = n;
402 out:
403 return ret;
406 power_attr(resume);
408 static ssize_t image_size_show(struct kset *kset, char *buf)
410 return sprintf(buf, "%lu\n", image_size);
413 static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
415 unsigned long size;
417 if (sscanf(buf, "%lu", &size) == 1) {
418 image_size = size;
419 return n;
422 return -EINVAL;
425 power_attr(image_size);
427 static struct attribute * g[] = {
428 &disk_attr.attr,
429 &resume_attr.attr,
430 &image_size_attr.attr,
431 NULL,
435 static struct attribute_group attr_group = {
436 .attrs = g,
440 static int __init pm_disk_init(void)
442 return sysfs_create_group(&power_subsys.kobj, &attr_group);
445 core_initcall(pm_disk_init);
448 static int __init resume_setup(char *str)
450 if (noresume)
451 return 1;
453 strncpy( resume_file, str, 255 );
454 return 1;
457 static int __init resume_offset_setup(char *str)
459 unsigned long long offset;
461 if (noresume)
462 return 1;
464 if (sscanf(str, "%llu", &offset) == 1)
465 swsusp_resume_block = offset;
467 return 1;
470 static int __init noresume_setup(char *str)
472 noresume = 1;
473 return 1;
476 __setup("noresume", noresume_setup);
477 __setup("resume_offset=", resume_offset_setup);
478 __setup("resume=", resume_setup);