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