blob: 04b3a83d686f63e430723e2a3e26a1fc3d3e0601 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Rafael J. Wysocki8b759b82009-06-10 01:27:49 +02002 * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 * Copyright (c) 2004 Pavel Machek <pavel@suse.cz>
Rafael J. Wysocki8b759b82009-06-10 01:27:49 +02007 * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/suspend.h>
13#include <linux/syscalls.h>
14#include <linux/reboot.h>
15#include <linux/string.h>
16#include <linux/device.h>
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -070017#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/delay.h>
19#include <linux/fs.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070020#include <linux/mount.h>
Eric W. Biederman88d10bb2005-09-22 21:43:46 -070021#include <linux/pm.h>
Rafael J. Wysocki97c78012006-10-11 01:20:45 -070022#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070023#include <linux/cpu.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080024#include <linux/freezer.h>
Rafael J. Wysockic7510852009-04-12 20:06:56 +020025#include <scsi/scsi_scan.h>
Magnus Damma8af7892009-03-31 15:23:37 -070026#include <asm/suspend.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "power.h"
29
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int noresume = 0;
Adrian Bunk47a460d2008-02-04 22:30:06 -080032static char resume_file[256] = CONFIG_PM_STD_PARTITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080034sector_t swsusp_resume_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070036enum {
37 HIBERNATION_INVALID,
38 HIBERNATION_PLATFORM,
39 HIBERNATION_TEST,
40 HIBERNATION_TESTPROC,
41 HIBERNATION_SHUTDOWN,
42 HIBERNATION_REBOOT,
43 /* keep last */
44 __HIBERNATION_AFTER_LAST
45};
46#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
47#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
48
49static int hibernation_mode = HIBERNATION_SHUTDOWN;
50
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070051static struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070052
53/**
54 * hibernation_set_ops - set the global hibernate operations
55 * @ops: the hibernation operations to use in subsequent hibernation transitions
56 */
57
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070058void hibernation_set_ops(struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070059{
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +010060 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
61 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -070062 && ops->restore_cleanup)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070063 WARN_ON(1);
64 return;
65 }
66 mutex_lock(&pm_mutex);
67 hibernation_ops = ops;
68 if (ops)
69 hibernation_mode = HIBERNATION_PLATFORM;
70 else if (hibernation_mode == HIBERNATION_PLATFORM)
71 hibernation_mode = HIBERNATION_SHUTDOWN;
72
73 mutex_unlock(&pm_mutex);
74}
75
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +010076static bool entering_platform_hibernation;
77
78bool system_entering_hibernation(void)
79{
80 return entering_platform_hibernation;
81}
82EXPORT_SYMBOL(system_entering_hibernation);
83
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +010084#ifdef CONFIG_PM_DEBUG
85static void hibernation_debug_sleep(void)
86{
87 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
88 mdelay(5000);
89}
90
91static int hibernation_testmode(int mode)
92{
93 if (hibernation_mode == mode) {
94 hibernation_debug_sleep();
95 return 1;
96 }
97 return 0;
98}
99
100static int hibernation_test(int level)
101{
102 if (pm_test_level == level) {
103 hibernation_debug_sleep();
104 return 1;
105 }
106 return 0;
107}
108#else /* !CONFIG_PM_DEBUG */
109static int hibernation_testmode(int mode) { return 0; }
110static int hibernation_test(int level) { return 0; }
111#endif /* !CONFIG_PM_DEBUG */
112
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700113/**
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100114 * platform_begin - tell the platform driver that we're starting
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700115 * hibernation
116 */
117
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100118static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700119{
120 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100121 hibernation_ops->begin() : 0;
122}
123
124/**
125 * platform_end - tell the platform driver that we've entered the
126 * working state
127 */
128
129static void platform_end(int platform_mode)
130{
131 if (platform_mode && hibernation_ops)
132 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700133}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/**
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700136 * platform_pre_snapshot - prepare the machine for hibernation using the
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800137 * platform driver if so configured and return an error code if it fails
138 */
139
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700140static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800141{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700142 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700143 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800144}
145
146/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700147 * platform_leave - prepare the machine for switching to the normal mode
148 * of operation using the platform driver (called with interrupts disabled)
149 */
150
151static void platform_leave(int platform_mode)
152{
153 if (platform_mode && hibernation_ops)
154 hibernation_ops->leave();
155}
156
157/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700158 * platform_finish - switch the machine to the normal mode of operation
159 * using the platform driver (must be called after platform_prepare())
160 */
161
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700162static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700163{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700164 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700165 hibernation_ops->finish();
166}
167
168/**
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700169 * platform_pre_restore - prepare the platform for the restoration from a
170 * hibernation image. If the restore fails after this function has been
171 * called, platform_restore_cleanup() must be called.
172 */
173
174static int platform_pre_restore(int platform_mode)
175{
176 return (platform_mode && hibernation_ops) ?
177 hibernation_ops->pre_restore() : 0;
178}
179
180/**
181 * platform_restore_cleanup - switch the platform to the normal mode of
182 * operation after a failing restore. If platform_pre_restore() has been
183 * called before the failing restore, this function must be called too,
184 * regardless of the result of platform_pre_restore().
185 */
186
187static void platform_restore_cleanup(int platform_mode)
188{
189 if (platform_mode && hibernation_ops)
190 hibernation_ops->restore_cleanup();
191}
192
193/**
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200194 * platform_recover - recover the platform from a failure to suspend
195 * devices.
196 */
197
198static void platform_recover(int platform_mode)
199{
200 if (platform_mode && hibernation_ops && hibernation_ops->recover)
201 hibernation_ops->recover();
202}
203
204/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700205 * create_image - freeze devices that need to be frozen with interrupts
206 * off, create the hibernation image and thaw those devices. Control
207 * reappears in this routine after a restore.
208 */
209
Adrian Bunk47a460d2008-02-04 22:30:06 -0800210static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700211{
212 int error;
213
214 error = arch_prepare_suspend();
215 if (error)
216 return error;
217
Alan Sternd1616302009-05-24 22:05:42 +0200218 /* At this point, dpm_suspend_start() has been called, but *not*
219 * dpm_suspend_noirq(). We *must* call dpm_suspend_noirq() now.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700220 * 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 */
Alan Sternd1616302009-05-24 22:05:42 +0200224 error = dpm_suspend_noirq(PMSG_FREEZE);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700225 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. Wysocki32bdfac2009-05-24 21:15:07 +0200228 return error;
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
Bjorn Helgaas44840792009-05-15 23:30:50 +0200242 error = sysdev_suspend(PMSG_FREEZE);
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100243 if (error) {
Bjorn Helgaas44840792009-05-15 23:30:50 +0200244 printk(KERN_ERR "PM: Some system devices failed to power down, "
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100245 "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();
Alan Sternd1616302009-05-24 22:05:42 +0200265 /* NOTE: dpm_resume_noirq() is just a resume() for devices
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700266 * 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
Alan Sternd1616302009-05-24 22:05:42 +0200278 dpm_resume_noirq(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200279 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100280
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700281 return error;
282}
283
284/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700285 * hibernation_snapshot - quiesce devices and create the hibernation
286 * snapshot image.
287 * @platform_mode - if set, use the platform driver, if available, to
Nick Andrew877d0312009-01-26 11:06:57 +0100288 * prepare the platform firmware for the power transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700289 *
290 * Must be called with pm_mutex held
291 */
292
293int hibernation_snapshot(int platform_mode)
294{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100295 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700296
Zhang Rui3fe03132008-10-26 20:50:26 +0100297 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700298 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700299 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700300
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200301 /* Preallocate image memory before shutting down devices. */
302 error = hibernate_preallocate_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700303 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100304 goto Close;
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700305
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700306 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +0200307 error = dpm_suspend_start(PMSG_FREEZE);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700308 if (error)
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200309 goto Recover_platform;
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700310
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100311 if (hibernation_test(TEST_DEVICES))
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200312 goto Recover_platform;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700313
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100314 error = create_image(platform_mode);
315 /* Control returns here after successful restore */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100316
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100317 Resume_devices:
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200318 /* We may need to release the preallocated image pages here. */
319 if (error || !in_suspend)
320 swsusp_free();
321
Alan Sternd1616302009-05-24 22:05:42 +0200322 dpm_resume_end(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200323 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700324 resume_console();
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100325 Close:
326 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700327 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200328
329 Recover_platform:
330 platform_recover(platform_mode);
331 goto Resume_devices;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700332}
333
334/**
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100335 * resume_target_kernel - prepare devices that need to be suspended with
336 * interrupts off, restore the contents of highmem that have not been
337 * restored yet from the image and run the low level code that will restore
338 * the remaining contents of memory and switch to the just restored target
339 * kernel.
340 */
341
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100342static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100343{
344 int error;
345
Alan Sternd1616302009-05-24 22:05:42 +0200346 error = dpm_suspend_noirq(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100347 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100348 printk(KERN_ERR "PM: Some devices failed to power down, "
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100349 "aborting resume\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200350 return error;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100351 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100352
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100353 error = platform_pre_restore(platform_mode);
354 if (error)
355 goto Cleanup;
356
357 error = disable_nonboot_cpus();
358 if (error)
359 goto Enable_cpus;
360
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100361 local_irq_disable();
362
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100363 error = sysdev_suspend(PMSG_QUIESCE);
364 if (error)
365 goto Enable_irqs;
366
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100367 /* We'll ignore saved state, but this gets preempt count (etc) right */
368 save_processor_state();
369 error = restore_highmem();
370 if (!error) {
371 error = swsusp_arch_resume();
372 /*
373 * The code below is only ever reached in case of a failure.
374 * Otherwise execution continues at place where
375 * swsusp_arch_suspend() was called
376 */
377 BUG_ON(!error);
378 /* This call to restore_highmem() undos the previous one */
379 restore_highmem();
380 }
381 /*
382 * The only reason why swsusp_arch_resume() can fail is memory being
383 * very tight, so we have to free it as soon as we can to avoid
384 * subsequent failures
385 */
386 swsusp_free();
387 restore_processor_state();
388 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100389
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100390 sysdev_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100391
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100392 Enable_irqs:
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100393 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100394
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100395 Enable_cpus:
396 enable_nonboot_cpus();
397
398 Cleanup:
399 platform_restore_cleanup(platform_mode);
400
Alan Sternd1616302009-05-24 22:05:42 +0200401 dpm_resume_noirq(PMSG_RECOVER);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100402
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100403 return error;
404}
405
406/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700407 * hibernation_restore - quiesce devices and restore the hibernation
408 * snapshot image. If successful, control returns in hibernation_snaphot()
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700409 * @platform_mode - if set, use the platform driver, if available, to
Nick Andrew877d0312009-01-26 11:06:57 +0100410 * prepare the platform firmware for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700411 *
412 * Must be called with pm_mutex held
413 */
414
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700415int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700416{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100417 int error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700418
419 pm_prepare_console();
420 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +0200421 error = dpm_suspend_start(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700422 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100423 error = resume_target_kernel(platform_mode);
Alan Sternd1616302009-05-24 22:05:42 +0200424 dpm_resume_end(PMSG_RECOVER);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700425 }
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700426 resume_console();
427 pm_restore_console();
428 return error;
429}
430
431/**
432 * hibernation_platform_enter - enter the hibernation state using the
433 * platform driver (if available)
434 */
435
436int hibernation_platform_enter(void)
437{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100438 int error;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700439
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700440 if (!hibernation_ops)
441 return -ENOSYS;
442
443 /*
444 * We have cancelled the power transition by running
445 * hibernation_ops->finish() before saving the image, so we should let
446 * the firmware know that we're going to enter the sleep state after all
447 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100448 error = hibernation_ops->begin();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700449 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100450 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700451
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100452 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700453 suspend_console();
Alan Sternd1616302009-05-24 22:05:42 +0200454 error = dpm_suspend_start(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200455 if (error) {
456 if (hibernation_ops->recover)
457 hibernation_ops->recover();
458 goto Resume_devices;
459 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700460
Alan Sternd1616302009-05-24 22:05:42 +0200461 error = dpm_suspend_noirq(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100462 if (error)
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200463 goto Resume_devices;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100464
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100465 error = hibernation_ops->prepare();
466 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200467 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100468
469 error = disable_nonboot_cpus();
470 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200471 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100472
473 local_irq_disable();
474 sysdev_suspend(PMSG_HIBERNATE);
475 hibernation_ops->enter();
476 /* We should never get here */
477 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700478
479 /*
480 * We don't need to reenable the nonboot CPUs or resume consoles, since
481 * the system is going to be halted anyway.
482 */
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200483 Platform_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700484 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100485
Alan Sternd1616302009-05-24 22:05:42 +0200486 dpm_suspend_noirq(PMSG_RESTORE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100487
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700488 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100489 entering_platform_hibernation = false;
Alan Sternd1616302009-05-24 22:05:42 +0200490 dpm_resume_end(PMSG_RESTORE);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700491 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100492
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100493 Close:
494 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100495
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700496 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700497}
498
499/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700500 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 *
Johannes Bergfe0c9352007-04-30 15:09:51 -0700502 * Use the platform driver, if configured so; otherwise try
503 * to power off or reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
505
Johannes Bergfe0c9352007-04-30 15:09:51 -0700506static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700508 switch (hibernation_mode) {
509 case HIBERNATION_TEST:
510 case HIBERNATION_TESTPROC:
Johannes Bergfe0c9352007-04-30 15:09:51 -0700511 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700512 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600513 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700515 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700516 hibernation_platform_enter();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700517 case HIBERNATION_SHUTDOWN:
518 kernel_power_off();
519 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600521 kernel_halt();
Johannes Bergfe0c9352007-04-30 15:09:51 -0700522 /*
523 * Valid image is on the disk, if we continue we risk serious data
524 * corruption after resume.
525 */
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100526 printk(KERN_CRIT "PM: Please power down manually\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 while(1);
528}
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530static int prepare_processes(void)
531{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800532 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (freeze_processes()) {
535 error = -EBUSY;
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100536 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700538 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700542 * hibernate - The granpappy of the built-in hibernation management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 */
544
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700545int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 int error;
548
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700549 mutex_lock(&pm_mutex);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700550 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700551 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
552 error = -EBUSY;
553 goto Unlock;
554 }
555
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100556 pm_prepare_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700557 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
558 if (error)
559 goto Exit;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700560
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700561 error = usermodehelper_disable();
562 if (error)
563 goto Exit;
564
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700565 /* Allocate memory management structures */
566 error = create_basic_memory_bitmaps();
567 if (error)
568 goto Exit;
569
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100570 printk(KERN_INFO "PM: Syncing filesystems ... ");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700571 sys_sync();
572 printk("done.\n");
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700575 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700576 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100578 if (hibernation_test(TEST_FREEZER))
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800579 goto Thaw;
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100580
581 if (hibernation_testmode(HIBERNATION_TESTPROC))
582 goto Thaw;
583
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700584 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200585 if (error)
586 goto Thaw;
587
588 if (in_suspend) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700589 unsigned int flags = 0;
590
591 if (hibernation_mode == HIBERNATION_PLATFORM)
592 flags |= SF_PLATFORM_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700594 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700595 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (!error)
Johannes Bergfe0c9352007-04-30 15:09:51 -0700597 power_down();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800598 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800600 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200601
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800602 Thaw:
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100603 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700604 Finish:
605 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700606 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700607 Exit:
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700608 pm_notifier_call_chain(PM_POST_HIBERNATION);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100609 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700610 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700611 Unlock:
612 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return error;
614}
615
616
617/**
618 * software_resume - Resume from a saved image.
619 *
620 * Called as a late_initcall (so all devices are discovered and
621 * initialized), we call swsusp to see if we have a saved image or not.
622 * If so, we quiesce devices, the restore the saved image. We will
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700623 * return above (in hibernate() ) if everything goes well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 * Otherwise, we fail gracefully and return to the normally
625 * scheduled program.
626 *
627 */
628
629static int software_resume(void)
630{
631 int error;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700632 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Johannes Berg60a0d232007-11-14 17:00:16 -0800634 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100635 * If the user said "noresume".. bail out early.
636 */
637 if (noresume)
638 return 0;
639
640 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800641 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
642 * is configured into the kernel. Since the regular hibernate
643 * trigger path is via sysfs which takes a buffer mutex before
644 * calling hibernate functions (which take pm_mutex) this can
645 * cause lockdep to complain about a possible ABBA deadlock
646 * which cannot happen since we're in the boot code here and
647 * sysfs can't be invoked yet. Therefore, we use a subclass
648 * here to avoid lockdep complaining.
649 */
650 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200651
652 if (swsusp_resume_device)
653 goto Check_image;
654
655 if (!strlen(resume_file)) {
656 error = -ENOENT;
657 goto Unlock;
658 }
659
660 pr_debug("PM: Checking image partition %s\n", resume_file);
661
662 /* Check if the device is there */
663 swsusp_resume_device = name_to_dev_t(resume_file);
Pavel Machek3efa1472005-07-07 17:56:43 -0700664 if (!swsusp_resume_device) {
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100665 /*
666 * Some device discovery might still be in progress; we need
667 * to wait for this to finish.
668 */
669 wait_for_device_probe();
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200670 /*
671 * We can't depend on SCSI devices being available after loading
672 * one of their modules until scsi_complete_async_scans() is
673 * called and the resume device usually is a SCSI one.
674 */
675 scsi_complete_async_scans();
676
Pavel Machek3efa1472005-07-07 17:56:43 -0700677 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200678 if (!swsusp_resume_device) {
679 error = -ENODEV;
680 goto Unlock;
681 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700682 }
683
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200684 Check_image:
685 pr_debug("PM: Resume from partition %d:%d\n",
686 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100688 pr_debug("PM: Checking hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800689 error = swsusp_check();
690 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700691 goto Unlock;
692
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700693 /* The snapshot device should not be opened while we're running */
694 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
695 error = -EBUSY;
696 goto Unlock;
697 }
698
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100699 pm_prepare_console();
Alan Sternc3e94d82007-11-19 23:38:25 +0100700 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
701 if (error)
702 goto Finish;
703
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700704 error = usermodehelper_disable();
705 if (error)
706 goto Finish;
707
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700708 error = create_basic_memory_bitmaps();
709 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700710 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 pr_debug("PM: Preparing processes for restore.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800713 error = prepare_processes();
714 if (error) {
Al Viroc2dd0da2007-10-08 13:21:10 -0400715 swsusp_close(FMODE_READ);
Li Shaohua5a72e042005-06-25 14:55:06 -0700716 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100719 pr_debug("PM: Reading hibernation image.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700721 error = swsusp_read(&flags);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800722 if (!error)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700723 hibernation_restore(flags & SF_PLATFORM_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800725 printk(KERN_ERR "PM: Restore failed, recovering.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700726 swsusp_free();
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100727 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 Done:
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700729 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700730 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700731 Finish:
Alan Sternc3e94d82007-11-19 23:38:25 +0100732 pm_notifier_call_chain(PM_POST_RESTORE);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100733 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700734 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700735 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700736 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800737 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 pr_debug("PM: Resume from disk failed.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700739 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742late_initcall(software_resume);
743
744
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700745static const char * const hibernation_modes[] = {
746 [HIBERNATION_PLATFORM] = "platform",
747 [HIBERNATION_SHUTDOWN] = "shutdown",
748 [HIBERNATION_REBOOT] = "reboot",
749 [HIBERNATION_TEST] = "test",
750 [HIBERNATION_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751};
752
753/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700754 * disk - Control hibernation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700756 * Suspend-to-disk can be handled in several ways. We have a few options
757 * for putting the system to sleep - using the platform driver (e.g. ACPI
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700758 * or other hibernation_ops), powering off the system or rebooting the
759 * system (for testing) as well as the two test modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700761 * The system can support 'platform', and that is known a priori (and
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700762 * encoded by the presence of hibernation_ops). However, the user may
763 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
764 * test modes, 'test' or 'testproc'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 *
766 * show() will display what the mode is currently set to.
767 * store() will accept one of
768 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 * 'platform'
770 * 'shutdown'
771 * 'reboot'
Johannes Berg11d77d02007-04-30 15:09:53 -0700772 * 'test'
773 * 'testproc'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700775 * It will only change to 'platform' if the system
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700776 * supports it (as determined by having hibernation_ops).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 */
778
Kay Sievers386f2752007-11-02 13:47:53 +0100779static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
780 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700782 int i;
783 char *start = buf;
784
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700785 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
786 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700787 continue;
788 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700789 case HIBERNATION_SHUTDOWN:
790 case HIBERNATION_REBOOT:
791 case HIBERNATION_TEST:
792 case HIBERNATION_TESTPROC:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700793 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700794 case HIBERNATION_PLATFORM:
795 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700796 break;
797 /* not a valid mode, continue with loop */
798 continue;
799 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700800 if (i == hibernation_mode)
801 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700802 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700803 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700804 }
805 buf += sprintf(buf, "\n");
806 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
809
Kay Sievers386f2752007-11-02 13:47:53 +0100810static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
811 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 int error = 0;
814 int i;
815 int len;
816 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700817 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 p = memchr(buf, '\n', n);
820 len = p ? p - buf : n;
821
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800822 mutex_lock(&pm_mutex);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700823 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700824 if (len == strlen(hibernation_modes[i])
825 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 mode = i;
827 break;
828 }
829 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700830 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c9352007-04-30 15:09:51 -0700831 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700832 case HIBERNATION_SHUTDOWN:
833 case HIBERNATION_REBOOT:
834 case HIBERNATION_TEST:
835 case HIBERNATION_TESTPROC:
836 hibernation_mode = mode;
Johannes Bergfe0c9352007-04-30 15:09:51 -0700837 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700838 case HIBERNATION_PLATFORM:
839 if (hibernation_ops)
840 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 else
842 error = -EINVAL;
843 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700844 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 error = -EINVAL;
846
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700847 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100848 pr_debug("PM: Hibernation mode set to '%s'\n",
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700849 hibernation_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800850 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return error ? error : n;
852}
853
854power_attr(disk);
855
Kay Sievers386f2752007-11-02 13:47:53 +0100856static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
857 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
860 MINOR(swsusp_resume_device));
861}
862
Kay Sievers386f2752007-11-02 13:47:53 +0100863static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
864 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800868 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Andrew Mortona5762192006-01-06 00:09:50 -0800870 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
871 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Andrew Mortona5762192006-01-06 00:09:50 -0800873 res = MKDEV(maj,min);
874 if (maj != MAJOR(res) || min != MINOR(res))
875 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800877 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800878 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800879 mutex_unlock(&pm_mutex);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100880 printk(KERN_INFO "PM: Starting manual resume from disk\n");
Andrew Mortona5762192006-01-06 00:09:50 -0800881 noresume = 0;
882 software_resume();
883 ret = n;
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800884 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800885 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
888power_attr(resume);
889
Kay Sievers386f2752007-11-02 13:47:53 +0100890static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
891 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800892{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800893 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800894}
895
Kay Sievers386f2752007-11-02 13:47:53 +0100896static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
897 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800898{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800899 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800900
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800901 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800902 image_size = size;
903 return n;
904 }
905
906 return -EINVAL;
907}
908
909power_attr(image_size);
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911static struct attribute * g[] = {
912 &disk_attr.attr,
913 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800914 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 NULL,
916};
917
918
919static struct attribute_group attr_group = {
920 .attrs = g,
921};
922
923
924static int __init pm_disk_init(void)
925{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800926 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
929core_initcall(pm_disk_init);
930
931
932static int __init resume_setup(char *str)
933{
934 if (noresume)
935 return 1;
936
937 strncpy( resume_file, str, 255 );
938 return 1;
939}
940
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800941static int __init resume_offset_setup(char *str)
942{
943 unsigned long long offset;
944
945 if (noresume)
946 return 1;
947
948 if (sscanf(str, "%llu", &offset) == 1)
949 swsusp_resume_block = offset;
950
951 return 1;
952}
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954static int __init noresume_setup(char *str)
955{
956 noresume = 1;
957 return 1;
958}
959
960__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800961__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962__setup("resume=", resume_setup);