blob: 64e42ab8b57c3008e5d1f1574557eef3667939d7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Andrew Mortond53d9f12005-07-12 13:58:07 -070019#include <linux/mount.h>
Eric W. Biederman88d10bb2005-09-22 21:43:46 -070020#include <linux/pm.h>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070021#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070022#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080023#include <linux/freezer.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "power.h"
26
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static int noresume = 0;
29char resume_file[256] = CONFIG_PM_STD_PARTITION;
30dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080031sector_t swsusp_resume_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070033enum {
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
46static int hibernation_mode = HIBERNATION_SHUTDOWN;
47
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070048static struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070049
50/**
51 * hibernation_set_ops - set the global hibernate operations
52 * @ops: the hibernation operations to use in subsequent hibernation transitions
53 */
54
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070055void hibernation_set_ops(struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070056{
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -070057 if (ops && !(ops->start && ops->pre_snapshot && ops->finish
58 && ops->prepare && ops->enter && ops->pre_restore
59 && ops->restore_cleanup)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070060 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
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +010073#ifdef CONFIG_PM_DEBUG
74static void hibernation_debug_sleep(void)
75{
76 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
77 mdelay(5000);
78}
79
80static int hibernation_testmode(int mode)
81{
82 if (hibernation_mode == mode) {
83 hibernation_debug_sleep();
84 return 1;
85 }
86 return 0;
87}
88
89static 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 */
98static int hibernation_testmode(int mode) { return 0; }
99static int hibernation_test(int level) { return 0; }
100#endif /* !CONFIG_PM_DEBUG */
101
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700102/**
103 * platform_start - tell the platform driver that we're starting
104 * hibernation
105 */
106
107static int platform_start(int platform_mode)
108{
109 return (platform_mode && hibernation_ops) ?
110 hibernation_ops->start() : 0;
111}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/**
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700114 * platform_pre_snapshot - prepare the machine for hibernation using the
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800115 * platform driver if so configured and return an error code if it fails
116 */
117
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700118static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800119{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700120 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700121 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800122}
123
124/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700125 * platform_leave - prepare the machine for switching to the normal mode
126 * of operation using the platform driver (called with interrupts disabled)
127 */
128
129static void platform_leave(int platform_mode)
130{
131 if (platform_mode && hibernation_ops)
132 hibernation_ops->leave();
133}
134
135/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700136 * platform_finish - switch the machine to the normal mode of operation
137 * using the platform driver (must be called after platform_prepare())
138 */
139
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700140static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700141{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700142 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700143 hibernation_ops->finish();
144}
145
146/**
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700147 * platform_pre_restore - prepare the platform for the restoration from a
148 * hibernation image. If the restore fails after this function has been
149 * called, platform_restore_cleanup() must be called.
150 */
151
152static int platform_pre_restore(int platform_mode)
153{
154 return (platform_mode && hibernation_ops) ?
155 hibernation_ops->pre_restore() : 0;
156}
157
158/**
159 * platform_restore_cleanup - switch the platform to the normal mode of
160 * operation after a failing restore. If platform_pre_restore() has been
161 * called before the failing restore, this function must be called too,
162 * regardless of the result of platform_pre_restore().
163 */
164
165static void platform_restore_cleanup(int platform_mode)
166{
167 if (platform_mode && hibernation_ops)
168 hibernation_ops->restore_cleanup();
169}
170
171/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700172 * create_image - freeze devices that need to be frozen with interrupts
173 * off, create the hibernation image and thaw those devices. Control
174 * reappears in this routine after a restore.
175 */
176
177int create_image(int platform_mode)
178{
179 int error;
180
181 error = arch_prepare_suspend();
182 if (error)
183 return error;
184
185 local_irq_disable();
186 /* At this point, device_suspend() has been called, but *not*
187 * device_power_down(). We *must* call device_power_down() now.
188 * Otherwise, drivers for some devices (e.g. interrupt controllers)
189 * become desynchronized with the actual state of the hardware
190 * at resume time, and evil weirdness ensues.
191 */
192 error = device_power_down(PMSG_FREEZE);
193 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100194 printk(KERN_ERR "PM: Some devices failed to power down, "
195 "aborting hibernation\n");
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700196 goto Enable_irqs;
197 }
198
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100199 if (hibernation_test(TEST_CORE))
200 goto Power_up;
201
202 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700203 save_processor_state();
204 error = swsusp_arch_suspend();
205 if (error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100206 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
207 error);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700208 /* Restore control flow magically appears here */
209 restore_processor_state();
210 if (!in_suspend)
211 platform_leave(platform_mode);
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100212 Power_up:
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700213 /* NOTE: device_power_up() is just a resume() for devices
214 * that suspended with irqs off ... no overall powerup.
215 */
216 device_power_up();
217 Enable_irqs:
218 local_irq_enable();
219 return error;
220}
221
222/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700223 * hibernation_snapshot - quiesce devices and create the hibernation
224 * snapshot image.
225 * @platform_mode - if set, use the platform driver, if available, to
226 * prepare the platform frimware for the power transition.
227 *
228 * Must be called with pm_mutex held
229 */
230
231int hibernation_snapshot(int platform_mode)
232{
233 int error;
234
235 /* Free memory before shutting down devices. */
236 error = swsusp_shrink_memory();
237 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700238 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700239
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700240 error = platform_start(platform_mode);
241 if (error)
242 return error;
243
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700244 suspend_console();
245 error = device_suspend(PMSG_FREEZE);
246 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700247 goto Resume_console;
248
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100249 if (hibernation_test(TEST_DEVICES))
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700250 goto Resume_devices;
251
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100252 error = platform_pre_snapshot(platform_mode);
253 if (error || hibernation_test(TEST_PLATFORM))
254 goto Finish;
255
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700256 error = disable_nonboot_cpus();
257 if (!error) {
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100258 if (hibernation_test(TEST_CPUS))
259 goto Enable_cpus;
260
261 if (hibernation_testmode(HIBERNATION_TEST))
262 goto Enable_cpus;
263
264 error = create_image(platform_mode);
265 /* Control returns here after successful restore */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700266 }
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100267 Enable_cpus:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700268 enable_nonboot_cpus();
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100269 Finish:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700270 platform_finish(platform_mode);
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100271 Resume_devices:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700272 device_resume();
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700273 Resume_console:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700274 resume_console();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700275 return error;
276}
277
278/**
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100279 * resume_target_kernel - prepare devices that need to be suspended with
280 * interrupts off, restore the contents of highmem that have not been
281 * restored yet from the image and run the low level code that will restore
282 * the remaining contents of memory and switch to the just restored target
283 * kernel.
284 */
285
286static int resume_target_kernel(void)
287{
288 int error;
289
290 local_irq_disable();
291 error = device_power_down(PMSG_PRETHAW);
292 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100293 printk(KERN_ERR "PM: Some devices failed to power down, "
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100294 "aborting resume\n");
295 goto Enable_irqs;
296 }
297 /* We'll ignore saved state, but this gets preempt count (etc) right */
298 save_processor_state();
299 error = restore_highmem();
300 if (!error) {
301 error = swsusp_arch_resume();
302 /*
303 * The code below is only ever reached in case of a failure.
304 * Otherwise execution continues at place where
305 * swsusp_arch_suspend() was called
306 */
307 BUG_ON(!error);
308 /* This call to restore_highmem() undos the previous one */
309 restore_highmem();
310 }
311 /*
312 * The only reason why swsusp_arch_resume() can fail is memory being
313 * very tight, so we have to free it as soon as we can to avoid
314 * subsequent failures
315 */
316 swsusp_free();
317 restore_processor_state();
318 touch_softlockup_watchdog();
319 device_power_up();
320 Enable_irqs:
321 local_irq_enable();
322 return error;
323}
324
325/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700326 * hibernation_restore - quiesce devices and restore the hibernation
327 * snapshot image. If successful, control returns in hibernation_snaphot()
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700328 * @platform_mode - if set, use the platform driver, if available, to
329 * prepare the platform frimware for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700330 *
331 * Must be called with pm_mutex held
332 */
333
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700334int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700335{
336 int error;
337
338 pm_prepare_console();
339 suspend_console();
340 error = device_suspend(PMSG_PRETHAW);
341 if (error)
342 goto Finish;
343
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700344 error = platform_pre_restore(platform_mode);
345 if (!error) {
346 error = disable_nonboot_cpus();
347 if (!error)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100348 error = resume_target_kernel();
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700349 enable_nonboot_cpus();
350 }
351 platform_restore_cleanup(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700352 device_resume();
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700353 Finish:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700354 resume_console();
355 pm_restore_console();
356 return error;
357}
358
359/**
360 * hibernation_platform_enter - enter the hibernation state using the
361 * platform driver (if available)
362 */
363
364int hibernation_platform_enter(void)
365{
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700366 int error;
367
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700368 if (!hibernation_ops)
369 return -ENOSYS;
370
371 /*
372 * We have cancelled the power transition by running
373 * hibernation_ops->finish() before saving the image, so we should let
374 * the firmware know that we're going to enter the sleep state after all
375 */
376 error = hibernation_ops->start();
377 if (error)
378 return error;
379
380 suspend_console();
381 error = device_suspend(PMSG_SUSPEND);
382 if (error)
383 goto Resume_console;
384
385 error = hibernation_ops->prepare();
386 if (error)
387 goto Resume_devices;
388
389 error = disable_nonboot_cpus();
390 if (error)
391 goto Finish;
392
393 local_irq_disable();
394 error = device_power_down(PMSG_SUSPEND);
395 if (!error) {
396 hibernation_ops->enter();
397 /* We should never get here */
398 while (1);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700399 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700400 local_irq_enable();
401
402 /*
403 * We don't need to reenable the nonboot CPUs or resume consoles, since
404 * the system is going to be halted anyway.
405 */
406 Finish:
407 hibernation_ops->finish();
408 Resume_devices:
409 device_resume();
410 Resume_console:
411 resume_console();
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700412 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700413}
414
415/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700416 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 *
Johannes Bergfe0c9352007-04-30 15:09:51 -0700418 * Use the platform driver, if configured so; otherwise try
419 * to power off or reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 */
421
Johannes Bergfe0c9352007-04-30 15:09:51 -0700422static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700424 switch (hibernation_mode) {
425 case HIBERNATION_TEST:
426 case HIBERNATION_TESTPROC:
Johannes Bergfe0c9352007-04-30 15:09:51 -0700427 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700428 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600429 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700431 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700432 hibernation_platform_enter();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700433 case HIBERNATION_SHUTDOWN:
434 kernel_power_off();
435 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600437 kernel_halt();
Johannes Bergfe0c9352007-04-30 15:09:51 -0700438 /*
439 * Valid image is on the disk, if we continue we risk serious data
440 * corruption after resume.
441 */
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100442 printk(KERN_CRIT "PM: Please power down manually\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 while(1);
444}
445
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800446static void unprepare_processes(void)
447{
448 thaw_processes();
449 pm_restore_console();
450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452static int prepare_processes(void)
453{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800454 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 pm_prepare_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (freeze_processes()) {
458 error = -EBUSY;
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800459 unprepare_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700461 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700465 * hibernate - The granpappy of the built-in hibernation management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 */
467
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700468int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 int error;
471
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700472 mutex_lock(&pm_mutex);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700473 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700474 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
475 error = -EBUSY;
476 goto Unlock;
477 }
478
479 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
480 if (error)
481 goto Exit;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700482
483 /* Allocate memory management structures */
484 error = create_basic_memory_bitmaps();
485 if (error)
486 goto Exit;
487
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100488 printk(KERN_INFO "PM: Syncing filesystems ... ");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700489 sys_sync();
490 printk("done.\n");
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700493 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700494 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100496 if (hibernation_test(TEST_FREEZER))
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800497 goto Thaw;
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100498
499 if (hibernation_testmode(HIBERNATION_TESTPROC))
500 goto Thaw;
501
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700502 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
503 if (in_suspend && !error) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700504 unsigned int flags = 0;
505
506 if (hibernation_mode == HIBERNATION_PLATFORM)
507 flags |= SF_PLATFORM_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700509 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700510 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!error)
Johannes Bergfe0c9352007-04-30 15:09:51 -0700512 power_down();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800513 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700515 swsusp_free();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800516 }
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800517 Thaw:
Pavel Machek99dc7d62005-09-03 15:57:05 -0700518 unprepare_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700519 Finish:
520 free_basic_memory_bitmaps();
521 Exit:
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700522 pm_notifier_call_chain(PM_POST_HIBERNATION);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700523 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700524 Unlock:
525 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return error;
527}
528
529
530/**
531 * software_resume - Resume from a saved image.
532 *
533 * Called as a late_initcall (so all devices are discovered and
534 * initialized), we call swsusp to see if we have a saved image or not.
535 * If so, we quiesce devices, the restore the saved image. We will
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700536 * return above (in hibernate() ) if everything goes well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * Otherwise, we fail gracefully and return to the normally
538 * scheduled program.
539 *
540 */
541
542static int software_resume(void)
543{
544 int error;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700545 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Johannes Berg60a0d232007-11-14 17:00:16 -0800547 /*
548 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
549 * is configured into the kernel. Since the regular hibernate
550 * trigger path is via sysfs which takes a buffer mutex before
551 * calling hibernate functions (which take pm_mutex) this can
552 * cause lockdep to complain about a possible ABBA deadlock
553 * which cannot happen since we're in the boot code here and
554 * sysfs can't be invoked yet. Therefore, we use a subclass
555 * here to avoid lockdep complaining.
556 */
557 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
Pavel Machek3efa1472005-07-07 17:56:43 -0700558 if (!swsusp_resume_device) {
Shaohua Lidd5d6662005-09-03 15:57:04 -0700559 if (!strlen(resume_file)) {
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800560 mutex_unlock(&pm_mutex);
Pavel Machek3efa1472005-07-07 17:56:43 -0700561 return -ENOENT;
Shaohua Lidd5d6662005-09-03 15:57:04 -0700562 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700563 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100564 pr_debug("PM: Resume from partition %s\n", resume_file);
Pavel Machek3efa1472005-07-07 17:56:43 -0700565 } else {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100566 pr_debug("PM: Resume from partition %d:%d\n",
567 MAJOR(swsusp_resume_device),
568 MINOR(swsusp_resume_device));
Pavel Machek3efa1472005-07-07 17:56:43 -0700569 }
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (noresume) {
572 /**
Rafael J. Wysocki95758092007-12-08 02:06:57 +0100573 * FIXME: If noresume is specified, we need to find the
574 * partition and reset it back to normal swap space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 */
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800576 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return 0;
578 }
579
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100580 pr_debug("PM: Checking hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800581 error = swsusp_check();
582 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700583 goto Unlock;
584
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700585 /* The snapshot device should not be opened while we're running */
586 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
587 error = -EBUSY;
588 goto Unlock;
589 }
590
Alan Sternc3e94d82007-11-19 23:38:25 +0100591 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
592 if (error)
593 goto Finish;
594
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700595 error = create_basic_memory_bitmaps();
596 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700597 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 pr_debug("PM: Preparing processes for restore.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800600 error = prepare_processes();
601 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 swsusp_close();
Li Shaohua5a72e042005-06-25 14:55:06 -0700603 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100606 pr_debug("PM: Reading hibernation image.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700608 error = swsusp_read(&flags);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800609 if (!error)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700610 hibernation_restore(flags & SF_PLATFORM_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800612 printk(KERN_ERR "PM: Restore failed, recovering.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700613 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 unprepare_processes();
615 Done:
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700616 free_basic_memory_bitmaps();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700617 Finish:
Alan Sternc3e94d82007-11-19 23:38:25 +0100618 pm_notifier_call_chain(PM_POST_RESTORE);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700619 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700620 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700621 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800622 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 pr_debug("PM: Resume from disk failed.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700624 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
627late_initcall(software_resume);
628
629
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700630static const char * const hibernation_modes[] = {
631 [HIBERNATION_PLATFORM] = "platform",
632 [HIBERNATION_SHUTDOWN] = "shutdown",
633 [HIBERNATION_REBOOT] = "reboot",
634 [HIBERNATION_TEST] = "test",
635 [HIBERNATION_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636};
637
638/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700639 * disk - Control hibernation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700641 * Suspend-to-disk can be handled in several ways. We have a few options
642 * for putting the system to sleep - using the platform driver (e.g. ACPI
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700643 * or other hibernation_ops), powering off the system or rebooting the
644 * system (for testing) as well as the two test modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700646 * The system can support 'platform', and that is known a priori (and
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700647 * encoded by the presence of hibernation_ops). However, the user may
648 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
649 * test modes, 'test' or 'testproc'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 *
651 * show() will display what the mode is currently set to.
652 * store() will accept one of
653 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 * 'platform'
655 * 'shutdown'
656 * 'reboot'
Johannes Berg11d77d02007-04-30 15:09:53 -0700657 * 'test'
658 * 'testproc'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700660 * It will only change to 'platform' if the system
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700661 * supports it (as determined by having hibernation_ops).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
663
Kay Sievers386f2752007-11-02 13:47:53 +0100664static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
665 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700667 int i;
668 char *start = buf;
669
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700670 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
671 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700672 continue;
673 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700674 case HIBERNATION_SHUTDOWN:
675 case HIBERNATION_REBOOT:
676 case HIBERNATION_TEST:
677 case HIBERNATION_TESTPROC:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700678 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700679 case HIBERNATION_PLATFORM:
680 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700681 break;
682 /* not a valid mode, continue with loop */
683 continue;
684 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700685 if (i == hibernation_mode)
686 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700687 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700688 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700689 }
690 buf += sprintf(buf, "\n");
691 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
694
Kay Sievers386f2752007-11-02 13:47:53 +0100695static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
696 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 int error = 0;
699 int i;
700 int len;
701 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700702 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 p = memchr(buf, '\n', n);
705 len = p ? p - buf : n;
706
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800707 mutex_lock(&pm_mutex);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700708 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700709 if (len == strlen(hibernation_modes[i])
710 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 mode = i;
712 break;
713 }
714 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700715 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c9352007-04-30 15:09:51 -0700716 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700717 case HIBERNATION_SHUTDOWN:
718 case HIBERNATION_REBOOT:
719 case HIBERNATION_TEST:
720 case HIBERNATION_TESTPROC:
721 hibernation_mode = mode;
Johannes Bergfe0c9352007-04-30 15:09:51 -0700722 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700723 case HIBERNATION_PLATFORM:
724 if (hibernation_ops)
725 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 else
727 error = -EINVAL;
728 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700729 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 error = -EINVAL;
731
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700732 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100733 pr_debug("PM: Hibernation mode set to '%s'\n",
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700734 hibernation_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800735 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return error ? error : n;
737}
738
739power_attr(disk);
740
Kay Sievers386f2752007-11-02 13:47:53 +0100741static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
742 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
745 MINOR(swsusp_resume_device));
746}
747
Kay Sievers386f2752007-11-02 13:47:53 +0100748static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
749 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800753 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Andrew Mortona5762192006-01-06 00:09:50 -0800755 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
756 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Andrew Mortona5762192006-01-06 00:09:50 -0800758 res = MKDEV(maj,min);
759 if (maj != MAJOR(res) || min != MINOR(res))
760 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800762 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800763 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800764 mutex_unlock(&pm_mutex);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100765 printk(KERN_INFO "PM: Starting manual resume from disk\n");
Andrew Mortona5762192006-01-06 00:09:50 -0800766 noresume = 0;
767 software_resume();
768 ret = n;
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800769 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800770 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773power_attr(resume);
774
Kay Sievers386f2752007-11-02 13:47:53 +0100775static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
776 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800777{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800778 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800779}
780
Kay Sievers386f2752007-11-02 13:47:53 +0100781static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
782 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800783{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800784 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800785
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800786 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800787 image_size = size;
788 return n;
789 }
790
791 return -EINVAL;
792}
793
794power_attr(image_size);
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796static struct attribute * g[] = {
797 &disk_attr.attr,
798 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800799 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 NULL,
801};
802
803
804static struct attribute_group attr_group = {
805 .attrs = g,
806};
807
808
809static int __init pm_disk_init(void)
810{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800811 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
814core_initcall(pm_disk_init);
815
816
817static int __init resume_setup(char *str)
818{
819 if (noresume)
820 return 1;
821
822 strncpy( resume_file, str, 255 );
823 return 1;
824}
825
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800826static int __init resume_offset_setup(char *str)
827{
828 unsigned long long offset;
829
830 if (noresume)
831 return 1;
832
833 if (sscanf(str, "%llu", &offset) == 1)
834 swsusp_resume_block = offset;
835
836 return 1;
837}
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839static int __init noresume_setup(char *str)
840{
841 noresume = 1;
842 return 1;
843}
844
845__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800846__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847__setup("resume=", resume_setup);