ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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  *
7  * This file is released under the GPLv2.
8  *
9  */
10
11 #define DEBUG
12
13
14 #include <linux/suspend.h>
15 #include <linux/syscalls.h>
16 #include <linux/reboot.h>
17 #include <linux/string.h>
18 #include <linux/delay.h>
19 #include <linux/fs.h>
20 #include "power.h"
21
22
23 extern u32 pm_disk_mode;
24 extern struct pm_ops * pm_ops;
25
26 extern int pmdisk_save(void);
27 extern int pmdisk_write(void);
28 extern int pmdisk_read(void);
29 extern int pmdisk_restore(void);
30 extern int pmdisk_free(void);
31
32
33 /**
34  *      power_down - Shut machine down for hibernate.
35  *      @mode:          Suspend-to-disk mode
36  *
37  *      Use the platform driver, if configured so, and return gracefully if it
38  *      fails.
39  *      Otherwise, try to power off and reboot. If they fail, halt the machine,
40  *      there ain't no turning back.
41  */
42
43 static int power_down(u32 mode)
44 {
45         unsigned long flags;
46         int error = 0;
47
48         local_irq_save(flags);
49         device_power_down(PM_SUSPEND_DISK);
50         switch(mode) {
51         case PM_DISK_PLATFORM:
52                 error = pm_ops->enter(PM_SUSPEND_DISK);
53                 break;
54         case PM_DISK_SHUTDOWN:
55                 printk("Powering off system\n");
56                 machine_power_off();
57                 break;
58         case PM_DISK_REBOOT:
59                 machine_restart(NULL);
60                 break;
61         }
62         machine_halt();
63         device_power_up();
64         local_irq_restore(flags);
65         return 0;
66 }
67
68
69 static int in_suspend __nosavedata = 0;
70
71
72 /**
73  *      free_some_memory -  Try to free as much memory as possible
74  *
75  *      ... but do not OOM-kill anyone
76  *
77  *      Notice: all userland should be stopped at this point, or
78  *      livelock is possible.
79  */
80
81 static void free_some_memory(void)
82 {
83         printk("Freeing memory: ");
84         while (shrink_all_memory(10000))
85                 printk(".");
86         printk("|\n");
87 }
88
89
90 static inline void platform_finish(void)
91 {
92         if (pm_disk_mode == PM_DISK_PLATFORM) {
93                 if (pm_ops && pm_ops->finish)
94                         pm_ops->finish(PM_SUSPEND_DISK);
95         }
96 }
97
98 static void finish(void)
99 {
100         device_resume();
101         platform_finish();
102         thaw_processes();
103         pm_restore_console();
104 }
105
106
107 static int prepare(void)
108 {
109         int error;
110
111         pm_prepare_console();
112
113         sys_sync();
114         if (freeze_processes()) {
115                 error = -EBUSY;
116                 goto Thaw;
117         }
118
119         if (pm_disk_mode == PM_DISK_PLATFORM) {
120                 if (pm_ops && pm_ops->prepare) {
121                         if ((error = pm_ops->prepare(PM_SUSPEND_DISK)))
122                                 goto Thaw;
123                 }
124         }
125
126         /* Free memory before shutting down devices. */
127         free_some_memory();
128
129         if ((error = device_suspend(PM_SUSPEND_DISK)))
130                 goto Finish;
131
132         return 0;
133  Finish:
134         platform_finish();
135  Thaw:
136         thaw_processes();
137         pm_restore_console();
138         return error;
139 }
140
141
142 /**
143  *      pm_suspend_disk - The granpappy of power management.
144  *
145  *      If we're going through the firmware, then get it over with quickly.
146  *
147  *      If not, then call pmdis to do it's thing, then figure out how
148  *      to power down the system.
149  */
150
151 int pm_suspend_disk(void)
152 {
153         int error;
154
155         if ((error = prepare()))
156                 return error;
157
158         pr_debug("PM: Attempting to suspend to disk.\n");
159         if (pm_disk_mode == PM_DISK_FIRMWARE)
160                 return pm_ops->enter(PM_SUSPEND_DISK);
161
162         pr_debug("PM: snapshotting memory.\n");
163         in_suspend = 1;
164         if ((error = pmdisk_save()))
165                 goto Done;
166
167         if (in_suspend) {
168                 pr_debug("PM: writing image.\n");
169
170                 /*
171                  * FIXME: Leftover from swsusp. Are they necessary?
172                  */
173                 mb();
174                 barrier();
175
176                 error = pmdisk_write();
177                 if (!error) {
178                         error = power_down(pm_disk_mode);
179                         pr_debug("PM: Power down failed.\n");
180                 }
181         } else
182                 pr_debug("PM: Image restored successfully.\n");
183         pmdisk_free();
184  Done:
185         finish();
186         return error;
187 }
188
189
190 /**
191  *      pm_resume - Resume from a saved image.
192  *
193  *      Called as a late_initcall (so all devices are discovered and
194  *      initialized), we call pmdisk to see if we have a saved image or not.
195  *      If so, we quiesce devices, the restore the saved image. We will
196  *      return above (in pm_suspend_disk() ) if everything goes well.
197  *      Otherwise, we fail gracefully and return to the normally
198  *      scheduled program.
199  *
200  */
201
202 static int pm_resume(void)
203 {
204         int error;
205
206         pr_debug("PM: Reading pmdisk image.\n");
207
208         if ((error = pmdisk_read()))
209                 goto Done;
210
211         pr_debug("PM: Preparing system for restore.\n");
212
213         if ((error = prepare()))
214                 goto Free;
215
216         barrier();
217         mb();
218
219         /* FIXME: The following (comment and mdelay()) are from swsusp.
220          * Are they really necessary?
221          *
222          * We do not want some readahead with DMA to corrupt our memory, right?
223          * Do it with disabled interrupts for best effect. That way, if some
224          * driver scheduled DMA, we have good chance for DMA to finish ;-).
225          */
226         pr_debug("PM: Waiting for DMAs to settle down.\n");
227         mdelay(1000);
228
229         pr_debug("PM: Restoring saved image.\n");
230         pmdisk_restore();
231         pr_debug("PM: Restore failed, recovering.n");
232         finish();
233  Free:
234         pmdisk_free();
235  Done:
236         pr_debug("PM: Resume from disk failed.\n");
237         return 0;
238 }
239
240 late_initcall(pm_resume);
241
242
243 static char * pm_disk_modes[] = {
244         [PM_DISK_FIRMWARE]      = "firmware",
245         [PM_DISK_PLATFORM]      = "platform",
246         [PM_DISK_SHUTDOWN]      = "shutdown",
247         [PM_DISK_REBOOT]        = "reboot",
248 };
249
250 /**
251  *      disk - Control suspend-to-disk mode
252  *
253  *      Suspend-to-disk can be handled in several ways. The greatest
254  *      distinction is who writes memory to disk - the firmware or the OS.
255  *      If the firmware does it, we assume that it also handles suspending
256  *      the system.
257  *      If the OS does it, then we have three options for putting the system
258  *      to sleep - using the platform driver (e.g. ACPI or other PM registers),
259  *      powering off the system or rebooting the system (for testing).
260  *
261  *      The system will support either 'firmware' or 'platform', and that is
262  *      known a priori (and encoded in pm_ops). But, the user may choose
263  *      'shutdown' or 'reboot' as alternatives.
264  *
265  *      show() will display what the mode is currently set to.
266  *      store() will accept one of
267  *
268  *      'firmware'
269  *      'platform'
270  *      'shutdown'
271  *      'reboot'
272  *
273  *      It will only change to 'firmware' or 'platform' if the system
274  *      supports it (as determined from pm_ops->pm_disk_mode).
275  */
276
277 static ssize_t disk_show(struct subsystem * subsys, char * buf)
278 {
279         return sprintf(buf,"%s\n",pm_disk_modes[pm_disk_mode]);
280 }
281
282
283 static ssize_t disk_store(struct subsystem * s, const char * buf, size_t n)
284 {
285         int error = 0;
286         int i;
287         int len;
288         char *p;
289         u32 mode = 0;
290
291         p = memchr(buf, '\n', n);
292         len = p ? p - buf : n;
293
294         down(&pm_sem);
295         for (i = PM_DISK_FIRMWARE; i < PM_DISK_MAX; i++) {
296                 if (!strncmp(buf, pm_disk_modes[i], len)) {
297                         mode = i;
298                         break;
299                 }
300         }
301         if (mode) {
302                 if (mode == PM_DISK_SHUTDOWN || mode == PM_DISK_REBOOT)
303                         pm_disk_mode = mode;
304                 else {
305                         if (pm_ops && pm_ops->enter &&
306                             (mode == pm_ops->pm_disk_mode))
307                                 pm_disk_mode = mode;
308                         else
309                                 error = -EINVAL;
310                 }
311         } else
312                 error = -EINVAL;
313
314         pr_debug("PM: suspend-to-disk mode set to '%s'\n",
315                  pm_disk_modes[mode]);
316         up(&pm_sem);
317         return error ? error : n;
318 }
319
320 power_attr(disk);
321
322 static struct attribute * g[] = {
323         &disk_attr.attr,
324         NULL,
325 };
326
327
328 static struct attribute_group attr_group = {
329         .attrs = g,
330 };
331
332
333 static int __init pm_disk_init(void)
334 {
335         return sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
336 }
337
338 core_initcall(pm_disk_init);