blob: ae6bbc903b7d1993577375a7b7e77faf184c33a9 [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:
Rafael J. Wysocki9185cfa2006-11-01 13:23:14 +010047 if (pm_ops && pm_ops->enter) {
48 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
49 error = pm_ops->enter(PM_SUSPEND_DISK);
50 break;
51 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 case PM_DISK_SHUTDOWN:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -060053 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 break;
55 case PM_DISK_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -060056 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 break;
58 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -060059 kernel_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 /* Valid image is on the disk, if we continue we risk serious data corruption
61 after resume. */
62 printk(KERN_CRIT "Please power me down manually\n");
63 while(1);
64}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static inline void platform_finish(void)
67{
68 if (pm_disk_mode == PM_DISK_PLATFORM) {
69 if (pm_ops && pm_ops->finish)
70 pm_ops->finish(PM_SUSPEND_DISK);
71 }
72}
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static int prepare_processes(void)
75{
76 int error;
77
78 pm_prepare_console();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070079
80 error = disable_nonboot_cpus();
81 if (error)
82 goto enable_cpus;
Li Shaohua5a72e042005-06-25 14:55:06 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (freeze_processes()) {
85 error = -EBUSY;
Li Shaohua5a72e042005-06-25 14:55:06 -070086 goto thaw;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 /* Free memory before shutting down devices. */
Rafael J. Wysocki72a97e02006-01-06 00:13:46 -080090 if (!(error = swsusp_shrink_memory()))
91 return 0;
Li Shaohua5a72e042005-06-25 14:55:06 -070092thaw:
93 thaw_processes();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070094enable_cpus:
Li Shaohua5a72e042005-06-25 14:55:06 -070095 enable_nonboot_cpus();
96 pm_restore_console();
97 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100static void unprepare_processes(void)
101{
Li Shaohua5a72e042005-06-25 14:55:06 -0700102 platform_finish();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 thaw_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700104 enable_nonboot_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 pm_restore_console();
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/**
David Brownellf1cc0a82006-08-14 23:11:08 -0700109 * pm_suspend_disk - The granpappy of hibernation power management.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *
111 * If we're going through the firmware, then get it over with quickly.
112 *
113 * If not, then call swsusp to do its thing, then figure out how
114 * to power down the system.
115 */
116
117int pm_suspend_disk(void)
118{
119 int error;
120
121 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700122 if (error)
123 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700125 suspend_console();
Pavel Machek99dc7d62005-09-03 15:57:05 -0700126 error = device_suspend(PMSG_FREEZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (error) {
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700128 resume_console();
Pavel Machek99dc7d62005-09-03 15:57:05 -0700129 printk("Some devices failed to suspend\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 unprepare_processes();
131 return error;
132 }
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 pr_debug("PM: snapshotting memory.\n");
135 in_suspend = 1;
136 if ((error = swsusp_suspend()))
137 goto Done;
138
139 if (in_suspend) {
Rafael J. Wysocki0245b3e2005-10-30 15:00:01 -0800140 device_resume();
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700141 resume_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 pr_debug("PM: writing image.\n");
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800143 error = swsusp_write();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (!error)
145 power_down(pm_disk_mode);
Pavel Machek99dc7d62005-09-03 15:57:05 -0700146 else {
Pavel Machek99dc7d62005-09-03 15:57:05 -0700147 swsusp_free();
148 unprepare_processes();
149 return error;
150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 } else
152 pr_debug("PM: Image restored successfully.\n");
Pavel Machek99dc7d62005-09-03 15:57:05 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 swsusp_free();
155 Done:
Pavel Machek99dc7d62005-09-03 15:57:05 -0700156 device_resume();
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700157 resume_console();
Pavel Machek99dc7d62005-09-03 15:57:05 -0700158 unprepare_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 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
Shaohua Lidd5d6662005-09-03 15:57:04 -0700179 down(&pm_sem);
Pavel Machek3efa1472005-07-07 17:56:43 -0700180 if (!swsusp_resume_device) {
Shaohua Lidd5d6662005-09-03 15:57:04 -0700181 if (!strlen(resume_file)) {
182 up(&pm_sem);
Pavel Machek3efa1472005-07-07 17:56:43 -0700183 return -ENOENT;
Shaohua Lidd5d6662005-09-03 15:57:04 -0700184 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700185 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 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 */
Shaohua Lidd5d6662005-09-03 15:57:04 -0700197 up(&pm_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 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();
Li Shaohua5a72e042005-06-25 14:55:06 -0700210 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212
213 pr_debug("PM: Reading swsusp image.\n");
214
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800215 if ((error = swsusp_read())) {
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800216 swsusp_free();
217 goto Thaw;
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 pr_debug("PM: Preparing devices for restore.\n");
221
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700222 suspend_console();
David Brownellf1cc0a82006-08-14 23:11:08 -0700223 if ((error = device_suspend(PMSG_PRETHAW))) {
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700224 resume_console();
Pavel Machek99dc7d62005-09-03 15:57:05 -0700225 printk("Some devices failed to suspend\n");
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800226 swsusp_free();
227 goto Thaw;
Pavel Machek99dc7d62005-09-03 15:57:05 -0700228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 mb();
231
232 pr_debug("PM: Restoring saved image.\n");
233 swsusp_resume();
234 pr_debug("PM: Restore failed, recovering.n");
Pavel Machek99dc7d62005-09-03 15:57:05 -0700235 device_resume();
Rafael J. Wysocki97c78012006-10-11 01:20:45 -0700236 resume_console();
Rafael J. Wysocki2c1b4a52005-10-30 14:59:58 -0800237 Thaw:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 unprepare_processes();
239 Done:
Shaohua Lidd5d6662005-09-03 15:57:04 -0700240 /* For success case, the suspend path will release the lock */
241 up(&pm_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 pr_debug("PM: Resume from disk failed.\n");
243 return 0;
244}
245
246late_initcall(software_resume);
247
248
Andreas Mohr3b364b82006-06-25 05:47:56 -0700249static const char * const pm_disk_modes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 [PM_DISK_FIRMWARE] = "firmware",
251 [PM_DISK_PLATFORM] = "platform",
252 [PM_DISK_SHUTDOWN] = "shutdown",
253 [PM_DISK_REBOOT] = "reboot",
254};
255
256/**
257 * disk - Control suspend-to-disk mode
258 *
259 * Suspend-to-disk can be handled in several ways. The greatest
260 * distinction is who writes memory to disk - the firmware or the OS.
261 * If the firmware does it, we assume that it also handles suspending
262 * the system.
263 * If the OS does it, then we have three options for putting the system
264 * to sleep - using the platform driver (e.g. ACPI or other PM registers),
265 * powering off the system or rebooting the system (for testing).
266 *
267 * The system will support either 'firmware' or 'platform', and that is
268 * known a priori (and encoded in pm_ops). But, the user may choose
269 * 'shutdown' or 'reboot' as alternatives.
270 *
271 * show() will display what the mode is currently set to.
272 * store() will accept one of
273 *
274 * 'firmware'
275 * 'platform'
276 * 'shutdown'
277 * 'reboot'
278 *
279 * It will only change to 'firmware' or 'platform' if the system
280 * supports it (as determined from pm_ops->pm_disk_mode).
281 */
282
283static ssize_t disk_show(struct subsystem * subsys, char * buf)
284{
285 return sprintf(buf, "%s\n", pm_disk_modes[pm_disk_mode]);
286}
287
288
289static ssize_t disk_store(struct subsystem * s, const char * buf, size_t n)
290{
291 int error = 0;
292 int i;
293 int len;
294 char *p;
295 suspend_disk_method_t mode = 0;
296
297 p = memchr(buf, '\n', n);
298 len = p ? p - buf : n;
299
300 down(&pm_sem);
301 for (i = PM_DISK_FIRMWARE; i < PM_DISK_MAX; i++) {
302 if (!strncmp(buf, pm_disk_modes[i], len)) {
303 mode = i;
304 break;
305 }
306 }
307 if (mode) {
308 if (mode == PM_DISK_SHUTDOWN || mode == PM_DISK_REBOOT)
309 pm_disk_mode = mode;
310 else {
311 if (pm_ops && pm_ops->enter &&
312 (mode == pm_ops->pm_disk_mode))
313 pm_disk_mode = mode;
314 else
315 error = -EINVAL;
316 }
317 } else
318 error = -EINVAL;
319
320 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
321 pm_disk_modes[mode]);
322 up(&pm_sem);
323 return error ? error : n;
324}
325
326power_attr(disk);
327
328static ssize_t resume_show(struct subsystem * subsys, char *buf)
329{
330 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
331 MINOR(swsusp_resume_device));
332}
333
Andrew Mortona5762192006-01-06 00:09:50 -0800334static ssize_t resume_store(struct subsystem *subsys, const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800338 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Andrew Mortona5762192006-01-06 00:09:50 -0800340 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
341 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Andrew Mortona5762192006-01-06 00:09:50 -0800343 res = MKDEV(maj,min);
344 if (maj != MAJOR(res) || min != MINOR(res))
345 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Andrew Mortona5762192006-01-06 00:09:50 -0800347 down(&pm_sem);
348 swsusp_resume_device = res;
349 up(&pm_sem);
350 printk("Attempting manual resume\n");
351 noresume = 0;
352 software_resume();
353 ret = n;
354out:
355 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358power_attr(resume);
359
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800360static ssize_t image_size_show(struct subsystem * subsys, char *buf)
361{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800362 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800363}
364
365static ssize_t image_size_store(struct subsystem * subsys, const char * buf, size_t n)
366{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800367 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800368
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800369 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800370 image_size = size;
371 return n;
372 }
373
374 return -EINVAL;
375}
376
377power_attr(image_size);
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379static struct attribute * g[] = {
380 &disk_attr.attr,
381 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800382 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 NULL,
384};
385
386
387static struct attribute_group attr_group = {
388 .attrs = g,
389};
390
391
392static int __init pm_disk_init(void)
393{
394 return sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
395}
396
397core_initcall(pm_disk_init);
398
399
400static int __init resume_setup(char *str)
401{
402 if (noresume)
403 return 1;
404
405 strncpy( resume_file, str, 255 );
406 return 1;
407}
408
409static int __init noresume_setup(char *str)
410{
411 noresume = 1;
412 return 1;
413}
414
415__setup("noresume", noresume_setup);
416__setup("resume=", resume_setup);