Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
8b759b84 | 2 | * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support. |
1da177e4 LT |
3 | * |
4 | * Copyright (c) 2003 Patrick Mochel | |
5 | * Copyright (c) 2003 Open Source Development Lab | |
a2531293 | 6 | * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz> |
8b759b84 | 7 | * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc. |
62c552cc | 8 | * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com> |
1da177e4 LT |
9 | * |
10 | * This file is released under the GPLv2. | |
1da177e4 LT |
11 | */ |
12 | ||
2872de13 RW |
13 | #define pr_fmt(fmt) "PM: " fmt |
14 | ||
6e5fdeed | 15 | #include <linux/export.h> |
1da177e4 | 16 | #include <linux/suspend.h> |
1da177e4 LT |
17 | #include <linux/reboot.h> |
18 | #include <linux/string.h> | |
19 | #include <linux/device.h> | |
6f8d7022 | 20 | #include <linux/async.h> |
1da177e4 LT |
21 | #include <linux/delay.h> |
22 | #include <linux/fs.h> | |
d53d9f16 | 23 | #include <linux/mount.h> |
88d10bba | 24 | #include <linux/pm.h> |
38b8d208 | 25 | #include <linux/nmi.h> |
97c7801c | 26 | #include <linux/console.h> |
e3920fb4 | 27 | #include <linux/cpu.h> |
7dfb7103 | 28 | #include <linux/freezer.h> |
5a0e3ad6 | 29 | #include <linux/gfp.h> |
40dc166c | 30 | #include <linux/syscore_ops.h> |
2df83fa4 MB |
31 | #include <linux/ctype.h> |
32 | #include <linux/genhd.h> | |
db597605 | 33 | #include <linux/ktime.h> |
bb3632c6 | 34 | #include <trace/events/power.h> |
d53d9f16 | 35 | |
1da177e4 LT |
36 | #include "power.h" |
37 | ||
38 | ||
d231ff1a BS |
39 | static int nocompress; |
40 | static int noresume; | |
a6e15a39 | 41 | static int nohibernate; |
d231ff1a | 42 | static int resume_wait; |
f6514be5 | 43 | static unsigned int resume_delay; |
47a460d5 | 44 | static char resume_file[256] = CONFIG_PM_STD_PARTITION; |
1da177e4 | 45 | dev_t swsusp_resume_device; |
9a154d9d | 46 | sector_t swsusp_resume_block; |
d6efc2f7 | 47 | __visible int in_suspend __nosavedata; |
1da177e4 | 48 | |
a3d25c27 RW |
49 | enum { |
50 | HIBERNATION_INVALID, | |
51 | HIBERNATION_PLATFORM, | |
a3d25c27 RW |
52 | HIBERNATION_SHUTDOWN, |
53 | HIBERNATION_REBOOT, | |
62c552cc BS |
54 | #ifdef CONFIG_SUSPEND |
55 | HIBERNATION_SUSPEND, | |
56 | #endif | |
fe12c00d | 57 | HIBERNATION_TEST_RESUME, |
a3d25c27 RW |
58 | /* keep last */ |
59 | __HIBERNATION_AFTER_LAST | |
60 | }; | |
61 | #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1) | |
62 | #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1) | |
63 | ||
64 | static int hibernation_mode = HIBERNATION_SHUTDOWN; | |
65 | ||
97819a26 | 66 | bool freezer_test_done; |
aa9a7b11 | 67 | |
073ef1f6 | 68 | static const struct platform_hibernation_ops *hibernation_ops; |
a3d25c27 | 69 | |
a6e15a39 KC |
70 | bool hibernation_available(void) |
71 | { | |
72 | return (nohibernate == 0); | |
73 | } | |
74 | ||
a3d25c27 | 75 | /** |
f42a9813 RW |
76 | * hibernation_set_ops - Set the global hibernate operations. |
77 | * @ops: Hibernation operations to use in subsequent hibernation transitions. | |
a3d25c27 | 78 | */ |
073ef1f6 | 79 | void hibernation_set_ops(const struct platform_hibernation_ops *ops) |
a3d25c27 | 80 | { |
caea99ef RW |
81 | if (ops && !(ops->begin && ops->end && ops->pre_snapshot |
82 | && ops->prepare && ops->finish && ops->enter && ops->pre_restore | |
5729c63a | 83 | && ops->restore_cleanup && ops->leave)) { |
a3d25c27 RW |
84 | WARN_ON(1); |
85 | return; | |
86 | } | |
bcda53fa | 87 | lock_system_sleep(); |
a3d25c27 RW |
88 | hibernation_ops = ops; |
89 | if (ops) | |
90 | hibernation_mode = HIBERNATION_PLATFORM; | |
91 | else if (hibernation_mode == HIBERNATION_PLATFORM) | |
92 | hibernation_mode = HIBERNATION_SHUTDOWN; | |
93 | ||
bcda53fa | 94 | unlock_system_sleep(); |
a3d25c27 | 95 | } |
e0c7855e | 96 | EXPORT_SYMBOL_GPL(hibernation_set_ops); |
a3d25c27 | 97 | |
abfe2d7b RW |
98 | static bool entering_platform_hibernation; |
99 | ||
100 | bool system_entering_hibernation(void) | |
101 | { | |
102 | return entering_platform_hibernation; | |
103 | } | |
104 | EXPORT_SYMBOL(system_entering_hibernation); | |
105 | ||
4cc79776 RW |
106 | #ifdef CONFIG_PM_DEBUG |
107 | static void hibernation_debug_sleep(void) | |
108 | { | |
2872de13 | 109 | pr_info("hibernation debug: Waiting for 5 seconds.\n"); |
4cc79776 RW |
110 | mdelay(5000); |
111 | } | |
112 | ||
4cc79776 RW |
113 | static int hibernation_test(int level) |
114 | { | |
115 | if (pm_test_level == level) { | |
116 | hibernation_debug_sleep(); | |
117 | return 1; | |
118 | } | |
119 | return 0; | |
120 | } | |
121 | #else /* !CONFIG_PM_DEBUG */ | |
4cc79776 RW |
122 | static int hibernation_test(int level) { return 0; } |
123 | #endif /* !CONFIG_PM_DEBUG */ | |
124 | ||
74f270af | 125 | /** |
f42a9813 RW |
126 | * platform_begin - Call platform to start hibernation. |
127 | * @platform_mode: Whether or not to use the platform driver. | |
74f270af | 128 | */ |
caea99ef | 129 | static int platform_begin(int platform_mode) |
74f270af RW |
130 | { |
131 | return (platform_mode && hibernation_ops) ? | |
caea99ef RW |
132 | hibernation_ops->begin() : 0; |
133 | } | |
134 | ||
135 | /** | |
f42a9813 RW |
136 | * platform_end - Call platform to finish transition to the working state. |
137 | * @platform_mode: Whether or not to use the platform driver. | |
caea99ef | 138 | */ |
caea99ef RW |
139 | static void platform_end(int platform_mode) |
140 | { | |
141 | if (platform_mode && hibernation_ops) | |
142 | hibernation_ops->end(); | |
74f270af | 143 | } |
a3d25c27 | 144 | |
8a05aac2 | 145 | /** |
f42a9813 RW |
146 | * platform_pre_snapshot - Call platform to prepare the machine for hibernation. |
147 | * @platform_mode: Whether or not to use the platform driver. | |
148 | * | |
149 | * Use the platform driver to prepare the system for creating a hibernate image, | |
150 | * if so configured, and return an error code if that fails. | |
8a05aac2 SS |
151 | */ |
152 | ||
74f270af | 153 | static int platform_pre_snapshot(int platform_mode) |
8a05aac2 | 154 | { |
7777fab9 | 155 | return (platform_mode && hibernation_ops) ? |
74f270af | 156 | hibernation_ops->pre_snapshot() : 0; |
a3d25c27 | 157 | } |
8a05aac2 | 158 | |
c7e0831d | 159 | /** |
f42a9813 RW |
160 | * platform_leave - Call platform to prepare a transition to the working state. |
161 | * @platform_mode: Whether or not to use the platform driver. | |
162 | * | |
163 | * Use the platform driver prepare to prepare the machine for switching to the | |
164 | * normal mode of operation. | |
165 | * | |
166 | * This routine is called on one CPU with interrupts disabled. | |
c7e0831d | 167 | */ |
c7e0831d RW |
168 | static void platform_leave(int platform_mode) |
169 | { | |
170 | if (platform_mode && hibernation_ops) | |
171 | hibernation_ops->leave(); | |
172 | } | |
173 | ||
a3d25c27 | 174 | /** |
f42a9813 RW |
175 | * platform_finish - Call platform to switch the system to the working state. |
176 | * @platform_mode: Whether or not to use the platform driver. | |
177 | * | |
178 | * Use the platform driver to switch the machine to the normal mode of | |
179 | * operation. | |
180 | * | |
181 | * This routine must be called after platform_prepare(). | |
a3d25c27 | 182 | */ |
7777fab9 | 183 | static void platform_finish(int platform_mode) |
a3d25c27 | 184 | { |
7777fab9 | 185 | if (platform_mode && hibernation_ops) |
a3d25c27 | 186 | hibernation_ops->finish(); |
8a05aac2 SS |
187 | } |
188 | ||
a634cc10 | 189 | /** |
f42a9813 RW |
190 | * platform_pre_restore - Prepare for hibernate image restoration. |
191 | * @platform_mode: Whether or not to use the platform driver. | |
192 | * | |
193 | * Use the platform driver to prepare the system for resume from a hibernation | |
194 | * image. | |
195 | * | |
196 | * If the restore fails after this function has been called, | |
197 | * platform_restore_cleanup() must be called. | |
a634cc10 | 198 | */ |
a634cc10 RW |
199 | static int platform_pre_restore(int platform_mode) |
200 | { | |
201 | return (platform_mode && hibernation_ops) ? | |
202 | hibernation_ops->pre_restore() : 0; | |
203 | } | |
204 | ||
205 | /** | |
f42a9813 RW |
206 | * platform_restore_cleanup - Switch to the working state after failing restore. |
207 | * @platform_mode: Whether or not to use the platform driver. | |
208 | * | |
209 | * Use the platform driver to switch the system to the normal mode of operation | |
210 | * after a failing restore. | |
211 | * | |
212 | * If platform_pre_restore() has been called before the failing restore, this | |
213 | * function must be called too, regardless of the result of | |
214 | * platform_pre_restore(). | |
a634cc10 | 215 | */ |
a634cc10 RW |
216 | static void platform_restore_cleanup(int platform_mode) |
217 | { | |
218 | if (platform_mode && hibernation_ops) | |
219 | hibernation_ops->restore_cleanup(); | |
220 | } | |
221 | ||
d8f3de0d | 222 | /** |
f42a9813 RW |
223 | * platform_recover - Recover from a failure to suspend devices. |
224 | * @platform_mode: Whether or not to use the platform driver. | |
d8f3de0d | 225 | */ |
d8f3de0d RW |
226 | static void platform_recover(int platform_mode) |
227 | { | |
228 | if (platform_mode && hibernation_ops && hibernation_ops->recover) | |
229 | hibernation_ops->recover(); | |
230 | } | |
231 | ||
8e60c6a1 | 232 | /** |
f42a9813 RW |
233 | * swsusp_show_speed - Print time elapsed between two events during hibernation. |
234 | * @start: Starting event. | |
235 | * @stop: Final event. | |
236 | * @nr_pages: Number of memory pages processed between @start and @stop. | |
237 | * @msg: Additional diagnostic message to print. | |
8e60c6a1 | 238 | */ |
db597605 TR |
239 | void swsusp_show_speed(ktime_t start, ktime_t stop, |
240 | unsigned nr_pages, char *msg) | |
8e60c6a1 | 241 | { |
db597605 | 242 | ktime_t diff; |
4881f603 CG |
243 | u64 elapsed_centisecs64; |
244 | unsigned int centisecs; | |
245 | unsigned int k; | |
246 | unsigned int kps; | |
8e60c6a1 | 247 | |
db597605 TR |
248 | diff = ktime_sub(stop, start); |
249 | elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC); | |
8e60c6a1 NC |
250 | centisecs = elapsed_centisecs64; |
251 | if (centisecs == 0) | |
252 | centisecs = 1; /* avoid div-by-zero */ | |
253 | k = nr_pages * (PAGE_SIZE / 1024); | |
254 | kps = (k * 100) / centisecs; | |
2872de13 RW |
255 | pr_info("%s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n", |
256 | msg, k, centisecs / 100, centisecs % 100, kps / 1000, | |
257 | (kps % 1000) / 10); | |
8e60c6a1 NC |
258 | } |
259 | ||
c7e0831d | 260 | /** |
f42a9813 RW |
261 | * create_image - Create a hibernation image. |
262 | * @platform_mode: Whether or not to use the platform driver. | |
263 | * | |
cf579dfb RW |
264 | * Execute device drivers' "late" and "noirq" freeze callbacks, create a |
265 | * hibernation image and run the drivers' "noirq" and "early" thaw callbacks. | |
f42a9813 RW |
266 | * |
267 | * Control reappears in this routine after the subsequent restore. | |
c7e0831d | 268 | */ |
47a460d5 | 269 | static int create_image(int platform_mode) |
c7e0831d RW |
270 | { |
271 | int error; | |
272 | ||
cf579dfb | 273 | error = dpm_suspend_end(PMSG_FREEZE); |
c7e0831d | 274 | if (error) { |
2872de13 | 275 | pr_err("Some devices failed to power down, aborting hibernation\n"); |
32bdfac5 | 276 | return error; |
c7e0831d | 277 | } |
2ed8d2b3 | 278 | |
4aecd671 RW |
279 | error = platform_pre_snapshot(platform_mode); |
280 | if (error || hibernation_test(TEST_PLATFORM)) | |
281 | goto Platform_finish; | |
282 | ||
2f1a6fbb | 283 | error = suspend_disable_secondary_cpus(); |
48580ab8 | 284 | if (error || hibernation_test(TEST_CPUS)) |
4aecd671 RW |
285 | goto Enable_cpus; |
286 | ||
2ed8d2b3 RW |
287 | local_irq_disable(); |
288 | ||
c1a957d1 TG |
289 | system_state = SYSTEM_SUSPEND; |
290 | ||
2e711c04 | 291 | error = syscore_suspend(); |
770824bd | 292 | if (error) { |
2872de13 | 293 | pr_err("Some system devices failed to power down, aborting hibernation\n"); |
4aecd671 | 294 | goto Enable_irqs; |
770824bd | 295 | } |
c7e0831d | 296 | |
a2867e08 | 297 | if (hibernation_test(TEST_CORE) || pm_wakeup_pending()) |
4cc79776 RW |
298 | goto Power_up; |
299 | ||
300 | in_suspend = 1; | |
c7e0831d | 301 | save_processor_state(); |
bb3632c6 | 302 | trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true); |
c7e0831d | 303 | error = swsusp_arch_suspend(); |
62822e2e TG |
304 | /* Restore control flow magically appears here */ |
305 | restore_processor_state(); | |
bb3632c6 | 306 | trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false); |
c7e0831d | 307 | if (error) |
2872de13 RW |
308 | pr_err("Error %d creating hibernation image\n", error); |
309 | ||
1ad1410f | 310 | if (!in_suspend) { |
c125e96f | 311 | events_check_enabled = false; |
1ad1410f AA |
312 | clear_free_pages(); |
313 | } | |
362e77d1 BM |
314 | |
315 | platform_leave(platform_mode); | |
4aecd671 | 316 | |
4cc79776 | 317 | Power_up: |
40dc166c | 318 | syscore_resume(); |
2ed8d2b3 | 319 | |
4aecd671 | 320 | Enable_irqs: |
c1a957d1 | 321 | system_state = SYSTEM_RUNNING; |
2ed8d2b3 RW |
322 | local_irq_enable(); |
323 | ||
4aecd671 | 324 | Enable_cpus: |
2f1a6fbb | 325 | suspend_enable_secondary_cpus(); |
4aecd671 RW |
326 | |
327 | Platform_finish: | |
328 | platform_finish(platform_mode); | |
329 | ||
cf579dfb | 330 | dpm_resume_start(in_suspend ? |
1eede070 | 331 | (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE); |
2ed8d2b3 | 332 | |
c7e0831d RW |
333 | return error; |
334 | } | |
335 | ||
7777fab9 | 336 | /** |
f42a9813 RW |
337 | * hibernation_snapshot - Quiesce devices and create a hibernation image. |
338 | * @platform_mode: If set, use platform driver to prepare for the transition. | |
7777fab9 | 339 | * |
55f2503c | 340 | * This routine must be called with system_transition_mutex held. |
7777fab9 | 341 | */ |
7777fab9 RW |
342 | int hibernation_snapshot(int platform_mode) |
343 | { | |
953a2063 | 344 | pm_message_t msg; |
cbe2f5a6 | 345 | int error; |
7777fab9 | 346 | |
27614273 | 347 | pm_suspend_clear_flags(); |
3fe0313e | 348 | error = platform_begin(platform_mode); |
7777fab9 | 349 | if (error) |
d074ee02 | 350 | goto Close; |
7777fab9 | 351 | |
64a473cb RW |
352 | /* Preallocate image memory before shutting down devices. */ |
353 | error = hibernate_preallocate_memory(); | |
2aede851 RW |
354 | if (error) |
355 | goto Close; | |
356 | ||
357 | error = freeze_kernel_threads(); | |
358 | if (error) | |
bb58dd5d | 359 | goto Cleanup; |
2aede851 | 360 | |
48580ab8 | 361 | if (hibernation_test(TEST_FREEZER)) { |
aa9a7b11 SB |
362 | |
363 | /* | |
364 | * Indicate to the caller that we are returning due to a | |
365 | * successful freezer test. | |
366 | */ | |
367 | freezer_test_done = true; | |
51d6ff7a | 368 | goto Thaw; |
aa9a7b11 SB |
369 | } |
370 | ||
2aede851 | 371 | error = dpm_prepare(PMSG_FREEZE); |
bb58dd5d | 372 | if (error) { |
953a2063 | 373 | dpm_complete(PMSG_RECOVER); |
51d6ff7a | 374 | goto Thaw; |
bb58dd5d | 375 | } |
74f270af | 376 | |
7777fab9 | 377 | suspend_console(); |
c9e664f1 | 378 | pm_restrict_gfp_mask(); |
953a2063 | 379 | |
91e7c75b | 380 | error = dpm_suspend(PMSG_FREEZE); |
10a1803d | 381 | |
953a2063 SB |
382 | if (error || hibernation_test(TEST_DEVICES)) |
383 | platform_recover(platform_mode); | |
384 | else | |
385 | error = create_image(platform_mode); | |
7777fab9 | 386 | |
c9e664f1 | 387 | /* |
953a2063 SB |
388 | * In the case that we call create_image() above, the control |
389 | * returns here (1) after the image has been created or the | |
c9e664f1 RW |
390 | * image creation has failed and (2) after a successful restore. |
391 | */ | |
4cc79776 | 392 | |
64a473cb RW |
393 | /* We may need to release the preallocated image pages here. */ |
394 | if (error || !in_suspend) | |
395 | swsusp_free(); | |
396 | ||
91e7c75b RW |
397 | msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE; |
398 | dpm_resume(msg); | |
c9e664f1 RW |
399 | |
400 | if (error || !in_suspend) | |
401 | pm_restore_gfp_mask(); | |
402 | ||
7777fab9 | 403 | resume_console(); |
91e7c75b RW |
404 | dpm_complete(msg); |
405 | ||
caea99ef RW |
406 | Close: |
407 | platform_end(platform_mode); | |
7777fab9 | 408 | return error; |
d8f3de0d | 409 | |
51d6ff7a SB |
410 | Thaw: |
411 | thaw_kernel_threads(); | |
bb58dd5d RW |
412 | Cleanup: |
413 | swsusp_free(); | |
414 | goto Close; | |
7777fab9 RW |
415 | } |
416 | ||
406f992e RW |
417 | int __weak hibernate_resume_nonboot_cpu_disable(void) |
418 | { | |
2f1a6fbb | 419 | return suspend_disable_secondary_cpus(); |
406f992e RW |
420 | } |
421 | ||
72df68ca | 422 | /** |
f42a9813 RW |
423 | * resume_target_kernel - Restore system state from a hibernation image. |
424 | * @platform_mode: Whether or not to use the platform driver. | |
425 | * | |
cf579dfb RW |
426 | * Execute device drivers' "noirq" and "late" freeze callbacks, restore the |
427 | * contents of highmem that have not been restored yet from the image and run | |
428 | * the low-level code that will restore the remaining contents of memory and | |
429 | * switch to the just restored target kernel. | |
72df68ca | 430 | */ |
4aecd671 | 431 | static int resume_target_kernel(bool platform_mode) |
72df68ca RW |
432 | { |
433 | int error; | |
434 | ||
cf579dfb | 435 | error = dpm_suspend_end(PMSG_QUIESCE); |
72df68ca | 436 | if (error) { |
2872de13 | 437 | pr_err("Some devices failed to power down, aborting resume\n"); |
32bdfac5 | 438 | return error; |
72df68ca | 439 | } |
2ed8d2b3 | 440 | |
4aecd671 RW |
441 | error = platform_pre_restore(platform_mode); |
442 | if (error) | |
443 | goto Cleanup; | |
444 | ||
406f992e | 445 | error = hibernate_resume_nonboot_cpu_disable(); |
4aecd671 RW |
446 | if (error) |
447 | goto Enable_cpus; | |
448 | ||
2ed8d2b3 | 449 | local_irq_disable(); |
c1a957d1 | 450 | system_state = SYSTEM_SUSPEND; |
2ed8d2b3 | 451 | |
2e711c04 | 452 | error = syscore_suspend(); |
4aecd671 RW |
453 | if (error) |
454 | goto Enable_irqs; | |
455 | ||
72df68ca RW |
456 | save_processor_state(); |
457 | error = restore_highmem(); | |
458 | if (!error) { | |
459 | error = swsusp_arch_resume(); | |
460 | /* | |
461 | * The code below is only ever reached in case of a failure. | |
4e2d9491 RW |
462 | * Otherwise, execution continues at the place where |
463 | * swsusp_arch_suspend() was called. | |
72df68ca RW |
464 | */ |
465 | BUG_ON(!error); | |
4e2d9491 RW |
466 | /* |
467 | * This call to restore_highmem() reverts the changes made by | |
468 | * the previous one. | |
469 | */ | |
72df68ca RW |
470 | restore_highmem(); |
471 | } | |
472 | /* | |
473 | * The only reason why swsusp_arch_resume() can fail is memory being | |
474 | * very tight, so we have to free it as soon as we can to avoid | |
4e2d9491 | 475 | * subsequent failures. |
72df68ca RW |
476 | */ |
477 | swsusp_free(); | |
478 | restore_processor_state(); | |
479 | touch_softlockup_watchdog(); | |
2ed8d2b3 | 480 | |
40dc166c | 481 | syscore_resume(); |
2ed8d2b3 | 482 | |
4aecd671 | 483 | Enable_irqs: |
c1a957d1 | 484 | system_state = SYSTEM_RUNNING; |
72df68ca | 485 | local_irq_enable(); |
2ed8d2b3 | 486 | |
4aecd671 | 487 | Enable_cpus: |
2f1a6fbb | 488 | suspend_enable_secondary_cpus(); |
4aecd671 RW |
489 | |
490 | Cleanup: | |
491 | platform_restore_cleanup(platform_mode); | |
492 | ||
cf579dfb | 493 | dpm_resume_start(PMSG_RECOVER); |
2ed8d2b3 | 494 | |
72df68ca RW |
495 | return error; |
496 | } | |
497 | ||
7777fab9 | 498 | /** |
f42a9813 RW |
499 | * hibernation_restore - Quiesce devices and restore from a hibernation image. |
500 | * @platform_mode: If set, use platform driver to prepare for the transition. | |
7777fab9 | 501 | * |
55f2503c PL |
502 | * This routine must be called with system_transition_mutex held. If it is |
503 | * successful, control reappears in the restored target kernel in | |
504 | * hibernation_snapshot(). | |
7777fab9 | 505 | */ |
a634cc10 | 506 | int hibernation_restore(int platform_mode) |
7777fab9 | 507 | { |
cbe2f5a6 | 508 | int error; |
7777fab9 RW |
509 | |
510 | pm_prepare_console(); | |
511 | suspend_console(); | |
c9e664f1 | 512 | pm_restrict_gfp_mask(); |
d1616302 | 513 | error = dpm_suspend_start(PMSG_QUIESCE); |
a634cc10 | 514 | if (!error) { |
4aecd671 | 515 | error = resume_target_kernel(platform_mode); |
94fb823f ID |
516 | /* |
517 | * The above should either succeed and jump to the new kernel, | |
518 | * or return with an error. Otherwise things are just | |
519 | * undefined, so let's be paranoid. | |
520 | */ | |
521 | BUG_ON(!error); | |
a634cc10 | 522 | } |
94fb823f | 523 | dpm_resume_end(PMSG_RECOVER); |
c9e664f1 | 524 | pm_restore_gfp_mask(); |
7777fab9 RW |
525 | resume_console(); |
526 | pm_restore_console(); | |
527 | return error; | |
528 | } | |
529 | ||
530 | /** | |
f42a9813 | 531 | * hibernation_platform_enter - Power off the system using the platform driver. |
7777fab9 | 532 | */ |
7777fab9 RW |
533 | int hibernation_platform_enter(void) |
534 | { | |
cbe2f5a6 | 535 | int error; |
b1457bcc | 536 | |
9cd9a005 RW |
537 | if (!hibernation_ops) |
538 | return -ENOSYS; | |
539 | ||
540 | /* | |
541 | * We have cancelled the power transition by running | |
542 | * hibernation_ops->finish() before saving the image, so we should let | |
543 | * the firmware know that we're going to enter the sleep state after all | |
544 | */ | |
caea99ef | 545 | error = hibernation_ops->begin(); |
9cd9a005 | 546 | if (error) |
caea99ef | 547 | goto Close; |
9cd9a005 | 548 | |
abfe2d7b | 549 | entering_platform_hibernation = true; |
9cd9a005 | 550 | suspend_console(); |
d1616302 | 551 | error = dpm_suspend_start(PMSG_HIBERNATE); |
d8f3de0d RW |
552 | if (error) { |
553 | if (hibernation_ops->recover) | |
554 | hibernation_ops->recover(); | |
555 | goto Resume_devices; | |
556 | } | |
9cd9a005 | 557 | |
cf579dfb | 558 | error = dpm_suspend_end(PMSG_HIBERNATE); |
4aecd671 | 559 | if (error) |
32bdfac5 | 560 | goto Resume_devices; |
4aecd671 | 561 | |
9cd9a005 RW |
562 | error = hibernation_ops->prepare(); |
563 | if (error) | |
e681c9dd | 564 | goto Platform_finish; |
9cd9a005 | 565 | |
2f1a6fbb | 566 | error = suspend_disable_secondary_cpus(); |
9cd9a005 | 567 | if (error) |
8c506608 | 568 | goto Enable_cpus; |
2ed8d2b3 | 569 | |
4aecd671 | 570 | local_irq_disable(); |
c1a957d1 | 571 | system_state = SYSTEM_SUSPEND; |
40dc166c | 572 | syscore_suspend(); |
a2867e08 | 573 | if (pm_wakeup_pending()) { |
c125e96f RW |
574 | error = -EAGAIN; |
575 | goto Power_up; | |
576 | } | |
577 | ||
4aecd671 RW |
578 | hibernation_ops->enter(); |
579 | /* We should never get here */ | |
580 | while (1); | |
9cd9a005 | 581 | |
c125e96f | 582 | Power_up: |
40dc166c | 583 | syscore_resume(); |
c1a957d1 | 584 | system_state = SYSTEM_RUNNING; |
c125e96f | 585 | local_irq_enable(); |
8c506608 VK |
586 | |
587 | Enable_cpus: | |
2f1a6fbb | 588 | suspend_enable_secondary_cpus(); |
c125e96f | 589 | |
e681c9dd | 590 | Platform_finish: |
9cd9a005 | 591 | hibernation_ops->finish(); |
2ed8d2b3 | 592 | |
cf579dfb | 593 | dpm_resume_start(PMSG_RESTORE); |
4aecd671 | 594 | |
9cd9a005 | 595 | Resume_devices: |
abfe2d7b | 596 | entering_platform_hibernation = false; |
d1616302 | 597 | dpm_resume_end(PMSG_RESTORE); |
9cd9a005 | 598 | resume_console(); |
2ed8d2b3 | 599 | |
caea99ef RW |
600 | Close: |
601 | hibernation_ops->end(); | |
2ed8d2b3 | 602 | |
b1457bcc | 603 | return error; |
7777fab9 RW |
604 | } |
605 | ||
1da177e4 | 606 | /** |
f42a9813 | 607 | * power_down - Shut the machine down for hibernation. |
1da177e4 | 608 | * |
f42a9813 RW |
609 | * Use the platform driver, if configured, to put the system into the sleep |
610 | * state corresponding to hibernation, or try to power it off or reboot, | |
611 | * depending on the value of hibernation_mode. | |
1da177e4 | 612 | */ |
fe0c935a | 613 | static void power_down(void) |
1da177e4 | 614 | { |
62c552cc BS |
615 | #ifdef CONFIG_SUSPEND |
616 | int error; | |
81d45bdf RW |
617 | |
618 | if (hibernation_mode == HIBERNATION_SUSPEND) { | |
619 | error = suspend_devices_and_enter(PM_SUSPEND_MEM); | |
620 | if (error) { | |
621 | hibernation_mode = hibernation_ops ? | |
622 | HIBERNATION_PLATFORM : | |
623 | HIBERNATION_SHUTDOWN; | |
624 | } else { | |
625 | /* Restore swap signature. */ | |
626 | error = swsusp_unmark(); | |
627 | if (error) | |
2872de13 | 628 | pr_err("Swap will be unusable! Try swapon -a.\n"); |
81d45bdf RW |
629 | |
630 | return; | |
631 | } | |
632 | } | |
62c552cc BS |
633 | #endif |
634 | ||
a3d25c27 | 635 | switch (hibernation_mode) { |
a3d25c27 | 636 | case HIBERNATION_REBOOT: |
fdde86ac | 637 | kernel_restart(NULL); |
1da177e4 | 638 | break; |
a3d25c27 | 639 | case HIBERNATION_PLATFORM: |
7777fab9 | 640 | hibernation_platform_enter(); |
82837ad5 | 641 | /* Fall through */ |
9cd9a005 | 642 | case HIBERNATION_SHUTDOWN: |
2c730785 SC |
643 | if (pm_power_off) |
644 | kernel_power_off(); | |
9cd9a005 | 645 | break; |
1da177e4 | 646 | } |
fdde86ac | 647 | kernel_halt(); |
fe0c935a JB |
648 | /* |
649 | * Valid image is on the disk, if we continue we risk serious data | |
650 | * corruption after resume. | |
651 | */ | |
2872de13 | 652 | pr_crit("Power down manually\n"); |
2c730785 SC |
653 | while (1) |
654 | cpu_relax(); | |
1da177e4 LT |
655 | } |
656 | ||
fe12c00d CY |
657 | static int load_image_and_restore(void) |
658 | { | |
659 | int error; | |
660 | unsigned int flags; | |
661 | ||
8d8b2441 | 662 | pm_pr_dbg("Loading hibernation image.\n"); |
fe12c00d CY |
663 | |
664 | lock_device_hotplug(); | |
665 | error = create_basic_memory_bitmaps(); | |
666 | if (error) | |
667 | goto Unlock; | |
668 | ||
669 | error = swsusp_read(&flags); | |
670 | swsusp_close(FMODE_READ); | |
671 | if (!error) | |
672 | hibernation_restore(flags & SF_PLATFORM_MODE); | |
673 | ||
2872de13 | 674 | pr_err("Failed to load hibernation image, recovering.\n"); |
fe12c00d CY |
675 | swsusp_free(); |
676 | free_basic_memory_bitmaps(); | |
677 | Unlock: | |
678 | unlock_device_hotplug(); | |
679 | ||
680 | return error; | |
681 | } | |
682 | ||
1da177e4 | 683 | /** |
f42a9813 | 684 | * hibernate - Carry out system hibernation, including saving the image. |
1da177e4 | 685 | */ |
a3d25c27 | 686 | int hibernate(void) |
1da177e4 | 687 | { |
ea00f4f4 | 688 | int error, nr_calls = 0; |
fe12c00d | 689 | bool snapshot_test = false; |
1da177e4 | 690 | |
a6e15a39 | 691 | if (!hibernation_available()) { |
8d8b2441 | 692 | pm_pr_dbg("Hibernation not available.\n"); |
a6e15a39 KC |
693 | return -EPERM; |
694 | } | |
695 | ||
bcda53fa | 696 | lock_system_sleep(); |
0709db60 | 697 | /* The snapshot device should not be opened while we're running */ |
b10d9117 RW |
698 | if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { |
699 | error = -EBUSY; | |
700 | goto Unlock; | |
701 | } | |
702 | ||
8915aa20 | 703 | pr_info("hibernation entry\n"); |
5a0a2f30 | 704 | pm_prepare_console(); |
ea00f4f4 LW |
705 | error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls); |
706 | if (error) { | |
707 | nr_calls--; | |
b10d9117 | 708 | goto Exit; |
ea00f4f4 | 709 | } |
0709db60 | 710 | |
b5dee313 | 711 | ksys_sync_helper(); |
232b1432 | 712 | |
03afed8b | 713 | error = freeze_processes(); |
5a72e04d | 714 | if (error) |
8fd37a4c RW |
715 | goto Exit; |
716 | ||
942f4015 | 717 | lock_device_hotplug(); |
8fd37a4c RW |
718 | /* Allocate memory management structures */ |
719 | error = create_basic_memory_bitmaps(); | |
720 | if (error) | |
721 | goto Thaw; | |
1da177e4 | 722 | |
7777fab9 | 723 | error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM); |
a556d5b5 | 724 | if (error || freezer_test_done) |
8fd37a4c | 725 | goto Free_bitmaps; |
64a473cb RW |
726 | |
727 | if (in_suspend) { | |
a634cc10 RW |
728 | unsigned int flags = 0; |
729 | ||
730 | if (hibernation_mode == HIBERNATION_PLATFORM) | |
731 | flags |= SF_PLATFORM_MODE; | |
f996fc96 BS |
732 | if (nocompress) |
733 | flags |= SF_NOCOMPRESS_MODE; | |
081a9d04 BS |
734 | else |
735 | flags |= SF_CRC32_MODE; | |
736 | ||
8d8b2441 | 737 | pm_pr_dbg("Writing image.\n"); |
a634cc10 | 738 | error = swsusp_write(flags); |
7777fab9 | 739 | swsusp_free(); |
fe12c00d CY |
740 | if (!error) { |
741 | if (hibernation_mode == HIBERNATION_TEST_RESUME) | |
742 | snapshot_test = true; | |
743 | else | |
744 | power_down(); | |
745 | } | |
5262a475 | 746 | in_suspend = 0; |
c9e664f1 | 747 | pm_restore_gfp_mask(); |
b918f6e6 | 748 | } else { |
8d8b2441 | 749 | pm_pr_dbg("Image restored successfully.\n"); |
b918f6e6 | 750 | } |
64a473cb | 751 | |
8fd37a4c RW |
752 | Free_bitmaps: |
753 | free_basic_memory_bitmaps(); | |
b918f6e6 | 754 | Thaw: |
942f4015 | 755 | unlock_device_hotplug(); |
fe12c00d | 756 | if (snapshot_test) { |
8d8b2441 | 757 | pm_pr_dbg("Checking hibernation image\n"); |
fe12c00d CY |
758 | error = swsusp_check(); |
759 | if (!error) | |
760 | error = load_image_and_restore(); | |
761 | } | |
5a0a2f30 | 762 | thaw_processes(); |
a556d5b5 SB |
763 | |
764 | /* Don't bother checking whether freezer_test_done is true */ | |
765 | freezer_test_done = false; | |
0709db60 | 766 | Exit: |
ea00f4f4 | 767 | __pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL); |
5a0a2f30 | 768 | pm_restore_console(); |
0709db60 | 769 | atomic_inc(&snapshot_device_available); |
b10d9117 | 770 | Unlock: |
bcda53fa | 771 | unlock_system_sleep(); |
8915aa20 RW |
772 | pr_info("hibernation exit\n"); |
773 | ||
1da177e4 LT |
774 | return error; |
775 | } | |
776 | ||
777 | ||
778 | /** | |
f42a9813 RW |
779 | * software_resume - Resume from a saved hibernation image. |
780 | * | |
781 | * This routine is called as a late initcall, when all devices have been | |
782 | * discovered and initialized already. | |
1da177e4 | 783 | * |
f42a9813 RW |
784 | * The image reading code is called to see if there is a hibernation image |
785 | * available for reading. If that is the case, devices are quiesced and the | |
786 | * contents of memory is restored from the saved image. | |
1da177e4 | 787 | * |
f42a9813 | 788 | * If this is successful, control reappears in the restored target kernel in |
d439e64f | 789 | * hibernation_snapshot() which returns to hibernate(). Otherwise, the routine |
f42a9813 RW |
790 | * attempts to recover gracefully and make the kernel return to the normal mode |
791 | * of operation. | |
1da177e4 | 792 | */ |
1da177e4 LT |
793 | static int software_resume(void) |
794 | { | |
ea00f4f4 | 795 | int error, nr_calls = 0; |
1da177e4 | 796 | |
eed3ee08 AV |
797 | /* |
798 | * If the user said "noresume".. bail out early. | |
799 | */ | |
a6e15a39 | 800 | if (noresume || !hibernation_available()) |
eed3ee08 AV |
801 | return 0; |
802 | ||
60a0d233 JB |
803 | /* |
804 | * name_to_dev_t() below takes a sysfs buffer mutex when sysfs | |
805 | * is configured into the kernel. Since the regular hibernate | |
806 | * trigger path is via sysfs which takes a buffer mutex before | |
55f2503c PL |
807 | * calling hibernate functions (which take system_transition_mutex) |
808 | * this can cause lockdep to complain about a possible ABBA deadlock | |
60a0d233 JB |
809 | * which cannot happen since we're in the boot code here and |
810 | * sysfs can't be invoked yet. Therefore, we use a subclass | |
811 | * here to avoid lockdep complaining. | |
812 | */ | |
55f2503c | 813 | mutex_lock_nested(&system_transition_mutex, SINGLE_DEPTH_NESTING); |
0c8454f5 RW |
814 | |
815 | if (swsusp_resume_device) | |
816 | goto Check_image; | |
817 | ||
818 | if (!strlen(resume_file)) { | |
819 | error = -ENOENT; | |
820 | goto Unlock; | |
821 | } | |
822 | ||
8d8b2441 | 823 | pm_pr_dbg("Checking hibernation image partition %s\n", resume_file); |
0c8454f5 | 824 | |
f126f733 | 825 | if (resume_delay) { |
2872de13 | 826 | pr_info("Waiting %dsec before reading resume device ...\n", |
f126f733 BS |
827 | resume_delay); |
828 | ssleep(resume_delay); | |
829 | } | |
830 | ||
0c8454f5 RW |
831 | /* Check if the device is there */ |
832 | swsusp_resume_device = name_to_dev_t(resume_file); | |
2df83fa4 MB |
833 | |
834 | /* | |
835 | * name_to_dev_t is ineffective to verify parition if resume_file is in | |
836 | * integer format. (e.g. major:minor) | |
837 | */ | |
838 | if (isdigit(resume_file[0]) && resume_wait) { | |
839 | int partno; | |
840 | while (!get_gendisk(swsusp_resume_device, &partno)) | |
841 | msleep(10); | |
842 | } | |
843 | ||
3efa147a | 844 | if (!swsusp_resume_device) { |
eed3ee08 AV |
845 | /* |
846 | * Some device discovery might still be in progress; we need | |
847 | * to wait for this to finish. | |
848 | */ | |
849 | wait_for_device_probe(); | |
6f8d7022 BS |
850 | |
851 | if (resume_wait) { | |
852 | while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0) | |
853 | msleep(10); | |
854 | async_synchronize_full(); | |
855 | } | |
856 | ||
3efa147a | 857 | swsusp_resume_device = name_to_dev_t(resume_file); |
0c8454f5 RW |
858 | if (!swsusp_resume_device) { |
859 | error = -ENODEV; | |
860 | goto Unlock; | |
861 | } | |
3efa147a PM |
862 | } |
863 | ||
0c8454f5 | 864 | Check_image: |
8d8b2441 | 865 | pm_pr_dbg("Hibernation image partition %d:%d present\n", |
0c8454f5 | 866 | MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device)); |
1da177e4 | 867 | |
8d8b2441 | 868 | pm_pr_dbg("Looking for hibernation image.\n"); |
ed746e3b RW |
869 | error = swsusp_check(); |
870 | if (error) | |
74dfd666 | 871 | goto Unlock; |
1da177e4 | 872 | |
0709db60 RW |
873 | /* The snapshot device should not be opened while we're running */ |
874 | if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { | |
875 | error = -EBUSY; | |
76b57e61 | 876 | swsusp_close(FMODE_READ); |
0709db60 RW |
877 | goto Unlock; |
878 | } | |
879 | ||
8915aa20 | 880 | pr_info("resume from hibernation\n"); |
5a0a2f30 | 881 | pm_prepare_console(); |
ea00f4f4 LW |
882 | error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls); |
883 | if (error) { | |
884 | nr_calls--; | |
8fd37a4c | 885 | goto Close_Finish; |
ea00f4f4 | 886 | } |
1bfcf130 | 887 | |
8d8b2441 | 888 | pm_pr_dbg("Preparing processes for restore.\n"); |
03afed8b | 889 | error = freeze_processes(); |
8fd37a4c RW |
890 | if (error) |
891 | goto Close_Finish; | |
fe12c00d | 892 | error = load_image_and_restore(); |
8fd37a4c | 893 | thaw_processes(); |
0709db60 | 894 | Finish: |
ea00f4f4 | 895 | __pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL); |
5a0a2f30 | 896 | pm_restore_console(); |
8915aa20 | 897 | pr_info("resume from hibernation failed (%d)\n", error); |
0709db60 | 898 | atomic_inc(&snapshot_device_available); |
dd5d666b | 899 | /* For success case, the suspend path will release the lock */ |
74dfd666 | 900 | Unlock: |
55f2503c | 901 | mutex_unlock(&system_transition_mutex); |
8d8b2441 | 902 | pm_pr_dbg("Hibernation image not present or could not be loaded.\n"); |
7777fab9 | 903 | return error; |
8fd37a4c | 904 | Close_Finish: |
76b57e61 JS |
905 | swsusp_close(FMODE_READ); |
906 | goto Finish; | |
1da177e4 LT |
907 | } |
908 | ||
d3c345db | 909 | late_initcall_sync(software_resume); |
1da177e4 LT |
910 | |
911 | ||
a3d25c27 RW |
912 | static const char * const hibernation_modes[] = { |
913 | [HIBERNATION_PLATFORM] = "platform", | |
914 | [HIBERNATION_SHUTDOWN] = "shutdown", | |
915 | [HIBERNATION_REBOOT] = "reboot", | |
62c552cc BS |
916 | #ifdef CONFIG_SUSPEND |
917 | [HIBERNATION_SUSPEND] = "suspend", | |
918 | #endif | |
fe12c00d | 919 | [HIBERNATION_TEST_RESUME] = "test_resume", |
1da177e4 LT |
920 | }; |
921 | ||
f42a9813 RW |
922 | /* |
923 | * /sys/power/disk - Control hibernation mode. | |
1da177e4 | 924 | * |
f42a9813 RW |
925 | * Hibernation can be handled in several ways. There are a few different ways |
926 | * to put the system into the sleep state: using the platform driver (e.g. ACPI | |
927 | * or other hibernation_ops), powering it off or rebooting it (for testing | |
48580ab8 | 928 | * mostly). |
1da177e4 | 929 | * |
f42a9813 RW |
930 | * The sysfs file /sys/power/disk provides an interface for selecting the |
931 | * hibernation mode to use. Reading from this file causes the available modes | |
48580ab8 | 932 | * to be printed. There are 3 modes that can be supported: |
1da177e4 | 933 | * |
1da177e4 LT |
934 | * 'platform' |
935 | * 'shutdown' | |
936 | * 'reboot' | |
937 | * | |
f42a9813 RW |
938 | * If a platform hibernation driver is in use, 'platform' will be supported |
939 | * and will be used by default. Otherwise, 'shutdown' will be used by default. | |
940 | * The selected option (i.e. the one corresponding to the current value of | |
941 | * hibernation_mode) is enclosed by a square bracket. | |
942 | * | |
943 | * To select a given hibernation mode it is necessary to write the mode's | |
944 | * string representation (as returned by reading from /sys/power/disk) back | |
945 | * into /sys/power/disk. | |
1da177e4 LT |
946 | */ |
947 | ||
386f275f KS |
948 | static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr, |
949 | char *buf) | |
1da177e4 | 950 | { |
f0ced9b2 JB |
951 | int i; |
952 | char *start = buf; | |
953 | ||
a6e15a39 KC |
954 | if (!hibernation_available()) |
955 | return sprintf(buf, "[disabled]\n"); | |
956 | ||
a3d25c27 RW |
957 | for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { |
958 | if (!hibernation_modes[i]) | |
f0ced9b2 JB |
959 | continue; |
960 | switch (i) { | |
a3d25c27 RW |
961 | case HIBERNATION_SHUTDOWN: |
962 | case HIBERNATION_REBOOT: | |
62c552cc BS |
963 | #ifdef CONFIG_SUSPEND |
964 | case HIBERNATION_SUSPEND: | |
965 | #endif | |
fe12c00d | 966 | case HIBERNATION_TEST_RESUME: |
f0ced9b2 | 967 | break; |
a3d25c27 RW |
968 | case HIBERNATION_PLATFORM: |
969 | if (hibernation_ops) | |
f0ced9b2 JB |
970 | break; |
971 | /* not a valid mode, continue with loop */ | |
972 | continue; | |
973 | } | |
a3d25c27 RW |
974 | if (i == hibernation_mode) |
975 | buf += sprintf(buf, "[%s] ", hibernation_modes[i]); | |
f0ced9b2 | 976 | else |
a3d25c27 | 977 | buf += sprintf(buf, "%s ", hibernation_modes[i]); |
f0ced9b2 JB |
978 | } |
979 | buf += sprintf(buf, "\n"); | |
980 | return buf-start; | |
1da177e4 LT |
981 | } |
982 | ||
386f275f KS |
983 | static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, |
984 | const char *buf, size_t n) | |
1da177e4 LT |
985 | { |
986 | int error = 0; | |
987 | int i; | |
988 | int len; | |
989 | char *p; | |
a3d25c27 | 990 | int mode = HIBERNATION_INVALID; |
1da177e4 | 991 | |
a6e15a39 KC |
992 | if (!hibernation_available()) |
993 | return -EPERM; | |
994 | ||
1da177e4 LT |
995 | p = memchr(buf, '\n', n); |
996 | len = p ? p - buf : n; | |
997 | ||
bcda53fa | 998 | lock_system_sleep(); |
a3d25c27 | 999 | for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { |
8d98a690 RW |
1000 | if (len == strlen(hibernation_modes[i]) |
1001 | && !strncmp(buf, hibernation_modes[i], len)) { | |
1da177e4 LT |
1002 | mode = i; |
1003 | break; | |
1004 | } | |
1005 | } | |
a3d25c27 | 1006 | if (mode != HIBERNATION_INVALID) { |
fe0c935a | 1007 | switch (mode) { |
a3d25c27 RW |
1008 | case HIBERNATION_SHUTDOWN: |
1009 | case HIBERNATION_REBOOT: | |
62c552cc BS |
1010 | #ifdef CONFIG_SUSPEND |
1011 | case HIBERNATION_SUSPEND: | |
1012 | #endif | |
fe12c00d | 1013 | case HIBERNATION_TEST_RESUME: |
a3d25c27 | 1014 | hibernation_mode = mode; |
fe0c935a | 1015 | break; |
a3d25c27 RW |
1016 | case HIBERNATION_PLATFORM: |
1017 | if (hibernation_ops) | |
1018 | hibernation_mode = mode; | |
1da177e4 LT |
1019 | else |
1020 | error = -EINVAL; | |
1021 | } | |
a3d25c27 | 1022 | } else |
1da177e4 LT |
1023 | error = -EINVAL; |
1024 | ||
a3d25c27 | 1025 | if (!error) |
8d8b2441 RW |
1026 | pm_pr_dbg("Hibernation mode set to '%s'\n", |
1027 | hibernation_modes[mode]); | |
bcda53fa | 1028 | unlock_system_sleep(); |
1da177e4 LT |
1029 | return error ? error : n; |
1030 | } | |
1031 | ||
1032 | power_attr(disk); | |
1033 | ||
386f275f KS |
1034 | static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr, |
1035 | char *buf) | |
1da177e4 LT |
1036 | { |
1037 | return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device), | |
1038 | MINOR(swsusp_resume_device)); | |
1039 | } | |
1040 | ||
386f275f KS |
1041 | static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr, |
1042 | const char *buf, size_t n) | |
1da177e4 | 1043 | { |
1da177e4 | 1044 | dev_t res; |
421a5fa1 SC |
1045 | int len = n; |
1046 | char *name; | |
1da177e4 | 1047 | |
421a5fa1 SC |
1048 | if (len && buf[len-1] == '\n') |
1049 | len--; | |
1050 | name = kstrndup(buf, len, GFP_KERNEL); | |
1051 | if (!name) | |
1052 | return -ENOMEM; | |
1da177e4 | 1053 | |
421a5fa1 SC |
1054 | res = name_to_dev_t(name); |
1055 | kfree(name); | |
1056 | if (!res) | |
1057 | return -EINVAL; | |
1da177e4 | 1058 | |
bcda53fa | 1059 | lock_system_sleep(); |
a576219a | 1060 | swsusp_resume_device = res; |
bcda53fa | 1061 | unlock_system_sleep(); |
64846407 | 1062 | pm_pr_dbg("Configured resume from disk to %u\n", swsusp_resume_device); |
a576219a AM |
1063 | noresume = 0; |
1064 | software_resume(); | |
421a5fa1 | 1065 | return n; |
1da177e4 LT |
1066 | } |
1067 | ||
1068 | power_attr(resume); | |
1069 | ||
35506467 ML |
1070 | static ssize_t resume_offset_show(struct kobject *kobj, |
1071 | struct kobj_attribute *attr, char *buf) | |
1072 | { | |
1073 | return sprintf(buf, "%llu\n", (unsigned long long)swsusp_resume_block); | |
1074 | } | |
1075 | ||
1076 | static ssize_t resume_offset_store(struct kobject *kobj, | |
1077 | struct kobj_attribute *attr, const char *buf, | |
1078 | size_t n) | |
1079 | { | |
1080 | unsigned long long offset; | |
1081 | int rc; | |
1082 | ||
1083 | rc = kstrtoull(buf, 0, &offset); | |
1084 | if (rc) | |
1085 | return rc; | |
1086 | swsusp_resume_block = offset; | |
1087 | ||
1088 | return n; | |
1089 | } | |
1090 | ||
1091 | power_attr(resume_offset); | |
1092 | ||
386f275f KS |
1093 | static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr, |
1094 | char *buf) | |
ca0aec0f | 1095 | { |
853609b6 | 1096 | return sprintf(buf, "%lu\n", image_size); |
ca0aec0f RW |
1097 | } |
1098 | ||
386f275f KS |
1099 | static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr, |
1100 | const char *buf, size_t n) | |
ca0aec0f | 1101 | { |
853609b6 | 1102 | unsigned long size; |
ca0aec0f | 1103 | |
853609b6 | 1104 | if (sscanf(buf, "%lu", &size) == 1) { |
ca0aec0f RW |
1105 | image_size = size; |
1106 | return n; | |
1107 | } | |
1108 | ||
1109 | return -EINVAL; | |
1110 | } | |
1111 | ||
1112 | power_attr(image_size); | |
1113 | ||
ddeb6487 RW |
1114 | static ssize_t reserved_size_show(struct kobject *kobj, |
1115 | struct kobj_attribute *attr, char *buf) | |
1116 | { | |
1117 | return sprintf(buf, "%lu\n", reserved_size); | |
1118 | } | |
1119 | ||
1120 | static ssize_t reserved_size_store(struct kobject *kobj, | |
1121 | struct kobj_attribute *attr, | |
1122 | const char *buf, size_t n) | |
1123 | { | |
1124 | unsigned long size; | |
1125 | ||
1126 | if (sscanf(buf, "%lu", &size) == 1) { | |
1127 | reserved_size = size; | |
1128 | return n; | |
1129 | } | |
1130 | ||
1131 | return -EINVAL; | |
1132 | } | |
1133 | ||
1134 | power_attr(reserved_size); | |
1135 | ||
1da177e4 LT |
1136 | static struct attribute * g[] = { |
1137 | &disk_attr.attr, | |
35506467 | 1138 | &resume_offset_attr.attr, |
1da177e4 | 1139 | &resume_attr.attr, |
ca0aec0f | 1140 | &image_size_attr.attr, |
ddeb6487 | 1141 | &reserved_size_attr.attr, |
1da177e4 LT |
1142 | NULL, |
1143 | }; | |
1144 | ||
1145 | ||
59494fe2 | 1146 | static const struct attribute_group attr_group = { |
1da177e4 LT |
1147 | .attrs = g, |
1148 | }; | |
1149 | ||
1150 | ||
1151 | static int __init pm_disk_init(void) | |
1152 | { | |
d76e15fb | 1153 | return sysfs_create_group(power_kobj, &attr_group); |
1da177e4 LT |
1154 | } |
1155 | ||
1156 | core_initcall(pm_disk_init); | |
1157 | ||
1158 | ||
1159 | static int __init resume_setup(char *str) | |
1160 | { | |
1161 | if (noresume) | |
1162 | return 1; | |
1163 | ||
1164 | strncpy( resume_file, str, 255 ); | |
1165 | return 1; | |
1166 | } | |
1167 | ||
9a154d9d RW |
1168 | static int __init resume_offset_setup(char *str) |
1169 | { | |
1170 | unsigned long long offset; | |
1171 | ||
1172 | if (noresume) | |
1173 | return 1; | |
1174 | ||
1175 | if (sscanf(str, "%llu", &offset) == 1) | |
1176 | swsusp_resume_block = offset; | |
1177 | ||
1178 | return 1; | |
1179 | } | |
1180 | ||
f996fc96 BS |
1181 | static int __init hibernate_setup(char *str) |
1182 | { | |
2f88e41a | 1183 | if (!strncmp(str, "noresume", 8)) { |
f996fc96 | 1184 | noresume = 1; |
2f88e41a | 1185 | } else if (!strncmp(str, "nocompress", 10)) { |
f996fc96 | 1186 | nocompress = 1; |
2f88e41a | 1187 | } else if (!strncmp(str, "no", 2)) { |
a6e15a39 KC |
1188 | noresume = 1; |
1189 | nohibernate = 1; | |
0f5bf6d0 | 1190 | } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) |
4c0b6c10 RW |
1191 | && !strncmp(str, "protect_image", 13)) { |
1192 | enable_restore_image_protection(); | |
a6e15a39 | 1193 | } |
f996fc96 BS |
1194 | return 1; |
1195 | } | |
1196 | ||
1da177e4 LT |
1197 | static int __init noresume_setup(char *str) |
1198 | { | |
1199 | noresume = 1; | |
1200 | return 1; | |
1201 | } | |
1202 | ||
6f8d7022 BS |
1203 | static int __init resumewait_setup(char *str) |
1204 | { | |
1205 | resume_wait = 1; | |
1206 | return 1; | |
1207 | } | |
1208 | ||
f126f733 BS |
1209 | static int __init resumedelay_setup(char *str) |
1210 | { | |
f6514be5 | 1211 | int rc = kstrtouint(str, 0, &resume_delay); |
317cf7e5 FF |
1212 | |
1213 | if (rc) | |
1214 | return rc; | |
f126f733 BS |
1215 | return 1; |
1216 | } | |
1217 | ||
a6e15a39 KC |
1218 | static int __init nohibernate_setup(char *str) |
1219 | { | |
1220 | noresume = 1; | |
1221 | nohibernate = 1; | |
1222 | return 1; | |
1223 | } | |
1224 | ||
1da177e4 | 1225 | __setup("noresume", noresume_setup); |
9a154d9d | 1226 | __setup("resume_offset=", resume_offset_setup); |
1da177e4 | 1227 | __setup("resume=", resume_setup); |
f996fc96 | 1228 | __setup("hibernate=", hibernate_setup); |
6f8d7022 | 1229 | __setup("resumewait", resumewait_setup); |
f126f733 | 1230 | __setup("resumedelay=", resumedelay_setup); |
a6e15a39 | 1231 | __setup("nohibernate", nohibernate_setup); |