[PATCH] swsusp: low level interface
[deliverable/linux.git] / kernel / power / disk.c
CommitLineData
1da177e4
LT
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>
d53d9f16 19#include <linux/mount.h>
88d10bba 20#include <linux/pm.h>
d53d9f16 21
1da177e4
LT
22#include "power.h"
23
24
25extern suspend_disk_method_t pm_disk_mode;
1da177e4 26
72a97e08 27extern int swsusp_shrink_memory(void);
1da177e4 28extern int swsusp_suspend(void);
f577eb30 29extern int swsusp_write(void);
1da177e4 30extern int swsusp_check(void);
f577eb30 31extern int swsusp_read(void);
1da177e4
LT
32extern void swsusp_close(void);
33extern int swsusp_resume(void);
1da177e4
LT
34
35
36static int noresume = 0;
37char resume_file[256] = CONFIG_PM_STD_PARTITION;
38dev_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
50static void power_down(suspend_disk_method_t mode)
51{
1da177e4
LT
52 int error = 0;
53
1da177e4
LT
54 switch(mode) {
55 case PM_DISK_PLATFORM:
729b4d4c 56 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
1da177e4
LT
57 error = pm_ops->enter(PM_SUSPEND_DISK);
58 break;
59 case PM_DISK_SHUTDOWN:
fdde86ac 60 kernel_power_off();
1da177e4
LT
61 break;
62 case PM_DISK_REBOOT:
fdde86ac 63 kernel_restart(NULL);
1da177e4
LT
64 break;
65 }
fdde86ac 66 kernel_halt();
1da177e4
LT
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
1da177e4
LT
73static 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
1da177e4
LT
81static int prepare_processes(void)
82{
83 int error;
84
85 pm_prepare_console();
1da177e4 86 sys_sync();
5a72e04d
LS
87 disable_nonboot_cpus();
88
1da177e4
LT
89 if (freeze_processes()) {
90 error = -EBUSY;
5a72e04d 91 goto thaw;
1da177e4
LT
92 }
93
1da177e4 94 /* Free memory before shutting down devices. */
72a97e08
RW
95 if (!(error = swsusp_shrink_memory()))
96 return 0;
5a72e04d
LS
97thaw:
98 thaw_processes();
99 enable_nonboot_cpus();
100 pm_restore_console();
101 return error;
1da177e4
LT
102}
103
104static void unprepare_processes(void)
105{
5a72e04d 106 platform_finish();
1da177e4 107 thaw_processes();
5a72e04d 108 enable_nonboot_cpus();
1da177e4
LT
109 pm_restore_console();
110}
111
1da177e4
LT
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
121int pm_suspend_disk(void)
122{
123 int error;
124
125 error = prepare_processes();
5a72e04d
LS
126 if (error)
127 return error;
1da177e4 128
99dc7d63 129 error = device_suspend(PMSG_FREEZE);
1da177e4 130 if (error) {
99dc7d63 131 printk("Some devices failed to suspend\n");
1da177e4
LT
132 unprepare_processes();
133 return error;
134 }
135
1da177e4
LT
136 pr_debug("PM: snapshotting memory.\n");
137 in_suspend = 1;
138 if ((error = swsusp_suspend()))
139 goto Done;
140
141 if (in_suspend) {
0245b3e7 142 device_resume();
1da177e4 143 pr_debug("PM: writing image.\n");
f577eb30 144 error = swsusp_write();
1da177e4
LT
145 if (!error)
146 power_down(pm_disk_mode);
99dc7d63 147 else {
99dc7d63
PM
148 swsusp_free();
149 unprepare_processes();
150 return error;
151 }
1da177e4
LT
152 } else
153 pr_debug("PM: Image restored successfully.\n");
99dc7d63 154
1da177e4
LT
155 swsusp_free();
156 Done:
99dc7d63
PM
157 device_resume();
158 unprepare_processes();
1da177e4
LT
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
175static int software_resume(void)
176{
177 int error;
178
dd5d666b 179 down(&pm_sem);
3efa147a 180 if (!swsusp_resume_device) {
dd5d666b
SL
181 if (!strlen(resume_file)) {
182 up(&pm_sem);
3efa147a 183 return -ENOENT;
dd5d666b 184 }
3efa147a
PM
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
1da177e4
LT
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 */
dd5d666b 197 up(&pm_sem);
1da177e4
LT
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();
5a72e04d 210 goto Done;
1da177e4
LT
211 }
212
213 pr_debug("PM: Reading swsusp image.\n");
214
f577eb30 215 if ((error = swsusp_read())) {
2c1b4a5c
RW
216 swsusp_free();
217 goto Thaw;
218 }
1da177e4
LT
219
220 pr_debug("PM: Preparing devices for restore.\n");
221
99dc7d63
PM
222 if ((error = device_suspend(PMSG_FREEZE))) {
223 printk("Some devices failed to suspend\n");
2c1b4a5c
RW
224 swsusp_free();
225 goto Thaw;
99dc7d63 226 }
1da177e4
LT
227
228 mb();
229
230 pr_debug("PM: Restoring saved image.\n");
231 swsusp_resume();
232 pr_debug("PM: Restore failed, recovering.n");
99dc7d63 233 device_resume();
2c1b4a5c 234 Thaw:
1da177e4
LT
235 unprepare_processes();
236 Done:
dd5d666b
SL
237 /* For success case, the suspend path will release the lock */
238 up(&pm_sem);
1da177e4
LT
239 pr_debug("PM: Resume from disk failed.\n");
240 return 0;
241}
242
243late_initcall(software_resume);
244
245
246static 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
280static 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
286static 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
323power_attr(disk);
324
325static 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
a576219a 331static ssize_t resume_store(struct subsystem *subsys, const char *buf, size_t n)
1da177e4 332{
1da177e4 333 unsigned int maj, min;
1da177e4 334 dev_t res;
a576219a 335 int ret = -EINVAL;
1da177e4 336
a576219a
AM
337 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
338 goto out;
1da177e4 339
a576219a
AM
340 res = MKDEV(maj,min);
341 if (maj != MAJOR(res) || min != MINOR(res))
342 goto out;
1da177e4 343
a576219a
AM
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;
351out:
352 return ret;
1da177e4
LT
353}
354
355power_attr(resume);
356
ca0aec0f
RW
357static ssize_t image_size_show(struct subsystem * subsys, char *buf)
358{
853609b6 359 return sprintf(buf, "%lu\n", image_size);
ca0aec0f
RW
360}
361
362static ssize_t image_size_store(struct subsystem * subsys, const char * buf, size_t n)
363{
853609b6 364 unsigned long size;
ca0aec0f 365
853609b6 366 if (sscanf(buf, "%lu", &size) == 1) {
ca0aec0f
RW
367 image_size = size;
368 return n;
369 }
370
371 return -EINVAL;
372}
373
374power_attr(image_size);
375
1da177e4
LT
376static struct attribute * g[] = {
377 &disk_attr.attr,
378 &resume_attr.attr,
ca0aec0f 379 &image_size_attr.attr,
1da177e4
LT
380 NULL,
381};
382
383
384static struct attribute_group attr_group = {
385 .attrs = g,
386};
387
388
389static int __init pm_disk_init(void)
390{
391 return sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
392}
393
394core_initcall(pm_disk_init);
395
396
397static 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
406static 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);
This page took 0.181067 seconds and 5 git commands to generate.