blob: aa9e916da4d53051eed6e7d12e9ddd37baaab3cd [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/gfp.h>
Rafael J. Wysockic7510852009-04-12 20:06:56 +020026#include <scsi/scsi_scan.h>
Magnus Damma8af7892009-03-31 15:23:37 -070027#include <asm/suspend.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "power.h"
30
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static int noresume = 0;
Adrian Bunk47a460d2008-02-04 22:30:06 -080033static char resume_file[256] = CONFIG_PM_STD_PARTITION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034dev_t swsusp_resume_device;
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -080035sector_t swsusp_resume_block;
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +010036int in_suspend __nosavedata = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070038enum {
39 HIBERNATION_INVALID,
40 HIBERNATION_PLATFORM,
41 HIBERNATION_TEST,
42 HIBERNATION_TESTPROC,
43 HIBERNATION_SHUTDOWN,
44 HIBERNATION_REBOOT,
45 /* keep last */
46 __HIBERNATION_AFTER_LAST
47};
48#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
49#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
50
51static int hibernation_mode = HIBERNATION_SHUTDOWN;
52
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070053static struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070054
55/**
56 * hibernation_set_ops - set the global hibernate operations
57 * @ops: the hibernation operations to use in subsequent hibernation transitions
58 */
59
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070060void hibernation_set_ops(struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070061{
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +010062 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
63 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -070064 && ops->restore_cleanup)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070065 WARN_ON(1);
66 return;
67 }
68 mutex_lock(&pm_mutex);
69 hibernation_ops = ops;
70 if (ops)
71 hibernation_mode = HIBERNATION_PLATFORM;
72 else if (hibernation_mode == HIBERNATION_PLATFORM)
73 hibernation_mode = HIBERNATION_SHUTDOWN;
74
75 mutex_unlock(&pm_mutex);
76}
77
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +010078static bool entering_platform_hibernation;
79
80bool system_entering_hibernation(void)
81{
82 return entering_platform_hibernation;
83}
84EXPORT_SYMBOL(system_entering_hibernation);
85
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +010086#ifdef CONFIG_PM_DEBUG
87static void hibernation_debug_sleep(void)
88{
89 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
90 mdelay(5000);
91}
92
93static int hibernation_testmode(int mode)
94{
95 if (hibernation_mode == mode) {
96 hibernation_debug_sleep();
97 return 1;
98 }
99 return 0;
100}
101
102static int hibernation_test(int level)
103{
104 if (pm_test_level == level) {
105 hibernation_debug_sleep();
106 return 1;
107 }
108 return 0;
109}
110#else /* !CONFIG_PM_DEBUG */
111static int hibernation_testmode(int mode) { return 0; }
112static int hibernation_test(int level) { return 0; }
113#endif /* !CONFIG_PM_DEBUG */
114
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700115/**
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100116 * platform_begin - tell the platform driver that we're starting
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700117 * hibernation
118 */
119
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100120static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700121{
122 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100123 hibernation_ops->begin() : 0;
124}
125
126/**
127 * platform_end - tell the platform driver that we've entered the
128 * working state
129 */
130
131static void platform_end(int platform_mode)
132{
133 if (platform_mode && hibernation_ops)
134 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700135}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/**
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700138 * platform_pre_snapshot - prepare the machine for hibernation using the
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800139 * platform driver if so configured and return an error code if it fails
140 */
141
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700142static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800143{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700144 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700145 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800146}
147
148/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700149 * platform_leave - prepare the machine for switching to the normal mode
150 * of operation using the platform driver (called with interrupts disabled)
151 */
152
153static void platform_leave(int platform_mode)
154{
155 if (platform_mode && hibernation_ops)
156 hibernation_ops->leave();
157}
158
159/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700160 * platform_finish - switch the machine to the normal mode of operation
161 * using the platform driver (must be called after platform_prepare())
162 */
163
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700164static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700165{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700166 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700167 hibernation_ops->finish();
168}
169
170/**
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700171 * platform_pre_restore - prepare the platform for the restoration from a
172 * hibernation image. If the restore fails after this function has been
173 * called, platform_restore_cleanup() must be called.
174 */
175
176static int platform_pre_restore(int platform_mode)
177{
178 return (platform_mode && hibernation_ops) ?
179 hibernation_ops->pre_restore() : 0;
180}
181
182/**
183 * platform_restore_cleanup - switch the platform to the normal mode of
184 * operation after a failing restore. If platform_pre_restore() has been
185 * called before the failing restore, this function must be called too,
186 * regardless of the result of platform_pre_restore().
187 */
188
189static void platform_restore_cleanup(int platform_mode)
190{
191 if (platform_mode && hibernation_ops)
192 hibernation_ops->restore_cleanup();
193}
194
195/**
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200196 * platform_recover - recover the platform from a failure to suspend
197 * devices.
198 */
199
200static void platform_recover(int platform_mode)
201{
202 if (platform_mode && hibernation_ops && hibernation_ops->recover)
203 hibernation_ops->recover();
204}
205
206/**
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100207 * swsusp_show_speed - print the time elapsed between two events.
208 * @start: Starting event.
209 * @stop: Final event.
210 * @nr_pages - number of pages processed between @start and @stop
211 * @msg - introductory message to print
212 */
213
214void swsusp_show_speed(struct timeval *start, struct timeval *stop,
215 unsigned nr_pages, char *msg)
216{
217 s64 elapsed_centisecs64;
218 int centisecs;
219 int k;
220 int kps;
221
222 elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
223 do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
224 centisecs = elapsed_centisecs64;
225 if (centisecs == 0)
226 centisecs = 1; /* avoid div-by-zero */
227 k = nr_pages * (PAGE_SIZE / 1024);
228 kps = (k * 100) / centisecs;
229 printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
230 msg, k,
231 centisecs / 100, centisecs % 100,
232 kps / 1000, (kps % 1000) / 10);
233}
234
235/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700236 * create_image - freeze devices that need to be frozen with interrupts
237 * off, create the hibernation image and thaw those devices. Control
238 * reappears in this routine after a restore.
239 */
240
Adrian Bunk47a460d2008-02-04 22:30:06 -0800241static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700242{
243 int error;
244
245 error = arch_prepare_suspend();
246 if (error)
247 return error;
248
Alan Sternd1616302009-05-24 22:05:42 +0200249 /* At this point, dpm_suspend_start() has been called, but *not*
250 * dpm_suspend_noirq(). We *must* call dpm_suspend_noirq() now.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700251 * Otherwise, drivers for some devices (e.g. interrupt controllers)
252 * become desynchronized with the actual state of the hardware
253 * at resume time, and evil weirdness ensues.
254 */
Alan Sternd1616302009-05-24 22:05:42 +0200255 error = dpm_suspend_noirq(PMSG_FREEZE);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700256 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100257 printk(KERN_ERR "PM: Some devices failed to power down, "
258 "aborting hibernation\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200259 return error;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700260 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100261
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100262 error = platform_pre_snapshot(platform_mode);
263 if (error || hibernation_test(TEST_PLATFORM))
264 goto Platform_finish;
265
266 error = disable_nonboot_cpus();
267 if (error || hibernation_test(TEST_CPUS)
268 || hibernation_testmode(HIBERNATION_TEST))
269 goto Enable_cpus;
270
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100271 local_irq_disable();
272
Bjorn Helgaas44840792009-05-15 23:30:50 +0200273 error = sysdev_suspend(PMSG_FREEZE);
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100274 if (error) {
Bjorn Helgaas44840792009-05-15 23:30:50 +0200275 printk(KERN_ERR "PM: Some system devices failed to power down, "
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100276 "aborting hibernation\n");
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100277 goto Enable_irqs;
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100278 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700279
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100280 if (hibernation_test(TEST_CORE))
281 goto Power_up;
282
283 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700284 save_processor_state();
285 error = swsusp_arch_suspend();
286 if (error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100287 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
288 error);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700289 /* Restore control flow magically appears here */
290 restore_processor_state();
291 if (!in_suspend)
292 platform_leave(platform_mode);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100293
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100294 Power_up:
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100295 sysdev_resume();
Alan Sternd1616302009-05-24 22:05:42 +0200296 /* NOTE: dpm_resume_noirq() is just a resume() for devices
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700297 * that suspended with irqs off ... no overall powerup.
298 */
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100299
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100300 Enable_irqs:
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100301 local_irq_enable();
302
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100303 Enable_cpus:
304 enable_nonboot_cpus();
305
306 Platform_finish:
307 platform_finish(platform_mode);
308
Alan Sternd1616302009-05-24 22:05:42 +0200309 dpm_resume_noirq(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200310 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100311
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700312 return error;
313}
314
315/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700316 * hibernation_snapshot - quiesce devices and create the hibernation
317 * snapshot image.
318 * @platform_mode - if set, use the platform driver, if available, to
Nick Andrew877d0312009-01-26 11:06:57 +0100319 * prepare the platform firmware for the power transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700320 *
321 * Must be called with pm_mutex held
322 */
323
324int hibernation_snapshot(int platform_mode)
325{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100326 int error;
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800327 gfp_t saved_mask;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700328
Zhang Rui3fe03132008-10-26 20:50:26 +0100329 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700330 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700331 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700332
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200333 /* Preallocate image memory before shutting down devices. */
334 error = hibernate_preallocate_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700335 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100336 goto Close;
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700337
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700338 suspend_console();
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800339 saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
Alan Sternd1616302009-05-24 22:05:42 +0200340 error = dpm_suspend_start(PMSG_FREEZE);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700341 if (error)
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200342 goto Recover_platform;
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700343
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100344 if (hibernation_test(TEST_DEVICES))
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200345 goto Recover_platform;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700346
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100347 error = create_image(platform_mode);
348 /* Control returns here after successful restore */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100349
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100350 Resume_devices:
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200351 /* We may need to release the preallocated image pages here. */
352 if (error || !in_suspend)
353 swsusp_free();
354
Alan Sternd1616302009-05-24 22:05:42 +0200355 dpm_resume_end(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200356 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800357 set_gfp_allowed_mask(saved_mask);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700358 resume_console();
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100359 Close:
360 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700361 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200362
363 Recover_platform:
364 platform_recover(platform_mode);
365 goto Resume_devices;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700366}
367
368/**
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100369 * resume_target_kernel - prepare devices that need to be suspended with
370 * interrupts off, restore the contents of highmem that have not been
371 * restored yet from the image and run the low level code that will restore
372 * the remaining contents of memory and switch to the just restored target
373 * kernel.
374 */
375
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100376static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100377{
378 int error;
379
Alan Sternd1616302009-05-24 22:05:42 +0200380 error = dpm_suspend_noirq(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100381 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100382 printk(KERN_ERR "PM: Some devices failed to power down, "
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100383 "aborting resume\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200384 return error;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100385 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100386
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100387 error = platform_pre_restore(platform_mode);
388 if (error)
389 goto Cleanup;
390
391 error = disable_nonboot_cpus();
392 if (error)
393 goto Enable_cpus;
394
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100395 local_irq_disable();
396
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100397 error = sysdev_suspend(PMSG_QUIESCE);
398 if (error)
399 goto Enable_irqs;
400
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100401 /* We'll ignore saved state, but this gets preempt count (etc) right */
402 save_processor_state();
403 error = restore_highmem();
404 if (!error) {
405 error = swsusp_arch_resume();
406 /*
407 * The code below is only ever reached in case of a failure.
408 * Otherwise execution continues at place where
409 * swsusp_arch_suspend() was called
410 */
411 BUG_ON(!error);
412 /* This call to restore_highmem() undos the previous one */
413 restore_highmem();
414 }
415 /*
416 * The only reason why swsusp_arch_resume() can fail is memory being
417 * very tight, so we have to free it as soon as we can to avoid
418 * subsequent failures
419 */
420 swsusp_free();
421 restore_processor_state();
422 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100423
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100424 sysdev_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100425
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100426 Enable_irqs:
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100427 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100428
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100429 Enable_cpus:
430 enable_nonboot_cpus();
431
432 Cleanup:
433 platform_restore_cleanup(platform_mode);
434
Alan Sternd1616302009-05-24 22:05:42 +0200435 dpm_resume_noirq(PMSG_RECOVER);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100436
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100437 return error;
438}
439
440/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700441 * hibernation_restore - quiesce devices and restore the hibernation
442 * snapshot image. If successful, control returns in hibernation_snaphot()
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700443 * @platform_mode - if set, use the platform driver, if available, to
Nick Andrew877d0312009-01-26 11:06:57 +0100444 * prepare the platform firmware for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700445 *
446 * Must be called with pm_mutex held
447 */
448
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700449int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700450{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100451 int error;
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800452 gfp_t saved_mask;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700453
454 pm_prepare_console();
455 suspend_console();
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800456 saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
Alan Sternd1616302009-05-24 22:05:42 +0200457 error = dpm_suspend_start(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700458 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100459 error = resume_target_kernel(platform_mode);
Alan Sternd1616302009-05-24 22:05:42 +0200460 dpm_resume_end(PMSG_RECOVER);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700461 }
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800462 set_gfp_allowed_mask(saved_mask);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700463 resume_console();
464 pm_restore_console();
465 return error;
466}
467
468/**
469 * hibernation_platform_enter - enter the hibernation state using the
470 * platform driver (if available)
471 */
472
473int hibernation_platform_enter(void)
474{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100475 int error;
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800476 gfp_t saved_mask;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700477
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700478 if (!hibernation_ops)
479 return -ENOSYS;
480
481 /*
482 * We have cancelled the power transition by running
483 * hibernation_ops->finish() before saving the image, so we should let
484 * the firmware know that we're going to enter the sleep state after all
485 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100486 error = hibernation_ops->begin();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700487 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100488 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700489
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100490 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700491 suspend_console();
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800492 saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
Alan Sternd1616302009-05-24 22:05:42 +0200493 error = dpm_suspend_start(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200494 if (error) {
495 if (hibernation_ops->recover)
496 hibernation_ops->recover();
497 goto Resume_devices;
498 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700499
Alan Sternd1616302009-05-24 22:05:42 +0200500 error = dpm_suspend_noirq(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100501 if (error)
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200502 goto Resume_devices;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100503
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100504 error = hibernation_ops->prepare();
505 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200506 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100507
508 error = disable_nonboot_cpus();
509 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200510 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100511
512 local_irq_disable();
513 sysdev_suspend(PMSG_HIBERNATE);
514 hibernation_ops->enter();
515 /* We should never get here */
516 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700517
518 /*
519 * We don't need to reenable the nonboot CPUs or resume consoles, since
520 * the system is going to be halted anyway.
521 */
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200522 Platform_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700523 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100524
Alan Sternd1616302009-05-24 22:05:42 +0200525 dpm_suspend_noirq(PMSG_RESTORE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100526
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700527 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100528 entering_platform_hibernation = false;
Alan Sternd1616302009-05-24 22:05:42 +0200529 dpm_resume_end(PMSG_RESTORE);
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800530 set_gfp_allowed_mask(saved_mask);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700531 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100532
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100533 Close:
534 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100535
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700536 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700537}
538
539/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700540 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 *
Johannes Bergfe0c9352007-04-30 15:09:51 -0700542 * Use the platform driver, if configured so; otherwise try
543 * to power off or reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
545
Johannes Bergfe0c9352007-04-30 15:09:51 -0700546static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700548 switch (hibernation_mode) {
549 case HIBERNATION_TEST:
550 case HIBERNATION_TESTPROC:
Johannes Bergfe0c9352007-04-30 15:09:51 -0700551 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700552 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600553 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700555 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700556 hibernation_platform_enter();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700557 case HIBERNATION_SHUTDOWN:
558 kernel_power_off();
559 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600561 kernel_halt();
Johannes Bergfe0c9352007-04-30 15:09:51 -0700562 /*
563 * Valid image is on the disk, if we continue we risk serious data
564 * corruption after resume.
565 */
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100566 printk(KERN_CRIT "PM: Please power down manually\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 while(1);
568}
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570static int prepare_processes(void)
571{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800572 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (freeze_processes()) {
575 error = -EBUSY;
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100576 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700578 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700582 * hibernate - The granpappy of the built-in hibernation management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 */
584
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700585int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 int error;
588
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700589 mutex_lock(&pm_mutex);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700590 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700591 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
592 error = -EBUSY;
593 goto Unlock;
594 }
595
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100596 pm_prepare_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700597 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
598 if (error)
599 goto Exit;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700600
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700601 error = usermodehelper_disable();
602 if (error)
603 goto Exit;
604
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700605 /* Allocate memory management structures */
606 error = create_basic_memory_bitmaps();
607 if (error)
608 goto Exit;
609
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100610 printk(KERN_INFO "PM: Syncing filesystems ... ");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700611 sys_sync();
612 printk("done.\n");
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700615 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700616 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100618 if (hibernation_test(TEST_FREEZER))
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800619 goto Thaw;
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100620
621 if (hibernation_testmode(HIBERNATION_TESTPROC))
622 goto Thaw;
623
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700624 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200625 if (error)
626 goto Thaw;
627
628 if (in_suspend) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700629 unsigned int flags = 0;
630
631 if (hibernation_mode == HIBERNATION_PLATFORM)
632 flags |= SF_PLATFORM_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700634 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700635 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (!error)
Johannes Bergfe0c9352007-04-30 15:09:51 -0700637 power_down();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800638 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800640 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200641
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800642 Thaw:
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100643 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700644 Finish:
645 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700646 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700647 Exit:
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700648 pm_notifier_call_chain(PM_POST_HIBERNATION);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100649 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700650 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700651 Unlock:
652 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return error;
654}
655
656
657/**
658 * software_resume - Resume from a saved image.
659 *
660 * Called as a late_initcall (so all devices are discovered and
661 * initialized), we call swsusp to see if we have a saved image or not.
662 * If so, we quiesce devices, the restore the saved image. We will
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700663 * return above (in hibernate() ) if everything goes well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 * Otherwise, we fail gracefully and return to the normally
665 * scheduled program.
666 *
667 */
668
669static int software_resume(void)
670{
671 int error;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700672 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Johannes Berg60a0d232007-11-14 17:00:16 -0800674 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100675 * If the user said "noresume".. bail out early.
676 */
677 if (noresume)
678 return 0;
679
680 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800681 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
682 * is configured into the kernel. Since the regular hibernate
683 * trigger path is via sysfs which takes a buffer mutex before
684 * calling hibernate functions (which take pm_mutex) this can
685 * cause lockdep to complain about a possible ABBA deadlock
686 * which cannot happen since we're in the boot code here and
687 * sysfs can't be invoked yet. Therefore, we use a subclass
688 * here to avoid lockdep complaining.
689 */
690 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200691
692 if (swsusp_resume_device)
693 goto Check_image;
694
695 if (!strlen(resume_file)) {
696 error = -ENOENT;
697 goto Unlock;
698 }
699
700 pr_debug("PM: Checking image partition %s\n", resume_file);
701
702 /* Check if the device is there */
703 swsusp_resume_device = name_to_dev_t(resume_file);
Pavel Machek3efa1472005-07-07 17:56:43 -0700704 if (!swsusp_resume_device) {
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100705 /*
706 * Some device discovery might still be in progress; we need
707 * to wait for this to finish.
708 */
709 wait_for_device_probe();
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200710 /*
711 * We can't depend on SCSI devices being available after loading
712 * one of their modules until scsi_complete_async_scans() is
713 * called and the resume device usually is a SCSI one.
714 */
715 scsi_complete_async_scans();
716
Pavel Machek3efa1472005-07-07 17:56:43 -0700717 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200718 if (!swsusp_resume_device) {
719 error = -ENODEV;
720 goto Unlock;
721 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700722 }
723
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200724 Check_image:
725 pr_debug("PM: Resume from partition %d:%d\n",
726 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100728 pr_debug("PM: Checking hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800729 error = swsusp_check();
730 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700731 goto Unlock;
732
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700733 /* The snapshot device should not be opened while we're running */
734 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
735 error = -EBUSY;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200736 swsusp_close(FMODE_READ);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700737 goto Unlock;
738 }
739
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100740 pm_prepare_console();
Alan Sternc3e94d82007-11-19 23:38:25 +0100741 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
742 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200743 goto close_finish;
Alan Sternc3e94d82007-11-19 23:38:25 +0100744
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700745 error = usermodehelper_disable();
746 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200747 goto close_finish;
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700748
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700749 error = create_basic_memory_bitmaps();
750 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200751 goto close_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 pr_debug("PM: Preparing processes for restore.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800754 error = prepare_processes();
755 if (error) {
Al Viroc2dd0da2007-10-08 13:21:10 -0400756 swsusp_close(FMODE_READ);
Li Shaohua5a72e042005-06-25 14:55:06 -0700757 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100760 pr_debug("PM: Reading hibernation image.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700762 error = swsusp_read(&flags);
Jiri Slaby76b57e62009-10-07 22:37:35 +0200763 swsusp_close(FMODE_READ);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800764 if (!error)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700765 hibernation_restore(flags & SF_PLATFORM_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800767 printk(KERN_ERR "PM: Restore failed, recovering.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700768 swsusp_free();
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100769 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 Done:
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700771 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700772 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700773 Finish:
Alan Sternc3e94d82007-11-19 23:38:25 +0100774 pm_notifier_call_chain(PM_POST_RESTORE);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100775 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700776 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700777 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700778 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800779 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 pr_debug("PM: Resume from disk failed.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700781 return error;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200782close_finish:
783 swsusp_close(FMODE_READ);
784 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787late_initcall(software_resume);
788
789
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700790static const char * const hibernation_modes[] = {
791 [HIBERNATION_PLATFORM] = "platform",
792 [HIBERNATION_SHUTDOWN] = "shutdown",
793 [HIBERNATION_REBOOT] = "reboot",
794 [HIBERNATION_TEST] = "test",
795 [HIBERNATION_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796};
797
798/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700799 * disk - Control hibernation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700801 * Suspend-to-disk can be handled in several ways. We have a few options
802 * for putting the system to sleep - using the platform driver (e.g. ACPI
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700803 * or other hibernation_ops), powering off the system or rebooting the
804 * system (for testing) as well as the two test modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700806 * The system can support 'platform', and that is known a priori (and
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700807 * encoded by the presence of hibernation_ops). However, the user may
808 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
809 * test modes, 'test' or 'testproc'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 *
811 * show() will display what the mode is currently set to.
812 * store() will accept one of
813 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 * 'platform'
815 * 'shutdown'
816 * 'reboot'
Johannes Berg11d77d02007-04-30 15:09:53 -0700817 * 'test'
818 * 'testproc'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700820 * It will only change to 'platform' if the system
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700821 * supports it (as determined by having hibernation_ops).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 */
823
Kay Sievers386f2752007-11-02 13:47:53 +0100824static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
825 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700827 int i;
828 char *start = buf;
829
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700830 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
831 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700832 continue;
833 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700834 case HIBERNATION_SHUTDOWN:
835 case HIBERNATION_REBOOT:
836 case HIBERNATION_TEST:
837 case HIBERNATION_TESTPROC:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700838 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700839 case HIBERNATION_PLATFORM:
840 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700841 break;
842 /* not a valid mode, continue with loop */
843 continue;
844 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700845 if (i == hibernation_mode)
846 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700847 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700848 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700849 }
850 buf += sprintf(buf, "\n");
851 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854
Kay Sievers386f2752007-11-02 13:47:53 +0100855static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
856 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 int error = 0;
859 int i;
860 int len;
861 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700862 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 p = memchr(buf, '\n', n);
865 len = p ? p - buf : n;
866
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800867 mutex_lock(&pm_mutex);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700868 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700869 if (len == strlen(hibernation_modes[i])
870 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 mode = i;
872 break;
873 }
874 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700875 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c9352007-04-30 15:09:51 -0700876 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700877 case HIBERNATION_SHUTDOWN:
878 case HIBERNATION_REBOOT:
879 case HIBERNATION_TEST:
880 case HIBERNATION_TESTPROC:
881 hibernation_mode = mode;
Johannes Bergfe0c9352007-04-30 15:09:51 -0700882 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700883 case HIBERNATION_PLATFORM:
884 if (hibernation_ops)
885 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 else
887 error = -EINVAL;
888 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700889 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 error = -EINVAL;
891
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700892 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100893 pr_debug("PM: Hibernation mode set to '%s'\n",
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700894 hibernation_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800895 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return error ? error : n;
897}
898
899power_attr(disk);
900
Kay Sievers386f2752007-11-02 13:47:53 +0100901static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
902 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
905 MINOR(swsusp_resume_device));
906}
907
Kay Sievers386f2752007-11-02 13:47:53 +0100908static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
909 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800913 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Andrew Mortona5762192006-01-06 00:09:50 -0800915 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
916 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Andrew Mortona5762192006-01-06 00:09:50 -0800918 res = MKDEV(maj,min);
919 if (maj != MAJOR(res) || min != MINOR(res))
920 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800922 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800923 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800924 mutex_unlock(&pm_mutex);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100925 printk(KERN_INFO "PM: Starting manual resume from disk\n");
Andrew Mortona5762192006-01-06 00:09:50 -0800926 noresume = 0;
927 software_resume();
928 ret = n;
Rafael J. Wysocki59a49332006-12-06 20:34:44 -0800929 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800930 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
933power_attr(resume);
934
Kay Sievers386f2752007-11-02 13:47:53 +0100935static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
936 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800937{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800938 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800939}
940
Kay Sievers386f2752007-11-02 13:47:53 +0100941static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
942 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800943{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800944 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800945
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800946 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800947 image_size = size;
948 return n;
949 }
950
951 return -EINVAL;
952}
953
954power_attr(image_size);
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956static struct attribute * g[] = {
957 &disk_attr.attr,
958 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800959 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 NULL,
961};
962
963
964static struct attribute_group attr_group = {
965 .attrs = g,
966};
967
968
969static int __init pm_disk_init(void)
970{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800971 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
974core_initcall(pm_disk_init);
975
976
977static int __init resume_setup(char *str)
978{
979 if (noresume)
980 return 1;
981
982 strncpy( resume_file, str, 255 );
983 return 1;
984}
985
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800986static int __init resume_offset_setup(char *str)
987{
988 unsigned long long offset;
989
990 if (noresume)
991 return 1;
992
993 if (sscanf(str, "%llu", &offset) == 1)
994 swsusp_resume_block = offset;
995
996 return 1;
997}
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999static int __init noresume_setup(char *str)
1000{
1001 noresume = 1;
1002 return 1;
1003}
1004
1005__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001006__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007__setup("resume=", resume_setup);