]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - kernel/power/disk.c
c3f0e61365dd3a5e8e1b03252ace6110e187a352
[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 platform_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 platform_hibernation_ops *ops)
56 {
57         if (ops && !(ops->start && ops->pre_snapshot && ops->finish
58             && ops->prepare && ops->enter && ops->pre_restore
59             && ops->restore_cleanup)) {
60                 WARN_ON(1);
61                 return;
62         }
63         mutex_lock(&pm_mutex);
64         hibernation_ops = ops;
65         if (ops)
66                 hibernation_mode = HIBERNATION_PLATFORM;
67         else if (hibernation_mode == HIBERNATION_PLATFORM)
68                 hibernation_mode = HIBERNATION_SHUTDOWN;
69
70         mutex_unlock(&pm_mutex);
71 }
72
73 /**
74  *      platform_start - tell the platform driver that we're starting
75  *      hibernation
76  */
77
78 static int platform_start(int platform_mode)
79 {
80         return (platform_mode && hibernation_ops) ?
81                 hibernation_ops->start() : 0;
82 }
83
84 /**
85  *      platform_pre_snapshot - prepare the machine for hibernation using the
86  *      platform driver if so configured and return an error code if it fails
87  */
88
89 static int platform_pre_snapshot(int platform_mode)
90 {
91         return (platform_mode && hibernation_ops) ?
92                 hibernation_ops->pre_snapshot() : 0;
93 }
94
95 /**
96  *      platform_leave - prepare the machine for switching to the normal mode
97  *      of operation using the platform driver (called with interrupts disabled)
98  */
99
100 static void platform_leave(int platform_mode)
101 {
102         if (platform_mode && hibernation_ops)
103                 hibernation_ops->leave();
104 }
105
106 /**
107  *      platform_finish - switch the machine to the normal mode of operation
108  *      using the platform driver (must be called after platform_prepare())
109  */
110
111 static void platform_finish(int platform_mode)
112 {
113         if (platform_mode && hibernation_ops)
114                 hibernation_ops->finish();
115 }
116
117 /**
118  *      platform_pre_restore - prepare the platform for the restoration from a
119  *      hibernation image.  If the restore fails after this function has been
120  *      called, platform_restore_cleanup() must be called.
121  */
122
123 static int platform_pre_restore(int platform_mode)
124 {
125         return (platform_mode && hibernation_ops) ?
126                 hibernation_ops->pre_restore() : 0;
127 }
128
129 /**
130  *      platform_restore_cleanup - switch the platform to the normal mode of
131  *      operation after a failing restore.  If platform_pre_restore() has been
132  *      called before the failing restore, this function must be called too,
133  *      regardless of the result of platform_pre_restore().
134  */
135
136 static void platform_restore_cleanup(int platform_mode)
137 {
138         if (platform_mode && hibernation_ops)
139                 hibernation_ops->restore_cleanup();
140 }
141
142 /**
143  *      create_image - freeze devices that need to be frozen with interrupts
144  *      off, create the hibernation image and thaw those devices.  Control
145  *      reappears in this routine after a restore.
146  */
147
148 int create_image(int platform_mode)
149 {
150         int error;
151
152         error = arch_prepare_suspend();
153         if (error)
154                 return error;
155
156         local_irq_disable();
157         /* At this point, device_suspend() has been called, but *not*
158          * device_power_down(). We *must* call device_power_down() now.
159          * Otherwise, drivers for some devices (e.g. interrupt controllers)
160          * become desynchronized with the actual state of the hardware
161          * at resume time, and evil weirdness ensues.
162          */
163         error = device_power_down(PMSG_FREEZE);
164         if (error) {
165                 printk(KERN_ERR "Some devices failed to power down, "
166                         KERN_ERR "aborting suspend\n");
167                 goto Enable_irqs;
168         }
169
170         save_processor_state();
171         error = swsusp_arch_suspend();
172         if (error)
173                 printk(KERN_ERR "Error %d while creating the image\n", error);
174         /* Restore control flow magically appears here */
175         restore_processor_state();
176         if (!in_suspend)
177                 platform_leave(platform_mode);
178         /* NOTE:  device_power_up() is just a resume() for devices
179          * that suspended with irqs off ... no overall powerup.
180          */
181         device_power_up();
182  Enable_irqs:
183         local_irq_enable();
184         return error;
185 }
186
187 /**
188  *      hibernation_snapshot - quiesce devices and create the hibernation
189  *      snapshot image.
190  *      @platform_mode - if set, use the platform driver, if available, to
191  *                       prepare the platform frimware for the power transition.
192  *
193  *      Must be called with pm_mutex held
194  */
195
196 int hibernation_snapshot(int platform_mode)
197 {
198         int error;
199
200         /* Free memory before shutting down devices. */
201         error = swsusp_shrink_memory();
202         if (error)
203                 return error;
204
205         error = platform_start(platform_mode);
206         if (error)
207                 return error;
208
209         suspend_console();
210         error = device_suspend(PMSG_FREEZE);
211         if (error)
212                 goto Resume_console;
213
214         error = platform_pre_snapshot(platform_mode);
215         if (error)
216                 goto Resume_devices;
217
218         error = disable_nonboot_cpus();
219         if (!error) {
220                 if (hibernation_mode != HIBERNATION_TEST) {
221                         in_suspend = 1;
222                         error = create_image(platform_mode);
223                         /* Control returns here after successful restore */
224                 } else {
225                         printk("swsusp debug: Waiting for 5 seconds.\n");
226                         mdelay(5000);
227                 }
228         }
229         enable_nonboot_cpus();
230  Resume_devices:
231         platform_finish(platform_mode);
232         device_resume();
233  Resume_console:
234         resume_console();
235         return error;
236 }
237
238 /**
239  *      hibernation_restore - quiesce devices and restore the hibernation
240  *      snapshot image.  If successful, control returns in hibernation_snaphot()
241  *      @platform_mode - if set, use the platform driver, if available, to
242  *                       prepare the platform frimware for the transition.
243  *
244  *      Must be called with pm_mutex held
245  */
246
247 int hibernation_restore(int platform_mode)
248 {
249         int error;
250
251         pm_prepare_console();
252         suspend_console();
253         error = device_suspend(PMSG_PRETHAW);
254         if (error)
255                 goto Finish;
256
257         error = platform_pre_restore(platform_mode);
258         if (!error) {
259                 error = disable_nonboot_cpus();
260                 if (!error)
261                         error = swsusp_resume();
262                 enable_nonboot_cpus();
263         }
264         platform_restore_cleanup(platform_mode);
265         device_resume();
266  Finish:
267         resume_console();
268         pm_restore_console();
269         return error;
270 }
271
272 /**
273  *      hibernation_platform_enter - enter the hibernation state using the
274  *      platform driver (if available)
275  */
276
277 int hibernation_platform_enter(void)
278 {
279         int error;
280
281         if (!hibernation_ops)
282                 return -ENOSYS;
283
284         /*
285          * We have cancelled the power transition by running
286          * hibernation_ops->finish() before saving the image, so we should let
287          * the firmware know that we're going to enter the sleep state after all
288          */
289         error = hibernation_ops->start();
290         if (error)
291                 return error;
292
293         suspend_console();
294         error = device_suspend(PMSG_SUSPEND);
295         if (error)
296                 goto Resume_console;
297
298         error = hibernation_ops->prepare();
299         if (error)
300                 goto Resume_devices;
301
302         error = disable_nonboot_cpus();
303         if (error)
304                 goto Finish;
305
306         local_irq_disable();
307         error = device_power_down(PMSG_SUSPEND);
308         if (!error) {
309                 hibernation_ops->enter();
310                 /* We should never get here */
311                 while (1);
312         }
313         local_irq_enable();
314
315         /*
316          * We don't need to reenable the nonboot CPUs or resume consoles, since
317          * the system is going to be halted anyway.
318          */
319  Finish:
320         hibernation_ops->finish();
321  Resume_devices:
322         device_resume();
323  Resume_console:
324         resume_console();
325         return error;
326 }
327
328 /**
329  *      power_down - Shut the machine down for hibernation.
330  *
331  *      Use the platform driver, if configured so; otherwise try
332  *      to power off or reboot.
333  */
334
335 static void power_down(void)
336 {
337         switch (hibernation_mode) {
338         case HIBERNATION_TEST:
339         case HIBERNATION_TESTPROC:
340                 break;
341         case HIBERNATION_REBOOT:
342                 kernel_restart(NULL);
343                 break;
344         case HIBERNATION_PLATFORM:
345                 hibernation_platform_enter();
346         case HIBERNATION_SHUTDOWN:
347                 kernel_power_off();
348                 break;
349         }
350         kernel_halt();
351         /*
352          * Valid image is on the disk, if we continue we risk serious data
353          * corruption after resume.
354          */
355         printk(KERN_CRIT "Please power me down manually\n");
356         while(1);
357 }
358
359 static void unprepare_processes(void)
360 {
361         thaw_processes();
362         pm_restore_console();
363 }
364
365 static int prepare_processes(void)
366 {
367         int error = 0;
368
369         pm_prepare_console();
370         if (freeze_processes()) {
371                 error = -EBUSY;
372                 unprepare_processes();
373         }
374         return error;
375 }
376
377 /**
378  *      hibernate - The granpappy of the built-in hibernation management
379  */
380
381 int hibernate(void)
382 {
383         int error;
384
385         mutex_lock(&pm_mutex);
386         /* The snapshot device should not be opened while we're running */
387         if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
388                 error = -EBUSY;
389                 goto Unlock;
390         }
391
392         error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
393         if (error)
394                 goto Exit;
395
396         /* Allocate memory management structures */
397         error = create_basic_memory_bitmaps();
398         if (error)
399                 goto Exit;
400
401         printk("Syncing filesystems ... ");
402         sys_sync();
403         printk("done.\n");
404
405         error = prepare_processes();
406         if (error)
407                 goto Finish;
408
409         if (hibernation_mode == HIBERNATION_TESTPROC) {
410                 printk("swsusp debug: Waiting for 5 seconds.\n");
411                 mdelay(5000);
412                 goto Thaw;
413         }
414         error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
415         if (in_suspend && !error) {
416                 unsigned int flags = 0;
417
418                 if (hibernation_mode == HIBERNATION_PLATFORM)
419                         flags |= SF_PLATFORM_MODE;
420                 pr_debug("PM: writing image.\n");
421                 error = swsusp_write(flags);
422                 swsusp_free();
423                 if (!error)
424                         power_down();
425         } else {
426                 pr_debug("PM: Image restored successfully.\n");
427                 swsusp_free();
428         }
429  Thaw:
430         unprepare_processes();
431  Finish:
432         free_basic_memory_bitmaps();
433  Exit:
434         pm_notifier_call_chain(PM_POST_HIBERNATION);
435         atomic_inc(&snapshot_device_available);
436  Unlock:
437         mutex_unlock(&pm_mutex);
438         return error;
439 }
440
441
442 /**
443  *      software_resume - Resume from a saved image.
444  *
445  *      Called as a late_initcall (so all devices are discovered and
446  *      initialized), we call swsusp to see if we have a saved image or not.
447  *      If so, we quiesce devices, the restore the saved image. We will
448  *      return above (in hibernate() ) if everything goes well.
449  *      Otherwise, we fail gracefully and return to the normally
450  *      scheduled program.
451  *
452  */
453
454 static int software_resume(void)
455 {
456         int error;
457         unsigned int flags;
458
459         /*
460          * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
461          * is configured into the kernel. Since the regular hibernate
462          * trigger path is via sysfs which takes a buffer mutex before
463          * calling hibernate functions (which take pm_mutex) this can
464          * cause lockdep to complain about a possible ABBA deadlock
465          * which cannot happen since we're in the boot code here and
466          * sysfs can't be invoked yet. Therefore, we use a subclass
467          * here to avoid lockdep complaining.
468          */
469         mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
470         if (!swsusp_resume_device) {
471                 if (!strlen(resume_file)) {
472                         mutex_unlock(&pm_mutex);
473                         return -ENOENT;
474                 }
475                 swsusp_resume_device = name_to_dev_t(resume_file);
476                 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
477         } else {
478                 pr_debug("swsusp: Resume From Partition %d:%d\n",
479                          MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
480         }
481
482         if (noresume) {
483                 /**
484                  * FIXME: If noresume is specified, we need to find the partition
485                  * and reset it back to normal swap space.
486                  */
487                 mutex_unlock(&pm_mutex);
488                 return 0;
489         }
490
491         pr_debug("PM: Checking swsusp image.\n");
492         error = swsusp_check();
493         if (error)
494                 goto Unlock;
495
496         /* The snapshot device should not be opened while we're running */
497         if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
498                 error = -EBUSY;
499                 goto Unlock;
500         }
501
502         error = create_basic_memory_bitmaps();
503         if (error)
504                 goto Finish;
505
506         pr_debug("PM: Preparing processes for restore.\n");
507         error = prepare_processes();
508         if (error) {
509                 swsusp_close();
510                 goto Done;
511         }
512
513         pr_debug("PM: Reading swsusp image.\n");
514
515         error = swsusp_read(&flags);
516         if (!error)
517                 hibernation_restore(flags & SF_PLATFORM_MODE);
518
519         printk(KERN_ERR "PM: Restore failed, recovering.\n");
520         swsusp_free();
521         unprepare_processes();
522  Done:
523         free_basic_memory_bitmaps();
524  Finish:
525         atomic_inc(&snapshot_device_available);
526         /* For success case, the suspend path will release the lock */
527  Unlock:
528         mutex_unlock(&pm_mutex);
529         pr_debug("PM: Resume from disk failed.\n");
530         return error;
531 }
532
533 late_initcall(software_resume);
534
535
536 static const char * const hibernation_modes[] = {
537         [HIBERNATION_PLATFORM]  = "platform",
538         [HIBERNATION_SHUTDOWN]  = "shutdown",
539         [HIBERNATION_REBOOT]    = "reboot",
540         [HIBERNATION_TEST]      = "test",
541         [HIBERNATION_TESTPROC]  = "testproc",
542 };
543
544 /**
545  *      disk - Control hibernation mode
546  *
547  *      Suspend-to-disk can be handled in several ways. We have a few options
548  *      for putting the system to sleep - using the platform driver (e.g. ACPI
549  *      or other hibernation_ops), powering off the system or rebooting the
550  *      system (for testing) as well as the two test modes.
551  *
552  *      The system can support 'platform', and that is known a priori (and
553  *      encoded by the presence of hibernation_ops). However, the user may
554  *      choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
555  *      test modes, 'test' or 'testproc'.
556  *
557  *      show() will display what the mode is currently set to.
558  *      store() will accept one of
559  *
560  *      'platform'
561  *      'shutdown'
562  *      'reboot'
563  *      'test'
564  *      'testproc'
565  *
566  *      It will only change to 'platform' if the system
567  *      supports it (as determined by having hibernation_ops).
568  */
569
570 static ssize_t disk_show(struct kset *kset, char *buf)
571 {
572         int i;
573         char *start = buf;
574
575         for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
576                 if (!hibernation_modes[i])
577                         continue;
578                 switch (i) {
579                 case HIBERNATION_SHUTDOWN:
580                 case HIBERNATION_REBOOT:
581                 case HIBERNATION_TEST:
582                 case HIBERNATION_TESTPROC:
583                         break;
584                 case HIBERNATION_PLATFORM:
585                         if (hibernation_ops)
586                                 break;
587                         /* not a valid mode, continue with loop */
588                         continue;
589                 }
590                 if (i == hibernation_mode)
591                         buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
592                 else
593                         buf += sprintf(buf, "%s ", hibernation_modes[i]);
594         }
595         buf += sprintf(buf, "\n");
596         return buf-start;
597 }
598
599
600 static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
601 {
602         int error = 0;
603         int i;
604         int len;
605         char *p;
606         int mode = HIBERNATION_INVALID;
607
608         p = memchr(buf, '\n', n);
609         len = p ? p - buf : n;
610
611         mutex_lock(&pm_mutex);
612         for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
613                 if (len == strlen(hibernation_modes[i])
614                     && !strncmp(buf, hibernation_modes[i], len)) {
615                         mode = i;
616                         break;
617                 }
618         }
619         if (mode != HIBERNATION_INVALID) {
620                 switch (mode) {
621                 case HIBERNATION_SHUTDOWN:
622                 case HIBERNATION_REBOOT:
623                 case HIBERNATION_TEST:
624                 case HIBERNATION_TESTPROC:
625                         hibernation_mode = mode;
626                         break;
627                 case HIBERNATION_PLATFORM:
628                         if (hibernation_ops)
629                                 hibernation_mode = mode;
630                         else
631                                 error = -EINVAL;
632                 }
633         } else
634                 error = -EINVAL;
635
636         if (!error)
637                 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
638                          hibernation_modes[mode]);
639         mutex_unlock(&pm_mutex);
640         return error ? error : n;
641 }
642
643 power_attr(disk);
644
645 static ssize_t resume_show(struct kset *kset, char *buf)
646 {
647         return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
648                        MINOR(swsusp_resume_device));
649 }
650
651 static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
652 {
653         unsigned int maj, min;
654         dev_t res;
655         int ret = -EINVAL;
656
657         if (sscanf(buf, "%u:%u", &maj, &min) != 2)
658                 goto out;
659
660         res = MKDEV(maj,min);
661         if (maj != MAJOR(res) || min != MINOR(res))
662                 goto out;
663
664         mutex_lock(&pm_mutex);
665         swsusp_resume_device = res;
666         mutex_unlock(&pm_mutex);
667         printk("Attempting manual resume\n");
668         noresume = 0;
669         software_resume();
670         ret = n;
671  out:
672         return ret;
673 }
674
675 power_attr(resume);
676
677 static ssize_t image_size_show(struct kset *kset, char *buf)
678 {
679         return sprintf(buf, "%lu\n", image_size);
680 }
681
682 static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
683 {
684         unsigned long size;
685
686         if (sscanf(buf, "%lu", &size) == 1) {
687                 image_size = size;
688                 return n;
689         }
690
691         return -EINVAL;
692 }
693
694 power_attr(image_size);
695
696 static struct attribute * g[] = {
697         &disk_attr.attr,
698         &resume_attr.attr,
699         &image_size_attr.attr,
700         NULL,
701 };
702
703
704 static struct attribute_group attr_group = {
705         .attrs = g,
706 };
707
708
709 static int __init pm_disk_init(void)
710 {
711         return sysfs_create_group(&power_kset->kobj, &attr_group);
712 }
713
714 core_initcall(pm_disk_init);
715
716
717 static int __init resume_setup(char *str)
718 {
719         if (noresume)
720                 return 1;
721
722         strncpy( resume_file, str, 255 );
723         return 1;
724 }
725
726 static int __init resume_offset_setup(char *str)
727 {
728         unsigned long long offset;
729
730         if (noresume)
731                 return 1;
732
733         if (sscanf(str, "%llu", &offset) == 1)
734                 swsusp_resume_block = offset;
735
736         return 1;
737 }
738
739 static int __init noresume_setup(char *str)
740 {
741         noresume = 1;
742         return 1;
743 }
744
745 __setup("noresume", noresume_setup);
746 __setup("resume_offset=", resume_offset_setup);
747 __setup("resume=", resume_setup);