blob: bbd85c60f7419f5fca1cbf1d0b5d6e6aad37f625 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/power/disk.c - Suspend-to-disk support.
3 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 * Copyright (c) 2004 Pavel Machek <pavel@suse.cz>
7 *
8 * This file is released under the GPLv2.
9 *
10 */
11
12#include <linux/suspend.h>
13#include <linux/syscalls.h>
14#include <linux/reboot.h>
15#include <linux/string.h>
16#include <linux/device.h>
17#include <linux/delay.h>
18#include <linux/fs.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070019#include <linux/mount.h>
Eric W. Biederman88d10bb2005-09-22 21:43:46 -070020#include <linux/pm.h>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070021#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070022#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080023#include <linux/freezer.h>
Rafael J. Wysocki41108eb2008-08-28 14:39:12 +020024#include <linux/ftrace.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. Wysocki4cc79772007-11-19 23:42:31 +010074#ifdef CONFIG_PM_DEBUG
75static void hibernation_debug_sleep(void)
76{
77 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
78 mdelay(5000);
79}
80
81static int hibernation_testmode(int mode)
82{
83 if (hibernation_mode == mode) {
84 hibernation_debug_sleep();
85 return 1;
86 }
87 return 0;
88}
89
90static int hibernation_test(int level)
91{
92 if (pm_test_level == level) {
93 hibernation_debug_sleep();
94 return 1;
95 }
96 return 0;
97}
98#else /* !CONFIG_PM_DEBUG */
99static int hibernation_testmode(int mode) { return 0; }
100static int hibernation_test(int level) { return 0; }
101#endif /* !CONFIG_PM_DEBUG */
102
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700103/**
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100104 * platform_begin - tell the platform driver that we're starting
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700105 * hibernation
106 */
107
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100108static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700109{
110 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100111 hibernation_ops->begin() : 0;
112}
113
114/**
115 * platform_end - tell the platform driver that we've entered the
116 * working state
117 */
118
119static void platform_end(int platform_mode)
120{
121 if (platform_mode && hibernation_ops)
122 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700123}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/**
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700126 * platform_pre_snapshot - prepare the machine for hibernation using the
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800127 * platform driver if so configured and return an error code if it fails
128 */
129
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700130static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800131{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700132 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700133 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800134}
135
136/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700137 * platform_leave - prepare the machine for switching to the normal mode
138 * of operation using the platform driver (called with interrupts disabled)
139 */
140
141static void platform_leave(int platform_mode)
142{
143 if (platform_mode && hibernation_ops)
144 hibernation_ops->leave();
145}
146
147/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700148 * platform_finish - switch the machine to the normal mode of operation
149 * using the platform driver (must be called after platform_prepare())
150 */
151
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700152static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700153{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700154 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700155 hibernation_ops->finish();
156}
157
158/**
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700159 * platform_pre_restore - prepare the platform for the restoration from a
160 * hibernation image. If the restore fails after this function has been
161 * called, platform_restore_cleanup() must be called.
162 */
163
164static int platform_pre_restore(int platform_mode)
165{
166 return (platform_mode && hibernation_ops) ?
167 hibernation_ops->pre_restore() : 0;
168}
169
170/**
171 * platform_restore_cleanup - switch the platform to the normal mode of
172 * operation after a failing restore. If platform_pre_restore() has been
173 * called before the failing restore, this function must be called too,
174 * regardless of the result of platform_pre_restore().
175 */
176
177static void platform_restore_cleanup(int platform_mode)
178{
179 if (platform_mode && hibernation_ops)
180 hibernation_ops->restore_cleanup();
181}
182
183/**
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200184 * platform_recover - recover the platform from a failure to suspend
185 * devices.
186 */
187
188static void platform_recover(int platform_mode)
189{
190 if (platform_mode && hibernation_ops && hibernation_ops->recover)
191 hibernation_ops->recover();
192}
193
194/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700195 * create_image - freeze devices that need to be frozen with interrupts
196 * off, create the hibernation image and thaw those devices. Control
197 * reappears in this routine after a restore.
198 */
199
Adrian Bunk47a460d2008-02-04 22:30:06 -0800200static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700201{
202 int error;
203
204 error = arch_prepare_suspend();
205 if (error)
206 return error;
207
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200208 device_pm_lock();
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700209 local_irq_disable();
210 /* At this point, device_suspend() has been called, but *not*
211 * device_power_down(). We *must* call device_power_down() now.
212 * Otherwise, drivers for some devices (e.g. interrupt controllers)
213 * become desynchronized with the actual state of the hardware
214 * at resume time, and evil weirdness ensues.
215 */
216 error = device_power_down(PMSG_FREEZE);
217 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100218 printk(KERN_ERR "PM: Some devices failed to power down, "
219 "aborting hibernation\n");
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700220 goto Enable_irqs;
221 }
222
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100223 if (hibernation_test(TEST_CORE))
224 goto Power_up;
225
226 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700227 save_processor_state();
228 error = swsusp_arch_suspend();
229 if (error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100230 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
231 error);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700232 /* Restore control flow magically appears here */
233 restore_processor_state();
234 if (!in_suspend)
235 platform_leave(platform_mode);
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100236 Power_up:
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700237 /* NOTE: device_power_up() is just a resume() for devices
238 * that suspended with irqs off ... no overall powerup.
239 */
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200240 device_power_up(in_suspend ?
241 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700242 Enable_irqs:
243 local_irq_enable();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200244 device_pm_unlock();
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700245 return error;
246}
247
248/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700249 * hibernation_snapshot - quiesce devices and create the hibernation
250 * snapshot image.
251 * @platform_mode - if set, use the platform driver, if available, to
252 * prepare the platform frimware for the power transition.
253 *
254 * Must be called with pm_mutex held
255 */
256
257int hibernation_snapshot(int platform_mode)
258{
Rafael J. Wysocki41108eb2008-08-28 14:39:12 +0200259 int error, ftrace_save;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700260
261 /* Free memory before shutting down devices. */
262 error = swsusp_shrink_memory();
263 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700264 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700265
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100266 error = platform_begin(platform_mode);
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700267 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100268 goto Close;
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700269
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700270 suspend_console();
Rafael J. Wysocki41108eb2008-08-28 14:39:12 +0200271 ftrace_save = __ftrace_enabled_save();
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700272 error = device_suspend(PMSG_FREEZE);
273 if (error)
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200274 goto Recover_platform;
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700275
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100276 if (hibernation_test(TEST_DEVICES))
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200277 goto Recover_platform;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700278
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100279 error = platform_pre_snapshot(platform_mode);
280 if (error || hibernation_test(TEST_PLATFORM))
281 goto Finish;
282
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700283 error = disable_nonboot_cpus();
284 if (!error) {
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100285 if (hibernation_test(TEST_CPUS))
286 goto Enable_cpus;
287
288 if (hibernation_testmode(HIBERNATION_TEST))
289 goto Enable_cpus;
290
291 error = create_image(platform_mode);
292 /* Control returns here after successful restore */
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700293 }
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100294 Enable_cpus:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700295 enable_nonboot_cpus();
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100296 Finish:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700297 platform_finish(platform_mode);
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100298 Resume_devices:
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200299 device_resume(in_suspend ?
300 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki41108eb2008-08-28 14:39:12 +0200301 __ftrace_enabled_restore(ftrace_save);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700302 resume_console();
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100303 Close:
304 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700305 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200306
307 Recover_platform:
308 platform_recover(platform_mode);
309 goto Resume_devices;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700310}
311
312/**
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100313 * resume_target_kernel - prepare devices that need to be suspended with
314 * interrupts off, restore the contents of highmem that have not been
315 * restored yet from the image and run the low level code that will restore
316 * the remaining contents of memory and switch to the just restored target
317 * kernel.
318 */
319
320static int resume_target_kernel(void)
321{
322 int error;
323
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200324 device_pm_lock();
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100325 local_irq_disable();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200326 error = device_power_down(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100327 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100328 printk(KERN_ERR "PM: Some devices failed to power down, "
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100329 "aborting resume\n");
330 goto Enable_irqs;
331 }
332 /* We'll ignore saved state, but this gets preempt count (etc) right */
333 save_processor_state();
334 error = restore_highmem();
335 if (!error) {
336 error = swsusp_arch_resume();
337 /*
338 * The code below is only ever reached in case of a failure.
339 * Otherwise execution continues at place where
340 * swsusp_arch_suspend() was called
341 */
342 BUG_ON(!error);
343 /* This call to restore_highmem() undos the previous one */
344 restore_highmem();
345 }
346 /*
347 * The only reason why swsusp_arch_resume() can fail is memory being
348 * very tight, so we have to free it as soon as we can to avoid
349 * subsequent failures
350 */
351 swsusp_free();
352 restore_processor_state();
353 touch_softlockup_watchdog();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200354 device_power_up(PMSG_RECOVER);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100355 Enable_irqs:
356 local_irq_enable();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200357 device_pm_unlock();
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100358 return error;
359}
360
361/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700362 * hibernation_restore - quiesce devices and restore the hibernation
363 * snapshot image. If successful, control returns in hibernation_snaphot()
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700364 * @platform_mode - if set, use the platform driver, if available, to
365 * prepare the platform frimware for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700366 *
367 * Must be called with pm_mutex held
368 */
369
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700370int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700371{
Rafael J. Wysocki41108eb2008-08-28 14:39:12 +0200372 int error, ftrace_save;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700373
374 pm_prepare_console();
375 suspend_console();
Rafael J. Wysocki41108eb2008-08-28 14:39:12 +0200376 ftrace_save = __ftrace_enabled_save();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200377 error = device_suspend(PMSG_QUIESCE);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700378 if (error)
379 goto Finish;
380
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700381 error = platform_pre_restore(platform_mode);
382 if (!error) {
383 error = disable_nonboot_cpus();
384 if (!error)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100385 error = resume_target_kernel();
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700386 enable_nonboot_cpus();
387 }
388 platform_restore_cleanup(platform_mode);
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200389 device_resume(PMSG_RECOVER);
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700390 Finish:
Rafael J. Wysocki41108eb2008-08-28 14:39:12 +0200391 __ftrace_enabled_restore(ftrace_save);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700392 resume_console();
393 pm_restore_console();
394 return error;
395}
396
397/**
398 * hibernation_platform_enter - enter the hibernation state using the
399 * platform driver (if available)
400 */
401
402int hibernation_platform_enter(void)
403{
Rafael J. Wysocki41108eb2008-08-28 14:39:12 +0200404 int error, ftrace_save;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700405
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700406 if (!hibernation_ops)
407 return -ENOSYS;
408
409 /*
410 * We have cancelled the power transition by running
411 * hibernation_ops->finish() before saving the image, so we should let
412 * the firmware know that we're going to enter the sleep state after all
413 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100414 error = hibernation_ops->begin();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700415 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100416 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700417
418 suspend_console();
Rafael J. Wysocki41108eb2008-08-28 14:39:12 +0200419 ftrace_save = __ftrace_enabled_save();
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100420 error = device_suspend(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200421 if (error) {
422 if (hibernation_ops->recover)
423 hibernation_ops->recover();
424 goto Resume_devices;
425 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700426
427 error = hibernation_ops->prepare();
428 if (error)
429 goto Resume_devices;
430
431 error = disable_nonboot_cpus();
432 if (error)
433 goto Finish;
434
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200435 device_pm_lock();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700436 local_irq_disable();
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +0100437 error = device_power_down(PMSG_HIBERNATE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700438 if (!error) {
439 hibernation_ops->enter();
440 /* We should never get here */
441 while (1);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700442 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700443 local_irq_enable();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200444 device_pm_unlock();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700445
446 /*
447 * We don't need to reenable the nonboot CPUs or resume consoles, since
448 * the system is going to be halted anyway.
449 */
450 Finish:
451 hibernation_ops->finish();
452 Resume_devices:
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200453 device_resume(PMSG_RESTORE);
Rafael J. Wysocki41108eb2008-08-28 14:39:12 +0200454 __ftrace_enabled_restore(ftrace_save);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700455 resume_console();
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100456 Close:
457 hibernation_ops->end();
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700458 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700459}
460
461/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700462 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 *
Johannes Bergfe0c9352007-04-30 15:09:51 -0700464 * Use the platform driver, if configured so; otherwise try
465 * to power off or reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 */
467
Johannes Bergfe0c9352007-04-30 15:09:51 -0700468static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700470 switch (hibernation_mode) {
471 case HIBERNATION_TEST:
472 case HIBERNATION_TESTPROC:
Johannes Bergfe0c9352007-04-30 15:09:51 -0700473 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700474 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600475 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700477 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700478 hibernation_platform_enter();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700479 case HIBERNATION_SHUTDOWN:
480 kernel_power_off();
481 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600483 kernel_halt();
Johannes Bergfe0c9352007-04-30 15:09:51 -0700484 /*
485 * Valid image is on the disk, if we continue we risk serious data
486 * corruption after resume.
487 */
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100488 printk(KERN_CRIT "PM: Please power down manually\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 while(1);
490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492static int prepare_processes(void)
493{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800494 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (freeze_processes()) {
497 error = -EBUSY;
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100498 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700500 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700504 * hibernate - The granpappy of the built-in hibernation management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 */
506
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700507int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 int error;
510
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700511 mutex_lock(&pm_mutex);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700512 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700513 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
514 error = -EBUSY;
515 goto Unlock;
516 }
517
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100518 pm_prepare_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700519 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
520 if (error)
521 goto Exit;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700522
523 /* Allocate memory management structures */
524 error = create_basic_memory_bitmaps();
525 if (error)
526 goto Exit;
527
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100528 printk(KERN_INFO "PM: Syncing filesystems ... ");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700529 sys_sync();
530 printk("done.\n");
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700533 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700534 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100536 if (hibernation_test(TEST_FREEZER))
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800537 goto Thaw;
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100538
539 if (hibernation_testmode(HIBERNATION_TESTPROC))
540 goto Thaw;
541
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700542 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
543 if (in_suspend && !error) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700544 unsigned int flags = 0;
545
546 if (hibernation_mode == HIBERNATION_PLATFORM)
547 flags |= SF_PLATFORM_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700549 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700550 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!error)
Johannes Bergfe0c9352007-04-30 15:09:51 -0700552 power_down();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800553 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700555 swsusp_free();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800556 }
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800557 Thaw:
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100558 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700559 Finish:
560 free_basic_memory_bitmaps();
561 Exit:
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700562 pm_notifier_call_chain(PM_POST_HIBERNATION);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100563 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700564 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700565 Unlock:
566 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return error;
568}
569
570
571/**
572 * software_resume - Resume from a saved image.
573 *
574 * Called as a late_initcall (so all devices are discovered and
575 * initialized), we call swsusp to see if we have a saved image or not.
576 * If so, we quiesce devices, the restore the saved image. We will
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700577 * return above (in hibernate() ) if everything goes well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 * Otherwise, we fail gracefully and return to the normally
579 * scheduled program.
580 *
581 */
582
583static int software_resume(void)
584{
585 int error;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700586 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Johannes Berg60a0d232007-11-14 17:00:16 -0800588 /*
589 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
590 * is configured into the kernel. Since the regular hibernate
591 * trigger path is via sysfs which takes a buffer mutex before
592 * calling hibernate functions (which take pm_mutex) this can
593 * cause lockdep to complain about a possible ABBA deadlock
594 * which cannot happen since we're in the boot code here and
595 * sysfs can't be invoked yet. Therefore, we use a subclass
596 * here to avoid lockdep complaining.
597 */
598 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
Pavel Machek3efa1472005-07-07 17:56:43 -0700599 if (!swsusp_resume_device) {
Shaohua Lidd5d6662005-09-03 15:57:04 -0700600 if (!strlen(resume_file)) {
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800601 mutex_unlock(&pm_mutex);
Pavel Machek3efa1472005-07-07 17:56:43 -0700602 return -ENOENT;
Shaohua Lidd5d6662005-09-03 15:57:04 -0700603 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700604 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100605 pr_debug("PM: Resume from partition %s\n", resume_file);
Pavel Machek3efa1472005-07-07 17:56:43 -0700606 } else {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100607 pr_debug("PM: Resume from partition %d:%d\n",
608 MAJOR(swsusp_resume_device),
609 MINOR(swsusp_resume_device));
Pavel Machek3efa1472005-07-07 17:56:43 -0700610 }
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (noresume) {
613 /**
Rafael J. Wysocki95758092007-12-08 02:06:57 +0100614 * FIXME: If noresume is specified, we need to find the
615 * partition and reset it back to normal swap space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800617 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return 0;
619 }
620
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100621 pr_debug("PM: Checking hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800622 error = swsusp_check();
623 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700624 goto Unlock;
625
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700626 /* The snapshot device should not be opened while we're running */
627 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
628 error = -EBUSY;
629 goto Unlock;
630 }
631
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100632 pm_prepare_console();
Alan Sternc3e94d82007-11-19 23:38:25 +0100633 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
634 if (error)
635 goto Finish;
636
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700637 error = create_basic_memory_bitmaps();
638 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700639 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 pr_debug("PM: Preparing processes for restore.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800642 error = prepare_processes();
643 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 swsusp_close();
Li Shaohua5a72e042005-06-25 14:55:06 -0700645 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100648 pr_debug("PM: Reading hibernation image.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700650 error = swsusp_read(&flags);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800651 if (!error)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700652 hibernation_restore(flags & SF_PLATFORM_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800654 printk(KERN_ERR "PM: Restore failed, recovering.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700655 swsusp_free();
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100656 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 Done:
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700658 free_basic_memory_bitmaps();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700659 Finish:
Alan Sternc3e94d82007-11-19 23:38:25 +0100660 pm_notifier_call_chain(PM_POST_RESTORE);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100661 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700662 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700663 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700664 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800665 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 pr_debug("PM: Resume from disk failed.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700667 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670late_initcall(software_resume);
671
672
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700673static const char * const hibernation_modes[] = {
674 [HIBERNATION_PLATFORM] = "platform",
675 [HIBERNATION_SHUTDOWN] = "shutdown",
676 [HIBERNATION_REBOOT] = "reboot",
677 [HIBERNATION_TEST] = "test",
678 [HIBERNATION_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679};
680
681/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700682 * disk - Control hibernation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700684 * Suspend-to-disk can be handled in several ways. We have a few options
685 * for putting the system to sleep - using the platform driver (e.g. ACPI
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700686 * or other hibernation_ops), powering off the system or rebooting the
687 * system (for testing) as well as the two test modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700689 * The system can support 'platform', and that is known a priori (and
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700690 * encoded by the presence of hibernation_ops). However, the user may
691 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
692 * test modes, 'test' or 'testproc'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 *
694 * show() will display what the mode is currently set to.
695 * store() will accept one of
696 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 * 'platform'
698 * 'shutdown'
699 * 'reboot'
Johannes Berg11d77d02007-04-30 15:09:53 -0700700 * 'test'
701 * 'testproc'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700703 * It will only change to 'platform' if the system
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700704 * supports it (as determined by having hibernation_ops).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 */
706
Kay Sievers386f2752007-11-02 13:47:53 +0100707static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
708 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700710 int i;
711 char *start = buf;
712
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700713 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
714 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700715 continue;
716 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700717 case HIBERNATION_SHUTDOWN:
718 case HIBERNATION_REBOOT:
719 case HIBERNATION_TEST:
720 case HIBERNATION_TESTPROC:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700721 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700722 case HIBERNATION_PLATFORM:
723 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700724 break;
725 /* not a valid mode, continue with loop */
726 continue;
727 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700728 if (i == hibernation_mode)
729 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700730 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700731 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700732 }
733 buf += sprintf(buf, "\n");
734 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
737
Kay Sievers386f2752007-11-02 13:47:53 +0100738static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
739 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 int error = 0;
742 int i;
743 int len;
744 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700745 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 p = memchr(buf, '\n', n);
748 len = p ? p - buf : n;
749
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800750 mutex_lock(&pm_mutex);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700751 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700752 if (len == strlen(hibernation_modes[i])
753 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 mode = i;
755 break;
756 }
757 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700758 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c9352007-04-30 15:09:51 -0700759 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700760 case HIBERNATION_SHUTDOWN:
761 case HIBERNATION_REBOOT:
762 case HIBERNATION_TEST:
763 case HIBERNATION_TESTPROC:
764 hibernation_mode = mode;
Johannes Bergfe0c9352007-04-30 15:09:51 -0700765 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700766 case HIBERNATION_PLATFORM:
767 if (hibernation_ops)
768 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 else
770 error = -EINVAL;
771 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700772 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 error = -EINVAL;
774
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700775 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100776 pr_debug("PM: Hibernation mode set to '%s'\n",
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700777 hibernation_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800778 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return error ? error : n;
780}
781
782power_attr(disk);
783
Kay Sievers386f2752007-11-02 13:47:53 +0100784static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
785 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
788 MINOR(swsusp_resume_device));
789}
790
Kay Sievers386f2752007-11-02 13:47:53 +0100791static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
792 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800796 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Andrew Mortona5762192006-01-06 00:09:50 -0800798 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
799 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Andrew Mortona5762192006-01-06 00:09:50 -0800801 res = MKDEV(maj,min);
802 if (maj != MAJOR(res) || min != MINOR(res))
803 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800805 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800806 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800807 mutex_unlock(&pm_mutex);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100808 printk(KERN_INFO "PM: Starting manual resume from disk\n");
Andrew Mortona5762192006-01-06 00:09:50 -0800809 noresume = 0;
810 software_resume();
811 ret = n;
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800812 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800813 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
816power_attr(resume);
817
Kay Sievers386f2752007-11-02 13:47:53 +0100818static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
819 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800820{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800821 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800822}
823
Kay Sievers386f2752007-11-02 13:47:53 +0100824static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
825 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800826{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800827 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800828
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800829 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800830 image_size = size;
831 return n;
832 }
833
834 return -EINVAL;
835}
836
837power_attr(image_size);
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839static struct attribute * g[] = {
840 &disk_attr.attr,
841 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800842 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 NULL,
844};
845
846
847static struct attribute_group attr_group = {
848 .attrs = g,
849};
850
851
852static int __init pm_disk_init(void)
853{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800854 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
857core_initcall(pm_disk_init);
858
859
860static int __init resume_setup(char *str)
861{
862 if (noresume)
863 return 1;
864
865 strncpy( resume_file, str, 255 );
866 return 1;
867}
868
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800869static int __init resume_offset_setup(char *str)
870{
871 unsigned long long offset;
872
873 if (noresume)
874 return 1;
875
876 if (sscanf(str, "%llu", &offset) == 1)
877 swsusp_resume_block = offset;
878
879 return 1;
880}
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882static int __init noresume_setup(char *str)
883{
884 noresume = 1;
885 return 1;
886}
887
888__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800889__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890__setup("resume=", resume_setup);