blob: 0854770b63b9c8b42db6b2727aed6155d3e701c2 [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>
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -070017#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/delay.h>
19#include <linux/fs.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070020#include <linux/mount.h>
Eric W. Biederman88d10bb2005-09-22 21:43:46 -070021#include <linux/pm.h>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070022#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070023#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080024#include <linux/freezer.h>
Rafael J. Wysockic7510852009-04-12 20:06:56 +020025#include <scsi/scsi_scan.h>
Magnus Damma8af7892009-03-31 15:23:37 -070026#include <asm/suspend.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "power.h"
29
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int noresume = 0;
Adrian Bunk47a460d2008-02-04 22:30:06 -080032static char resume_file[256] = CONFIG_PM_STD_PARTITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080034sector_t swsusp_resume_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070036enum {
37 HIBERNATION_INVALID,
38 HIBERNATION_PLATFORM,
39 HIBERNATION_TEST,
40 HIBERNATION_TESTPROC,
41 HIBERNATION_SHUTDOWN,
42 HIBERNATION_REBOOT,
43 /* keep last */
44 __HIBERNATION_AFTER_LAST
45};
46#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
47#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
48
49static int hibernation_mode = HIBERNATION_SHUTDOWN;
50
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070051static struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070052
53/**
54 * hibernation_set_ops - set the global hibernate operations
55 * @ops: the hibernation operations to use in subsequent hibernation transitions
56 */
57
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070058void hibernation_set_ops(struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070059{
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +010060 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
61 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -070062 && ops->restore_cleanup)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070063 WARN_ON(1);
64 return;
65 }
66 mutex_lock(&pm_mutex);
67 hibernation_ops = ops;
68 if (ops)
69 hibernation_mode = HIBERNATION_PLATFORM;
70 else if (hibernation_mode == HIBERNATION_PLATFORM)
71 hibernation_mode = HIBERNATION_SHUTDOWN;
72
73 mutex_unlock(&pm_mutex);
74}
75
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +010076static bool entering_platform_hibernation;
77
78bool system_entering_hibernation(void)
79{
80 return entering_platform_hibernation;
81}
82EXPORT_SYMBOL(system_entering_hibernation);
83
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +010084#ifdef CONFIG_PM_DEBUG
85static void hibernation_debug_sleep(void)
86{
87 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
88 mdelay(5000);
89}
90
91static int hibernation_testmode(int mode)
92{
93 if (hibernation_mode == mode) {
94 hibernation_debug_sleep();
95 return 1;
96 }
97 return 0;
98}
99
100static int hibernation_test(int level)
101{
102 if (pm_test_level == level) {
103 hibernation_debug_sleep();
104 return 1;
105 }
106 return 0;
107}
108#else /* !CONFIG_PM_DEBUG */
109static int hibernation_testmode(int mode) { return 0; }
110static int hibernation_test(int level) { return 0; }
111#endif /* !CONFIG_PM_DEBUG */
112
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700113/**
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100114 * platform_begin - tell the platform driver that we're starting
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700115 * hibernation
116 */
117
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100118static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700119{
120 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100121 hibernation_ops->begin() : 0;
122}
123
124/**
125 * platform_end - tell the platform driver that we've entered the
126 * working state
127 */
128
129static void platform_end(int platform_mode)
130{
131 if (platform_mode && hibernation_ops)
132 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700133}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/**
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700136 * platform_pre_snapshot - prepare the machine for hibernation using the
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800137 * platform driver if so configured and return an error code if it fails
138 */
139
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700140static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800141{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700142 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700143 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800144}
145
146/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700147 * platform_leave - prepare the machine for switching to the normal mode
148 * of operation using the platform driver (called with interrupts disabled)
149 */
150
151static void platform_leave(int platform_mode)
152{
153 if (platform_mode && hibernation_ops)
154 hibernation_ops->leave();
155}
156
157/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700158 * platform_finish - switch the machine to the normal mode of operation
159 * using the platform driver (must be called after platform_prepare())
160 */
161
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700162static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700163{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700164 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700165 hibernation_ops->finish();
166}
167
168/**
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700169 * platform_pre_restore - prepare the platform for the restoration from a
170 * hibernation image. If the restore fails after this function has been
171 * called, platform_restore_cleanup() must be called.
172 */
173
174static int platform_pre_restore(int platform_mode)
175{
176 return (platform_mode && hibernation_ops) ?
177 hibernation_ops->pre_restore() : 0;
178}
179
180/**
181 * platform_restore_cleanup - switch the platform to the normal mode of
182 * operation after a failing restore. If platform_pre_restore() has been
183 * called before the failing restore, this function must be called too,
184 * regardless of the result of platform_pre_restore().
185 */
186
187static void platform_restore_cleanup(int platform_mode)
188{
189 if (platform_mode && hibernation_ops)
190 hibernation_ops->restore_cleanup();
191}
192
193/**
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200194 * platform_recover - recover the platform from a failure to suspend
195 * devices.
196 */
197
198static void platform_recover(int platform_mode)
199{
200 if (platform_mode && hibernation_ops && hibernation_ops->recover)
201 hibernation_ops->recover();
202}
203
204/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700205 * create_image - freeze devices that need to be frozen with interrupts
206 * off, create the hibernation image and thaw those devices. Control
207 * reappears in this routine after a restore.
208 */
209
Adrian Bunk47a460d2008-02-04 22:30:06 -0800210static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700211{
212 int error;
213
214 error = arch_prepare_suspend();
215 if (error)
216 return error;
217
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200218 device_pm_lock();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100219
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700220 /* At this point, device_suspend() has been called, but *not*
221 * device_power_down(). We *must* call device_power_down() now.
222 * Otherwise, drivers for some devices (e.g. interrupt controllers)
223 * become desynchronized with the actual state of the hardware
224 * at resume time, and evil weirdness ensues.
225 */
226 error = device_power_down(PMSG_FREEZE);
227 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100228 printk(KERN_ERR "PM: Some devices failed to power down, "
229 "aborting hibernation\n");
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100230 goto Unlock;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700231 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100232
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100233 error = platform_pre_snapshot(platform_mode);
234 if (error || hibernation_test(TEST_PLATFORM))
235 goto Platform_finish;
236
237 error = disable_nonboot_cpus();
238 if (error || hibernation_test(TEST_CPUS)
239 || hibernation_testmode(HIBERNATION_TEST))
240 goto Enable_cpus;
241
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100242 local_irq_disable();
243
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100244 sysdev_suspend(PMSG_FREEZE);
245 if (error) {
246 printk(KERN_ERR "PM: Some devices failed to power down, "
247 "aborting hibernation\n");
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100248 goto Enable_irqs;
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100249 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700250
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100251 if (hibernation_test(TEST_CORE))
252 goto Power_up;
253
254 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700255 save_processor_state();
256 error = swsusp_arch_suspend();
257 if (error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100258 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
259 error);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700260 /* Restore control flow magically appears here */
261 restore_processor_state();
262 if (!in_suspend)
263 platform_leave(platform_mode);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100264
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100265 Power_up:
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100266 sysdev_resume();
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700267 /* NOTE: device_power_up() is just a resume() for devices
268 * that suspended with irqs off ... no overall powerup.
269 */
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100270
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100271 Enable_irqs:
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100272 local_irq_enable();
273
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100274 Enable_cpus:
275 enable_nonboot_cpus();
276
277 Platform_finish:
278 platform_finish(platform_mode);
279
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200280 device_power_up(in_suspend ?
281 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100282
283 Unlock:
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200284 device_pm_unlock();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100285
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700286 return error;
287}
288
289/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700290 * hibernation_snapshot - quiesce devices and create the hibernation
291 * snapshot image.
292 * @platform_mode - if set, use the platform driver, if available, to
Nick Andrew877d0312009-01-26 11:06:57 +0100293 * prepare the platform firmware for the power transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700294 *
295 * Must be called with pm_mutex held
296 */
297
298int hibernation_snapshot(int platform_mode)
299{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100300 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700301
Zhang Rui3fe03132008-10-26 20:50:26 +0100302 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700303 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700304 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700305
Zhang Rui3fe03132008-10-26 20:50:26 +0100306 /* Free memory before shutting down devices. */
307 error = swsusp_shrink_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700308 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100309 goto Close;
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700310
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700311 suspend_console();
312 error = device_suspend(PMSG_FREEZE);
313 if (error)
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200314 goto Recover_platform;
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700315
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100316 if (hibernation_test(TEST_DEVICES))
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200317 goto Recover_platform;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700318
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100319 error = create_image(platform_mode);
320 /* Control returns here after successful restore */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100321
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100322 Resume_devices:
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200323 device_resume(in_suspend ?
324 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700325 resume_console();
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100326 Close:
327 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700328 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200329
330 Recover_platform:
331 platform_recover(platform_mode);
332 goto Resume_devices;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700333}
334
335/**
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100336 * resume_target_kernel - prepare devices that need to be suspended with
337 * interrupts off, restore the contents of highmem that have not been
338 * restored yet from the image and run the low level code that will restore
339 * the remaining contents of memory and switch to the just restored target
340 * kernel.
341 */
342
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100343static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100344{
345 int error;
346
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200347 device_pm_lock();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100348
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200349 error = device_power_down(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100350 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100351 printk(KERN_ERR "PM: Some devices failed to power down, "
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100352 "aborting resume\n");
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100353 goto Unlock;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100354 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100355
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100356 error = platform_pre_restore(platform_mode);
357 if (error)
358 goto Cleanup;
359
360 error = disable_nonboot_cpus();
361 if (error)
362 goto Enable_cpus;
363
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100364 local_irq_disable();
365
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100366 error = sysdev_suspend(PMSG_QUIESCE);
367 if (error)
368 goto Enable_irqs;
369
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100370 /* We'll ignore saved state, but this gets preempt count (etc) right */
371 save_processor_state();
372 error = restore_highmem();
373 if (!error) {
374 error = swsusp_arch_resume();
375 /*
376 * The code below is only ever reached in case of a failure.
377 * Otherwise execution continues at place where
378 * swsusp_arch_suspend() was called
379 */
380 BUG_ON(!error);
381 /* This call to restore_highmem() undos the previous one */
382 restore_highmem();
383 }
384 /*
385 * The only reason why swsusp_arch_resume() can fail is memory being
386 * very tight, so we have to free it as soon as we can to avoid
387 * subsequent failures
388 */
389 swsusp_free();
390 restore_processor_state();
391 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100392
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100393 sysdev_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100394
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100395 Enable_irqs:
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100396 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100397
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100398 Enable_cpus:
399 enable_nonboot_cpus();
400
401 Cleanup:
402 platform_restore_cleanup(platform_mode);
403
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100404 device_power_up(PMSG_RECOVER);
405
406 Unlock:
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200407 device_pm_unlock();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100408
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100409 return error;
410}
411
412/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700413 * hibernation_restore - quiesce devices and restore the hibernation
414 * snapshot image. If successful, control returns in hibernation_snaphot()
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700415 * @platform_mode - if set, use the platform driver, if available, to
Nick Andrew877d0312009-01-26 11:06:57 +0100416 * prepare the platform firmware for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700417 *
418 * Must be called with pm_mutex held
419 */
420
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700421int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700422{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100423 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700424
425 pm_prepare_console();
426 suspend_console();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200427 error = device_suspend(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700428 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100429 error = resume_target_kernel(platform_mode);
430 device_resume(PMSG_RECOVER);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700431 }
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700432 resume_console();
433 pm_restore_console();
434 return error;
435}
436
437/**
438 * hibernation_platform_enter - enter the hibernation state using the
439 * platform driver (if available)
440 */
441
442int hibernation_platform_enter(void)
443{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100444 int error;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700445
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700446 if (!hibernation_ops)
447 return -ENOSYS;
448
449 /*
450 * We have cancelled the power transition by running
451 * hibernation_ops->finish() before saving the image, so we should let
452 * the firmware know that we're going to enter the sleep state after all
453 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100454 error = hibernation_ops->begin();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700455 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100456 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700457
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100458 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700459 suspend_console();
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100460 error = device_suspend(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200461 if (error) {
462 if (hibernation_ops->recover)
463 hibernation_ops->recover();
464 goto Resume_devices;
465 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700466
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200467 device_pm_lock();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100468
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100469 error = device_power_down(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100470 if (error)
471 goto Unlock;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100472
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100473 error = hibernation_ops->prepare();
474 if (error)
475 goto Platofrm_finish;
476
477 error = disable_nonboot_cpus();
478 if (error)
479 goto Platofrm_finish;
480
481 local_irq_disable();
482 sysdev_suspend(PMSG_HIBERNATE);
483 hibernation_ops->enter();
484 /* We should never get here */
485 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700486
487 /*
488 * We don't need to reenable the nonboot CPUs or resume consoles, since
489 * the system is going to be halted anyway.
490 */
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100491 Platofrm_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700492 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100493
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100494 device_power_up(PMSG_RESTORE);
495
496 Unlock:
497 device_pm_unlock();
498
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700499 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100500 entering_platform_hibernation = false;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200501 device_resume(PMSG_RESTORE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700502 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100503
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100504 Close:
505 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100506
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700507 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700508}
509
510/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700511 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 *
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700513 * Use the platform driver, if configured so; otherwise try
514 * to power off or reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 */
516
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700517static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700519 switch (hibernation_mode) {
520 case HIBERNATION_TEST:
521 case HIBERNATION_TESTPROC:
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700522 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700523 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600524 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700526 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700527 hibernation_platform_enter();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700528 case HIBERNATION_SHUTDOWN:
529 kernel_power_off();
530 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600532 kernel_halt();
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700533 /*
534 * Valid image is on the disk, if we continue we risk serious data
535 * corruption after resume.
536 */
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100537 printk(KERN_CRIT "PM: Please power down manually\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 while(1);
539}
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541static int prepare_processes(void)
542{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800543 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (freeze_processes()) {
546 error = -EBUSY;
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100547 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700549 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700553 * hibernate - The granpappy of the built-in hibernation management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
555
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700556int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 int error;
559
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700560 mutex_lock(&pm_mutex);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700561 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700562 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
563 error = -EBUSY;
564 goto Unlock;
565 }
566
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100567 pm_prepare_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700568 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
569 if (error)
570 goto Exit;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700571
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700572 error = usermodehelper_disable();
573 if (error)
574 goto Exit;
575
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700576 /* Allocate memory management structures */
577 error = create_basic_memory_bitmaps();
578 if (error)
579 goto Exit;
580
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100581 printk(KERN_INFO "PM: Syncing filesystems ... ");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700582 sys_sync();
583 printk("done.\n");
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700586 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700587 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100589 if (hibernation_test(TEST_FREEZER))
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800590 goto Thaw;
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100591
592 if (hibernation_testmode(HIBERNATION_TESTPROC))
593 goto Thaw;
594
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700595 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
596 if (in_suspend && !error) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700597 unsigned int flags = 0;
598
599 if (hibernation_mode == HIBERNATION_PLATFORM)
600 flags |= SF_PLATFORM_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700602 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700603 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (!error)
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700605 power_down();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800606 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700608 swsusp_free();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800609 }
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800610 Thaw:
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100611 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700612 Finish:
613 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700614 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700615 Exit:
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700616 pm_notifier_call_chain(PM_POST_HIBERNATION);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100617 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700618 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700619 Unlock:
620 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return error;
622}
623
624
625/**
626 * software_resume - Resume from a saved image.
627 *
628 * Called as a late_initcall (so all devices are discovered and
629 * initialized), we call swsusp to see if we have a saved image or not.
630 * If so, we quiesce devices, the restore the saved image. We will
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700631 * return above (in hibernate() ) if everything goes well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 * Otherwise, we fail gracefully and return to the normally
633 * scheduled program.
634 *
635 */
636
637static int software_resume(void)
638{
639 int error;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700640 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Johannes Berg60a0d232007-11-14 17:00:16 -0800642 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100643 * If the user said "noresume".. bail out early.
644 */
645 if (noresume)
646 return 0;
647
648 /*
Rafael J. Wysockic7510852009-04-12 20:06:56 +0200649 * We can't depend on SCSI devices being available after loading one of
650 * their modules if scsi_complete_async_scans() is not called and the
651 * resume device usually is a SCSI one.
652 */
653 scsi_complete_async_scans();
654
655 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800656 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
657 * is configured into the kernel. Since the regular hibernate
658 * trigger path is via sysfs which takes a buffer mutex before
659 * calling hibernate functions (which take pm_mutex) this can
660 * cause lockdep to complain about a possible ABBA deadlock
661 * which cannot happen since we're in the boot code here and
662 * sysfs can't be invoked yet. Therefore, we use a subclass
663 * here to avoid lockdep complaining.
664 */
665 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
Pavel Machek3efa1472005-07-07 17:56:43 -0700666 if (!swsusp_resume_device) {
Shaohua Lidd5d6662005-09-03 15:57:04 -0700667 if (!strlen(resume_file)) {
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800668 mutex_unlock(&pm_mutex);
Pavel Machek3efa1472005-07-07 17:56:43 -0700669 return -ENOENT;
Shaohua Lidd5d6662005-09-03 15:57:04 -0700670 }
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100671 /*
672 * Some device discovery might still be in progress; we need
673 * to wait for this to finish.
674 */
675 wait_for_device_probe();
Pavel Machek3efa1472005-07-07 17:56:43 -0700676 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100677 pr_debug("PM: Resume from partition %s\n", resume_file);
Pavel Machek3efa1472005-07-07 17:56:43 -0700678 } else {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100679 pr_debug("PM: Resume from partition %d:%d\n",
680 MAJOR(swsusp_resume_device),
681 MINOR(swsusp_resume_device));
Pavel Machek3efa1472005-07-07 17:56:43 -0700682 }
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (noresume) {
685 /**
Rafael J. Wysocki95758092007-12-08 02:06:57 +0100686 * FIXME: If noresume is specified, we need to find the
687 * partition and reset it back to normal swap space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800689 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return 0;
691 }
692
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100693 pr_debug("PM: Checking hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800694 error = swsusp_check();
695 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700696 goto Unlock;
697
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700698 /* The snapshot device should not be opened while we're running */
699 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
700 error = -EBUSY;
701 goto Unlock;
702 }
703
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100704 pm_prepare_console();
Alan Sternc3e94d82007-11-19 23:38:25 +0100705 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
706 if (error)
707 goto Finish;
708
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700709 error = usermodehelper_disable();
710 if (error)
711 goto Finish;
712
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700713 error = create_basic_memory_bitmaps();
714 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700715 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 pr_debug("PM: Preparing processes for restore.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800718 error = prepare_processes();
719 if (error) {
Al Viroc2dd0da2007-10-08 13:21:10 -0400720 swsusp_close(FMODE_READ);
Li Shaohua5a72e042005-06-25 14:55:06 -0700721 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100724 pr_debug("PM: Reading hibernation image.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700726 error = swsusp_read(&flags);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800727 if (!error)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700728 hibernation_restore(flags & SF_PLATFORM_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800730 printk(KERN_ERR "PM: Restore failed, recovering.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700731 swsusp_free();
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100732 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 Done:
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700734 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700735 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700736 Finish:
Alan Sternc3e94d82007-11-19 23:38:25 +0100737 pm_notifier_call_chain(PM_POST_RESTORE);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100738 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700739 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700740 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700741 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800742 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 pr_debug("PM: Resume from disk failed.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700744 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
747late_initcall(software_resume);
748
749
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700750static const char * const hibernation_modes[] = {
751 [HIBERNATION_PLATFORM] = "platform",
752 [HIBERNATION_SHUTDOWN] = "shutdown",
753 [HIBERNATION_REBOOT] = "reboot",
754 [HIBERNATION_TEST] = "test",
755 [HIBERNATION_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756};
757
758/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700759 * disk - Control hibernation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700761 * Suspend-to-disk can be handled in several ways. We have a few options
762 * for putting the system to sleep - using the platform driver (e.g. ACPI
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700763 * or other hibernation_ops), powering off the system or rebooting the
764 * system (for testing) as well as the two test modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700766 * The system can support 'platform', and that is known a priori (and
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700767 * encoded by the presence of hibernation_ops). However, the user may
768 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
769 * test modes, 'test' or 'testproc'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 *
771 * show() will display what the mode is currently set to.
772 * store() will accept one of
773 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 * 'platform'
775 * 'shutdown'
776 * 'reboot'
Johannes Berg11d77d02007-04-30 15:09:53 -0700777 * 'test'
778 * 'testproc'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700780 * It will only change to 'platform' if the system
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700781 * supports it (as determined by having hibernation_ops).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 */
783
Kay Sievers386f2752007-11-02 13:47:53 +0100784static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
785 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700787 int i;
788 char *start = buf;
789
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700790 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
791 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700792 continue;
793 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700794 case HIBERNATION_SHUTDOWN:
795 case HIBERNATION_REBOOT:
796 case HIBERNATION_TEST:
797 case HIBERNATION_TESTPROC:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700798 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700799 case HIBERNATION_PLATFORM:
800 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700801 break;
802 /* not a valid mode, continue with loop */
803 continue;
804 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700805 if (i == hibernation_mode)
806 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700807 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700808 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700809 }
810 buf += sprintf(buf, "\n");
811 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
814
Kay Sievers386f2752007-11-02 13:47:53 +0100815static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
816 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
818 int error = 0;
819 int i;
820 int len;
821 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700822 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 p = memchr(buf, '\n', n);
825 len = p ? p - buf : n;
826
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800827 mutex_lock(&pm_mutex);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700828 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700829 if (len == strlen(hibernation_modes[i])
830 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 mode = i;
832 break;
833 }
834 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700835 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700836 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700837 case HIBERNATION_SHUTDOWN:
838 case HIBERNATION_REBOOT:
839 case HIBERNATION_TEST:
840 case HIBERNATION_TESTPROC:
841 hibernation_mode = mode;
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700842 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700843 case HIBERNATION_PLATFORM:
844 if (hibernation_ops)
845 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 else
847 error = -EINVAL;
848 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700849 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 error = -EINVAL;
851
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700852 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100853 pr_debug("PM: Hibernation mode set to '%s'\n",
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700854 hibernation_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800855 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return error ? error : n;
857}
858
859power_attr(disk);
860
Kay Sievers386f2752007-11-02 13:47:53 +0100861static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
862 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
865 MINOR(swsusp_resume_device));
866}
867
Kay Sievers386f2752007-11-02 13:47:53 +0100868static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
869 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800873 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Andrew Mortona5762192006-01-06 00:09:50 -0800875 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
876 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Andrew Mortona5762192006-01-06 00:09:50 -0800878 res = MKDEV(maj,min);
879 if (maj != MAJOR(res) || min != MINOR(res))
880 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800882 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800883 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800884 mutex_unlock(&pm_mutex);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100885 printk(KERN_INFO "PM: Starting manual resume from disk\n");
Andrew Mortona5762192006-01-06 00:09:50 -0800886 noresume = 0;
887 software_resume();
888 ret = n;
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800889 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800890 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893power_attr(resume);
894
Kay Sievers386f2752007-11-02 13:47:53 +0100895static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
896 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800897{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800898 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800899}
900
Kay Sievers386f2752007-11-02 13:47:53 +0100901static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
902 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800903{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800904 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800905
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800906 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800907 image_size = size;
908 return n;
909 }
910
911 return -EINVAL;
912}
913
914power_attr(image_size);
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916static struct attribute * g[] = {
917 &disk_attr.attr,
918 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800919 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 NULL,
921};
922
923
924static struct attribute_group attr_group = {
925 .attrs = g,
926};
927
928
929static int __init pm_disk_init(void)
930{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800931 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
934core_initcall(pm_disk_init);
935
936
937static int __init resume_setup(char *str)
938{
939 if (noresume)
940 return 1;
941
942 strncpy( resume_file, str, 255 );
943 return 1;
944}
945
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800946static int __init resume_offset_setup(char *str)
947{
948 unsigned long long offset;
949
950 if (noresume)
951 return 1;
952
953 if (sscanf(str, "%llu", &offset) == 1)
954 swsusp_resume_block = offset;
955
956 return 1;
957}
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959static int __init noresume_setup(char *str)
960{
961 noresume = 1;
962 return 1;
963}
964
965__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800966__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967__setup("resume=", resume_setup);