]>
Commit | Line | Data |
---|---|---|
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> |
97c7801c | 21 | #include <linux/console.h> |
e3920fb4 | 22 | #include <linux/cpu.h> |
7dfb7103 | 23 | #include <linux/freezer.h> |
d53d9f16 | 24 | |
1da177e4 LT |
25 | #include "power.h" |
26 | ||
27 | ||
1da177e4 LT |
28 | static int noresume = 0; |
29 | char resume_file[256] = CONFIG_PM_STD_PARTITION; | |
30 | dev_t swsusp_resume_device; | |
9a154d9d | 31 | sector_t swsusp_resume_block; |
1da177e4 | 32 | |
8a05aac2 SS |
33 | /** |
34 | * 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 | ||
38 | static inline int platform_prepare(void) | |
39 | { | |
40 | int error = 0; | |
41 | ||
fe0c935a JB |
42 | switch (pm_disk_mode) { |
43 | case PM_DISK_TEST: | |
44 | case PM_DISK_TESTPROC: | |
45 | case PM_DISK_SHUTDOWN: | |
46 | case PM_DISK_REBOOT: | |
47 | break; | |
48 | default: | |
8a05aac2 SS |
49 | if (pm_ops && pm_ops->prepare) |
50 | error = pm_ops->prepare(PM_SUSPEND_DISK); | |
51 | } | |
52 | return error; | |
53 | } | |
54 | ||
1da177e4 LT |
55 | /** |
56 | * power_down - Shut machine down for hibernate. | |
1da177e4 | 57 | * |
fe0c935a JB |
58 | * Use the platform driver, if configured so; otherwise try |
59 | * to power off or reboot. | |
1da177e4 LT |
60 | */ |
61 | ||
fe0c935a | 62 | static void power_down(void) |
1da177e4 | 63 | { |
fe0c935a JB |
64 | switch (pm_disk_mode) { |
65 | case PM_DISK_TEST: | |
66 | case PM_DISK_TESTPROC: | |
67 | break; | |
1da177e4 | 68 | case PM_DISK_SHUTDOWN: |
fdde86ac | 69 | kernel_power_off(); |
1da177e4 LT |
70 | break; |
71 | case PM_DISK_REBOOT: | |
fdde86ac | 72 | kernel_restart(NULL); |
1da177e4 | 73 | break; |
fe0c935a JB |
74 | default: |
75 | if (pm_ops && pm_ops->enter) { | |
76 | kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK); | |
77 | pm_ops->enter(PM_SUSPEND_DISK); | |
78 | break; | |
79 | } | |
1da177e4 | 80 | } |
fdde86ac | 81 | kernel_halt(); |
fe0c935a JB |
82 | /* |
83 | * Valid image is on the disk, if we continue we risk serious data | |
84 | * corruption after resume. | |
85 | */ | |
1da177e4 LT |
86 | printk(KERN_CRIT "Please power me down manually\n"); |
87 | while(1); | |
88 | } | |
89 | ||
1da177e4 LT |
90 | static inline void platform_finish(void) |
91 | { | |
fe0c935a JB |
92 | switch (pm_disk_mode) { |
93 | case PM_DISK_TEST: | |
94 | case PM_DISK_TESTPROC: | |
95 | case PM_DISK_SHUTDOWN: | |
96 | case PM_DISK_REBOOT: | |
97 | break; | |
98 | default: | |
1da177e4 LT |
99 | if (pm_ops && pm_ops->finish) |
100 | pm_ops->finish(PM_SUSPEND_DISK); | |
101 | } | |
102 | } | |
103 | ||
ed746e3b RW |
104 | static void unprepare_processes(void) |
105 | { | |
106 | thaw_processes(); | |
107 | pm_restore_console(); | |
108 | } | |
109 | ||
1da177e4 LT |
110 | static int prepare_processes(void) |
111 | { | |
b918f6e6 | 112 | int error = 0; |
1da177e4 LT |
113 | |
114 | pm_prepare_console(); | |
1da177e4 LT |
115 | if (freeze_processes()) { |
116 | error = -EBUSY; | |
ed746e3b | 117 | unprepare_processes(); |
1da177e4 | 118 | } |
5a72e04d | 119 | return error; |
1da177e4 LT |
120 | } |
121 | ||
1da177e4 | 122 | /** |
f1cc0a89 | 123 | * pm_suspend_disk - The granpappy of hibernation power management. |
1da177e4 | 124 | * |
1da177e4 LT |
125 | * If not, then call swsusp to do its thing, then figure out how |
126 | * to power down the system. | |
127 | */ | |
128 | ||
129 | int pm_suspend_disk(void) | |
130 | { | |
131 | int error; | |
132 | ||
0709db60 RW |
133 | /* The snapshot device should not be opened while we're running */ |
134 | if (!atomic_add_unless(&snapshot_device_available, -1, 0)) | |
135 | return -EBUSY; | |
136 | ||
137 | /* Allocate memory management structures */ | |
138 | error = create_basic_memory_bitmaps(); | |
139 | if (error) | |
140 | goto Exit; | |
141 | ||
1da177e4 | 142 | error = prepare_processes(); |
5a72e04d | 143 | if (error) |
0709db60 | 144 | goto Finish; |
1da177e4 | 145 | |
ed746e3b RW |
146 | if (pm_disk_mode == PM_DISK_TESTPROC) { |
147 | printk("swsusp debug: Waiting for 5 seconds.\n"); | |
148 | mdelay(5000); | |
149 | goto Thaw; | |
150 | } | |
74dfd666 | 151 | |
ed746e3b RW |
152 | /* Free memory before shutting down devices. */ |
153 | error = swsusp_shrink_memory(); | |
154 | if (error) | |
0709db60 | 155 | goto Thaw; |
ed746e3b RW |
156 | |
157 | error = platform_prepare(); | |
158 | if (error) | |
0709db60 | 159 | goto Thaw; |
b918f6e6 | 160 | |
97c7801c | 161 | suspend_console(); |
99dc7d63 | 162 | error = device_suspend(PMSG_FREEZE); |
1da177e4 | 163 | if (error) { |
ed746e3b RW |
164 | printk(KERN_ERR "PM: Some devices failed to suspend\n"); |
165 | goto Resume_devices; | |
b918f6e6 | 166 | } |
ed746e3b RW |
167 | error = disable_nonboot_cpus(); |
168 | if (error) | |
169 | goto Enable_cpus; | |
b918f6e6 RW |
170 | |
171 | if (pm_disk_mode == PM_DISK_TEST) { | |
172 | printk("swsusp debug: Waiting for 5 seconds.\n"); | |
173 | mdelay(5000); | |
ed746e3b | 174 | goto Enable_cpus; |
1da177e4 LT |
175 | } |
176 | ||
1da177e4 LT |
177 | pr_debug("PM: snapshotting memory.\n"); |
178 | in_suspend = 1; | |
ed746e3b RW |
179 | error = swsusp_suspend(); |
180 | if (error) | |
181 | goto Enable_cpus; | |
1da177e4 LT |
182 | |
183 | if (in_suspend) { | |
ed746e3b RW |
184 | enable_nonboot_cpus(); |
185 | platform_finish(); | |
0245b3e7 | 186 | device_resume(); |
97c7801c | 187 | resume_console(); |
1da177e4 | 188 | pr_debug("PM: writing image.\n"); |
f577eb30 | 189 | error = swsusp_write(); |
1da177e4 | 190 | if (!error) |
fe0c935a | 191 | power_down(); |
99dc7d63 | 192 | else { |
99dc7d63 | 193 | swsusp_free(); |
0709db60 | 194 | goto Thaw; |
99dc7d63 | 195 | } |
b918f6e6 | 196 | } else { |
1da177e4 | 197 | pr_debug("PM: Image restored successfully.\n"); |
b918f6e6 | 198 | } |
99dc7d63 | 199 | |
1da177e4 | 200 | swsusp_free(); |
ed746e3b RW |
201 | Enable_cpus: |
202 | enable_nonboot_cpus(); | |
203 | Resume_devices: | |
204 | platform_finish(); | |
99dc7d63 | 205 | device_resume(); |
97c7801c | 206 | resume_console(); |
b918f6e6 | 207 | Thaw: |
99dc7d63 | 208 | unprepare_processes(); |
0709db60 RW |
209 | Finish: |
210 | free_basic_memory_bitmaps(); | |
211 | Exit: | |
212 | atomic_inc(&snapshot_device_available); | |
1da177e4 LT |
213 | return error; |
214 | } | |
215 | ||
216 | ||
217 | /** | |
218 | * software_resume - Resume from a saved image. | |
219 | * | |
220 | * Called as a late_initcall (so all devices are discovered and | |
221 | * initialized), we call swsusp to see if we have a saved image or not. | |
222 | * If so, we quiesce devices, the restore the saved image. We will | |
223 | * return above (in pm_suspend_disk() ) if everything goes well. | |
224 | * Otherwise, we fail gracefully and return to the normally | |
225 | * scheduled program. | |
226 | * | |
227 | */ | |
228 | ||
229 | static int software_resume(void) | |
230 | { | |
231 | int error; | |
232 | ||
a6d70980 | 233 | mutex_lock(&pm_mutex); |
3efa147a | 234 | if (!swsusp_resume_device) { |
dd5d666b | 235 | if (!strlen(resume_file)) { |
a6d70980 | 236 | mutex_unlock(&pm_mutex); |
3efa147a | 237 | return -ENOENT; |
dd5d666b | 238 | } |
3efa147a PM |
239 | swsusp_resume_device = name_to_dev_t(resume_file); |
240 | pr_debug("swsusp: Resume From Partition %s\n", resume_file); | |
241 | } else { | |
242 | pr_debug("swsusp: Resume From Partition %d:%d\n", | |
243 | MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device)); | |
244 | } | |
245 | ||
1da177e4 LT |
246 | if (noresume) { |
247 | /** | |
248 | * FIXME: If noresume is specified, we need to find the partition | |
249 | * and reset it back to normal swap space. | |
250 | */ | |
a6d70980 | 251 | mutex_unlock(&pm_mutex); |
1da177e4 LT |
252 | return 0; |
253 | } | |
254 | ||
255 | pr_debug("PM: Checking swsusp image.\n"); | |
ed746e3b RW |
256 | error = swsusp_check(); |
257 | if (error) | |
74dfd666 | 258 | goto Unlock; |
1da177e4 | 259 | |
0709db60 RW |
260 | /* The snapshot device should not be opened while we're running */ |
261 | if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { | |
262 | error = -EBUSY; | |
263 | goto Unlock; | |
264 | } | |
265 | ||
74dfd666 RW |
266 | error = create_basic_memory_bitmaps(); |
267 | if (error) | |
0709db60 | 268 | goto Finish; |
1da177e4 | 269 | |
74dfd666 | 270 | pr_debug("PM: Preparing processes for restore.\n"); |
ed746e3b RW |
271 | error = prepare_processes(); |
272 | if (error) { | |
1da177e4 | 273 | swsusp_close(); |
5a72e04d | 274 | goto Done; |
1da177e4 LT |
275 | } |
276 | ||
277 | pr_debug("PM: Reading swsusp image.\n"); | |
278 | ||
ed746e3b RW |
279 | error = swsusp_read(); |
280 | if (error) { | |
2c1b4a5c RW |
281 | swsusp_free(); |
282 | goto Thaw; | |
283 | } | |
1da177e4 LT |
284 | |
285 | pr_debug("PM: Preparing devices for restore.\n"); | |
286 | ||
97c7801c | 287 | suspend_console(); |
ed746e3b RW |
288 | error = device_suspend(PMSG_PRETHAW); |
289 | if (error) | |
290 | goto Free; | |
1da177e4 | 291 | |
ed746e3b RW |
292 | error = disable_nonboot_cpus(); |
293 | if (!error) | |
294 | swsusp_resume(); | |
1da177e4 | 295 | |
ed746e3b RW |
296 | enable_nonboot_cpus(); |
297 | Free: | |
298 | swsusp_free(); | |
99dc7d63 | 299 | device_resume(); |
97c7801c | 300 | resume_console(); |
2c1b4a5c | 301 | Thaw: |
ed746e3b | 302 | printk(KERN_ERR "PM: Restore failed, recovering.\n"); |
1da177e4 LT |
303 | unprepare_processes(); |
304 | Done: | |
74dfd666 | 305 | free_basic_memory_bitmaps(); |
0709db60 RW |
306 | Finish: |
307 | atomic_inc(&snapshot_device_available); | |
dd5d666b | 308 | /* For success case, the suspend path will release the lock */ |
74dfd666 | 309 | Unlock: |
a6d70980 | 310 | mutex_unlock(&pm_mutex); |
1da177e4 LT |
311 | pr_debug("PM: Resume from disk failed.\n"); |
312 | return 0; | |
313 | } | |
314 | ||
315 | late_initcall(software_resume); | |
316 | ||
317 | ||
3b364b8d | 318 | static const char * const pm_disk_modes[] = { |
1da177e4 LT |
319 | [PM_DISK_PLATFORM] = "platform", |
320 | [PM_DISK_SHUTDOWN] = "shutdown", | |
321 | [PM_DISK_REBOOT] = "reboot", | |
b918f6e6 RW |
322 | [PM_DISK_TEST] = "test", |
323 | [PM_DISK_TESTPROC] = "testproc", | |
1da177e4 LT |
324 | }; |
325 | ||
326 | /** | |
327 | * disk - Control suspend-to-disk mode | |
328 | * | |
11d77d0c JB |
329 | * Suspend-to-disk can be handled in several ways. We have a few options |
330 | * for putting the system to sleep - using the platform driver (e.g. ACPI | |
331 | * or other pm_ops), powering off the system or rebooting the system | |
332 | * (for testing) as well as the two test modes. | |
1da177e4 | 333 | * |
11d77d0c JB |
334 | * The system can support 'platform', and that is known a priori (and |
335 | * encoded in pm_ops). However, the user may choose 'shutdown' or 'reboot' | |
336 | * as alternatives, as well as the test modes 'test' and 'testproc'. | |
1da177e4 LT |
337 | * |
338 | * show() will display what the mode is currently set to. | |
339 | * store() will accept one of | |
340 | * | |
1da177e4 LT |
341 | * 'platform' |
342 | * 'shutdown' | |
343 | * 'reboot' | |
11d77d0c JB |
344 | * 'test' |
345 | * 'testproc' | |
1da177e4 | 346 | * |
11d77d0c | 347 | * It will only change to 'platform' if the system |
1da177e4 LT |
348 | * supports it (as determined from pm_ops->pm_disk_mode). |
349 | */ | |
350 | ||
823bccfc | 351 | static ssize_t disk_show(struct kset *kset, char *buf) |
1da177e4 LT |
352 | { |
353 | return sprintf(buf, "%s\n", pm_disk_modes[pm_disk_mode]); | |
354 | } | |
355 | ||
356 | ||
823bccfc | 357 | static ssize_t disk_store(struct kset *kset, const char *buf, size_t n) |
1da177e4 LT |
358 | { |
359 | int error = 0; | |
360 | int i; | |
361 | int len; | |
362 | char *p; | |
363 | suspend_disk_method_t mode = 0; | |
364 | ||
365 | p = memchr(buf, '\n', n); | |
366 | len = p ? p - buf : n; | |
367 | ||
a6d70980 | 368 | mutex_lock(&pm_mutex); |
11d77d0c | 369 | for (i = PM_DISK_PLATFORM; i < PM_DISK_MAX; i++) { |
1da177e4 LT |
370 | if (!strncmp(buf, pm_disk_modes[i], len)) { |
371 | mode = i; | |
372 | break; | |
373 | } | |
374 | } | |
375 | if (mode) { | |
fe0c935a JB |
376 | switch (mode) { |
377 | case PM_DISK_SHUTDOWN: | |
378 | case PM_DISK_REBOOT: | |
379 | case PM_DISK_TEST: | |
380 | case PM_DISK_TESTPROC: | |
1da177e4 | 381 | pm_disk_mode = mode; |
fe0c935a JB |
382 | break; |
383 | default: | |
1da177e4 LT |
384 | if (pm_ops && pm_ops->enter && |
385 | (mode == pm_ops->pm_disk_mode)) | |
386 | pm_disk_mode = mode; | |
387 | else | |
388 | error = -EINVAL; | |
389 | } | |
b918f6e6 | 390 | } else { |
1da177e4 | 391 | error = -EINVAL; |
b918f6e6 | 392 | } |
1da177e4 LT |
393 | |
394 | pr_debug("PM: suspend-to-disk mode set to '%s'\n", | |
395 | pm_disk_modes[mode]); | |
a6d70980 | 396 | mutex_unlock(&pm_mutex); |
1da177e4 LT |
397 | return error ? error : n; |
398 | } | |
399 | ||
400 | power_attr(disk); | |
401 | ||
823bccfc | 402 | static ssize_t resume_show(struct kset *kset, char *buf) |
1da177e4 LT |
403 | { |
404 | return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device), | |
405 | MINOR(swsusp_resume_device)); | |
406 | } | |
407 | ||
823bccfc | 408 | static ssize_t resume_store(struct kset *kset, const char *buf, size_t n) |
1da177e4 | 409 | { |
1da177e4 | 410 | unsigned int maj, min; |
1da177e4 | 411 | dev_t res; |
a576219a | 412 | int ret = -EINVAL; |
1da177e4 | 413 | |
a576219a AM |
414 | if (sscanf(buf, "%u:%u", &maj, &min) != 2) |
415 | goto out; | |
1da177e4 | 416 | |
a576219a AM |
417 | res = MKDEV(maj,min); |
418 | if (maj != MAJOR(res) || min != MINOR(res)) | |
419 | goto out; | |
1da177e4 | 420 | |
a6d70980 | 421 | mutex_lock(&pm_mutex); |
a576219a | 422 | swsusp_resume_device = res; |
a6d70980 | 423 | mutex_unlock(&pm_mutex); |
a576219a AM |
424 | printk("Attempting manual resume\n"); |
425 | noresume = 0; | |
426 | software_resume(); | |
427 | ret = n; | |
59a49335 | 428 | out: |
a576219a | 429 | return ret; |
1da177e4 LT |
430 | } |
431 | ||
432 | power_attr(resume); | |
433 | ||
823bccfc | 434 | static ssize_t image_size_show(struct kset *kset, char *buf) |
ca0aec0f | 435 | { |
853609b6 | 436 | return sprintf(buf, "%lu\n", image_size); |
ca0aec0f RW |
437 | } |
438 | ||
823bccfc | 439 | static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n) |
ca0aec0f | 440 | { |
853609b6 | 441 | unsigned long size; |
ca0aec0f | 442 | |
853609b6 | 443 | if (sscanf(buf, "%lu", &size) == 1) { |
ca0aec0f RW |
444 | image_size = size; |
445 | return n; | |
446 | } | |
447 | ||
448 | return -EINVAL; | |
449 | } | |
450 | ||
451 | power_attr(image_size); | |
452 | ||
1da177e4 LT |
453 | static struct attribute * g[] = { |
454 | &disk_attr.attr, | |
455 | &resume_attr.attr, | |
ca0aec0f | 456 | &image_size_attr.attr, |
1da177e4 LT |
457 | NULL, |
458 | }; | |
459 | ||
460 | ||
461 | static struct attribute_group attr_group = { | |
462 | .attrs = g, | |
463 | }; | |
464 | ||
465 | ||
466 | static int __init pm_disk_init(void) | |
467 | { | |
823bccfc | 468 | return sysfs_create_group(&power_subsys.kobj, &attr_group); |
1da177e4 LT |
469 | } |
470 | ||
471 | core_initcall(pm_disk_init); | |
472 | ||
473 | ||
474 | static int __init resume_setup(char *str) | |
475 | { | |
476 | if (noresume) | |
477 | return 1; | |
478 | ||
479 | strncpy( resume_file, str, 255 ); | |
480 | return 1; | |
481 | } | |
482 | ||
9a154d9d RW |
483 | static int __init resume_offset_setup(char *str) |
484 | { | |
485 | unsigned long long offset; | |
486 | ||
487 | if (noresume) | |
488 | return 1; | |
489 | ||
490 | if (sscanf(str, "%llu", &offset) == 1) | |
491 | swsusp_resume_block = offset; | |
492 | ||
493 | return 1; | |
494 | } | |
495 | ||
1da177e4 LT |
496 | static int __init noresume_setup(char *str) |
497 | { | |
498 | noresume = 1; | |
499 | return 1; | |
500 | } | |
501 | ||
502 | __setup("noresume", noresume_setup); | |
9a154d9d | 503 | __setup("resume_offset=", resume_offset_setup); |
1da177e4 | 504 | __setup("resume=", resume_setup); |