]> Pileus Git - ~andy/linux/blob - kernel/power/disk.c
[PATCH] swsusp: low level interface
[~andy/linux] / 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
22 #include "power.h"
23
24
25 extern suspend_disk_method_t pm_disk_mode;
26
27 extern int swsusp_shrink_memory(void);
28 extern int swsusp_suspend(void);
29 extern int swsusp_write(void);
30 extern int swsusp_check(void);
31 extern int swsusp_read(void);
32 extern void swsusp_close(void);
33 extern int swsusp_resume(void);
34
35
36 static int noresume = 0;
37 char resume_file[256] = CONFIG_PM_STD_PARTITION;
38 dev_t swsusp_resume_device;
39
40 /**
41  *      power_down - Shut machine down for hibernate.
42  *      @mode:          Suspend-to-disk mode
43  *
44  *      Use the platform driver, if configured so, and return gracefully if it
45  *      fails.
46  *      Otherwise, try to power off and reboot. If they fail, halt the machine,
47  *      there ain't no turning back.
48  */
49
50 static void power_down(suspend_disk_method_t mode)
51 {
52         int error = 0;
53
54         switch(mode) {
55         case PM_DISK_PLATFORM:
56                 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
57                 error = pm_ops->enter(PM_SUSPEND_DISK);
58                 break;
59         case PM_DISK_SHUTDOWN:
60                 kernel_power_off();
61                 break;
62         case PM_DISK_REBOOT:
63                 kernel_restart(NULL);
64                 break;
65         }
66         kernel_halt();
67         /* Valid image is on the disk, if we continue we risk serious data corruption
68            after resume. */
69         printk(KERN_CRIT "Please power me down manually\n");
70         while(1);
71 }
72
73 static inline void platform_finish(void)
74 {
75         if (pm_disk_mode == PM_DISK_PLATFORM) {
76                 if (pm_ops && pm_ops->finish)
77                         pm_ops->finish(PM_SUSPEND_DISK);
78         }
79 }
80
81 static int prepare_processes(void)
82 {
83         int error;
84
85         pm_prepare_console();
86         sys_sync();
87         disable_nonboot_cpus();
88
89         if (freeze_processes()) {
90                 error = -EBUSY;
91                 goto thaw;
92         }
93
94         /* Free memory before shutting down devices. */
95         if (!(error = swsusp_shrink_memory()))
96                 return 0;
97 thaw:
98         thaw_processes();
99         enable_nonboot_cpus();
100         pm_restore_console();
101         return error;
102 }
103
104 static void unprepare_processes(void)
105 {
106         platform_finish();
107         thaw_processes();
108         enable_nonboot_cpus();
109         pm_restore_console();
110 }
111
112 /**
113  *      pm_suspend_disk - The granpappy of power management.
114  *
115  *      If we're going through the firmware, then get it over with quickly.
116  *
117  *      If not, then call swsusp to do its thing, then figure out how
118  *      to power down the system.
119  */
120
121 int pm_suspend_disk(void)
122 {
123         int error;
124
125         error = prepare_processes();
126         if (error)
127                 return error;
128
129         error = device_suspend(PMSG_FREEZE);
130         if (error) {
131                 printk("Some devices failed to suspend\n");
132                 unprepare_processes();
133                 return error;
134         }
135
136         pr_debug("PM: snapshotting memory.\n");
137         in_suspend = 1;
138         if ((error = swsusp_suspend()))
139                 goto Done;
140
141         if (in_suspend) {
142                 device_resume();
143                 pr_debug("PM: writing image.\n");
144                 error = swsusp_write();
145                 if (!error)
146                         power_down(pm_disk_mode);
147                 else {
148                         swsusp_free();
149                         unprepare_processes();
150                         return error;
151                 }
152         } else
153                 pr_debug("PM: Image restored successfully.\n");
154
155         swsusp_free();
156  Done:
157         device_resume();
158         unprepare_processes();
159         return error;
160 }
161
162
163 /**
164  *      software_resume - Resume from a saved image.
165  *
166  *      Called as a late_initcall (so all devices are discovered and
167  *      initialized), we call swsusp to see if we have a saved image or not.
168  *      If so, we quiesce devices, the restore the saved image. We will
169  *      return above (in pm_suspend_disk() ) if everything goes well.
170  *      Otherwise, we fail gracefully and return to the normally
171  *      scheduled program.
172  *
173  */
174
175 static int software_resume(void)
176 {
177         int error;
178
179         down(&pm_sem);
180         if (!swsusp_resume_device) {
181                 if (!strlen(resume_file)) {
182                         up(&pm_sem);
183                         return -ENOENT;
184                 }
185                 swsusp_resume_device = name_to_dev_t(resume_file);
186                 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
187         } else {
188                 pr_debug("swsusp: Resume From Partition %d:%d\n",
189                          MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
190         }
191
192         if (noresume) {
193                 /**
194                  * FIXME: If noresume is specified, we need to find the partition
195                  * and reset it back to normal swap space.
196                  */
197                 up(&pm_sem);
198                 return 0;
199         }
200
201         pr_debug("PM: Checking swsusp image.\n");
202
203         if ((error = swsusp_check()))
204                 goto Done;
205
206         pr_debug("PM: Preparing processes for restore.\n");
207
208         if ((error = prepare_processes())) {
209                 swsusp_close();
210                 goto Done;
211         }
212
213         pr_debug("PM: Reading swsusp image.\n");
214
215         if ((error = swsusp_read())) {
216                 swsusp_free();
217                 goto Thaw;
218         }
219
220         pr_debug("PM: Preparing devices for restore.\n");
221
222         if ((error = device_suspend(PMSG_FREEZE))) {
223                 printk("Some devices failed to suspend\n");
224                 swsusp_free();
225                 goto Thaw;
226         }
227
228         mb();
229
230         pr_debug("PM: Restoring saved image.\n");
231         swsusp_resume();
232         pr_debug("PM: Restore failed, recovering.n");
233         device_resume();
234  Thaw:
235         unprepare_processes();
236  Done:
237         /* For success case, the suspend path will release the lock */
238         up(&pm_sem);
239         pr_debug("PM: Resume from disk failed.\n");
240         return 0;
241 }
242
243 late_initcall(software_resume);
244
245
246 static char * pm_disk_modes[] = {
247         [PM_DISK_FIRMWARE]      = "firmware",
248         [PM_DISK_PLATFORM]      = "platform",
249         [PM_DISK_SHUTDOWN]      = "shutdown",
250         [PM_DISK_REBOOT]        = "reboot",
251 };
252
253 /**
254  *      disk - Control suspend-to-disk mode
255  *
256  *      Suspend-to-disk can be handled in several ways. The greatest
257  *      distinction is who writes memory to disk - the firmware or the OS.
258  *      If the firmware does it, we assume that it also handles suspending
259  *      the system.
260  *      If the OS does it, then we have three options for putting the system
261  *      to sleep - using the platform driver (e.g. ACPI or other PM registers),
262  *      powering off the system or rebooting the system (for testing).
263  *
264  *      The system will support either 'firmware' or 'platform', and that is
265  *      known a priori (and encoded in pm_ops). But, the user may choose
266  *      'shutdown' or 'reboot' as alternatives.
267  *
268  *      show() will display what the mode is currently set to.
269  *      store() will accept one of
270  *
271  *      'firmware'
272  *      'platform'
273  *      'shutdown'
274  *      'reboot'
275  *
276  *      It will only change to 'firmware' or 'platform' if the system
277  *      supports it (as determined from pm_ops->pm_disk_mode).
278  */
279
280 static ssize_t disk_show(struct subsystem * subsys, char * buf)
281 {
282         return sprintf(buf, "%s\n", pm_disk_modes[pm_disk_mode]);
283 }
284
285
286 static ssize_t disk_store(struct subsystem * s, const char * buf, size_t n)
287 {
288         int error = 0;
289         int i;
290         int len;
291         char *p;
292         suspend_disk_method_t mode = 0;
293
294         p = memchr(buf, '\n', n);
295         len = p ? p - buf : n;
296
297         down(&pm_sem);
298         for (i = PM_DISK_FIRMWARE; i < PM_DISK_MAX; i++) {
299                 if (!strncmp(buf, pm_disk_modes[i], len)) {
300                         mode = i;
301                         break;
302                 }
303         }
304         if (mode) {
305                 if (mode == PM_DISK_SHUTDOWN || mode == PM_DISK_REBOOT)
306                         pm_disk_mode = mode;
307                 else {
308                         if (pm_ops && pm_ops->enter &&
309                             (mode == pm_ops->pm_disk_mode))
310                                 pm_disk_mode = mode;
311                         else
312                                 error = -EINVAL;
313                 }
314         } else
315                 error = -EINVAL;
316
317         pr_debug("PM: suspend-to-disk mode set to '%s'\n",
318                  pm_disk_modes[mode]);
319         up(&pm_sem);
320         return error ? error : n;
321 }
322
323 power_attr(disk);
324
325 static ssize_t resume_show(struct subsystem * subsys, char *buf)
326 {
327         return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
328                        MINOR(swsusp_resume_device));
329 }
330
331 static ssize_t resume_store(struct subsystem *subsys, const char *buf, size_t n)
332 {
333         unsigned int maj, min;
334         dev_t res;
335         int ret = -EINVAL;
336
337         if (sscanf(buf, "%u:%u", &maj, &min) != 2)
338                 goto out;
339
340         res = MKDEV(maj,min);
341         if (maj != MAJOR(res) || min != MINOR(res))
342                 goto out;
343
344         down(&pm_sem);
345         swsusp_resume_device = res;
346         up(&pm_sem);
347         printk("Attempting manual resume\n");
348         noresume = 0;
349         software_resume();
350         ret = n;
351 out:
352         return ret;
353 }
354
355 power_attr(resume);
356
357 static ssize_t image_size_show(struct subsystem * subsys, char *buf)
358 {
359         return sprintf(buf, "%lu\n", image_size);
360 }
361
362 static ssize_t image_size_store(struct subsystem * subsys, const char * buf, size_t n)
363 {
364         unsigned long size;
365
366         if (sscanf(buf, "%lu", &size) == 1) {
367                 image_size = size;
368                 return n;
369         }
370
371         return -EINVAL;
372 }
373
374 power_attr(image_size);
375
376 static struct attribute * g[] = {
377         &disk_attr.attr,
378         &resume_attr.attr,
379         &image_size_attr.attr,
380         NULL,
381 };
382
383
384 static struct attribute_group attr_group = {
385         .attrs = g,
386 };
387
388
389 static int __init pm_disk_init(void)
390 {
391         return sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
392 }
393
394 core_initcall(pm_disk_init);
395
396
397 static int __init resume_setup(char *str)
398 {
399         if (noresume)
400                 return 1;
401
402         strncpy( resume_file, str, 255 );
403         return 1;
404 }
405
406 static int __init noresume_setup(char *str)
407 {
408         noresume = 1;
409         return 1;
410 }
411
412 __setup("noresume", noresume_setup);
413 __setup("resume=", resume_setup);