blob: b1fb7866b0b31d65ccea5946aa22e9c2881e0322 [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>
Andrew Mortond53d9f12005-07-12 13:58:07 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "power.h"
25
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static int noresume = 0;
28char resume_file[256] = CONFIG_PM_STD_PARTITION;
29dev_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
41static void power_down(suspend_disk_method_t mode)
42{
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 int error = 0;
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 switch(mode) {
46 case PM_DISK_PLATFORM:
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -050047 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 error = pm_ops->enter(PM_SUSPEND_DISK);
49 break;
50 case PM_DISK_SHUTDOWN:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -060051 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 break;
53 case PM_DISK_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -060054 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 break;
56 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -060057 kernel_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int prepare_processes(void)
73{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -080074 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 pm_prepare_console();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070077
78 error = disable_nonboot_cpus();
79 if (error)
80 goto enable_cpus;
Li Shaohua5a72e042005-06-25 14:55:06 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (freeze_processes()) {
83 error = -EBUSY;
Li Shaohua5a72e042005-06-25 14:55:06 -070084 goto thaw;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -080087 if (pm_disk_mode == PM_DISK_TESTPROC) {
88 printk("swsusp debug: Waiting for 5 seconds.\n");
89 mdelay(5000);
90 goto thaw;
91 }
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 /* Free memory before shutting down devices. */
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -080094 if (!(error = swsusp_shrink_memory()))
95 return 0;
Li Shaohua5a72e042005-06-25 14:55:06 -070096thaw:
97 thaw_processes();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070098enable_cpus:
Li Shaohua5a72e042005-06-25 14:55:06 -070099 enable_nonboot_cpus();
100 pm_restore_console();
101 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104static void unprepare_processes(void)
105{
Li Shaohua5a72e042005-06-25 14:55:06 -0700106 platform_finish();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 thaw_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700108 enable_nonboot_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 pm_restore_console();
110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/**
David Brownellf1cc0a82006-08-14 23:11:08 -0700113 * pm_suspend_disk - The granpappy of hibernation power management.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 *
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();
Li Shaohua5a72e042005-06-25 14:55:06 -0700126 if (error)
127 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800129 if (pm_disk_mode == PM_DISK_TESTPROC)
130 goto Thaw;
131
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700132 suspend_console();
Pavel Machek99dc7d62005-09-03 15:57:05 -0700133 error = device_suspend(PMSG_FREEZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (error) {
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700135 resume_console();
Pavel Machek99dc7d62005-09-03 15:57:05 -0700136 printk("Some devices failed to suspend\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800137 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 pr_debug("PM: snapshotting memory.\n");
147 in_suspend = 1;
148 if ((error = swsusp_suspend()))
149 goto Done;
150
151 if (in_suspend) {
Rafael J. Wysocki0245b3e2005-10-30 15:00:01 -0800152 device_resume();
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700153 resume_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 pr_debug("PM: writing image.\n");
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800155 error = swsusp_write();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (!error)
157 power_down(pm_disk_mode);
Pavel Machek99dc7d62005-09-03 15:57:05 -0700158 else {
Pavel Machek99dc7d62005-09-03 15:57:05 -0700159 swsusp_free();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800160 goto Thaw;
Pavel Machek99dc7d62005-09-03 15:57:05 -0700161 }
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800162 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800164 }
Pavel Machek99dc7d62005-09-03 15:57:05 -0700165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 swsusp_free();
167 Done:
Pavel Machek99dc7d62005-09-03 15:57:05 -0700168 device_resume();
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700169 resume_console();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800170 Thaw:
Pavel Machek99dc7d62005-09-03 15:57:05 -0700171 unprepare_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 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
188static int software_resume(void)
189{
190 int error;
191
Shaohua Lidd5d6662005-09-03 15:57:04 -0700192 down(&pm_sem);
Pavel Machek3efa1472005-07-07 17:56:43 -0700193 if (!swsusp_resume_device) {
Shaohua Lidd5d6662005-09-03 15:57:04 -0700194 if (!strlen(resume_file)) {
195 up(&pm_sem);
Pavel Machek3efa1472005-07-07 17:56:43 -0700196 return -ENOENT;
Shaohua Lidd5d6662005-09-03 15:57:04 -0700197 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700198 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 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 */
Shaohua Lidd5d6662005-09-03 15:57:04 -0700210 up(&pm_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 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();
Li Shaohua5a72e042005-06-25 14:55:06 -0700223 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
226 pr_debug("PM: Reading swsusp image.\n");
227
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800228 if ((error = swsusp_read())) {
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800229 swsusp_free();
230 goto Thaw;
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 pr_debug("PM: Preparing devices for restore.\n");
234
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700235 suspend_console();
David Brownellf1cc0a82006-08-14 23:11:08 -0700236 if ((error = device_suspend(PMSG_PRETHAW))) {
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700237 resume_console();
Pavel Machek99dc7d62005-09-03 15:57:05 -0700238 printk("Some devices failed to suspend\n");
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800239 swsusp_free();
240 goto Thaw;
Pavel Machek99dc7d62005-09-03 15:57:05 -0700241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 mb();
244
245 pr_debug("PM: Restoring saved image.\n");
246 swsusp_resume();
247 pr_debug("PM: Restore failed, recovering.n");
Pavel Machek99dc7d62005-09-03 15:57:05 -0700248 device_resume();
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700249 resume_console();
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800250 Thaw:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 unprepare_processes();
252 Done:
Shaohua Lidd5d6662005-09-03 15:57:04 -0700253 /* For success case, the suspend path will release the lock */
254 up(&pm_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 pr_debug("PM: Resume from disk failed.\n");
256 return 0;
257}
258
259late_initcall(software_resume);
260
261
Andreas Mohr3b364b82006-06-25 05:47:56 -0700262static const char * const pm_disk_modes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 [PM_DISK_FIRMWARE] = "firmware",
264 [PM_DISK_PLATFORM] = "platform",
265 [PM_DISK_SHUTDOWN] = "shutdown",
266 [PM_DISK_REBOOT] = "reboot",
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800267 [PM_DISK_TEST] = "test",
268 [PM_DISK_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
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
298static 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
304static 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) {
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800323 if (mode == PM_DISK_SHUTDOWN || mode == PM_DISK_REBOOT ||
324 mode == PM_DISK_TEST || mode == PM_DISK_TESTPROC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 pm_disk_mode = mode;
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800326 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (pm_ops && pm_ops->enter &&
328 (mode == pm_ops->pm_disk_mode))
329 pm_disk_mode = mode;
330 else
331 error = -EINVAL;
332 }
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800333 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 error = -EINVAL;
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
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
343power_attr(disk);
344
345static 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
Andrew Mortona5762192006-01-06 00:09:50 -0800351static ssize_t resume_store(struct subsystem *subsys, const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800355 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Andrew Mortona5762192006-01-06 00:09:50 -0800357 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
358 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Andrew Mortona5762192006-01-06 00:09:50 -0800360 res = MKDEV(maj,min);
361 if (maj != MAJOR(res) || min != MINOR(res))
362 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Andrew Mortona5762192006-01-06 00:09:50 -0800364 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;
371out:
372 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375power_attr(resume);
376
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800377static ssize_t image_size_show(struct subsystem * subsys, char *buf)
378{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800379 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800380}
381
382static ssize_t image_size_store(struct subsystem * subsys, const char * buf, size_t n)
383{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800384 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800385
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800386 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800387 image_size = size;
388 return n;
389 }
390
391 return -EINVAL;
392}
393
394power_attr(image_size);
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396static struct attribute * g[] = {
397 &disk_attr.attr,
398 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800399 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 NULL,
401};
402
403
404static struct attribute_group attr_group = {
405 .attrs = g,
406};
407
408
409static int __init pm_disk_init(void)
410{
411 return sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
412}
413
414core_initcall(pm_disk_init);
415
416
417static 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
426static 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);