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