]>
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> | |
1bfcf130 | 17 | #include <linux/kmod.h> |
1da177e4 LT |
18 | #include <linux/delay.h> |
19 | #include <linux/fs.h> | |
d53d9f16 | 20 | #include <linux/mount.h> |
88d10bba | 21 | #include <linux/pm.h> |
97c7801c | 22 | #include <linux/console.h> |
e3920fb4 | 23 | #include <linux/cpu.h> |
7dfb7103 | 24 | #include <linux/freezer.h> |
d53d9f16 | 25 | |
1da177e4 LT |
26 | #include "power.h" |
27 | ||
28 | ||
1da177e4 | 29 | static int noresume = 0; |
47a460d5 | 30 | static char resume_file[256] = CONFIG_PM_STD_PARTITION; |
1da177e4 | 31 | dev_t swsusp_resume_device; |
9a154d9d | 32 | sector_t swsusp_resume_block; |
1da177e4 | 33 | |
a3d25c27 RW |
34 | enum { |
35 | HIBERNATION_INVALID, | |
36 | HIBERNATION_PLATFORM, | |
37 | HIBERNATION_TEST, | |
38 | HIBERNATION_TESTPROC, | |
39 | HIBERNATION_SHUTDOWN, | |
40 | HIBERNATION_REBOOT, | |
41 | /* keep last */ | |
42 | __HIBERNATION_AFTER_LAST | |
43 | }; | |
44 | #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1) | |
45 | #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1) | |
46 | ||
47 | static int hibernation_mode = HIBERNATION_SHUTDOWN; | |
48 | ||
b3dac3b3 | 49 | static struct platform_hibernation_ops *hibernation_ops; |
a3d25c27 RW |
50 | |
51 | /** | |
52 | * hibernation_set_ops - set the global hibernate operations | |
53 | * @ops: the hibernation operations to use in subsequent hibernation transitions | |
54 | */ | |
55 | ||
b3dac3b3 | 56 | void hibernation_set_ops(struct platform_hibernation_ops *ops) |
a3d25c27 | 57 | { |
caea99ef RW |
58 | if (ops && !(ops->begin && ops->end && ops->pre_snapshot |
59 | && ops->prepare && ops->finish && ops->enter && ops->pre_restore | |
74f270af | 60 | && ops->restore_cleanup)) { |
a3d25c27 RW |
61 | WARN_ON(1); |
62 | return; | |
63 | } | |
64 | mutex_lock(&pm_mutex); | |
65 | hibernation_ops = ops; | |
66 | if (ops) | |
67 | hibernation_mode = HIBERNATION_PLATFORM; | |
68 | else if (hibernation_mode == HIBERNATION_PLATFORM) | |
69 | hibernation_mode = HIBERNATION_SHUTDOWN; | |
70 | ||
71 | mutex_unlock(&pm_mutex); | |
72 | } | |
73 | ||
abfe2d7b RW |
74 | static bool entering_platform_hibernation; |
75 | ||
76 | bool system_entering_hibernation(void) | |
77 | { | |
78 | return entering_platform_hibernation; | |
79 | } | |
80 | EXPORT_SYMBOL(system_entering_hibernation); | |
81 | ||
4cc79776 RW |
82 | #ifdef CONFIG_PM_DEBUG |
83 | static void hibernation_debug_sleep(void) | |
84 | { | |
85 | printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n"); | |
86 | mdelay(5000); | |
87 | } | |
88 | ||
89 | static int hibernation_testmode(int mode) | |
90 | { | |
91 | if (hibernation_mode == mode) { | |
92 | hibernation_debug_sleep(); | |
93 | return 1; | |
94 | } | |
95 | return 0; | |
96 | } | |
97 | ||
98 | static int hibernation_test(int level) | |
99 | { | |
100 | if (pm_test_level == level) { | |
101 | hibernation_debug_sleep(); | |
102 | return 1; | |
103 | } | |
104 | return 0; | |
105 | } | |
106 | #else /* !CONFIG_PM_DEBUG */ | |
107 | static int hibernation_testmode(int mode) { return 0; } | |
108 | static int hibernation_test(int level) { return 0; } | |
109 | #endif /* !CONFIG_PM_DEBUG */ | |
110 | ||
74f270af | 111 | /** |
caea99ef | 112 | * platform_begin - tell the platform driver that we're starting |
74f270af RW |
113 | * hibernation |
114 | */ | |
115 | ||
caea99ef | 116 | static int platform_begin(int platform_mode) |
74f270af RW |
117 | { |
118 | return (platform_mode && hibernation_ops) ? | |
caea99ef RW |
119 | hibernation_ops->begin() : 0; |
120 | } | |
121 | ||
122 | /** | |
123 | * platform_end - tell the platform driver that we've entered the | |
124 | * working state | |
125 | */ | |
126 | ||
127 | static void platform_end(int platform_mode) | |
128 | { | |
129 | if (platform_mode && hibernation_ops) | |
130 | hibernation_ops->end(); | |
74f270af | 131 | } |
a3d25c27 | 132 | |
8a05aac2 | 133 | /** |
74f270af | 134 | * platform_pre_snapshot - prepare the machine for hibernation using the |
8a05aac2 SS |
135 | * platform driver if so configured and return an error code if it fails |
136 | */ | |
137 | ||
74f270af | 138 | static int platform_pre_snapshot(int platform_mode) |
8a05aac2 | 139 | { |
7777fab9 | 140 | return (platform_mode && hibernation_ops) ? |
74f270af | 141 | hibernation_ops->pre_snapshot() : 0; |
a3d25c27 | 142 | } |
8a05aac2 | 143 | |
c7e0831d RW |
144 | /** |
145 | * platform_leave - prepare the machine for switching to the normal mode | |
146 | * of operation using the platform driver (called with interrupts disabled) | |
147 | */ | |
148 | ||
149 | static void platform_leave(int platform_mode) | |
150 | { | |
151 | if (platform_mode && hibernation_ops) | |
152 | hibernation_ops->leave(); | |
153 | } | |
154 | ||
a3d25c27 RW |
155 | /** |
156 | * platform_finish - switch the machine to the normal mode of operation | |
157 | * using the platform driver (must be called after platform_prepare()) | |
158 | */ | |
159 | ||
7777fab9 | 160 | static void platform_finish(int platform_mode) |
a3d25c27 | 161 | { |
7777fab9 | 162 | if (platform_mode && hibernation_ops) |
a3d25c27 | 163 | hibernation_ops->finish(); |
8a05aac2 SS |
164 | } |
165 | ||
a634cc10 RW |
166 | /** |
167 | * platform_pre_restore - prepare the platform for the restoration from a | |
168 | * hibernation image. If the restore fails after this function has been | |
169 | * called, platform_restore_cleanup() must be called. | |
170 | */ | |
171 | ||
172 | static int platform_pre_restore(int platform_mode) | |
173 | { | |
174 | return (platform_mode && hibernation_ops) ? | |
175 | hibernation_ops->pre_restore() : 0; | |
176 | } | |
177 | ||
178 | /** | |
179 | * platform_restore_cleanup - switch the platform to the normal mode of | |
180 | * operation after a failing restore. If platform_pre_restore() has been | |
181 | * called before the failing restore, this function must be called too, | |
182 | * regardless of the result of platform_pre_restore(). | |
183 | */ | |
184 | ||
185 | static void platform_restore_cleanup(int platform_mode) | |
186 | { | |
187 | if (platform_mode && hibernation_ops) | |
188 | hibernation_ops->restore_cleanup(); | |
189 | } | |
190 | ||
d8f3de0d RW |
191 | /** |
192 | * platform_recover - recover the platform from a failure to suspend | |
193 | * devices. | |
194 | */ | |
195 | ||
196 | static void platform_recover(int platform_mode) | |
197 | { | |
198 | if (platform_mode && hibernation_ops && hibernation_ops->recover) | |
199 | hibernation_ops->recover(); | |
200 | } | |
201 | ||
c7e0831d RW |
202 | /** |
203 | * create_image - freeze devices that need to be frozen with interrupts | |
204 | * off, create the hibernation image and thaw those devices. Control | |
205 | * reappears in this routine after a restore. | |
206 | */ | |
207 | ||
47a460d5 | 208 | static int create_image(int platform_mode) |
c7e0831d RW |
209 | { |
210 | int error; | |
211 | ||
212 | error = arch_prepare_suspend(); | |
213 | if (error) | |
214 | return error; | |
215 | ||
1eede070 | 216 | device_pm_lock(); |
c7e0831d RW |
217 | local_irq_disable(); |
218 | /* At this point, device_suspend() has been called, but *not* | |
219 | * device_power_down(). We *must* call device_power_down() now. | |
220 | * Otherwise, drivers for some devices (e.g. interrupt controllers) | |
221 | * become desynchronized with the actual state of the hardware | |
222 | * at resume time, and evil weirdness ensues. | |
223 | */ | |
224 | error = device_power_down(PMSG_FREEZE); | |
225 | if (error) { | |
23976728 RW |
226 | printk(KERN_ERR "PM: Some devices failed to power down, " |
227 | "aborting hibernation\n"); | |
c7e0831d RW |
228 | goto Enable_irqs; |
229 | } | |
230 | ||
4cc79776 RW |
231 | if (hibernation_test(TEST_CORE)) |
232 | goto Power_up; | |
233 | ||
234 | in_suspend = 1; | |
c7e0831d RW |
235 | save_processor_state(); |
236 | error = swsusp_arch_suspend(); | |
237 | if (error) | |
23976728 RW |
238 | printk(KERN_ERR "PM: Error %d creating hibernation image\n", |
239 | error); | |
c7e0831d RW |
240 | /* Restore control flow magically appears here */ |
241 | restore_processor_state(); | |
242 | if (!in_suspend) | |
243 | platform_leave(platform_mode); | |
4cc79776 | 244 | Power_up: |
c7e0831d RW |
245 | /* NOTE: device_power_up() is just a resume() for devices |
246 | * that suspended with irqs off ... no overall powerup. | |
247 | */ | |
1eede070 RW |
248 | device_power_up(in_suspend ? |
249 | (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE); | |
c7e0831d RW |
250 | Enable_irqs: |
251 | local_irq_enable(); | |
1eede070 | 252 | device_pm_unlock(); |
c7e0831d RW |
253 | return error; |
254 | } | |
255 | ||
7777fab9 RW |
256 | /** |
257 | * hibernation_snapshot - quiesce devices and create the hibernation | |
258 | * snapshot image. | |
259 | * @platform_mode - if set, use the platform driver, if available, to | |
260 | * prepare the platform frimware for the power transition. | |
261 | * | |
262 | * Must be called with pm_mutex held | |
263 | */ | |
264 | ||
265 | int hibernation_snapshot(int platform_mode) | |
266 | { | |
cbe2f5a6 | 267 | int error; |
7777fab9 | 268 | |
3fe0313e | 269 | error = platform_begin(platform_mode); |
7777fab9 | 270 | if (error) |
10a1803d | 271 | return error; |
7777fab9 | 272 | |
3fe0313e ZR |
273 | /* Free memory before shutting down devices. */ |
274 | error = swsusp_shrink_memory(); | |
74f270af | 275 | if (error) |
caea99ef | 276 | goto Close; |
74f270af | 277 | |
7777fab9 RW |
278 | suspend_console(); |
279 | error = device_suspend(PMSG_FREEZE); | |
10a1803d | 280 | if (error) |
d8f3de0d | 281 | goto Recover_platform; |
10a1803d | 282 | |
4cc79776 | 283 | if (hibernation_test(TEST_DEVICES)) |
d8f3de0d | 284 | goto Recover_platform; |
7777fab9 | 285 | |
4cc79776 RW |
286 | error = platform_pre_snapshot(platform_mode); |
287 | if (error || hibernation_test(TEST_PLATFORM)) | |
288 | goto Finish; | |
289 | ||
7777fab9 RW |
290 | error = disable_nonboot_cpus(); |
291 | if (!error) { | |
4cc79776 RW |
292 | if (hibernation_test(TEST_CPUS)) |
293 | goto Enable_cpus; | |
294 | ||
295 | if (hibernation_testmode(HIBERNATION_TEST)) | |
296 | goto Enable_cpus; | |
297 | ||
298 | error = create_image(platform_mode); | |
299 | /* Control returns here after successful restore */ | |
7777fab9 | 300 | } |
4cc79776 | 301 | Enable_cpus: |
7777fab9 | 302 | enable_nonboot_cpus(); |
4cc79776 | 303 | Finish: |
7777fab9 | 304 | platform_finish(platform_mode); |
4cc79776 | 305 | Resume_devices: |
1eede070 RW |
306 | device_resume(in_suspend ? |
307 | (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE); | |
7777fab9 | 308 | resume_console(); |
caea99ef RW |
309 | Close: |
310 | platform_end(platform_mode); | |
7777fab9 | 311 | return error; |
d8f3de0d RW |
312 | |
313 | Recover_platform: | |
314 | platform_recover(platform_mode); | |
315 | goto Resume_devices; | |
7777fab9 RW |
316 | } |
317 | ||
72df68ca RW |
318 | /** |
319 | * resume_target_kernel - prepare devices that need to be suspended with | |
320 | * interrupts off, restore the contents of highmem that have not been | |
321 | * restored yet from the image and run the low level code that will restore | |
322 | * the remaining contents of memory and switch to the just restored target | |
323 | * kernel. | |
324 | */ | |
325 | ||
326 | static int resume_target_kernel(void) | |
327 | { | |
328 | int error; | |
329 | ||
1eede070 | 330 | device_pm_lock(); |
72df68ca | 331 | local_irq_disable(); |
1eede070 | 332 | error = device_power_down(PMSG_QUIESCE); |
72df68ca | 333 | if (error) { |
23976728 | 334 | printk(KERN_ERR "PM: Some devices failed to power down, " |
72df68ca RW |
335 | "aborting resume\n"); |
336 | goto Enable_irqs; | |
337 | } | |
338 | /* We'll ignore saved state, but this gets preempt count (etc) right */ | |
339 | save_processor_state(); | |
340 | error = restore_highmem(); | |
341 | if (!error) { | |
342 | error = swsusp_arch_resume(); | |
343 | /* | |
344 | * The code below is only ever reached in case of a failure. | |
345 | * Otherwise execution continues at place where | |
346 | * swsusp_arch_suspend() was called | |
347 | */ | |
348 | BUG_ON(!error); | |
349 | /* This call to restore_highmem() undos the previous one */ | |
350 | restore_highmem(); | |
351 | } | |
352 | /* | |
353 | * The only reason why swsusp_arch_resume() can fail is memory being | |
354 | * very tight, so we have to free it as soon as we can to avoid | |
355 | * subsequent failures | |
356 | */ | |
357 | swsusp_free(); | |
358 | restore_processor_state(); | |
359 | touch_softlockup_watchdog(); | |
1eede070 | 360 | device_power_up(PMSG_RECOVER); |
72df68ca RW |
361 | Enable_irqs: |
362 | local_irq_enable(); | |
1eede070 | 363 | device_pm_unlock(); |
72df68ca RW |
364 | return error; |
365 | } | |
366 | ||
7777fab9 RW |
367 | /** |
368 | * hibernation_restore - quiesce devices and restore the hibernation | |
369 | * snapshot image. If successful, control returns in hibernation_snaphot() | |
a634cc10 RW |
370 | * @platform_mode - if set, use the platform driver, if available, to |
371 | * prepare the platform frimware for the transition. | |
7777fab9 RW |
372 | * |
373 | * Must be called with pm_mutex held | |
374 | */ | |
375 | ||
a634cc10 | 376 | int hibernation_restore(int platform_mode) |
7777fab9 | 377 | { |
cbe2f5a6 | 378 | int error; |
7777fab9 RW |
379 | |
380 | pm_prepare_console(); | |
381 | suspend_console(); | |
1eede070 | 382 | error = device_suspend(PMSG_QUIESCE); |
7777fab9 RW |
383 | if (error) |
384 | goto Finish; | |
385 | ||
a634cc10 RW |
386 | error = platform_pre_restore(platform_mode); |
387 | if (!error) { | |
388 | error = disable_nonboot_cpus(); | |
389 | if (!error) | |
72df68ca | 390 | error = resume_target_kernel(); |
a634cc10 RW |
391 | enable_nonboot_cpus(); |
392 | } | |
393 | platform_restore_cleanup(platform_mode); | |
1eede070 | 394 | device_resume(PMSG_RECOVER); |
10a1803d | 395 | Finish: |
7777fab9 RW |
396 | resume_console(); |
397 | pm_restore_console(); | |
398 | return error; | |
399 | } | |
400 | ||
401 | /** | |
402 | * hibernation_platform_enter - enter the hibernation state using the | |
403 | * platform driver (if available) | |
404 | */ | |
405 | ||
406 | int hibernation_platform_enter(void) | |
407 | { | |
cbe2f5a6 | 408 | int error; |
b1457bcc | 409 | |
9cd9a005 RW |
410 | if (!hibernation_ops) |
411 | return -ENOSYS; | |
412 | ||
413 | /* | |
414 | * We have cancelled the power transition by running | |
415 | * hibernation_ops->finish() before saving the image, so we should let | |
416 | * the firmware know that we're going to enter the sleep state after all | |
417 | */ | |
caea99ef | 418 | error = hibernation_ops->begin(); |
9cd9a005 | 419 | if (error) |
caea99ef | 420 | goto Close; |
9cd9a005 | 421 | |
abfe2d7b | 422 | entering_platform_hibernation = true; |
9cd9a005 | 423 | suspend_console(); |
3a2d5b70 | 424 | error = device_suspend(PMSG_HIBERNATE); |
d8f3de0d RW |
425 | if (error) { |
426 | if (hibernation_ops->recover) | |
427 | hibernation_ops->recover(); | |
428 | goto Resume_devices; | |
429 | } | |
9cd9a005 RW |
430 | |
431 | error = hibernation_ops->prepare(); | |
432 | if (error) | |
433 | goto Resume_devices; | |
434 | ||
435 | error = disable_nonboot_cpus(); | |
436 | if (error) | |
437 | goto Finish; | |
438 | ||
1eede070 | 439 | device_pm_lock(); |
9cd9a005 | 440 | local_irq_disable(); |
3a2d5b70 | 441 | error = device_power_down(PMSG_HIBERNATE); |
9cd9a005 RW |
442 | if (!error) { |
443 | hibernation_ops->enter(); | |
444 | /* We should never get here */ | |
445 | while (1); | |
7777fab9 | 446 | } |
9cd9a005 | 447 | local_irq_enable(); |
1eede070 | 448 | device_pm_unlock(); |
9cd9a005 RW |
449 | |
450 | /* | |
451 | * We don't need to reenable the nonboot CPUs or resume consoles, since | |
452 | * the system is going to be halted anyway. | |
453 | */ | |
454 | Finish: | |
455 | hibernation_ops->finish(); | |
456 | Resume_devices: | |
abfe2d7b | 457 | entering_platform_hibernation = false; |
1eede070 | 458 | device_resume(PMSG_RESTORE); |
9cd9a005 | 459 | resume_console(); |
caea99ef RW |
460 | Close: |
461 | hibernation_ops->end(); | |
b1457bcc | 462 | return error; |
7777fab9 RW |
463 | } |
464 | ||
1da177e4 | 465 | /** |
a3d25c27 | 466 | * power_down - Shut the machine down for hibernation. |
1da177e4 | 467 | * |
fe0c935a JB |
468 | * Use the platform driver, if configured so; otherwise try |
469 | * to power off or reboot. | |
1da177e4 LT |
470 | */ |
471 | ||
fe0c935a | 472 | static void power_down(void) |
1da177e4 | 473 | { |
a3d25c27 RW |
474 | switch (hibernation_mode) { |
475 | case HIBERNATION_TEST: | |
476 | case HIBERNATION_TESTPROC: | |
fe0c935a | 477 | break; |
a3d25c27 | 478 | case HIBERNATION_REBOOT: |
fdde86ac | 479 | kernel_restart(NULL); |
1da177e4 | 480 | break; |
a3d25c27 | 481 | case HIBERNATION_PLATFORM: |
7777fab9 | 482 | hibernation_platform_enter(); |
9cd9a005 RW |
483 | case HIBERNATION_SHUTDOWN: |
484 | kernel_power_off(); | |
485 | break; | |
1da177e4 | 486 | } |
fdde86ac | 487 | kernel_halt(); |
fe0c935a JB |
488 | /* |
489 | * Valid image is on the disk, if we continue we risk serious data | |
490 | * corruption after resume. | |
491 | */ | |
23976728 | 492 | printk(KERN_CRIT "PM: Please power down manually\n"); |
1da177e4 LT |
493 | while(1); |
494 | } | |
495 | ||
1da177e4 LT |
496 | static int prepare_processes(void) |
497 | { | |
b918f6e6 | 498 | int error = 0; |
1da177e4 | 499 | |
1da177e4 LT |
500 | if (freeze_processes()) { |
501 | error = -EBUSY; | |
5a0a2f30 | 502 | thaw_processes(); |
1da177e4 | 503 | } |
5a72e04d | 504 | return error; |
1da177e4 LT |
505 | } |
506 | ||
1da177e4 | 507 | /** |
a3d25c27 | 508 | * hibernate - The granpappy of the built-in hibernation management |
1da177e4 LT |
509 | */ |
510 | ||
a3d25c27 | 511 | int hibernate(void) |
1da177e4 LT |
512 | { |
513 | int error; | |
514 | ||
b10d9117 | 515 | mutex_lock(&pm_mutex); |
0709db60 | 516 | /* The snapshot device should not be opened while we're running */ |
b10d9117 RW |
517 | if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { |
518 | error = -EBUSY; | |
519 | goto Unlock; | |
520 | } | |
521 | ||
5a0a2f30 | 522 | pm_prepare_console(); |
b10d9117 RW |
523 | error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE); |
524 | if (error) | |
525 | goto Exit; | |
0709db60 | 526 | |
1bfcf130 RW |
527 | error = usermodehelper_disable(); |
528 | if (error) | |
529 | goto Exit; | |
530 | ||
0709db60 RW |
531 | /* Allocate memory management structures */ |
532 | error = create_basic_memory_bitmaps(); | |
533 | if (error) | |
534 | goto Exit; | |
535 | ||
23976728 | 536 | printk(KERN_INFO "PM: Syncing filesystems ... "); |
232b1432 RW |
537 | sys_sync(); |
538 | printk("done.\n"); | |
539 | ||
1da177e4 | 540 | error = prepare_processes(); |
5a72e04d | 541 | if (error) |
0709db60 | 542 | goto Finish; |
1da177e4 | 543 | |
4cc79776 | 544 | if (hibernation_test(TEST_FREEZER)) |
ed746e3b | 545 | goto Thaw; |
4cc79776 RW |
546 | |
547 | if (hibernation_testmode(HIBERNATION_TESTPROC)) | |
548 | goto Thaw; | |
549 | ||
7777fab9 RW |
550 | error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM); |
551 | if (in_suspend && !error) { | |
a634cc10 RW |
552 | unsigned int flags = 0; |
553 | ||
554 | if (hibernation_mode == HIBERNATION_PLATFORM) | |
555 | flags |= SF_PLATFORM_MODE; | |
1da177e4 | 556 | pr_debug("PM: writing image.\n"); |
a634cc10 | 557 | error = swsusp_write(flags); |
7777fab9 | 558 | swsusp_free(); |
1da177e4 | 559 | if (!error) |
fe0c935a | 560 | power_down(); |
b918f6e6 | 561 | } else { |
1da177e4 | 562 | pr_debug("PM: Image restored successfully.\n"); |
7777fab9 | 563 | swsusp_free(); |
b918f6e6 | 564 | } |
b918f6e6 | 565 | Thaw: |
5a0a2f30 | 566 | thaw_processes(); |
0709db60 RW |
567 | Finish: |
568 | free_basic_memory_bitmaps(); | |
1bfcf130 | 569 | usermodehelper_enable(); |
0709db60 | 570 | Exit: |
b10d9117 | 571 | pm_notifier_call_chain(PM_POST_HIBERNATION); |
5a0a2f30 | 572 | pm_restore_console(); |
0709db60 | 573 | atomic_inc(&snapshot_device_available); |
b10d9117 RW |
574 | Unlock: |
575 | mutex_unlock(&pm_mutex); | |
1da177e4 LT |
576 | return error; |
577 | } | |
578 | ||
579 | ||
580 | /** | |
581 | * software_resume - Resume from a saved image. | |
582 | * | |
583 | * Called as a late_initcall (so all devices are discovered and | |
584 | * initialized), we call swsusp to see if we have a saved image or not. | |
585 | * If so, we quiesce devices, the restore the saved image. We will | |
a3d25c27 | 586 | * return above (in hibernate() ) if everything goes well. |
1da177e4 LT |
587 | * Otherwise, we fail gracefully and return to the normally |
588 | * scheduled program. | |
589 | * | |
590 | */ | |
591 | ||
592 | static int software_resume(void) | |
593 | { | |
594 | int error; | |
a634cc10 | 595 | unsigned int flags; |
1da177e4 | 596 | |
60a0d233 JB |
597 | /* |
598 | * name_to_dev_t() below takes a sysfs buffer mutex when sysfs | |
599 | * is configured into the kernel. Since the regular hibernate | |
600 | * trigger path is via sysfs which takes a buffer mutex before | |
601 | * calling hibernate functions (which take pm_mutex) this can | |
602 | * cause lockdep to complain about a possible ABBA deadlock | |
603 | * which cannot happen since we're in the boot code here and | |
604 | * sysfs can't be invoked yet. Therefore, we use a subclass | |
605 | * here to avoid lockdep complaining. | |
606 | */ | |
607 | mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING); | |
3efa147a | 608 | if (!swsusp_resume_device) { |
dd5d666b | 609 | if (!strlen(resume_file)) { |
a6d70980 | 610 | mutex_unlock(&pm_mutex); |
3efa147a | 611 | return -ENOENT; |
dd5d666b | 612 | } |
3efa147a | 613 | swsusp_resume_device = name_to_dev_t(resume_file); |
23976728 | 614 | pr_debug("PM: Resume from partition %s\n", resume_file); |
3efa147a | 615 | } else { |
23976728 RW |
616 | pr_debug("PM: Resume from partition %d:%d\n", |
617 | MAJOR(swsusp_resume_device), | |
618 | MINOR(swsusp_resume_device)); | |
3efa147a PM |
619 | } |
620 | ||
1da177e4 LT |
621 | if (noresume) { |
622 | /** | |
9575809c RW |
623 | * FIXME: If noresume is specified, we need to find the |
624 | * partition and reset it back to normal swap space. | |
1da177e4 | 625 | */ |
a6d70980 | 626 | mutex_unlock(&pm_mutex); |
1da177e4 LT |
627 | return 0; |
628 | } | |
629 | ||
23976728 | 630 | pr_debug("PM: Checking hibernation image.\n"); |
ed746e3b RW |
631 | error = swsusp_check(); |
632 | if (error) | |
74dfd666 | 633 | goto Unlock; |
1da177e4 | 634 | |
0709db60 RW |
635 | /* The snapshot device should not be opened while we're running */ |
636 | if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { | |
637 | error = -EBUSY; | |
638 | goto Unlock; | |
639 | } | |
640 | ||
5a0a2f30 | 641 | pm_prepare_console(); |
c3e94d89 AS |
642 | error = pm_notifier_call_chain(PM_RESTORE_PREPARE); |
643 | if (error) | |
644 | goto Finish; | |
645 | ||
1bfcf130 RW |
646 | error = usermodehelper_disable(); |
647 | if (error) | |
648 | goto Finish; | |
649 | ||
74dfd666 RW |
650 | error = create_basic_memory_bitmaps(); |
651 | if (error) | |
0709db60 | 652 | goto Finish; |
1da177e4 | 653 | |
74dfd666 | 654 | pr_debug("PM: Preparing processes for restore.\n"); |
ed746e3b RW |
655 | error = prepare_processes(); |
656 | if (error) { | |
c2dd0dae | 657 | swsusp_close(FMODE_READ); |
5a72e04d | 658 | goto Done; |
1da177e4 LT |
659 | } |
660 | ||
23976728 | 661 | pr_debug("PM: Reading hibernation image.\n"); |
1da177e4 | 662 | |
a634cc10 | 663 | error = swsusp_read(&flags); |
ed746e3b | 664 | if (!error) |
a634cc10 | 665 | hibernation_restore(flags & SF_PLATFORM_MODE); |
1da177e4 | 666 | |
ed746e3b | 667 | printk(KERN_ERR "PM: Restore failed, recovering.\n"); |
7777fab9 | 668 | swsusp_free(); |
5a0a2f30 | 669 | thaw_processes(); |
1da177e4 | 670 | Done: |
74dfd666 | 671 | free_basic_memory_bitmaps(); |
1bfcf130 | 672 | usermodehelper_enable(); |
0709db60 | 673 | Finish: |
c3e94d89 | 674 | pm_notifier_call_chain(PM_POST_RESTORE); |
5a0a2f30 | 675 | pm_restore_console(); |
0709db60 | 676 | atomic_inc(&snapshot_device_available); |
dd5d666b | 677 | /* For success case, the suspend path will release the lock */ |
74dfd666 | 678 | Unlock: |
a6d70980 | 679 | mutex_unlock(&pm_mutex); |
1da177e4 | 680 | pr_debug("PM: Resume from disk failed.\n"); |
7777fab9 | 681 | return error; |
1da177e4 LT |
682 | } |
683 | ||
684 | late_initcall(software_resume); | |
685 | ||
686 | ||
a3d25c27 RW |
687 | static const char * const hibernation_modes[] = { |
688 | [HIBERNATION_PLATFORM] = "platform", | |
689 | [HIBERNATION_SHUTDOWN] = "shutdown", | |
690 | [HIBERNATION_REBOOT] = "reboot", | |
691 | [HIBERNATION_TEST] = "test", | |
692 | [HIBERNATION_TESTPROC] = "testproc", | |
1da177e4 LT |
693 | }; |
694 | ||
695 | /** | |
a3d25c27 | 696 | * disk - Control hibernation mode |
1da177e4 | 697 | * |
11d77d0c JB |
698 | * Suspend-to-disk can be handled in several ways. We have a few options |
699 | * for putting the system to sleep - using the platform driver (e.g. ACPI | |
a3d25c27 RW |
700 | * or other hibernation_ops), powering off the system or rebooting the |
701 | * system (for testing) as well as the two test modes. | |
1da177e4 | 702 | * |
11d77d0c | 703 | * The system can support 'platform', and that is known a priori (and |
a3d25c27 RW |
704 | * encoded by the presence of hibernation_ops). However, the user may |
705 | * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the | |
706 | * test modes, 'test' or 'testproc'. | |
1da177e4 LT |
707 | * |
708 | * show() will display what the mode is currently set to. | |
709 | * store() will accept one of | |
710 | * | |
1da177e4 LT |
711 | * 'platform' |
712 | * 'shutdown' | |
713 | * 'reboot' | |
11d77d0c JB |
714 | * 'test' |
715 | * 'testproc' | |
1da177e4 | 716 | * |
11d77d0c | 717 | * It will only change to 'platform' if the system |
a3d25c27 | 718 | * supports it (as determined by having hibernation_ops). |
1da177e4 LT |
719 | */ |
720 | ||
386f275f KS |
721 | static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr, |
722 | char *buf) | |
1da177e4 | 723 | { |
f0ced9b2 JB |
724 | int i; |
725 | char *start = buf; | |
726 | ||
a3d25c27 RW |
727 | for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { |
728 | if (!hibernation_modes[i]) | |
f0ced9b2 JB |
729 | continue; |
730 | switch (i) { | |
a3d25c27 RW |
731 | case HIBERNATION_SHUTDOWN: |
732 | case HIBERNATION_REBOOT: | |
733 | case HIBERNATION_TEST: | |
734 | case HIBERNATION_TESTPROC: | |
f0ced9b2 | 735 | break; |
a3d25c27 RW |
736 | case HIBERNATION_PLATFORM: |
737 | if (hibernation_ops) | |
f0ced9b2 JB |
738 | break; |
739 | /* not a valid mode, continue with loop */ | |
740 | continue; | |
741 | } | |
a3d25c27 RW |
742 | if (i == hibernation_mode) |
743 | buf += sprintf(buf, "[%s] ", hibernation_modes[i]); | |
f0ced9b2 | 744 | else |
a3d25c27 | 745 | buf += sprintf(buf, "%s ", hibernation_modes[i]); |
f0ced9b2 JB |
746 | } |
747 | buf += sprintf(buf, "\n"); | |
748 | return buf-start; | |
1da177e4 LT |
749 | } |
750 | ||
751 | ||
386f275f KS |
752 | static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, |
753 | const char *buf, size_t n) | |
1da177e4 LT |
754 | { |
755 | int error = 0; | |
756 | int i; | |
757 | int len; | |
758 | char *p; | |
a3d25c27 | 759 | int mode = HIBERNATION_INVALID; |
1da177e4 LT |
760 | |
761 | p = memchr(buf, '\n', n); | |
762 | len = p ? p - buf : n; | |
763 | ||
a6d70980 | 764 | mutex_lock(&pm_mutex); |
a3d25c27 | 765 | for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { |
8d98a690 RW |
766 | if (len == strlen(hibernation_modes[i]) |
767 | && !strncmp(buf, hibernation_modes[i], len)) { | |
1da177e4 LT |
768 | mode = i; |
769 | break; | |
770 | } | |
771 | } | |
a3d25c27 | 772 | if (mode != HIBERNATION_INVALID) { |
fe0c935a | 773 | switch (mode) { |
a3d25c27 RW |
774 | case HIBERNATION_SHUTDOWN: |
775 | case HIBERNATION_REBOOT: | |
776 | case HIBERNATION_TEST: | |
777 | case HIBERNATION_TESTPROC: | |
778 | hibernation_mode = mode; | |
fe0c935a | 779 | break; |
a3d25c27 RW |
780 | case HIBERNATION_PLATFORM: |
781 | if (hibernation_ops) | |
782 | hibernation_mode = mode; | |
1da177e4 LT |
783 | else |
784 | error = -EINVAL; | |
785 | } | |
a3d25c27 | 786 | } else |
1da177e4 LT |
787 | error = -EINVAL; |
788 | ||
a3d25c27 | 789 | if (!error) |
23976728 | 790 | pr_debug("PM: Hibernation mode set to '%s'\n", |
a3d25c27 | 791 | hibernation_modes[mode]); |
a6d70980 | 792 | mutex_unlock(&pm_mutex); |
1da177e4 LT |
793 | return error ? error : n; |
794 | } | |
795 | ||
796 | power_attr(disk); | |
797 | ||
386f275f KS |
798 | static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr, |
799 | char *buf) | |
1da177e4 LT |
800 | { |
801 | return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device), | |
802 | MINOR(swsusp_resume_device)); | |
803 | } | |
804 | ||
386f275f KS |
805 | static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr, |
806 | const char *buf, size_t n) | |
1da177e4 | 807 | { |
1da177e4 | 808 | unsigned int maj, min; |
1da177e4 | 809 | dev_t res; |
a576219a | 810 | int ret = -EINVAL; |
1da177e4 | 811 | |
a576219a AM |
812 | if (sscanf(buf, "%u:%u", &maj, &min) != 2) |
813 | goto out; | |
1da177e4 | 814 | |
a576219a AM |
815 | res = MKDEV(maj,min); |
816 | if (maj != MAJOR(res) || min != MINOR(res)) | |
817 | goto out; | |
1da177e4 | 818 | |
a6d70980 | 819 | mutex_lock(&pm_mutex); |
a576219a | 820 | swsusp_resume_device = res; |
a6d70980 | 821 | mutex_unlock(&pm_mutex); |
23976728 | 822 | printk(KERN_INFO "PM: Starting manual resume from disk\n"); |
a576219a AM |
823 | noresume = 0; |
824 | software_resume(); | |
825 | ret = n; | |
59a49335 | 826 | out: |
a576219a | 827 | return ret; |
1da177e4 LT |
828 | } |
829 | ||
830 | power_attr(resume); | |
831 | ||
386f275f KS |
832 | static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr, |
833 | char *buf) | |
ca0aec0f | 834 | { |
853609b6 | 835 | return sprintf(buf, "%lu\n", image_size); |
ca0aec0f RW |
836 | } |
837 | ||
386f275f KS |
838 | static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr, |
839 | const char *buf, size_t n) | |
ca0aec0f | 840 | { |
853609b6 | 841 | unsigned long size; |
ca0aec0f | 842 | |
853609b6 | 843 | if (sscanf(buf, "%lu", &size) == 1) { |
ca0aec0f RW |
844 | image_size = size; |
845 | return n; | |
846 | } | |
847 | ||
848 | return -EINVAL; | |
849 | } | |
850 | ||
851 | power_attr(image_size); | |
852 | ||
1da177e4 LT |
853 | static struct attribute * g[] = { |
854 | &disk_attr.attr, | |
855 | &resume_attr.attr, | |
ca0aec0f | 856 | &image_size_attr.attr, |
1da177e4 LT |
857 | NULL, |
858 | }; | |
859 | ||
860 | ||
861 | static struct attribute_group attr_group = { | |
862 | .attrs = g, | |
863 | }; | |
864 | ||
865 | ||
866 | static int __init pm_disk_init(void) | |
867 | { | |
d76e15fb | 868 | return sysfs_create_group(power_kobj, &attr_group); |
1da177e4 LT |
869 | } |
870 | ||
871 | core_initcall(pm_disk_init); | |
872 | ||
873 | ||
874 | static int __init resume_setup(char *str) | |
875 | { | |
876 | if (noresume) | |
877 | return 1; | |
878 | ||
879 | strncpy( resume_file, str, 255 ); | |
880 | return 1; | |
881 | } | |
882 | ||
9a154d9d RW |
883 | static int __init resume_offset_setup(char *str) |
884 | { | |
885 | unsigned long long offset; | |
886 | ||
887 | if (noresume) | |
888 | return 1; | |
889 | ||
890 | if (sscanf(str, "%llu", &offset) == 1) | |
891 | swsusp_resume_block = offset; | |
892 | ||
893 | return 1; | |
894 | } | |
895 | ||
1da177e4 LT |
896 | static int __init noresume_setup(char *str) |
897 | { | |
898 | noresume = 1; | |
899 | return 1; | |
900 | } | |
901 | ||
902 | __setup("noresume", noresume_setup); | |
9a154d9d | 903 | __setup("resume_offset=", resume_offset_setup); |
1da177e4 | 904 | __setup("resume=", resume_setup); |