blob: f3db382c2b2d4cf15c4e4f1ae988752b6bc68f81 [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>
Magnus Damma8af7892009-03-31 15:23:37 -070025#include <asm/suspend.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "power.h"
28
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static int noresume = 0;
Adrian Bunk47a460d2008-02-04 22:30:06 -080031static char resume_file[256] = CONFIG_PM_STD_PARTITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080033sector_t swsusp_resume_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070035enum {
36 HIBERNATION_INVALID,
37 HIBERNATION_PLATFORM,
38 HIBERNATION_TEST,
39 HIBERNATION_TESTPROC,
40 HIBERNATION_SHUTDOWN,
41 HIBERNATION_REBOOT,
42 /* keep last */
43 __HIBERNATION_AFTER_LAST
44};
45#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
46#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
47
48static int hibernation_mode = HIBERNATION_SHUTDOWN;
49
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070050static struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070051
52/**
53 * hibernation_set_ops - set the global hibernate operations
54 * @ops: the hibernation operations to use in subsequent hibernation transitions
55 */
56
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070057void hibernation_set_ops(struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070058{
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +010059 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
60 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -070061 && ops->restore_cleanup)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070062 WARN_ON(1);
63 return;
64 }
65 mutex_lock(&pm_mutex);
66 hibernation_ops = ops;
67 if (ops)
68 hibernation_mode = HIBERNATION_PLATFORM;
69 else if (hibernation_mode == HIBERNATION_PLATFORM)
70 hibernation_mode = HIBERNATION_SHUTDOWN;
71
72 mutex_unlock(&pm_mutex);
73}
74
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +010075static bool entering_platform_hibernation;
76
77bool system_entering_hibernation(void)
78{
79 return entering_platform_hibernation;
80}
81EXPORT_SYMBOL(system_entering_hibernation);
82
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +010083#ifdef CONFIG_PM_DEBUG
84static void hibernation_debug_sleep(void)
85{
86 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
87 mdelay(5000);
88}
89
90static int hibernation_testmode(int mode)
91{
92 if (hibernation_mode == mode) {
93 hibernation_debug_sleep();
94 return 1;
95 }
96 return 0;
97}
98
99static int hibernation_test(int level)
100{
101 if (pm_test_level == level) {
102 hibernation_debug_sleep();
103 return 1;
104 }
105 return 0;
106}
107#else /* !CONFIG_PM_DEBUG */
108static int hibernation_testmode(int mode) { return 0; }
109static int hibernation_test(int level) { return 0; }
110#endif /* !CONFIG_PM_DEBUG */
111
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700112/**
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100113 * platform_begin - tell the platform driver that we're starting
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700114 * hibernation
115 */
116
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100117static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700118{
119 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100120 hibernation_ops->begin() : 0;
121}
122
123/**
124 * platform_end - tell the platform driver that we've entered the
125 * working state
126 */
127
128static void platform_end(int platform_mode)
129{
130 if (platform_mode && hibernation_ops)
131 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700132}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/**
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700135 * platform_pre_snapshot - prepare the machine for hibernation using the
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800136 * platform driver if so configured and return an error code if it fails
137 */
138
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700139static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800140{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700141 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700142 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800143}
144
145/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700146 * platform_leave - prepare the machine for switching to the normal mode
147 * of operation using the platform driver (called with interrupts disabled)
148 */
149
150static void platform_leave(int platform_mode)
151{
152 if (platform_mode && hibernation_ops)
153 hibernation_ops->leave();
154}
155
156/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700157 * platform_finish - switch the machine to the normal mode of operation
158 * using the platform driver (must be called after platform_prepare())
159 */
160
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700161static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700162{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700163 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700164 hibernation_ops->finish();
165}
166
167/**
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700168 * platform_pre_restore - prepare the platform for the restoration from a
169 * hibernation image. If the restore fails after this function has been
170 * called, platform_restore_cleanup() must be called.
171 */
172
173static int platform_pre_restore(int platform_mode)
174{
175 return (platform_mode && hibernation_ops) ?
176 hibernation_ops->pre_restore() : 0;
177}
178
179/**
180 * platform_restore_cleanup - switch the platform to the normal mode of
181 * operation after a failing restore. If platform_pre_restore() has been
182 * called before the failing restore, this function must be called too,
183 * regardless of the result of platform_pre_restore().
184 */
185
186static void platform_restore_cleanup(int platform_mode)
187{
188 if (platform_mode && hibernation_ops)
189 hibernation_ops->restore_cleanup();
190}
191
192/**
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200193 * platform_recover - recover the platform from a failure to suspend
194 * devices.
195 */
196
197static void platform_recover(int platform_mode)
198{
199 if (platform_mode && hibernation_ops && hibernation_ops->recover)
200 hibernation_ops->recover();
201}
202
203/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700204 * create_image - freeze devices that need to be frozen with interrupts
205 * off, create the hibernation image and thaw those devices. Control
206 * reappears in this routine after a restore.
207 */
208
Adrian Bunk47a460d2008-02-04 22:30:06 -0800209static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700210{
211 int error;
212
213 error = arch_prepare_suspend();
214 if (error)
215 return error;
216
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200217 device_pm_lock();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100218
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700219 /* At this point, device_suspend() has been called, but *not*
220 * device_power_down(). We *must* call device_power_down() now.
221 * Otherwise, drivers for some devices (e.g. interrupt controllers)
222 * become desynchronized with the actual state of the hardware
223 * at resume time, and evil weirdness ensues.
224 */
225 error = device_power_down(PMSG_FREEZE);
226 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100227 printk(KERN_ERR "PM: Some devices failed to power down, "
228 "aborting hibernation\n");
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100229 goto Unlock;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700230 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100231
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100232 error = platform_pre_snapshot(platform_mode);
233 if (error || hibernation_test(TEST_PLATFORM))
234 goto Platform_finish;
235
236 error = disable_nonboot_cpus();
237 if (error || hibernation_test(TEST_CPUS)
238 || hibernation_testmode(HIBERNATION_TEST))
239 goto Enable_cpus;
240
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100241 local_irq_disable();
242
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100243 sysdev_suspend(PMSG_FREEZE);
244 if (error) {
245 printk(KERN_ERR "PM: Some devices failed to power down, "
246 "aborting hibernation\n");
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100247 goto Enable_irqs;
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100248 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700249
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100250 if (hibernation_test(TEST_CORE))
251 goto Power_up;
252
253 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700254 save_processor_state();
255 error = swsusp_arch_suspend();
256 if (error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100257 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
258 error);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700259 /* Restore control flow magically appears here */
260 restore_processor_state();
261 if (!in_suspend)
262 platform_leave(platform_mode);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100263
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100264 Power_up:
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100265 sysdev_resume();
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700266 /* NOTE: device_power_up() is just a resume() for devices
267 * that suspended with irqs off ... no overall powerup.
268 */
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100269
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100270 Enable_irqs:
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100271 local_irq_enable();
272
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100273 Enable_cpus:
274 enable_nonboot_cpus();
275
276 Platform_finish:
277 platform_finish(platform_mode);
278
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200279 device_power_up(in_suspend ?
280 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100281
282 Unlock:
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200283 device_pm_unlock();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100284
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700285 return error;
286}
287
288/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700289 * hibernation_snapshot - quiesce devices and create the hibernation
290 * snapshot image.
291 * @platform_mode - if set, use the platform driver, if available, to
292 * prepare the platform frimware for the power transition.
293 *
294 * Must be called with pm_mutex held
295 */
296
297int hibernation_snapshot(int platform_mode)
298{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100299 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700300
Zhang Rui3fe03132008-10-26 20:50:26 +0100301 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700302 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700303 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700304
Zhang Rui3fe03132008-10-26 20:50:26 +0100305 /* Free memory before shutting down devices. */
306 error = swsusp_shrink_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700307 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100308 goto Close;
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700309
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700310 suspend_console();
311 error = device_suspend(PMSG_FREEZE);
312 if (error)
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200313 goto Recover_platform;
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700314
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100315 if (hibernation_test(TEST_DEVICES))
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200316 goto Recover_platform;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700317
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100318 error = create_image(platform_mode);
319 /* Control returns here after successful restore */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100320
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100321 Resume_devices:
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200322 device_resume(in_suspend ?
323 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700324 resume_console();
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100325 Close:
326 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700327 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200328
329 Recover_platform:
330 platform_recover(platform_mode);
331 goto Resume_devices;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700332}
333
334/**
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100335 * resume_target_kernel - prepare devices that need to be suspended with
336 * interrupts off, restore the contents of highmem that have not been
337 * restored yet from the image and run the low level code that will restore
338 * the remaining contents of memory and switch to the just restored target
339 * kernel.
340 */
341
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100342static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100343{
344 int error;
345
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200346 device_pm_lock();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100347
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200348 error = device_power_down(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100349 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100350 printk(KERN_ERR "PM: Some devices failed to power down, "
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100351 "aborting resume\n");
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100352 goto Unlock;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100353 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100354
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100355 error = platform_pre_restore(platform_mode);
356 if (error)
357 goto Cleanup;
358
359 error = disable_nonboot_cpus();
360 if (error)
361 goto Enable_cpus;
362
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100363 local_irq_disable();
364
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100365 error = sysdev_suspend(PMSG_QUIESCE);
366 if (error)
367 goto Enable_irqs;
368
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100369 /* We'll ignore saved state, but this gets preempt count (etc) right */
370 save_processor_state();
371 error = restore_highmem();
372 if (!error) {
373 error = swsusp_arch_resume();
374 /*
375 * The code below is only ever reached in case of a failure.
376 * Otherwise execution continues at place where
377 * swsusp_arch_suspend() was called
378 */
379 BUG_ON(!error);
380 /* This call to restore_highmem() undos the previous one */
381 restore_highmem();
382 }
383 /*
384 * The only reason why swsusp_arch_resume() can fail is memory being
385 * very tight, so we have to free it as soon as we can to avoid
386 * subsequent failures
387 */
388 swsusp_free();
389 restore_processor_state();
390 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100391
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100392 sysdev_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100393
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100394 Enable_irqs:
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100395 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100396
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100397 Enable_cpus:
398 enable_nonboot_cpus();
399
400 Cleanup:
401 platform_restore_cleanup(platform_mode);
402
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100403 device_power_up(PMSG_RECOVER);
404
405 Unlock:
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200406 device_pm_unlock();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100407
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100408 return error;
409}
410
411/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700412 * hibernation_restore - quiesce devices and restore the hibernation
413 * snapshot image. If successful, control returns in hibernation_snaphot()
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700414 * @platform_mode - if set, use the platform driver, if available, to
415 * prepare the platform frimware for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700416 *
417 * Must be called with pm_mutex held
418 */
419
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700420int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700421{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100422 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700423
424 pm_prepare_console();
425 suspend_console();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200426 error = device_suspend(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700427 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100428 error = resume_target_kernel(platform_mode);
429 device_resume(PMSG_RECOVER);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700430 }
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700431 resume_console();
432 pm_restore_console();
433 return error;
434}
435
436/**
437 * hibernation_platform_enter - enter the hibernation state using the
438 * platform driver (if available)
439 */
440
441int hibernation_platform_enter(void)
442{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100443 int error;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700444
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700445 if (!hibernation_ops)
446 return -ENOSYS;
447
448 /*
449 * We have cancelled the power transition by running
450 * hibernation_ops->finish() before saving the image, so we should let
451 * the firmware know that we're going to enter the sleep state after all
452 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100453 error = hibernation_ops->begin();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700454 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100455 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700456
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100457 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700458 suspend_console();
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100459 error = device_suspend(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200460 if (error) {
461 if (hibernation_ops->recover)
462 hibernation_ops->recover();
463 goto Resume_devices;
464 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700465
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200466 device_pm_lock();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100467
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100468 error = device_power_down(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100469 if (error)
470 goto Unlock;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100471
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100472 error = hibernation_ops->prepare();
473 if (error)
474 goto Platofrm_finish;
475
476 error = disable_nonboot_cpus();
477 if (error)
478 goto Platofrm_finish;
479
480 local_irq_disable();
481 sysdev_suspend(PMSG_HIBERNATE);
482 hibernation_ops->enter();
483 /* We should never get here */
484 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700485
486 /*
487 * We don't need to reenable the nonboot CPUs or resume consoles, since
488 * the system is going to be halted anyway.
489 */
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100490 Platofrm_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700491 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100492
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100493 device_power_up(PMSG_RESTORE);
494
495 Unlock:
496 device_pm_unlock();
497
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700498 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100499 entering_platform_hibernation = false;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200500 device_resume(PMSG_RESTORE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700501 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100502
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100503 Close:
504 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100505
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700506 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700507}
508
509/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700510 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 *
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700512 * Use the platform driver, if configured so; otherwise try
513 * to power off or reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 */
515
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700516static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700518 switch (hibernation_mode) {
519 case HIBERNATION_TEST:
520 case HIBERNATION_TESTPROC:
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700521 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700522 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600523 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700525 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700526 hibernation_platform_enter();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700527 case HIBERNATION_SHUTDOWN:
528 kernel_power_off();
529 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600531 kernel_halt();
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700532 /*
533 * Valid image is on the disk, if we continue we risk serious data
534 * corruption after resume.
535 */
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100536 printk(KERN_CRIT "PM: Please power down manually\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 while(1);
538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540static int prepare_processes(void)
541{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800542 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (freeze_processes()) {
545 error = -EBUSY;
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100546 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700548 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700552 * hibernate - The granpappy of the built-in hibernation management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 */
554
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700555int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 int error;
558
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700559 mutex_lock(&pm_mutex);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700560 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700561 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
562 error = -EBUSY;
563 goto Unlock;
564 }
565
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100566 pm_prepare_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700567 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
568 if (error)
569 goto Exit;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700570
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700571 error = usermodehelper_disable();
572 if (error)
573 goto Exit;
574
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700575 /* Allocate memory management structures */
576 error = create_basic_memory_bitmaps();
577 if (error)
578 goto Exit;
579
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100580 printk(KERN_INFO "PM: Syncing filesystems ... ");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700581 sys_sync();
582 printk("done.\n");
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700585 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700586 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100588 if (hibernation_test(TEST_FREEZER))
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800589 goto Thaw;
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100590
591 if (hibernation_testmode(HIBERNATION_TESTPROC))
592 goto Thaw;
593
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700594 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
595 if (in_suspend && !error) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700596 unsigned int flags = 0;
597
598 if (hibernation_mode == HIBERNATION_PLATFORM)
599 flags |= SF_PLATFORM_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700601 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700602 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (!error)
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700604 power_down();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800605 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700607 swsusp_free();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800608 }
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800609 Thaw:
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100610 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700611 Finish:
612 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700613 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700614 Exit:
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700615 pm_notifier_call_chain(PM_POST_HIBERNATION);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100616 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700617 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700618 Unlock:
619 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return error;
621}
622
623
624/**
625 * software_resume - Resume from a saved image.
626 *
627 * Called as a late_initcall (so all devices are discovered and
628 * initialized), we call swsusp to see if we have a saved image or not.
629 * If so, we quiesce devices, the restore the saved image. We will
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700630 * return above (in hibernate() ) if everything goes well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * Otherwise, we fail gracefully and return to the normally
632 * scheduled program.
633 *
634 */
635
636static int software_resume(void)
637{
638 int error;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700639 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Johannes Berg60a0d232007-11-14 17:00:16 -0800641 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100642 * If the user said "noresume".. bail out early.
643 */
644 if (noresume)
645 return 0;
646
647 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800648 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
649 * is configured into the kernel. Since the regular hibernate
650 * trigger path is via sysfs which takes a buffer mutex before
651 * calling hibernate functions (which take pm_mutex) this can
652 * cause lockdep to complain about a possible ABBA deadlock
653 * which cannot happen since we're in the boot code here and
654 * sysfs can't be invoked yet. Therefore, we use a subclass
655 * here to avoid lockdep complaining.
656 */
657 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
Pavel Machek3efa1472005-07-07 17:56:43 -0700658 if (!swsusp_resume_device) {
Shaohua Lidd5d6662005-09-03 15:57:04 -0700659 if (!strlen(resume_file)) {
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800660 mutex_unlock(&pm_mutex);
Pavel Machek3efa1472005-07-07 17:56:43 -0700661 return -ENOENT;
Shaohua Lidd5d6662005-09-03 15:57:04 -0700662 }
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100663 /*
664 * Some device discovery might still be in progress; we need
665 * to wait for this to finish.
666 */
667 wait_for_device_probe();
Pavel Machek3efa1472005-07-07 17:56:43 -0700668 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100669 pr_debug("PM: Resume from partition %s\n", resume_file);
Pavel Machek3efa1472005-07-07 17:56:43 -0700670 } else {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100671 pr_debug("PM: Resume from partition %d:%d\n",
672 MAJOR(swsusp_resume_device),
673 MINOR(swsusp_resume_device));
Pavel Machek3efa1472005-07-07 17:56:43 -0700674 }
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (noresume) {
677 /**
Rafael J. Wysocki95758092007-12-08 02:06:57 +0100678 * FIXME: If noresume is specified, we need to find the
679 * partition and reset it back to normal swap space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 */
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800681 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return 0;
683 }
684
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100685 pr_debug("PM: Checking hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800686 error = swsusp_check();
687 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700688 goto Unlock;
689
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700690 /* The snapshot device should not be opened while we're running */
691 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
692 error = -EBUSY;
693 goto Unlock;
694 }
695
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100696 pm_prepare_console();
Alan Sternc3e94d82007-11-19 23:38:25 +0100697 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
698 if (error)
699 goto Finish;
700
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700701 error = usermodehelper_disable();
702 if (error)
703 goto Finish;
704
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700705 error = create_basic_memory_bitmaps();
706 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700707 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 pr_debug("PM: Preparing processes for restore.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800710 error = prepare_processes();
711 if (error) {
Al Viroc2dd0da2007-10-08 13:21:10 -0400712 swsusp_close(FMODE_READ);
Li Shaohua5a72e042005-06-25 14:55:06 -0700713 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100716 pr_debug("PM: Reading hibernation image.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700718 error = swsusp_read(&flags);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800719 if (!error)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700720 hibernation_restore(flags & SF_PLATFORM_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800722 printk(KERN_ERR "PM: Restore failed, recovering.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700723 swsusp_free();
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100724 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 Done:
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700726 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700727 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700728 Finish:
Alan Sternc3e94d82007-11-19 23:38:25 +0100729 pm_notifier_call_chain(PM_POST_RESTORE);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100730 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700731 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700732 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700733 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800734 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 pr_debug("PM: Resume from disk failed.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700736 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739late_initcall(software_resume);
740
741
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700742static const char * const hibernation_modes[] = {
743 [HIBERNATION_PLATFORM] = "platform",
744 [HIBERNATION_SHUTDOWN] = "shutdown",
745 [HIBERNATION_REBOOT] = "reboot",
746 [HIBERNATION_TEST] = "test",
747 [HIBERNATION_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748};
749
750/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700751 * disk - Control hibernation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700753 * Suspend-to-disk can be handled in several ways. We have a few options
754 * for putting the system to sleep - using the platform driver (e.g. ACPI
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700755 * or other hibernation_ops), powering off the system or rebooting the
756 * system (for testing) as well as the two test modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700758 * The system can support 'platform', and that is known a priori (and
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700759 * encoded by the presence of hibernation_ops). However, the user may
760 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
761 * test modes, 'test' or 'testproc'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 *
763 * show() will display what the mode is currently set to.
764 * store() will accept one of
765 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 * 'platform'
767 * 'shutdown'
768 * 'reboot'
Johannes Berg11d77d02007-04-30 15:09:53 -0700769 * 'test'
770 * 'testproc'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700772 * It will only change to 'platform' if the system
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700773 * supports it (as determined by having hibernation_ops).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 */
775
Kay Sievers386f2752007-11-02 13:47:53 +0100776static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
777 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700779 int i;
780 char *start = buf;
781
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700782 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
783 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700784 continue;
785 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700786 case HIBERNATION_SHUTDOWN:
787 case HIBERNATION_REBOOT:
788 case HIBERNATION_TEST:
789 case HIBERNATION_TESTPROC:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700790 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700791 case HIBERNATION_PLATFORM:
792 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700793 break;
794 /* not a valid mode, continue with loop */
795 continue;
796 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700797 if (i == hibernation_mode)
798 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700799 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700800 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700801 }
802 buf += sprintf(buf, "\n");
803 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
806
Kay Sievers386f2752007-11-02 13:47:53 +0100807static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
808 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 int error = 0;
811 int i;
812 int len;
813 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700814 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 p = memchr(buf, '\n', n);
817 len = p ? p - buf : n;
818
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800819 mutex_lock(&pm_mutex);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700820 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700821 if (len == strlen(hibernation_modes[i])
822 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 mode = i;
824 break;
825 }
826 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700827 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700828 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700829 case HIBERNATION_SHUTDOWN:
830 case HIBERNATION_REBOOT:
831 case HIBERNATION_TEST:
832 case HIBERNATION_TESTPROC:
833 hibernation_mode = mode;
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700834 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700835 case HIBERNATION_PLATFORM:
836 if (hibernation_ops)
837 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 else
839 error = -EINVAL;
840 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700841 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 error = -EINVAL;
843
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700844 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100845 pr_debug("PM: Hibernation mode set to '%s'\n",
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700846 hibernation_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800847 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return error ? error : n;
849}
850
851power_attr(disk);
852
Kay Sievers386f2752007-11-02 13:47:53 +0100853static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
854 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
857 MINOR(swsusp_resume_device));
858}
859
Kay Sievers386f2752007-11-02 13:47:53 +0100860static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
861 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800865 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Andrew Mortona5762192006-01-06 00:09:50 -0800867 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
868 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Andrew Mortona5762192006-01-06 00:09:50 -0800870 res = MKDEV(maj,min);
871 if (maj != MAJOR(res) || min != MINOR(res))
872 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800874 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800875 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800876 mutex_unlock(&pm_mutex);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100877 printk(KERN_INFO "PM: Starting manual resume from disk\n");
Andrew Mortona5762192006-01-06 00:09:50 -0800878 noresume = 0;
879 software_resume();
880 ret = n;
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800881 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800882 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885power_attr(resume);
886
Kay Sievers386f2752007-11-02 13:47:53 +0100887static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
888 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800889{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800890 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800891}
892
Kay Sievers386f2752007-11-02 13:47:53 +0100893static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
894 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800895{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800896 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800897
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800898 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800899 image_size = size;
900 return n;
901 }
902
903 return -EINVAL;
904}
905
906power_attr(image_size);
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908static struct attribute * g[] = {
909 &disk_attr.attr,
910 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800911 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 NULL,
913};
914
915
916static struct attribute_group attr_group = {
917 .attrs = g,
918};
919
920
921static int __init pm_disk_init(void)
922{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800923 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
926core_initcall(pm_disk_init);
927
928
929static int __init resume_setup(char *str)
930{
931 if (noresume)
932 return 1;
933
934 strncpy( resume_file, str, 255 );
935 return 1;
936}
937
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800938static int __init resume_offset_setup(char *str)
939{
940 unsigned long long offset;
941
942 if (noresume)
943 return 1;
944
945 if (sscanf(str, "%llu", &offset) == 1)
946 swsusp_resume_block = offset;
947
948 return 1;
949}
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951static int __init noresume_setup(char *str)
952{
953 noresume = 1;
954 return 1;
955}
956
957__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800958__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959__setup("resume=", resume_setup);