blob: e886d1332a1024d40f2edc6cde7d144ec2ea8f59 [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>
Andrew Mortond53d9f12005-07-12 13:58:07 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "power.h"
27
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static int noresume = 0;
Adrian Bunk47a460d2008-02-04 22:30:06 -080030static char resume_file[256] = CONFIG_PM_STD_PARTITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080032sector_t swsusp_resume_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070034enum {
35 HIBERNATION_INVALID,
36 HIBERNATION_PLATFORM,
37 HIBERNATION_TEST,
38 HIBERNATION_TESTPROC,
39 HIBERNATION_SHUTDOWN,
40 HIBERNATION_REBOOT,
41 /* keep last */
42 __HIBERNATION_AFTER_LAST
43};
44#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
45#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
46
47static int hibernation_mode = HIBERNATION_SHUTDOWN;
48
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070049static struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070050
51/**
52 * hibernation_set_ops - set the global hibernate operations
53 * @ops: the hibernation operations to use in subsequent hibernation transitions
54 */
55
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070056void hibernation_set_ops(struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070057{
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +010058 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
59 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -070060 && ops->restore_cleanup)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070061 WARN_ON(1);
62 return;
63 }
64 mutex_lock(&pm_mutex);
65 hibernation_ops = ops;
66 if (ops)
67 hibernation_mode = HIBERNATION_PLATFORM;
68 else if (hibernation_mode == HIBERNATION_PLATFORM)
69 hibernation_mode = HIBERNATION_SHUTDOWN;
70
71 mutex_unlock(&pm_mutex);
72}
73
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +010074static bool entering_platform_hibernation;
75
76bool system_entering_hibernation(void)
77{
78 return entering_platform_hibernation;
79}
80EXPORT_SYMBOL(system_entering_hibernation);
81
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +010082#ifdef CONFIG_PM_DEBUG
83static void hibernation_debug_sleep(void)
84{
85 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
86 mdelay(5000);
87}
88
89static int hibernation_testmode(int mode)
90{
91 if (hibernation_mode == mode) {
92 hibernation_debug_sleep();
93 return 1;
94 }
95 return 0;
96}
97
98static int hibernation_test(int level)
99{
100 if (pm_test_level == level) {
101 hibernation_debug_sleep();
102 return 1;
103 }
104 return 0;
105}
106#else /* !CONFIG_PM_DEBUG */
107static int hibernation_testmode(int mode) { return 0; }
108static int hibernation_test(int level) { return 0; }
109#endif /* !CONFIG_PM_DEBUG */
110
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700111/**
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100112 * platform_begin - tell the platform driver that we're starting
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700113 * hibernation
114 */
115
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100116static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700117{
118 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100119 hibernation_ops->begin() : 0;
120}
121
122/**
123 * platform_end - tell the platform driver that we've entered the
124 * working state
125 */
126
127static void platform_end(int platform_mode)
128{
129 if (platform_mode && hibernation_ops)
130 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700131}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/**
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700134 * platform_pre_snapshot - prepare the machine for hibernation using the
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800135 * platform driver if so configured and return an error code if it fails
136 */
137
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700138static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800139{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700140 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700141 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800142}
143
144/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700145 * platform_leave - prepare the machine for switching to the normal mode
146 * of operation using the platform driver (called with interrupts disabled)
147 */
148
149static void platform_leave(int platform_mode)
150{
151 if (platform_mode && hibernation_ops)
152 hibernation_ops->leave();
153}
154
155/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700156 * platform_finish - switch the machine to the normal mode of operation
157 * using the platform driver (must be called after platform_prepare())
158 */
159
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700160static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700161{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700162 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700163 hibernation_ops->finish();
164}
165
166/**
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700167 * platform_pre_restore - prepare the platform for the restoration from a
168 * hibernation image. If the restore fails after this function has been
169 * called, platform_restore_cleanup() must be called.
170 */
171
172static int platform_pre_restore(int platform_mode)
173{
174 return (platform_mode && hibernation_ops) ?
175 hibernation_ops->pre_restore() : 0;
176}
177
178/**
179 * platform_restore_cleanup - switch the platform to the normal mode of
180 * operation after a failing restore. If platform_pre_restore() has been
181 * called before the failing restore, this function must be called too,
182 * regardless of the result of platform_pre_restore().
183 */
184
185static void platform_restore_cleanup(int platform_mode)
186{
187 if (platform_mode && hibernation_ops)
188 hibernation_ops->restore_cleanup();
189}
190
191/**
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200192 * platform_recover - recover the platform from a failure to suspend
193 * devices.
194 */
195
196static void platform_recover(int platform_mode)
197{
198 if (platform_mode && hibernation_ops && hibernation_ops->recover)
199 hibernation_ops->recover();
200}
201
202/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700203 * create_image - freeze devices that need to be frozen with interrupts
204 * off, create the hibernation image and thaw those devices. Control
205 * reappears in this routine after a restore.
206 */
207
Adrian Bunk47a460d2008-02-04 22:30:06 -0800208static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700209{
210 int error;
211
212 error = arch_prepare_suspend();
213 if (error)
214 return error;
215
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200216 device_pm_lock();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100217
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700218 /* At this point, device_suspend() has been called, but *not*
219 * device_power_down(). We *must* call device_power_down() now.
220 * Otherwise, drivers for some devices (e.g. interrupt controllers)
221 * become desynchronized with the actual state of the hardware
222 * at resume time, and evil weirdness ensues.
223 */
224 error = device_power_down(PMSG_FREEZE);
225 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100226 printk(KERN_ERR "PM: Some devices failed to power down, "
227 "aborting hibernation\n");
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100228 goto Unlock;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700229 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100230
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100231 error = platform_pre_snapshot(platform_mode);
232 if (error || hibernation_test(TEST_PLATFORM))
233 goto Platform_finish;
234
235 error = disable_nonboot_cpus();
236 if (error || hibernation_test(TEST_CPUS)
237 || hibernation_testmode(HIBERNATION_TEST))
238 goto Enable_cpus;
239
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100240 local_irq_disable();
241
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100242 sysdev_suspend(PMSG_FREEZE);
243 if (error) {
244 printk(KERN_ERR "PM: Some devices failed to power down, "
245 "aborting hibernation\n");
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100246 goto Enable_irqs;
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100247 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700248
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100249 if (hibernation_test(TEST_CORE))
250 goto Power_up;
251
252 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700253 save_processor_state();
254 error = swsusp_arch_suspend();
255 if (error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100256 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
257 error);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700258 /* Restore control flow magically appears here */
259 restore_processor_state();
260 if (!in_suspend)
261 platform_leave(platform_mode);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100262
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100263 Power_up:
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100264 sysdev_resume();
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700265 /* NOTE: device_power_up() is just a resume() for devices
266 * that suspended with irqs off ... no overall powerup.
267 */
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100268
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100269 Enable_irqs:
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100270 local_irq_enable();
271
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100272 Enable_cpus:
273 enable_nonboot_cpus();
274
275 Platform_finish:
276 platform_finish(platform_mode);
277
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200278 device_power_up(in_suspend ?
279 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100280
281 Unlock:
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200282 device_pm_unlock();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100283
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700284 return error;
285}
286
287/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700288 * hibernation_snapshot - quiesce devices and create the hibernation
289 * snapshot image.
290 * @platform_mode - if set, use the platform driver, if available, to
291 * prepare the platform frimware for the power transition.
292 *
293 * Must be called with pm_mutex held
294 */
295
296int hibernation_snapshot(int platform_mode)
297{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100298 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700299
Zhang Rui3fe03132008-10-26 20:50:26 +0100300 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700301 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700302 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700303
Zhang Rui3fe03132008-10-26 20:50:26 +0100304 /* Free memory before shutting down devices. */
305 error = swsusp_shrink_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700306 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100307 goto Close;
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700308
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700309 suspend_console();
310 error = device_suspend(PMSG_FREEZE);
311 if (error)
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200312 goto Recover_platform;
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700313
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100314 if (hibernation_test(TEST_DEVICES))
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200315 goto Recover_platform;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700316
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100317 error = create_image(platform_mode);
318 /* Control returns here after successful restore */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100319
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100320 Resume_devices:
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200321 device_resume(in_suspend ?
322 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700323 resume_console();
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100324 Close:
325 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700326 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200327
328 Recover_platform:
329 platform_recover(platform_mode);
330 goto Resume_devices;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700331}
332
333/**
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100334 * resume_target_kernel - prepare devices that need to be suspended with
335 * interrupts off, restore the contents of highmem that have not been
336 * restored yet from the image and run the low level code that will restore
337 * the remaining contents of memory and switch to the just restored target
338 * kernel.
339 */
340
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100341static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100342{
343 int error;
344
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200345 device_pm_lock();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100346
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200347 error = device_power_down(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100348 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100349 printk(KERN_ERR "PM: Some devices failed to power down, "
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100350 "aborting resume\n");
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100351 goto Unlock;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100352 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100353
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100354 error = platform_pre_restore(platform_mode);
355 if (error)
356 goto Cleanup;
357
358 error = disable_nonboot_cpus();
359 if (error)
360 goto Enable_cpus;
361
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100362 local_irq_disable();
363
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100364 error = sysdev_suspend(PMSG_QUIESCE);
365 if (error)
366 goto Enable_irqs;
367
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100368 /* We'll ignore saved state, but this gets preempt count (etc) right */
369 save_processor_state();
370 error = restore_highmem();
371 if (!error) {
372 error = swsusp_arch_resume();
373 /*
374 * The code below is only ever reached in case of a failure.
375 * Otherwise execution continues at place where
376 * swsusp_arch_suspend() was called
377 */
378 BUG_ON(!error);
379 /* This call to restore_highmem() undos the previous one */
380 restore_highmem();
381 }
382 /*
383 * The only reason why swsusp_arch_resume() can fail is memory being
384 * very tight, so we have to free it as soon as we can to avoid
385 * subsequent failures
386 */
387 swsusp_free();
388 restore_processor_state();
389 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100390
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100391 sysdev_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100392
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100393 Enable_irqs:
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100394 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100395
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100396 Enable_cpus:
397 enable_nonboot_cpus();
398
399 Cleanup:
400 platform_restore_cleanup(platform_mode);
401
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100402 device_power_up(PMSG_RECOVER);
403
404 Unlock:
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200405 device_pm_unlock();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100406
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100407 return error;
408}
409
410/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700411 * hibernation_restore - quiesce devices and restore the hibernation
412 * snapshot image. If successful, control returns in hibernation_snaphot()
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700413 * @platform_mode - if set, use the platform driver, if available, to
414 * prepare the platform frimware for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700415 *
416 * Must be called with pm_mutex held
417 */
418
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700419int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700420{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100421 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700422
423 pm_prepare_console();
424 suspend_console();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200425 error = device_suspend(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700426 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100427 error = resume_target_kernel(platform_mode);
428 device_resume(PMSG_RECOVER);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700429 }
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700430 resume_console();
431 pm_restore_console();
432 return error;
433}
434
435/**
436 * hibernation_platform_enter - enter the hibernation state using the
437 * platform driver (if available)
438 */
439
440int hibernation_platform_enter(void)
441{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100442 int error;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700443
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700444 if (!hibernation_ops)
445 return -ENOSYS;
446
447 /*
448 * We have cancelled the power transition by running
449 * hibernation_ops->finish() before saving the image, so we should let
450 * the firmware know that we're going to enter the sleep state after all
451 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100452 error = hibernation_ops->begin();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700453 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100454 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700455
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100456 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700457 suspend_console();
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100458 error = device_suspend(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200459 if (error) {
460 if (hibernation_ops->recover)
461 hibernation_ops->recover();
462 goto Resume_devices;
463 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700464
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200465 device_pm_lock();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100466
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100467 error = device_power_down(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100468 if (error)
469 goto Unlock;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100470
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100471 error = hibernation_ops->prepare();
472 if (error)
473 goto Platofrm_finish;
474
475 error = disable_nonboot_cpus();
476 if (error)
477 goto Platofrm_finish;
478
479 local_irq_disable();
480 sysdev_suspend(PMSG_HIBERNATE);
481 hibernation_ops->enter();
482 /* We should never get here */
483 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700484
485 /*
486 * We don't need to reenable the nonboot CPUs or resume consoles, since
487 * the system is going to be halted anyway.
488 */
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100489 Platofrm_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700490 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100491
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100492 device_power_up(PMSG_RESTORE);
493
494 Unlock:
495 device_pm_unlock();
496
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700497 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100498 entering_platform_hibernation = false;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200499 device_resume(PMSG_RESTORE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700500 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100501
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100502 Close:
503 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100504
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700505 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700506}
507
508/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700509 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 *
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700511 * Use the platform driver, if configured so; otherwise try
512 * to power off or reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 */
514
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700515static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700517 switch (hibernation_mode) {
518 case HIBERNATION_TEST:
519 case HIBERNATION_TESTPROC:
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700520 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700521 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600522 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700524 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700525 hibernation_platform_enter();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700526 case HIBERNATION_SHUTDOWN:
527 kernel_power_off();
528 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600530 kernel_halt();
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700531 /*
532 * Valid image is on the disk, if we continue we risk serious data
533 * corruption after resume.
534 */
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100535 printk(KERN_CRIT "PM: Please power down manually\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 while(1);
537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539static int prepare_processes(void)
540{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800541 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (freeze_processes()) {
544 error = -EBUSY;
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100545 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700547 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700551 * hibernate - The granpappy of the built-in hibernation management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 */
553
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700554int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 int error;
557
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700558 mutex_lock(&pm_mutex);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700559 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700560 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
561 error = -EBUSY;
562 goto Unlock;
563 }
564
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100565 pm_prepare_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700566 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
567 if (error)
568 goto Exit;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700569
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700570 error = usermodehelper_disable();
571 if (error)
572 goto Exit;
573
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700574 /* Allocate memory management structures */
575 error = create_basic_memory_bitmaps();
576 if (error)
577 goto Exit;
578
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100579 printk(KERN_INFO "PM: Syncing filesystems ... ");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700580 sys_sync();
581 printk("done.\n");
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700584 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700585 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100587 if (hibernation_test(TEST_FREEZER))
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800588 goto Thaw;
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100589
590 if (hibernation_testmode(HIBERNATION_TESTPROC))
591 goto Thaw;
592
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700593 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
594 if (in_suspend && !error) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700595 unsigned int flags = 0;
596
597 if (hibernation_mode == HIBERNATION_PLATFORM)
598 flags |= SF_PLATFORM_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700600 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700601 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (!error)
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700603 power_down();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800604 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700606 swsusp_free();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800607 }
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800608 Thaw:
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100609 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700610 Finish:
611 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700612 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700613 Exit:
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700614 pm_notifier_call_chain(PM_POST_HIBERNATION);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100615 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700616 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700617 Unlock:
618 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return error;
620}
621
622
623/**
624 * software_resume - Resume from a saved image.
625 *
626 * Called as a late_initcall (so all devices are discovered and
627 * initialized), we call swsusp to see if we have a saved image or not.
628 * If so, we quiesce devices, the restore the saved image. We will
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700629 * return above (in hibernate() ) if everything goes well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 * Otherwise, we fail gracefully and return to the normally
631 * scheduled program.
632 *
633 */
634
635static int software_resume(void)
636{
637 int error;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700638 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Johannes Berg60a0d232007-11-14 17:00:16 -0800640 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100641 * If the user said "noresume".. bail out early.
642 */
643 if (noresume)
644 return 0;
645
646 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800647 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
648 * is configured into the kernel. Since the regular hibernate
649 * trigger path is via sysfs which takes a buffer mutex before
650 * calling hibernate functions (which take pm_mutex) this can
651 * cause lockdep to complain about a possible ABBA deadlock
652 * which cannot happen since we're in the boot code here and
653 * sysfs can't be invoked yet. Therefore, we use a subclass
654 * here to avoid lockdep complaining.
655 */
656 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
Pavel Machek3efa1472005-07-07 17:56:43 -0700657 if (!swsusp_resume_device) {
Shaohua Lidd5d6662005-09-03 15:57:04 -0700658 if (!strlen(resume_file)) {
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800659 mutex_unlock(&pm_mutex);
Pavel Machek3efa1472005-07-07 17:56:43 -0700660 return -ENOENT;
Shaohua Lidd5d6662005-09-03 15:57:04 -0700661 }
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100662 /*
663 * Some device discovery might still be in progress; we need
664 * to wait for this to finish.
665 */
666 wait_for_device_probe();
Pavel Machek3efa1472005-07-07 17:56:43 -0700667 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100668 pr_debug("PM: Resume from partition %s\n", resume_file);
Pavel Machek3efa1472005-07-07 17:56:43 -0700669 } else {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100670 pr_debug("PM: Resume from partition %d:%d\n",
671 MAJOR(swsusp_resume_device),
672 MINOR(swsusp_resume_device));
Pavel Machek3efa1472005-07-07 17:56:43 -0700673 }
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (noresume) {
676 /**
Rafael J. Wysocki95758092007-12-08 02:06:57 +0100677 * FIXME: If noresume is specified, we need to find the
678 * partition and reset it back to normal swap space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 */
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800680 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return 0;
682 }
683
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100684 pr_debug("PM: Checking hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800685 error = swsusp_check();
686 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700687 goto Unlock;
688
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700689 /* The snapshot device should not be opened while we're running */
690 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
691 error = -EBUSY;
692 goto Unlock;
693 }
694
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100695 pm_prepare_console();
Alan Sternc3e94d82007-11-19 23:38:25 +0100696 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
697 if (error)
698 goto Finish;
699
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700700 error = usermodehelper_disable();
701 if (error)
702 goto Finish;
703
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700704 error = create_basic_memory_bitmaps();
705 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700706 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 pr_debug("PM: Preparing processes for restore.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800709 error = prepare_processes();
710 if (error) {
Al Viroc2dd0da2007-10-08 13:21:10 -0400711 swsusp_close(FMODE_READ);
Li Shaohua5a72e042005-06-25 14:55:06 -0700712 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100715 pr_debug("PM: Reading hibernation image.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700717 error = swsusp_read(&flags);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800718 if (!error)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700719 hibernation_restore(flags & SF_PLATFORM_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800721 printk(KERN_ERR "PM: Restore failed, recovering.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700722 swsusp_free();
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100723 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 Done:
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700725 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700726 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700727 Finish:
Alan Sternc3e94d82007-11-19 23:38:25 +0100728 pm_notifier_call_chain(PM_POST_RESTORE);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100729 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700730 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700731 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700732 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800733 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 pr_debug("PM: Resume from disk failed.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700735 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738late_initcall(software_resume);
739
740
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700741static const char * const hibernation_modes[] = {
742 [HIBERNATION_PLATFORM] = "platform",
743 [HIBERNATION_SHUTDOWN] = "shutdown",
744 [HIBERNATION_REBOOT] = "reboot",
745 [HIBERNATION_TEST] = "test",
746 [HIBERNATION_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747};
748
749/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700750 * disk - Control hibernation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700752 * Suspend-to-disk can be handled in several ways. We have a few options
753 * for putting the system to sleep - using the platform driver (e.g. ACPI
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700754 * or other hibernation_ops), powering off the system or rebooting the
755 * system (for testing) as well as the two test modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700757 * The system can support 'platform', and that is known a priori (and
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700758 * encoded by the presence of hibernation_ops). However, the user may
759 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
760 * test modes, 'test' or 'testproc'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 *
762 * show() will display what the mode is currently set to.
763 * store() will accept one of
764 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 * 'platform'
766 * 'shutdown'
767 * 'reboot'
Johannes Berg11d77d02007-04-30 15:09:53 -0700768 * 'test'
769 * 'testproc'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700771 * It will only change to 'platform' if the system
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700772 * supports it (as determined by having hibernation_ops).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 */
774
Kay Sievers386f2752007-11-02 13:47:53 +0100775static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
776 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700778 int i;
779 char *start = buf;
780
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700781 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
782 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700783 continue;
784 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700785 case HIBERNATION_SHUTDOWN:
786 case HIBERNATION_REBOOT:
787 case HIBERNATION_TEST:
788 case HIBERNATION_TESTPROC:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700789 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700790 case HIBERNATION_PLATFORM:
791 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700792 break;
793 /* not a valid mode, continue with loop */
794 continue;
795 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700796 if (i == hibernation_mode)
797 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700798 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700799 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700800 }
801 buf += sprintf(buf, "\n");
802 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
805
Kay Sievers386f2752007-11-02 13:47:53 +0100806static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
807 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 int error = 0;
810 int i;
811 int len;
812 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700813 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 p = memchr(buf, '\n', n);
816 len = p ? p - buf : n;
817
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800818 mutex_lock(&pm_mutex);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700819 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700820 if (len == strlen(hibernation_modes[i])
821 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 mode = i;
823 break;
824 }
825 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700826 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700827 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700828 case HIBERNATION_SHUTDOWN:
829 case HIBERNATION_REBOOT:
830 case HIBERNATION_TEST:
831 case HIBERNATION_TESTPROC:
832 hibernation_mode = mode;
Johannes Bergfe0c935a2007-04-30 15:09:51 -0700833 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700834 case HIBERNATION_PLATFORM:
835 if (hibernation_ops)
836 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 else
838 error = -EINVAL;
839 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700840 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 error = -EINVAL;
842
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700843 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100844 pr_debug("PM: Hibernation mode set to '%s'\n",
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700845 hibernation_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800846 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return error ? error : n;
848}
849
850power_attr(disk);
851
Kay Sievers386f2752007-11-02 13:47:53 +0100852static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
853 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
856 MINOR(swsusp_resume_device));
857}
858
Kay Sievers386f2752007-11-02 13:47:53 +0100859static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
860 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800864 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Andrew Mortona5762192006-01-06 00:09:50 -0800866 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
867 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Andrew Mortona5762192006-01-06 00:09:50 -0800869 res = MKDEV(maj,min);
870 if (maj != MAJOR(res) || min != MINOR(res))
871 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800873 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800874 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800875 mutex_unlock(&pm_mutex);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100876 printk(KERN_INFO "PM: Starting manual resume from disk\n");
Andrew Mortona5762192006-01-06 00:09:50 -0800877 noresume = 0;
878 software_resume();
879 ret = n;
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800880 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800881 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
884power_attr(resume);
885
Kay Sievers386f2752007-11-02 13:47:53 +0100886static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
887 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800888{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800889 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800890}
891
Kay Sievers386f2752007-11-02 13:47:53 +0100892static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
893 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800894{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800895 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800896
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800897 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800898 image_size = size;
899 return n;
900 }
901
902 return -EINVAL;
903}
904
905power_attr(image_size);
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907static struct attribute * g[] = {
908 &disk_attr.attr,
909 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800910 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 NULL,
912};
913
914
915static struct attribute_group attr_group = {
916 .attrs = g,
917};
918
919
920static int __init pm_disk_init(void)
921{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800922 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
925core_initcall(pm_disk_init);
926
927
928static int __init resume_setup(char *str)
929{
930 if (noresume)
931 return 1;
932
933 strncpy( resume_file, str, 255 );
934 return 1;
935}
936
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800937static int __init resume_offset_setup(char *str)
938{
939 unsigned long long offset;
940
941 if (noresume)
942 return 1;
943
944 if (sscanf(str, "%llu", &offset) == 1)
945 swsusp_resume_block = offset;
946
947 return 1;
948}
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950static int __init noresume_setup(char *str)
951{
952 noresume = 1;
953 return 1;
954}
955
956__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800957__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958__setup("resume=", resume_setup);