Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/shaggy...
[deliverable/linux.git] / kernel / power / disk.c
1 /*
2 * kernel/power/disk.c - Suspend-to-disk support.
3 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 * Copyright (c) 2004 Pavel Machek <pavel@suse.cz>
7 *
8 * This file is released under the GPLv2.
9 *
10 */
11
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>
24
25 #include "power.h"
26
27
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;
32
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
36 */
37
38 static inline int platform_prepare(void)
39 {
40 int error = 0;
41
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);
51 }
52 return error;
53 }
54
55 /**
56 * power_down - Shut machine down for hibernate.
57 *
58 * Use the platform driver, if configured so; otherwise try
59 * to power off or reboot.
60 */
61
62 static void power_down(void)
63 {
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;
79 }
80 }
81 kernel_halt();
82 /*
83 * Valid image is on the disk, if we continue we risk serious data
84 * corruption after resume.
85 */
86 printk(KERN_CRIT "Please power me down manually\n");
87 while(1);
88 }
89
90 static inline void platform_finish(void)
91 {
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);
101 }
102 }
103
104 static void unprepare_processes(void)
105 {
106 thaw_processes();
107 pm_restore_console();
108 }
109
110 static int prepare_processes(void)
111 {
112 int error = 0;
113
114 pm_prepare_console();
115 if (freeze_processes()) {
116 error = -EBUSY;
117 unprepare_processes();
118 }
119 return error;
120 }
121
122 /**
123 * pm_suspend_disk - The granpappy of hibernation power management.
124 *
125 * If not, then call swsusp to do its thing, then figure out how
126 * to power down the system.
127 */
128
129 int pm_suspend_disk(void)
130 {
131 int error;
132
133 /* The snapshot device should not be opened while we're running */
134 if (!atomic_add_unless(&snapshot_device_available, -1, 0))
135 return -EBUSY;
136
137 /* Allocate memory management structures */
138 error = create_basic_memory_bitmaps();
139 if (error)
140 goto Exit;
141
142 error = prepare_processes();
143 if (error)
144 goto Finish;
145
146 if (pm_disk_mode == PM_DISK_TESTPROC) {
147 printk("swsusp debug: Waiting for 5 seconds.\n");
148 mdelay(5000);
149 goto Thaw;
150 }
151
152 /* Free memory before shutting down devices. */
153 error = swsusp_shrink_memory();
154 if (error)
155 goto Thaw;
156
157 error = platform_prepare();
158 if (error)
159 goto Thaw;
160
161 suspend_console();
162 error = device_suspend(PMSG_FREEZE);
163 if (error) {
164 printk(KERN_ERR "PM: Some devices failed to suspend\n");
165 goto Resume_devices;
166 }
167 error = disable_nonboot_cpus();
168 if (error)
169 goto Enable_cpus;
170
171 if (pm_disk_mode == PM_DISK_TEST) {
172 printk("swsusp debug: Waiting for 5 seconds.\n");
173 mdelay(5000);
174 goto Enable_cpus;
175 }
176
177 pr_debug("PM: snapshotting memory.\n");
178 in_suspend = 1;
179 error = swsusp_suspend();
180 if (error)
181 goto Enable_cpus;
182
183 if (in_suspend) {
184 enable_nonboot_cpus();
185 platform_finish();
186 device_resume();
187 resume_console();
188 pr_debug("PM: writing image.\n");
189 error = swsusp_write();
190 if (!error)
191 power_down();
192 else {
193 swsusp_free();
194 goto Thaw;
195 }
196 } else {
197 pr_debug("PM: Image restored successfully.\n");
198 }
199
200 swsusp_free();
201 Enable_cpus:
202 enable_nonboot_cpus();
203 Resume_devices:
204 platform_finish();
205 device_resume();
206 resume_console();
207 Thaw:
208 unprepare_processes();
209 Finish:
210 free_basic_memory_bitmaps();
211 Exit:
212 atomic_inc(&snapshot_device_available);
213 return error;
214 }
215
216
217 /**
218 * software_resume - Resume from a saved image.
219 *
220 * Called as a late_initcall (so all devices are discovered and
221 * initialized), we call swsusp to see if we have a saved image or not.
222 * If so, we quiesce devices, the restore the saved image. We will
223 * return above (in pm_suspend_disk() ) if everything goes well.
224 * Otherwise, we fail gracefully and return to the normally
225 * scheduled program.
226 *
227 */
228
229 static int software_resume(void)
230 {
231 int error;
232
233 mutex_lock(&pm_mutex);
234 if (!swsusp_resume_device) {
235 if (!strlen(resume_file)) {
236 mutex_unlock(&pm_mutex);
237 return -ENOENT;
238 }
239 swsusp_resume_device = name_to_dev_t(resume_file);
240 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
241 } else {
242 pr_debug("swsusp: Resume From Partition %d:%d\n",
243 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
244 }
245
246 if (noresume) {
247 /**
248 * FIXME: If noresume is specified, we need to find the partition
249 * and reset it back to normal swap space.
250 */
251 mutex_unlock(&pm_mutex);
252 return 0;
253 }
254
255 pr_debug("PM: Checking swsusp image.\n");
256 error = swsusp_check();
257 if (error)
258 goto Unlock;
259
260 /* The snapshot device should not be opened while we're running */
261 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
262 error = -EBUSY;
263 goto Unlock;
264 }
265
266 error = create_basic_memory_bitmaps();
267 if (error)
268 goto Finish;
269
270 pr_debug("PM: Preparing processes for restore.\n");
271 error = prepare_processes();
272 if (error) {
273 swsusp_close();
274 goto Done;
275 }
276
277 pr_debug("PM: Reading swsusp image.\n");
278
279 error = swsusp_read();
280 if (error) {
281 swsusp_free();
282 goto Thaw;
283 }
284
285 pr_debug("PM: Preparing devices for restore.\n");
286
287 suspend_console();
288 error = device_suspend(PMSG_PRETHAW);
289 if (error)
290 goto Free;
291
292 error = disable_nonboot_cpus();
293 if (!error)
294 swsusp_resume();
295
296 enable_nonboot_cpus();
297 Free:
298 swsusp_free();
299 device_resume();
300 resume_console();
301 Thaw:
302 printk(KERN_ERR "PM: Restore failed, recovering.\n");
303 unprepare_processes();
304 Done:
305 free_basic_memory_bitmaps();
306 Finish:
307 atomic_inc(&snapshot_device_available);
308 /* For success case, the suspend path will release the lock */
309 Unlock:
310 mutex_unlock(&pm_mutex);
311 pr_debug("PM: Resume from disk failed.\n");
312 return 0;
313 }
314
315 late_initcall(software_resume);
316
317
318 static const char * const pm_disk_modes[] = {
319 [PM_DISK_PLATFORM] = "platform",
320 [PM_DISK_SHUTDOWN] = "shutdown",
321 [PM_DISK_REBOOT] = "reboot",
322 [PM_DISK_TEST] = "test",
323 [PM_DISK_TESTPROC] = "testproc",
324 };
325
326 /**
327 * disk - Control suspend-to-disk mode
328 *
329 * Suspend-to-disk can be handled in several ways. We have a few options
330 * for putting the system to sleep - using the platform driver (e.g. ACPI
331 * or other pm_ops), powering off the system or rebooting the system
332 * (for testing) as well as the two test modes.
333 *
334 * The system can support 'platform', and that is known a priori (and
335 * encoded in pm_ops). However, the user may choose 'shutdown' or 'reboot'
336 * as alternatives, as well as the test modes 'test' and 'testproc'.
337 *
338 * show() will display what the mode is currently set to.
339 * store() will accept one of
340 *
341 * 'platform'
342 * 'shutdown'
343 * 'reboot'
344 * 'test'
345 * 'testproc'
346 *
347 * It will only change to 'platform' if the system
348 * supports it (as determined from pm_ops->pm_disk_mode).
349 */
350
351 static ssize_t disk_show(struct kset *kset, char *buf)
352 {
353 int i;
354 char *start = buf;
355
356 for (i = PM_DISK_PLATFORM; i < PM_DISK_MAX; i++) {
357 if (!pm_disk_modes[i])
358 continue;
359 switch (i) {
360 case PM_DISK_SHUTDOWN:
361 case PM_DISK_REBOOT:
362 case PM_DISK_TEST:
363 case PM_DISK_TESTPROC:
364 break;
365 default:
366 if (pm_ops && pm_ops->enter &&
367 (i == pm_ops->pm_disk_mode))
368 break;
369 /* not a valid mode, continue with loop */
370 continue;
371 }
372 if (i == pm_disk_mode)
373 buf += sprintf(buf, "[%s]", pm_disk_modes[i]);
374 else
375 buf += sprintf(buf, "%s", pm_disk_modes[i]);
376 if (i+1 != PM_DISK_MAX)
377 buf += sprintf(buf, " ");
378 }
379 buf += sprintf(buf, "\n");
380 return buf-start;
381 }
382
383
384 static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
385 {
386 int error = 0;
387 int i;
388 int len;
389 char *p;
390 suspend_disk_method_t mode = 0;
391
392 p = memchr(buf, '\n', n);
393 len = p ? p - buf : n;
394
395 mutex_lock(&pm_mutex);
396 for (i = PM_DISK_PLATFORM; i < PM_DISK_MAX; i++) {
397 if (!strncmp(buf, pm_disk_modes[i], len)) {
398 mode = i;
399 break;
400 }
401 }
402 if (mode) {
403 switch (mode) {
404 case PM_DISK_SHUTDOWN:
405 case PM_DISK_REBOOT:
406 case PM_DISK_TEST:
407 case PM_DISK_TESTPROC:
408 pm_disk_mode = mode;
409 break;
410 default:
411 if (pm_ops && pm_ops->enter &&
412 (mode == pm_ops->pm_disk_mode))
413 pm_disk_mode = mode;
414 else
415 error = -EINVAL;
416 }
417 } else {
418 error = -EINVAL;
419 }
420
421 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
422 pm_disk_modes[mode]);
423 mutex_unlock(&pm_mutex);
424 return error ? error : n;
425 }
426
427 power_attr(disk);
428
429 static ssize_t resume_show(struct kset *kset, char *buf)
430 {
431 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
432 MINOR(swsusp_resume_device));
433 }
434
435 static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
436 {
437 unsigned int maj, min;
438 dev_t res;
439 int ret = -EINVAL;
440
441 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
442 goto out;
443
444 res = MKDEV(maj,min);
445 if (maj != MAJOR(res) || min != MINOR(res))
446 goto out;
447
448 mutex_lock(&pm_mutex);
449 swsusp_resume_device = res;
450 mutex_unlock(&pm_mutex);
451 printk("Attempting manual resume\n");
452 noresume = 0;
453 software_resume();
454 ret = n;
455 out:
456 return ret;
457 }
458
459 power_attr(resume);
460
461 static ssize_t image_size_show(struct kset *kset, char *buf)
462 {
463 return sprintf(buf, "%lu\n", image_size);
464 }
465
466 static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
467 {
468 unsigned long size;
469
470 if (sscanf(buf, "%lu", &size) == 1) {
471 image_size = size;
472 return n;
473 }
474
475 return -EINVAL;
476 }
477
478 power_attr(image_size);
479
480 static struct attribute * g[] = {
481 &disk_attr.attr,
482 &resume_attr.attr,
483 &image_size_attr.attr,
484 NULL,
485 };
486
487
488 static struct attribute_group attr_group = {
489 .attrs = g,
490 };
491
492
493 static int __init pm_disk_init(void)
494 {
495 return sysfs_create_group(&power_subsys.kobj, &attr_group);
496 }
497
498 core_initcall(pm_disk_init);
499
500
501 static int __init resume_setup(char *str)
502 {
503 if (noresume)
504 return 1;
505
506 strncpy( resume_file, str, 255 );
507 return 1;
508 }
509
510 static int __init resume_offset_setup(char *str)
511 {
512 unsigned long long offset;
513
514 if (noresume)
515 return 1;
516
517 if (sscanf(str, "%llu", &offset) == 1)
518 swsusp_resume_block = offset;
519
520 return 1;
521 }
522
523 static int __init noresume_setup(char *str)
524 {
525 noresume = 1;
526 return 1;
527 }
528
529 __setup("noresume", noresume_setup);
530 __setup("resume_offset=", resume_offset_setup);
531 __setup("resume=", resume_setup);
This page took 0.04294 seconds and 6 git commands to generate.