blob: dee0ff40befd961bf4116472fe39b99cac65c83f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Andrew Mortond53d9f12005-07-12 13:58:07 -070019#include <linux/mount.h>
Eric W. Biederman88d10bb2005-09-22 21:43:46 -070020#include <linux/pm.h>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070021#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070022#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080023#include <linux/freezer.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "power.h"
26
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static int noresume = 0;
29char resume_file[256] = CONFIG_PM_STD_PARTITION;
30dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080031sector_t swsusp_resume_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/**
Stefan Seyfried8a05aac2006-12-06 20:34:21 -080034 * platform_prepare - prepare the machine for hibernation using the
35 * platform driver if so configured and return an error code if it fails
36 */
37
38static inline int platform_prepare(void)
39{
40 int error = 0;
41
42 if (pm_disk_mode == PM_DISK_PLATFORM) {
43 if (pm_ops && pm_ops->prepare)
44 error = pm_ops->prepare(PM_SUSPEND_DISK);
45 }
46 return error;
47}
48
49/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * power_down - Shut machine down for hibernate.
51 * @mode: Suspend-to-disk mode
52 *
53 * Use the platform driver, if configured so, and return gracefully if it
54 * fails.
55 * Otherwise, try to power off and reboot. If they fail, halt the machine,
56 * there ain't no turning back.
57 */
58
59static void power_down(suspend_disk_method_t mode)
60{
Rafael J. Wysocki94985132007-03-16 13:38:06 -080061 disable_nonboot_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 switch(mode) {
63 case PM_DISK_PLATFORM:
Rafael J. Wysocki9185cfa2006-11-01 13:23:14 +010064 if (pm_ops && pm_ops->enter) {
65 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
Len Browncfee47f2006-12-16 01:01:18 -050066 pm_ops->enter(PM_SUSPEND_DISK);
Rafael J. Wysocki9185cfa2006-11-01 13:23:14 +010067 break;
68 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 case PM_DISK_SHUTDOWN:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -060070 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 break;
72 case PM_DISK_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -060073 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 break;
75 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -060076 kernel_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 /* Valid image is on the disk, if we continue we risk serious data corruption
78 after resume. */
79 printk(KERN_CRIT "Please power me down manually\n");
80 while(1);
81}
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static inline void platform_finish(void)
84{
85 if (pm_disk_mode == PM_DISK_PLATFORM) {
86 if (pm_ops && pm_ops->finish)
87 pm_ops->finish(PM_SUSPEND_DISK);
88 }
89}
90
Rafael J. Wysockied746e32007-02-10 01:43:32 -080091static void unprepare_processes(void)
92{
93 thaw_processes();
94 pm_restore_console();
95}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static int prepare_processes(void)
98{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -080099 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 pm_prepare_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (freeze_processes()) {
103 error = -EBUSY;
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800104 unprepare_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700106 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/**
David Brownellf1cc0a82006-08-14 23:11:08 -0700110 * pm_suspend_disk - The granpappy of hibernation power management.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 *
112 * If we're going through the firmware, then get it over with quickly.
113 *
114 * If not, then call swsusp to do its thing, then figure out how
115 * to power down the system.
116 */
117
118int pm_suspend_disk(void)
119{
120 int error;
121
122 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700123 if (error)
124 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800126 if (pm_disk_mode == PM_DISK_TESTPROC) {
127 printk("swsusp debug: Waiting for 5 seconds.\n");
128 mdelay(5000);
129 goto Thaw;
130 }
131 /* Free memory before shutting down devices. */
132 error = swsusp_shrink_memory();
133 if (error)
134 goto Thaw;
135
136 error = platform_prepare();
137 if (error)
138 goto Thaw;
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800139
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700140 suspend_console();
Pavel Machek99dc7d62005-09-03 15:57:05 -0700141 error = device_suspend(PMSG_FREEZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (error) {
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800143 printk(KERN_ERR "PM: Some devices failed to suspend\n");
144 goto Resume_devices;
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800145 }
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800146 error = disable_nonboot_cpus();
147 if (error)
148 goto Enable_cpus;
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800149
150 if (pm_disk_mode == PM_DISK_TEST) {
151 printk("swsusp debug: Waiting for 5 seconds.\n");
152 mdelay(5000);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800153 goto Enable_cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 pr_debug("PM: snapshotting memory.\n");
157 in_suspend = 1;
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800158 error = swsusp_suspend();
159 if (error)
160 goto Enable_cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 if (in_suspend) {
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800163 enable_nonboot_cpus();
164 platform_finish();
Rafael J. Wysocki0245b3e2005-10-30 15:00:01 -0800165 device_resume();
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700166 resume_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 pr_debug("PM: writing image.\n");
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800168 error = swsusp_write();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (!error)
170 power_down(pm_disk_mode);
Pavel Machek99dc7d62005-09-03 15:57:05 -0700171 else {
Pavel Machek99dc7d62005-09-03 15:57:05 -0700172 swsusp_free();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800173 goto Thaw;
Pavel Machek99dc7d62005-09-03 15:57:05 -0700174 }
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800175 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800177 }
Pavel Machek99dc7d62005-09-03 15:57:05 -0700178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 swsusp_free();
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800180 Enable_cpus:
181 enable_nonboot_cpus();
182 Resume_devices:
183 platform_finish();
Pavel Machek99dc7d62005-09-03 15:57:05 -0700184 device_resume();
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700185 resume_console();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800186 Thaw:
Pavel Machek99dc7d62005-09-03 15:57:05 -0700187 unprepare_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return error;
189}
190
191
192/**
193 * software_resume - Resume from a saved image.
194 *
195 * Called as a late_initcall (so all devices are discovered and
196 * initialized), we call swsusp to see if we have a saved image or not.
197 * If so, we quiesce devices, the restore the saved image. We will
198 * return above (in pm_suspend_disk() ) if everything goes well.
199 * Otherwise, we fail gracefully and return to the normally
200 * scheduled program.
201 *
202 */
203
204static int software_resume(void)
205{
206 int error;
207
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800208 mutex_lock(&pm_mutex);
Pavel Machek3efa1472005-07-07 17:56:43 -0700209 if (!swsusp_resume_device) {
Shaohua Lidd5d6662005-09-03 15:57:04 -0700210 if (!strlen(resume_file)) {
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800211 mutex_unlock(&pm_mutex);
Pavel Machek3efa1472005-07-07 17:56:43 -0700212 return -ENOENT;
Shaohua Lidd5d6662005-09-03 15:57:04 -0700213 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700214 swsusp_resume_device = name_to_dev_t(resume_file);
215 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
216 } else {
217 pr_debug("swsusp: Resume From Partition %d:%d\n",
218 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
219 }
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (noresume) {
222 /**
223 * FIXME: If noresume is specified, we need to find the partition
224 * and reset it back to normal swap space.
225 */
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800226 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return 0;
228 }
229
230 pr_debug("PM: Checking swsusp image.\n");
231
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800232 error = swsusp_check();
233 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 goto Done;
235
236 pr_debug("PM: Preparing processes for restore.\n");
237
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800238 error = prepare_processes();
239 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 swsusp_close();
Li Shaohua5a72e042005-06-25 14:55:06 -0700241 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243
244 pr_debug("PM: Reading swsusp image.\n");
245
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800246 error = swsusp_read();
247 if (error) {
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800248 swsusp_free();
249 goto Thaw;
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 pr_debug("PM: Preparing devices for restore.\n");
253
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700254 suspend_console();
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800255 error = device_suspend(PMSG_PRETHAW);
256 if (error)
257 goto Free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800259 error = disable_nonboot_cpus();
260 if (!error)
261 swsusp_resume();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800263 enable_nonboot_cpus();
264 Free:
265 swsusp_free();
Pavel Machek99dc7d62005-09-03 15:57:05 -0700266 device_resume();
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700267 resume_console();
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800268 Thaw:
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800269 printk(KERN_ERR "PM: Restore failed, recovering.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 unprepare_processes();
271 Done:
Shaohua Lidd5d6662005-09-03 15:57:04 -0700272 /* For success case, the suspend path will release the lock */
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800273 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 pr_debug("PM: Resume from disk failed.\n");
275 return 0;
276}
277
278late_initcall(software_resume);
279
280
Andreas Mohr3b364b82006-06-25 05:47:56 -0700281static const char * const pm_disk_modes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 [PM_DISK_FIRMWARE] = "firmware",
283 [PM_DISK_PLATFORM] = "platform",
284 [PM_DISK_SHUTDOWN] = "shutdown",
285 [PM_DISK_REBOOT] = "reboot",
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800286 [PM_DISK_TEST] = "test",
287 [PM_DISK_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288};
289
290/**
291 * disk - Control suspend-to-disk mode
292 *
293 * Suspend-to-disk can be handled in several ways. The greatest
294 * distinction is who writes memory to disk - the firmware or the OS.
295 * If the firmware does it, we assume that it also handles suspending
296 * the system.
297 * If the OS does it, then we have three options for putting the system
298 * to sleep - using the platform driver (e.g. ACPI or other PM registers),
299 * powering off the system or rebooting the system (for testing).
300 *
301 * The system will support either 'firmware' or 'platform', and that is
302 * known a priori (and encoded in pm_ops). But, the user may choose
303 * 'shutdown' or 'reboot' as alternatives.
304 *
305 * show() will display what the mode is currently set to.
306 * store() will accept one of
307 *
308 * 'firmware'
309 * 'platform'
310 * 'shutdown'
311 * 'reboot'
312 *
313 * It will only change to 'firmware' or 'platform' if the system
314 * supports it (as determined from pm_ops->pm_disk_mode).
315 */
316
317static ssize_t disk_show(struct subsystem * subsys, char * buf)
318{
319 return sprintf(buf, "%s\n", pm_disk_modes[pm_disk_mode]);
320}
321
322
323static ssize_t disk_store(struct subsystem * s, const char * buf, size_t n)
324{
325 int error = 0;
326 int i;
327 int len;
328 char *p;
329 suspend_disk_method_t mode = 0;
330
331 p = memchr(buf, '\n', n);
332 len = p ? p - buf : n;
333
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800334 mutex_lock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 for (i = PM_DISK_FIRMWARE; i < PM_DISK_MAX; i++) {
336 if (!strncmp(buf, pm_disk_modes[i], len)) {
337 mode = i;
338 break;
339 }
340 }
341 if (mode) {
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800342 if (mode == PM_DISK_SHUTDOWN || mode == PM_DISK_REBOOT ||
343 mode == PM_DISK_TEST || mode == PM_DISK_TESTPROC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 pm_disk_mode = mode;
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800345 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (pm_ops && pm_ops->enter &&
347 (mode == pm_ops->pm_disk_mode))
348 pm_disk_mode = mode;
349 else
350 error = -EINVAL;
351 }
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800352 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 error = -EINVAL;
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
357 pm_disk_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800358 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return error ? error : n;
360}
361
362power_attr(disk);
363
364static ssize_t resume_show(struct subsystem * subsys, char *buf)
365{
366 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
367 MINOR(swsusp_resume_device));
368}
369
Andrew Mortona5762192006-01-06 00:09:50 -0800370static ssize_t resume_store(struct subsystem *subsys, const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800374 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Andrew Mortona5762192006-01-06 00:09:50 -0800376 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
377 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Andrew Mortona5762192006-01-06 00:09:50 -0800379 res = MKDEV(maj,min);
380 if (maj != MAJOR(res) || min != MINOR(res))
381 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800383 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800384 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800385 mutex_unlock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800386 printk("Attempting manual resume\n");
387 noresume = 0;
388 software_resume();
389 ret = n;
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800390 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800391 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394power_attr(resume);
395
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800396static ssize_t image_size_show(struct subsystem * subsys, char *buf)
397{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800398 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800399}
400
401static ssize_t image_size_store(struct subsystem * subsys, const char * buf, size_t n)
402{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800403 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800404
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800405 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800406 image_size = size;
407 return n;
408 }
409
410 return -EINVAL;
411}
412
413power_attr(image_size);
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415static struct attribute * g[] = {
416 &disk_attr.attr,
417 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800418 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 NULL,
420};
421
422
423static struct attribute_group attr_group = {
424 .attrs = g,
425};
426
427
428static int __init pm_disk_init(void)
429{
430 return sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
431}
432
433core_initcall(pm_disk_init);
434
435
436static int __init resume_setup(char *str)
437{
438 if (noresume)
439 return 1;
440
441 strncpy( resume_file, str, 255 );
442 return 1;
443}
444
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800445static int __init resume_offset_setup(char *str)
446{
447 unsigned long long offset;
448
449 if (noresume)
450 return 1;
451
452 if (sscanf(str, "%llu", &offset) == 1)
453 swsusp_resume_block = offset;
454
455 return 1;
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458static int __init noresume_setup(char *str)
459{
460 noresume = 1;
461 return 1;
462}
463
464__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800465__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466__setup("resume=", resume_setup);