blob: 432ee575c9ee4fd201481a9ebdb674c29b96dab8 [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. Wysockic7e08312007-10-18 03:04:55 -0700217 local_irq_disable();
218 /* 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. Wysockic7e08312007-10-18 03:04:55 -0700228 goto Enable_irqs;
229 }
230
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100231 if (hibernation_test(TEST_CORE))
232 goto Power_up;
233
234 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700235 save_processor_state();
236 error = swsusp_arch_suspend();
237 if (error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100238 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
239 error);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700240 /* Restore control flow magically appears here */
241 restore_processor_state();
242 if (!in_suspend)
243 platform_leave(platform_mode);
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100244 Power_up:
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700245 /* NOTE: device_power_up() is just a resume() for devices
246 * that suspended with irqs off ... no overall powerup.
247 */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200248 device_power_up(in_suspend ?
249 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700250 Enable_irqs:
251 local_irq_enable();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200252 device_pm_unlock();
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700253 return error;
254}
255
256/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700257 * hibernation_snapshot - quiesce devices and create the hibernation
258 * snapshot image.
259 * @platform_mode - if set, use the platform driver, if available, to
260 * prepare the platform frimware for the power transition.
261 *
262 * Must be called with pm_mutex held
263 */
264
265int hibernation_snapshot(int platform_mode)
266{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100267 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700268
Zhang Rui3fe03132008-10-26 20:50:26 +0100269 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700270 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700271 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700272
Zhang Rui3fe03132008-10-26 20:50:26 +0100273 /* Free memory before shutting down devices. */
274 error = swsusp_shrink_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700275 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100276 goto Close;
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700277
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700278 suspend_console();
279 error = device_suspend(PMSG_FREEZE);
280 if (error)
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200281 goto Recover_platform;
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700282
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100283 if (hibernation_test(TEST_DEVICES))
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200284 goto Recover_platform;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700285
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100286 error = platform_pre_snapshot(platform_mode);
287 if (error || hibernation_test(TEST_PLATFORM))
288 goto Finish;
289
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700290 error = disable_nonboot_cpus();
291 if (!error) {
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100292 if (hibernation_test(TEST_CPUS))
293 goto Enable_cpus;
294
295 if (hibernation_testmode(HIBERNATION_TEST))
296 goto Enable_cpus;
297
298 error = create_image(platform_mode);
299 /* Control returns here after successful restore */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700300 }
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100301 Enable_cpus:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700302 enable_nonboot_cpus();
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100303 Finish:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700304 platform_finish(platform_mode);
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100305 Resume_devices:
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200306 device_resume(in_suspend ?
307 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700308 resume_console();
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100309 Close:
310 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700311 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200312
313 Recover_platform:
314 platform_recover(platform_mode);
315 goto Resume_devices;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700316}
317
318/**
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100319 * resume_target_kernel - prepare devices that need to be suspended with
320 * interrupts off, restore the contents of highmem that have not been
321 * restored yet from the image and run the low level code that will restore
322 * the remaining contents of memory and switch to the just restored target
323 * kernel.
324 */
325
326static int resume_target_kernel(void)
327{
328 int error;
329
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200330 device_pm_lock();
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100331 local_irq_disable();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200332 error = device_power_down(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100333 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100334 printk(KERN_ERR "PM: Some devices failed to power down, "
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100335 "aborting resume\n");
336 goto Enable_irqs;
337 }
338 /* We'll ignore saved state, but this gets preempt count (etc) right */
339 save_processor_state();
340 error = restore_highmem();
341 if (!error) {
342 error = swsusp_arch_resume();
343 /*
344 * The code below is only ever reached in case of a failure.
345 * Otherwise execution continues at place where
346 * swsusp_arch_suspend() was called
347 */
348 BUG_ON(!error);
349 /* This call to restore_highmem() undos the previous one */
350 restore_highmem();
351 }
352 /*
353 * The only reason why swsusp_arch_resume() can fail is memory being
354 * very tight, so we have to free it as soon as we can to avoid
355 * subsequent failures
356 */
357 swsusp_free();
358 restore_processor_state();
359 touch_softlockup_watchdog();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200360 device_power_up(PMSG_RECOVER);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100361 Enable_irqs:
362 local_irq_enable();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200363 device_pm_unlock();
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100364 return error;
365}
366
367/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700368 * hibernation_restore - quiesce devices and restore the hibernation
369 * snapshot image. If successful, control returns in hibernation_snaphot()
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700370 * @platform_mode - if set, use the platform driver, if available, to
371 * prepare the platform frimware for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700372 *
373 * Must be called with pm_mutex held
374 */
375
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700376int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700377{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100378 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700379
380 pm_prepare_console();
381 suspend_console();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200382 error = device_suspend(PMSG_QUIESCE);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700383 if (error)
384 goto Finish;
385
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700386 error = platform_pre_restore(platform_mode);
387 if (!error) {
388 error = disable_nonboot_cpus();
389 if (!error)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100390 error = resume_target_kernel();
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700391 enable_nonboot_cpus();
392 }
393 platform_restore_cleanup(platform_mode);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200394 device_resume(PMSG_RECOVER);
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700395 Finish:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700396 resume_console();
397 pm_restore_console();
398 return error;
399}
400
401/**
402 * hibernation_platform_enter - enter the hibernation state using the
403 * platform driver (if available)
404 */
405
406int hibernation_platform_enter(void)
407{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100408 int error;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700409
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700410 if (!hibernation_ops)
411 return -ENOSYS;
412
413 /*
414 * We have cancelled the power transition by running
415 * hibernation_ops->finish() before saving the image, so we should let
416 * the firmware know that we're going to enter the sleep state after all
417 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100418 error = hibernation_ops->begin();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700419 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100420 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700421
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100422 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700423 suspend_console();
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100424 error = device_suspend(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200425 if (error) {
426 if (hibernation_ops->recover)
427 hibernation_ops->recover();
428 goto Resume_devices;
429 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700430
431 error = hibernation_ops->prepare();
432 if (error)
433 goto Resume_devices;
434
435 error = disable_nonboot_cpus();
436 if (error)
437 goto Finish;
438
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200439 device_pm_lock();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700440 local_irq_disable();
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100441 error = device_power_down(PMSG_HIBERNATE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700442 if (!error) {
443 hibernation_ops->enter();
444 /* We should never get here */
445 while (1);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700446 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700447 local_irq_enable();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200448 device_pm_unlock();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700449
450 /*
451 * We don't need to reenable the nonboot CPUs or resume consoles, since
452 * the system is going to be halted anyway.
453 */
454 Finish:
455 hibernation_ops->finish();
456 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100457 entering_platform_hibernation = false;
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200458 device_resume(PMSG_RESTORE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700459 resume_console();
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100460 Close:
461 hibernation_ops->end();
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700462 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700463}
464
465/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700466 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 *
Johannes Bergfe0c9352007-04-30 15:09:51 -0700468 * Use the platform driver, if configured so; otherwise try
469 * to power off or reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 */
471
Johannes Bergfe0c9352007-04-30 15:09:51 -0700472static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700474 switch (hibernation_mode) {
475 case HIBERNATION_TEST:
476 case HIBERNATION_TESTPROC:
Johannes Bergfe0c9352007-04-30 15:09:51 -0700477 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700478 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600479 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700481 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700482 hibernation_platform_enter();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700483 case HIBERNATION_SHUTDOWN:
484 kernel_power_off();
485 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600487 kernel_halt();
Johannes Bergfe0c9352007-04-30 15:09:51 -0700488 /*
489 * Valid image is on the disk, if we continue we risk serious data
490 * corruption after resume.
491 */
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100492 printk(KERN_CRIT "PM: Please power down manually\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 while(1);
494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496static int prepare_processes(void)
497{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800498 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (freeze_processes()) {
501 error = -EBUSY;
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100502 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700504 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700508 * hibernate - The granpappy of the built-in hibernation management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 */
510
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700511int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 int error;
514
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700515 mutex_lock(&pm_mutex);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700516 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700517 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
518 error = -EBUSY;
519 goto Unlock;
520 }
521
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100522 pm_prepare_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700523 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
524 if (error)
525 goto Exit;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700526
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700527 error = usermodehelper_disable();
528 if (error)
529 goto Exit;
530
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700531 /* Allocate memory management structures */
532 error = create_basic_memory_bitmaps();
533 if (error)
534 goto Exit;
535
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100536 printk(KERN_INFO "PM: Syncing filesystems ... ");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700537 sys_sync();
538 printk("done.\n");
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700541 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700542 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100544 if (hibernation_test(TEST_FREEZER))
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800545 goto Thaw;
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100546
547 if (hibernation_testmode(HIBERNATION_TESTPROC))
548 goto Thaw;
549
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700550 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
551 if (in_suspend && !error) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700552 unsigned int flags = 0;
553
554 if (hibernation_mode == HIBERNATION_PLATFORM)
555 flags |= SF_PLATFORM_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700557 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700558 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (!error)
Johannes Bergfe0c9352007-04-30 15:09:51 -0700560 power_down();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800561 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700563 swsusp_free();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800564 }
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800565 Thaw:
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100566 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700567 Finish:
568 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700569 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700570 Exit:
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700571 pm_notifier_call_chain(PM_POST_HIBERNATION);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100572 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700573 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700574 Unlock:
575 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return error;
577}
578
579
580/**
581 * software_resume - Resume from a saved image.
582 *
583 * Called as a late_initcall (so all devices are discovered and
584 * initialized), we call swsusp to see if we have a saved image or not.
585 * If so, we quiesce devices, the restore the saved image. We will
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700586 * return above (in hibernate() ) if everything goes well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 * Otherwise, we fail gracefully and return to the normally
588 * scheduled program.
589 *
590 */
591
592static int software_resume(void)
593{
594 int error;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700595 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Johannes Berg60a0d232007-11-14 17:00:16 -0800597 /*
598 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
599 * is configured into the kernel. Since the regular hibernate
600 * trigger path is via sysfs which takes a buffer mutex before
601 * calling hibernate functions (which take pm_mutex) this can
602 * cause lockdep to complain about a possible ABBA deadlock
603 * which cannot happen since we're in the boot code here and
604 * sysfs can't be invoked yet. Therefore, we use a subclass
605 * here to avoid lockdep complaining.
606 */
607 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
Pavel Machek3efa1472005-07-07 17:56:43 -0700608 if (!swsusp_resume_device) {
Shaohua Lidd5d6662005-09-03 15:57:04 -0700609 if (!strlen(resume_file)) {
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800610 mutex_unlock(&pm_mutex);
Pavel Machek3efa1472005-07-07 17:56:43 -0700611 return -ENOENT;
Shaohua Lidd5d6662005-09-03 15:57:04 -0700612 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700613 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100614 pr_debug("PM: Resume from partition %s\n", resume_file);
Pavel Machek3efa1472005-07-07 17:56:43 -0700615 } else {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100616 pr_debug("PM: Resume from partition %d:%d\n",
617 MAJOR(swsusp_resume_device),
618 MINOR(swsusp_resume_device));
Pavel Machek3efa1472005-07-07 17:56:43 -0700619 }
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (noresume) {
622 /**
Rafael J. Wysocki95758092007-12-08 02:06:57 +0100623 * FIXME: If noresume is specified, we need to find the
624 * partition and reset it back to normal swap space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 */
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800626 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return 0;
628 }
629
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100630 pr_debug("PM: Checking hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800631 error = swsusp_check();
632 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700633 goto Unlock;
634
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700635 /* The snapshot device should not be opened while we're running */
636 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
637 error = -EBUSY;
638 goto Unlock;
639 }
640
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100641 pm_prepare_console();
Alan Sternc3e94d82007-11-19 23:38:25 +0100642 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
643 if (error)
644 goto Finish;
645
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700646 error = usermodehelper_disable();
647 if (error)
648 goto Finish;
649
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700650 error = create_basic_memory_bitmaps();
651 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700652 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 pr_debug("PM: Preparing processes for restore.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800655 error = prepare_processes();
656 if (error) {
Al Viroc2dd0da2007-10-08 13:21:10 -0400657 swsusp_close(FMODE_READ);
Li Shaohua5a72e042005-06-25 14:55:06 -0700658 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100661 pr_debug("PM: Reading hibernation image.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700663 error = swsusp_read(&flags);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800664 if (!error)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700665 hibernation_restore(flags & SF_PLATFORM_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800667 printk(KERN_ERR "PM: Restore failed, recovering.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700668 swsusp_free();
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100669 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 Done:
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700671 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700672 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700673 Finish:
Alan Sternc3e94d82007-11-19 23:38:25 +0100674 pm_notifier_call_chain(PM_POST_RESTORE);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100675 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700676 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700677 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700678 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800679 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 pr_debug("PM: Resume from disk failed.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700681 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684late_initcall(software_resume);
685
686
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700687static const char * const hibernation_modes[] = {
688 [HIBERNATION_PLATFORM] = "platform",
689 [HIBERNATION_SHUTDOWN] = "shutdown",
690 [HIBERNATION_REBOOT] = "reboot",
691 [HIBERNATION_TEST] = "test",
692 [HIBERNATION_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693};
694
695/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700696 * disk - Control hibernation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700698 * Suspend-to-disk can be handled in several ways. We have a few options
699 * for putting the system to sleep - using the platform driver (e.g. ACPI
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700700 * or other hibernation_ops), powering off the system or rebooting the
701 * system (for testing) as well as the two test modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700703 * The system can support 'platform', and that is known a priori (and
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700704 * encoded by the presence of hibernation_ops). However, the user may
705 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
706 * test modes, 'test' or 'testproc'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 *
708 * show() will display what the mode is currently set to.
709 * store() will accept one of
710 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 * 'platform'
712 * 'shutdown'
713 * 'reboot'
Johannes Berg11d77d02007-04-30 15:09:53 -0700714 * 'test'
715 * 'testproc'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700717 * It will only change to 'platform' if the system
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700718 * supports it (as determined by having hibernation_ops).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 */
720
Kay Sievers386f2752007-11-02 13:47:53 +0100721static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
722 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700724 int i;
725 char *start = buf;
726
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700727 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
728 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700729 continue;
730 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700731 case HIBERNATION_SHUTDOWN:
732 case HIBERNATION_REBOOT:
733 case HIBERNATION_TEST:
734 case HIBERNATION_TESTPROC:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700735 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700736 case HIBERNATION_PLATFORM:
737 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700738 break;
739 /* not a valid mode, continue with loop */
740 continue;
741 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700742 if (i == hibernation_mode)
743 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700744 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700745 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700746 }
747 buf += sprintf(buf, "\n");
748 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751
Kay Sievers386f2752007-11-02 13:47:53 +0100752static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
753 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 int error = 0;
756 int i;
757 int len;
758 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700759 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 p = memchr(buf, '\n', n);
762 len = p ? p - buf : n;
763
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800764 mutex_lock(&pm_mutex);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700765 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700766 if (len == strlen(hibernation_modes[i])
767 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 mode = i;
769 break;
770 }
771 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700772 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c9352007-04-30 15:09:51 -0700773 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700774 case HIBERNATION_SHUTDOWN:
775 case HIBERNATION_REBOOT:
776 case HIBERNATION_TEST:
777 case HIBERNATION_TESTPROC:
778 hibernation_mode = mode;
Johannes Bergfe0c9352007-04-30 15:09:51 -0700779 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700780 case HIBERNATION_PLATFORM:
781 if (hibernation_ops)
782 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 else
784 error = -EINVAL;
785 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700786 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 error = -EINVAL;
788
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700789 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100790 pr_debug("PM: Hibernation mode set to '%s'\n",
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700791 hibernation_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800792 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return error ? error : n;
794}
795
796power_attr(disk);
797
Kay Sievers386f2752007-11-02 13:47:53 +0100798static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
799 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
802 MINOR(swsusp_resume_device));
803}
804
Kay Sievers386f2752007-11-02 13:47:53 +0100805static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
806 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800810 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Andrew Mortona5762192006-01-06 00:09:50 -0800812 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
813 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Andrew Mortona5762192006-01-06 00:09:50 -0800815 res = MKDEV(maj,min);
816 if (maj != MAJOR(res) || min != MINOR(res))
817 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800819 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800820 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800821 mutex_unlock(&pm_mutex);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100822 printk(KERN_INFO "PM: Starting manual resume from disk\n");
Andrew Mortona5762192006-01-06 00:09:50 -0800823 noresume = 0;
824 software_resume();
825 ret = n;
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800826 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800827 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
830power_attr(resume);
831
Kay Sievers386f2752007-11-02 13:47:53 +0100832static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
833 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800834{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800835 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800836}
837
Kay Sievers386f2752007-11-02 13:47:53 +0100838static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
839 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800840{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800841 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800842
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800843 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800844 image_size = size;
845 return n;
846 }
847
848 return -EINVAL;
849}
850
851power_attr(image_size);
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853static struct attribute * g[] = {
854 &disk_attr.attr,
855 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800856 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 NULL,
858};
859
860
861static struct attribute_group attr_group = {
862 .attrs = g,
863};
864
865
866static int __init pm_disk_init(void)
867{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800868 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
871core_initcall(pm_disk_init);
872
873
874static int __init resume_setup(char *str)
875{
876 if (noresume)
877 return 1;
878
879 strncpy( resume_file, str, 255 );
880 return 1;
881}
882
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800883static int __init resume_offset_setup(char *str)
884{
885 unsigned long long offset;
886
887 if (noresume)
888 return 1;
889
890 if (sscanf(str, "%llu", &offset) == 1)
891 swsusp_resume_block = offset;
892
893 return 1;
894}
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896static int __init noresume_setup(char *str)
897{
898 noresume = 1;
899 return 1;
900}
901
902__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800903__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904__setup("resume=", resume_setup);