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