]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - kernel/power/disk.c
swsusp: remove code duplication between disk.c and user.c
[linux-2.6-omap-h63xx.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 enum {
34         HIBERNATION_INVALID,
35         HIBERNATION_PLATFORM,
36         HIBERNATION_TEST,
37         HIBERNATION_TESTPROC,
38         HIBERNATION_SHUTDOWN,
39         HIBERNATION_REBOOT,
40         /* keep last */
41         __HIBERNATION_AFTER_LAST
42 };
43 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
44 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
45
46 static int hibernation_mode = HIBERNATION_SHUTDOWN;
47
48 static struct hibernation_ops *hibernation_ops;
49
50 /**
51  * hibernation_set_ops - set the global hibernate operations
52  * @ops: the hibernation operations to use in subsequent hibernation transitions
53  */
54
55 void hibernation_set_ops(struct hibernation_ops *ops)
56 {
57         if (ops && !(ops->prepare && ops->enter && ops->finish)) {
58                 WARN_ON(1);
59                 return;
60         }
61         mutex_lock(&pm_mutex);
62         hibernation_ops = ops;
63         if (ops)
64                 hibernation_mode = HIBERNATION_PLATFORM;
65         else if (hibernation_mode == HIBERNATION_PLATFORM)
66                 hibernation_mode = HIBERNATION_SHUTDOWN;
67
68         mutex_unlock(&pm_mutex);
69 }
70
71
72 /**
73  *      platform_prepare - prepare the machine for hibernation using the
74  *      platform driver if so configured and return an error code if it fails
75  */
76
77 static int platform_prepare(int platform_mode)
78 {
79         return (platform_mode && hibernation_ops) ?
80                 hibernation_ops->prepare() : 0;
81 }
82
83 /**
84  *      platform_finish - switch the machine to the normal mode of operation
85  *      using the platform driver (must be called after platform_prepare())
86  */
87
88 static void platform_finish(int platform_mode)
89 {
90         if (platform_mode && hibernation_ops)
91                 hibernation_ops->finish();
92 }
93
94 /**
95  *      hibernation_snapshot - quiesce devices and create the hibernation
96  *      snapshot image.
97  *      @platform_mode - if set, use the platform driver, if available, to
98  *                       prepare the platform frimware for the power transition.
99  *
100  *      Must be called with pm_mutex held
101  */
102
103 int hibernation_snapshot(int platform_mode)
104 {
105         int error;
106
107         /* Free memory before shutting down devices. */
108         error = swsusp_shrink_memory();
109         if (error)
110                 goto Finish;
111
112         error = platform_prepare(platform_mode);
113         if (error)
114                 goto Finish;
115
116         suspend_console();
117         error = device_suspend(PMSG_FREEZE);
118         if (error)
119                 goto Resume_devices;
120
121         error = disable_nonboot_cpus();
122         if (!error) {
123                 if (hibernation_mode != HIBERNATION_TEST) {
124                         in_suspend = 1;
125                         error = swsusp_suspend();
126                         /* Control returns here after successful restore */
127                 } else {
128                         printk("swsusp debug: Waiting for 5 seconds.\n");
129                         mdelay(5000);
130                 }
131         }
132         enable_nonboot_cpus();
133  Resume_devices:
134         platform_finish(platform_mode);
135         device_resume();
136         resume_console();
137  Finish:
138         return error;
139 }
140
141 /**
142  *      hibernation_restore - quiesce devices and restore the hibernation
143  *      snapshot image.  If successful, control returns in hibernation_snaphot()
144  *
145  *      Must be called with pm_mutex held
146  */
147
148 int hibernation_restore(void)
149 {
150         int error;
151
152         pm_prepare_console();
153         suspend_console();
154         error = device_suspend(PMSG_PRETHAW);
155         if (error)
156                 goto Finish;
157
158         error = disable_nonboot_cpus();
159         if (!error)
160                 error = swsusp_resume();
161
162         enable_nonboot_cpus();
163  Finish:
164         device_resume();
165         resume_console();
166         pm_restore_console();
167         return error;
168 }
169
170 /**
171  *      hibernation_platform_enter - enter the hibernation state using the
172  *      platform driver (if available)
173  */
174
175 int hibernation_platform_enter(void)
176 {
177         if (hibernation_ops) {
178                 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
179                 return hibernation_ops->enter();
180         } else {
181                 return -ENOSYS;
182         }
183 }
184
185 /**
186  *      power_down - Shut the machine down for hibernation.
187  *
188  *      Use the platform driver, if configured so; otherwise try
189  *      to power off or reboot.
190  */
191
192 static void power_down(void)
193 {
194         switch (hibernation_mode) {
195         case HIBERNATION_TEST:
196         case HIBERNATION_TESTPROC:
197                 break;
198         case HIBERNATION_SHUTDOWN:
199                 kernel_power_off();
200                 break;
201         case HIBERNATION_REBOOT:
202                 kernel_restart(NULL);
203                 break;
204         case HIBERNATION_PLATFORM:
205                 hibernation_platform_enter();
206         }
207         kernel_halt();
208         /*
209          * Valid image is on the disk, if we continue we risk serious data
210          * corruption after resume.
211          */
212         printk(KERN_CRIT "Please power me down manually\n");
213         while(1);
214 }
215
216 static void unprepare_processes(void)
217 {
218         thaw_processes();
219         pm_restore_console();
220 }
221
222 static int prepare_processes(void)
223 {
224         int error = 0;
225
226         pm_prepare_console();
227         if (freeze_processes()) {
228                 error = -EBUSY;
229                 unprepare_processes();
230         }
231         return error;
232 }
233
234 /**
235  *      hibernate - The granpappy of the built-in hibernation management
236  */
237
238 int hibernate(void)
239 {
240         int error;
241
242         /* The snapshot device should not be opened while we're running */
243         if (!atomic_add_unless(&snapshot_device_available, -1, 0))
244                 return -EBUSY;
245
246         /* Allocate memory management structures */
247         error = create_basic_memory_bitmaps();
248         if (error)
249                 goto Exit;
250
251         error = prepare_processes();
252         if (error)
253                 goto Finish;
254
255         mutex_lock(&pm_mutex);
256         if (hibernation_mode == HIBERNATION_TESTPROC) {
257                 printk("swsusp debug: Waiting for 5 seconds.\n");
258                 mdelay(5000);
259                 goto Thaw;
260         }
261         error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
262         if (in_suspend && !error) {
263                 pr_debug("PM: writing image.\n");
264                 error = swsusp_write();
265                 swsusp_free();
266                 if (!error)
267                         power_down();
268         } else {
269                 pr_debug("PM: Image restored successfully.\n");
270                 swsusp_free();
271         }
272  Thaw:
273         mutex_unlock(&pm_mutex);
274         unprepare_processes();
275  Finish:
276         free_basic_memory_bitmaps();
277  Exit:
278         atomic_inc(&snapshot_device_available);
279         return error;
280 }
281
282
283 /**
284  *      software_resume - Resume from a saved image.
285  *
286  *      Called as a late_initcall (so all devices are discovered and
287  *      initialized), we call swsusp to see if we have a saved image or not.
288  *      If so, we quiesce devices, the restore the saved image. We will
289  *      return above (in hibernate() ) if everything goes well.
290  *      Otherwise, we fail gracefully and return to the normally
291  *      scheduled program.
292  *
293  */
294
295 static int software_resume(void)
296 {
297         int error;
298
299         mutex_lock(&pm_mutex);
300         if (!swsusp_resume_device) {
301                 if (!strlen(resume_file)) {
302                         mutex_unlock(&pm_mutex);
303                         return -ENOENT;
304                 }
305                 swsusp_resume_device = name_to_dev_t(resume_file);
306                 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
307         } else {
308                 pr_debug("swsusp: Resume From Partition %d:%d\n",
309                          MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
310         }
311
312         if (noresume) {
313                 /**
314                  * FIXME: If noresume is specified, we need to find the partition
315                  * and reset it back to normal swap space.
316                  */
317                 mutex_unlock(&pm_mutex);
318                 return 0;
319         }
320
321         pr_debug("PM: Checking swsusp image.\n");
322         error = swsusp_check();
323         if (error)
324                 goto Unlock;
325
326         /* The snapshot device should not be opened while we're running */
327         if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
328                 error = -EBUSY;
329                 goto Unlock;
330         }
331
332         error = create_basic_memory_bitmaps();
333         if (error)
334                 goto Finish;
335
336         pr_debug("PM: Preparing processes for restore.\n");
337         error = prepare_processes();
338         if (error) {
339                 swsusp_close();
340                 goto Done;
341         }
342
343         pr_debug("PM: Reading swsusp image.\n");
344
345         error = swsusp_read();
346         if (!error)
347                 hibernation_restore();
348
349         printk(KERN_ERR "PM: Restore failed, recovering.\n");
350         swsusp_free();
351         unprepare_processes();
352  Done:
353         free_basic_memory_bitmaps();
354  Finish:
355         atomic_inc(&snapshot_device_available);
356         /* For success case, the suspend path will release the lock */
357  Unlock:
358         mutex_unlock(&pm_mutex);
359         pr_debug("PM: Resume from disk failed.\n");
360         return error;
361 }
362
363 late_initcall(software_resume);
364
365
366 static const char * const hibernation_modes[] = {
367         [HIBERNATION_PLATFORM]  = "platform",
368         [HIBERNATION_SHUTDOWN]  = "shutdown",
369         [HIBERNATION_REBOOT]    = "reboot",
370         [HIBERNATION_TEST]      = "test",
371         [HIBERNATION_TESTPROC]  = "testproc",
372 };
373
374 /**
375  *      disk - Control hibernation mode
376  *
377  *      Suspend-to-disk can be handled in several ways. We have a few options
378  *      for putting the system to sleep - using the platform driver (e.g. ACPI
379  *      or other hibernation_ops), powering off the system or rebooting the
380  *      system (for testing) as well as the two test modes.
381  *
382  *      The system can support 'platform', and that is known a priori (and
383  *      encoded by the presence of hibernation_ops). However, the user may
384  *      choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
385  *      test modes, 'test' or 'testproc'.
386  *
387  *      show() will display what the mode is currently set to.
388  *      store() will accept one of
389  *
390  *      'platform'
391  *      'shutdown'
392  *      'reboot'
393  *      'test'
394  *      'testproc'
395  *
396  *      It will only change to 'platform' if the system
397  *      supports it (as determined by having hibernation_ops).
398  */
399
400 static ssize_t disk_show(struct kset *kset, char *buf)
401 {
402         int i;
403         char *start = buf;
404
405         for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
406                 if (!hibernation_modes[i])
407                         continue;
408                 switch (i) {
409                 case HIBERNATION_SHUTDOWN:
410                 case HIBERNATION_REBOOT:
411                 case HIBERNATION_TEST:
412                 case HIBERNATION_TESTPROC:
413                         break;
414                 case HIBERNATION_PLATFORM:
415                         if (hibernation_ops)
416                                 break;
417                         /* not a valid mode, continue with loop */
418                         continue;
419                 }
420                 if (i == hibernation_mode)
421                         buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
422                 else
423                         buf += sprintf(buf, "%s ", hibernation_modes[i]);
424         }
425         buf += sprintf(buf, "\n");
426         return buf-start;
427 }
428
429
430 static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
431 {
432         int error = 0;
433         int i;
434         int len;
435         char *p;
436         int mode = HIBERNATION_INVALID;
437
438         p = memchr(buf, '\n', n);
439         len = p ? p - buf : n;
440
441         mutex_lock(&pm_mutex);
442         for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
443                 if (len == strlen(hibernation_modes[i])
444                     && !strncmp(buf, hibernation_modes[i], len)) {
445                         mode = i;
446                         break;
447                 }
448         }
449         if (mode != HIBERNATION_INVALID) {
450                 switch (mode) {
451                 case HIBERNATION_SHUTDOWN:
452                 case HIBERNATION_REBOOT:
453                 case HIBERNATION_TEST:
454                 case HIBERNATION_TESTPROC:
455                         hibernation_mode = mode;
456                         break;
457                 case HIBERNATION_PLATFORM:
458                         if (hibernation_ops)
459                                 hibernation_mode = mode;
460                         else
461                                 error = -EINVAL;
462                 }
463         } else
464                 error = -EINVAL;
465
466         if (!error)
467                 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
468                          hibernation_modes[mode]);
469         mutex_unlock(&pm_mutex);
470         return error ? error : n;
471 }
472
473 power_attr(disk);
474
475 static ssize_t resume_show(struct kset *kset, char *buf)
476 {
477         return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
478                        MINOR(swsusp_resume_device));
479 }
480
481 static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
482 {
483         unsigned int maj, min;
484         dev_t res;
485         int ret = -EINVAL;
486
487         if (sscanf(buf, "%u:%u", &maj, &min) != 2)
488                 goto out;
489
490         res = MKDEV(maj,min);
491         if (maj != MAJOR(res) || min != MINOR(res))
492                 goto out;
493
494         mutex_lock(&pm_mutex);
495         swsusp_resume_device = res;
496         mutex_unlock(&pm_mutex);
497         printk("Attempting manual resume\n");
498         noresume = 0;
499         software_resume();
500         ret = n;
501  out:
502         return ret;
503 }
504
505 power_attr(resume);
506
507 static ssize_t image_size_show(struct kset *kset, char *buf)
508 {
509         return sprintf(buf, "%lu\n", image_size);
510 }
511
512 static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
513 {
514         unsigned long size;
515
516         if (sscanf(buf, "%lu", &size) == 1) {
517                 image_size = size;
518                 return n;
519         }
520
521         return -EINVAL;
522 }
523
524 power_attr(image_size);
525
526 static struct attribute * g[] = {
527         &disk_attr.attr,
528         &resume_attr.attr,
529         &image_size_attr.attr,
530         NULL,
531 };
532
533
534 static struct attribute_group attr_group = {
535         .attrs = g,
536 };
537
538
539 static int __init pm_disk_init(void)
540 {
541         return sysfs_create_group(&power_subsys.kobj, &attr_group);
542 }
543
544 core_initcall(pm_disk_init);
545
546
547 static int __init resume_setup(char *str)
548 {
549         if (noresume)
550                 return 1;
551
552         strncpy( resume_file, str, 255 );
553         return 1;
554 }
555
556 static int __init resume_offset_setup(char *str)
557 {
558         unsigned long long offset;
559
560         if (noresume)
561                 return 1;
562
563         if (sscanf(str, "%llu", &offset) == 1)
564                 swsusp_resume_block = offset;
565
566         return 1;
567 }
568
569 static int __init noresume_setup(char *str)
570 {
571         noresume = 1;
572         return 1;
573 }
574
575 __setup("noresume", noresume_setup);
576 __setup("resume_offset=", resume_offset_setup);
577 __setup("resume=", resume_setup);