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