blob: da5288ec239286d9c2ae3f62b75a32ff3ed60f8b [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;
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +010035int in_suspend __nosavedata = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070037enum {
38 HIBERNATION_INVALID,
39 HIBERNATION_PLATFORM,
40 HIBERNATION_TEST,
41 HIBERNATION_TESTPROC,
42 HIBERNATION_SHUTDOWN,
43 HIBERNATION_REBOOT,
44 /* keep last */
45 __HIBERNATION_AFTER_LAST
46};
47#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
48#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
49
50static int hibernation_mode = HIBERNATION_SHUTDOWN;
51
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070052static struct platform_hibernation_ops *hibernation_ops;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070053
54/**
55 * hibernation_set_ops - set the global hibernate operations
56 * @ops: the hibernation operations to use in subsequent hibernation transitions
57 */
58
Rafael J. Wysockib3dac3b2007-10-18 03:04:43 -070059void hibernation_set_ops(struct platform_hibernation_ops *ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070060{
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +010061 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
62 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -070063 && ops->restore_cleanup)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -070064 WARN_ON(1);
65 return;
66 }
67 mutex_lock(&pm_mutex);
68 hibernation_ops = ops;
69 if (ops)
70 hibernation_mode = HIBERNATION_PLATFORM;
71 else if (hibernation_mode == HIBERNATION_PLATFORM)
72 hibernation_mode = HIBERNATION_SHUTDOWN;
73
74 mutex_unlock(&pm_mutex);
75}
76
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +010077static bool entering_platform_hibernation;
78
79bool system_entering_hibernation(void)
80{
81 return entering_platform_hibernation;
82}
83EXPORT_SYMBOL(system_entering_hibernation);
84
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +010085#ifdef CONFIG_PM_DEBUG
86static void hibernation_debug_sleep(void)
87{
88 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
89 mdelay(5000);
90}
91
92static int hibernation_testmode(int mode)
93{
94 if (hibernation_mode == mode) {
95 hibernation_debug_sleep();
96 return 1;
97 }
98 return 0;
99}
100
101static int hibernation_test(int level)
102{
103 if (pm_test_level == level) {
104 hibernation_debug_sleep();
105 return 1;
106 }
107 return 0;
108}
109#else /* !CONFIG_PM_DEBUG */
110static int hibernation_testmode(int mode) { return 0; }
111static int hibernation_test(int level) { return 0; }
112#endif /* !CONFIG_PM_DEBUG */
113
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700114/**
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100115 * platform_begin - tell the platform driver that we're starting
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700116 * hibernation
117 */
118
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100119static int platform_begin(int platform_mode)
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700120{
121 return (platform_mode && hibernation_ops) ?
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100122 hibernation_ops->begin() : 0;
123}
124
125/**
126 * platform_end - tell the platform driver that we've entered the
127 * working state
128 */
129
130static void platform_end(int platform_mode)
131{
132 if (platform_mode && hibernation_ops)
133 hibernation_ops->end();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700134}
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/**
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700137 * platform_pre_snapshot - prepare the machine for hibernation using the
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800138 * platform driver if so configured and return an error code if it fails
139 */
140
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700141static int platform_pre_snapshot(int platform_mode)
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800142{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700143 return (platform_mode && hibernation_ops) ?
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700144 hibernation_ops->pre_snapshot() : 0;
Stefan Seyfried8a05aac2006-12-06 20:34:21 -0800145}
146
147/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700148 * platform_leave - prepare the machine for switching to the normal mode
149 * of operation using the platform driver (called with interrupts disabled)
150 */
151
152static void platform_leave(int platform_mode)
153{
154 if (platform_mode && hibernation_ops)
155 hibernation_ops->leave();
156}
157
158/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700159 * platform_finish - switch the machine to the normal mode of operation
160 * using the platform driver (must be called after platform_prepare())
161 */
162
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700163static void platform_finish(int platform_mode)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700164{
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700165 if (platform_mode && hibernation_ops)
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700166 hibernation_ops->finish();
167}
168
169/**
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700170 * platform_pre_restore - prepare the platform for the restoration from a
171 * hibernation image. If the restore fails after this function has been
172 * called, platform_restore_cleanup() must be called.
173 */
174
175static int platform_pre_restore(int platform_mode)
176{
177 return (platform_mode && hibernation_ops) ?
178 hibernation_ops->pre_restore() : 0;
179}
180
181/**
182 * platform_restore_cleanup - switch the platform to the normal mode of
183 * operation after a failing restore. If platform_pre_restore() has been
184 * called before the failing restore, this function must be called too,
185 * regardless of the result of platform_pre_restore().
186 */
187
188static void platform_restore_cleanup(int platform_mode)
189{
190 if (platform_mode && hibernation_ops)
191 hibernation_ops->restore_cleanup();
192}
193
194/**
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200195 * platform_recover - recover the platform from a failure to suspend
196 * devices.
197 */
198
199static void platform_recover(int platform_mode)
200{
201 if (platform_mode && hibernation_ops && hibernation_ops->recover)
202 hibernation_ops->recover();
203}
204
205/**
Nigel Cunningham8e60c6a2009-12-06 16:16:07 +0100206 * swsusp_show_speed - print the time elapsed between two events.
207 * @start: Starting event.
208 * @stop: Final event.
209 * @nr_pages - number of pages processed between @start and @stop
210 * @msg - introductory message to print
211 */
212
213void swsusp_show_speed(struct timeval *start, struct timeval *stop,
214 unsigned nr_pages, char *msg)
215{
216 s64 elapsed_centisecs64;
217 int centisecs;
218 int k;
219 int kps;
220
221 elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
222 do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
223 centisecs = elapsed_centisecs64;
224 if (centisecs == 0)
225 centisecs = 1; /* avoid div-by-zero */
226 k = nr_pages * (PAGE_SIZE / 1024);
227 kps = (k * 100) / centisecs;
228 printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
229 msg, k,
230 centisecs / 100, centisecs % 100,
231 kps / 1000, (kps % 1000) / 10);
232}
233
234/**
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700235 * create_image - freeze devices that need to be frozen with interrupts
236 * off, create the hibernation image and thaw those devices. Control
237 * reappears in this routine after a restore.
238 */
239
Adrian Bunk47a460d2008-02-04 22:30:06 -0800240static int create_image(int platform_mode)
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700241{
242 int error;
243
244 error = arch_prepare_suspend();
245 if (error)
246 return error;
247
Alan Sternd1616302009-05-24 22:05:42 +0200248 /* At this point, dpm_suspend_start() has been called, but *not*
249 * dpm_suspend_noirq(). We *must* call dpm_suspend_noirq() now.
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700250 * Otherwise, drivers for some devices (e.g. interrupt controllers)
251 * become desynchronized with the actual state of the hardware
252 * at resume time, and evil weirdness ensues.
253 */
Alan Sternd1616302009-05-24 22:05:42 +0200254 error = dpm_suspend_noirq(PMSG_FREEZE);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700255 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100256 printk(KERN_ERR "PM: Some devices failed to power down, "
257 "aborting hibernation\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200258 return error;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700259 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100260
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100261 error = platform_pre_snapshot(platform_mode);
262 if (error || hibernation_test(TEST_PLATFORM))
263 goto Platform_finish;
264
265 error = disable_nonboot_cpus();
266 if (error || hibernation_test(TEST_CPUS)
267 || hibernation_testmode(HIBERNATION_TEST))
268 goto Enable_cpus;
269
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100270 local_irq_disable();
271
Bjorn Helgaas44840792009-05-15 23:30:50 +0200272 error = sysdev_suspend(PMSG_FREEZE);
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100273 if (error) {
Bjorn Helgaas44840792009-05-15 23:30:50 +0200274 printk(KERN_ERR "PM: Some system devices failed to power down, "
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100275 "aborting hibernation\n");
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100276 goto Enable_irqs;
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100277 }
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700278
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100279 if (hibernation_test(TEST_CORE))
280 goto Power_up;
281
282 in_suspend = 1;
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700283 save_processor_state();
284 error = swsusp_arch_suspend();
285 if (error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100286 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
287 error);
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700288 /* Restore control flow magically appears here */
289 restore_processor_state();
290 if (!in_suspend)
291 platform_leave(platform_mode);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100292
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100293 Power_up:
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100294 sysdev_resume();
Alan Sternd1616302009-05-24 22:05:42 +0200295 /* NOTE: dpm_resume_noirq() is just a resume() for devices
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700296 * that suspended with irqs off ... no overall powerup.
297 */
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100298
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100299 Enable_irqs:
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100300 local_irq_enable();
301
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100302 Enable_cpus:
303 enable_nonboot_cpus();
304
305 Platform_finish:
306 platform_finish(platform_mode);
307
Alan Sternd1616302009-05-24 22:05:42 +0200308 dpm_resume_noirq(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200309 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100310
Rafael J. Wysockic7e08312007-10-18 03:04:55 -0700311 return error;
312}
313
314/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700315 * hibernation_snapshot - quiesce devices and create the hibernation
316 * snapshot image.
317 * @platform_mode - if set, use the platform driver, if available, to
Nick Andrew877d0312009-01-26 11:06:57 +0100318 * prepare the platform firmware for the power transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700319 *
320 * Must be called with pm_mutex held
321 */
322
323int hibernation_snapshot(int platform_mode)
324{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100325 int error;
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800326 gfp_t saved_mask;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700327
Zhang Rui3fe03132008-10-26 20:50:26 +0100328 error = platform_begin(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700329 if (error)
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700330 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700331
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200332 /* Preallocate image memory before shutting down devices. */
333 error = hibernate_preallocate_memory();
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700334 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100335 goto Close;
Rafael J. Wysocki74f270a2007-10-18 03:04:42 -0700336
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700337 suspend_console();
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800338 saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
Alan Sternd1616302009-05-24 22:05:42 +0200339 error = dpm_suspend_start(PMSG_FREEZE);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700340 if (error)
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200341 goto Recover_platform;
Rafael J. Wysocki10a18032007-07-19 01:47:31 -0700342
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100343 if (hibernation_test(TEST_DEVICES))
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200344 goto Recover_platform;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700345
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100346 error = create_image(platform_mode);
347 /* Control returns here after successful restore */
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100348
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100349 Resume_devices:
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200350 /* We may need to release the preallocated image pages here. */
351 if (error || !in_suspend)
352 swsusp_free();
353
Alan Sternd1616302009-05-24 22:05:42 +0200354 dpm_resume_end(in_suspend ?
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200355 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800356 set_gfp_allowed_mask(saved_mask);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700357 resume_console();
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100358 Close:
359 platform_end(platform_mode);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700360 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200361
362 Recover_platform:
363 platform_recover(platform_mode);
364 goto Resume_devices;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700365}
366
367/**
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100368 * resume_target_kernel - prepare devices that need to be suspended with
369 * interrupts off, restore the contents of highmem that have not been
370 * restored yet from the image and run the low level code that will restore
371 * the remaining contents of memory and switch to the just restored target
372 * kernel.
373 */
374
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100375static int resume_target_kernel(bool platform_mode)
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100376{
377 int error;
378
Alan Sternd1616302009-05-24 22:05:42 +0200379 error = dpm_suspend_noirq(PMSG_QUIESCE);
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100380 if (error) {
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100381 printk(KERN_ERR "PM: Some devices failed to power down, "
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100382 "aborting resume\n");
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200383 return error;
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100384 }
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100385
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100386 error = platform_pre_restore(platform_mode);
387 if (error)
388 goto Cleanup;
389
390 error = disable_nonboot_cpus();
391 if (error)
392 goto Enable_cpus;
393
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100394 local_irq_disable();
395
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100396 error = sysdev_suspend(PMSG_QUIESCE);
397 if (error)
398 goto Enable_irqs;
399
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100400 /* We'll ignore saved state, but this gets preempt count (etc) right */
401 save_processor_state();
402 error = restore_highmem();
403 if (!error) {
404 error = swsusp_arch_resume();
405 /*
406 * The code below is only ever reached in case of a failure.
407 * Otherwise execution continues at place where
408 * swsusp_arch_suspend() was called
409 */
410 BUG_ON(!error);
411 /* This call to restore_highmem() undos the previous one */
412 restore_highmem();
413 }
414 /*
415 * The only reason why swsusp_arch_resume() can fail is memory being
416 * very tight, so we have to free it as soon as we can to avoid
417 * subsequent failures
418 */
419 swsusp_free();
420 restore_processor_state();
421 touch_softlockup_watchdog();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100422
Rafael J. Wysocki770824b2009-02-22 18:38:50 +0100423 sysdev_resume();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100424
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100425 Enable_irqs:
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100426 local_irq_enable();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100427
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100428 Enable_cpus:
429 enable_nonboot_cpus();
430
431 Cleanup:
432 platform_restore_cleanup(platform_mode);
433
Alan Sternd1616302009-05-24 22:05:42 +0200434 dpm_resume_noirq(PMSG_RECOVER);
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100435
Rafael J. Wysocki72df68c2007-12-08 02:04:21 +0100436 return error;
437}
438
439/**
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700440 * hibernation_restore - quiesce devices and restore the hibernation
441 * snapshot image. If successful, control returns in hibernation_snaphot()
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700442 * @platform_mode - if set, use the platform driver, if available, to
Nick Andrew877d0312009-01-26 11:06:57 +0100443 * prepare the platform firmware for the transition.
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700444 *
445 * Must be called with pm_mutex held
446 */
447
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700448int hibernation_restore(int platform_mode)
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700449{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100450 int error;
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800451 gfp_t saved_mask;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700452
453 pm_prepare_console();
454 suspend_console();
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800455 saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
Alan Sternd1616302009-05-24 22:05:42 +0200456 error = dpm_suspend_start(PMSG_QUIESCE);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700457 if (!error) {
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100458 error = resume_target_kernel(platform_mode);
Alan Sternd1616302009-05-24 22:05:42 +0200459 dpm_resume_end(PMSG_RECOVER);
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700460 }
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800461 set_gfp_allowed_mask(saved_mask);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700462 resume_console();
463 pm_restore_console();
464 return error;
465}
466
467/**
468 * hibernation_platform_enter - enter the hibernation state using the
469 * platform driver (if available)
470 */
471
472int hibernation_platform_enter(void)
473{
Ingo Molnarcbe2f5a2008-11-23 10:37:12 +0100474 int error;
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800475 gfp_t saved_mask;
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700476
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700477 if (!hibernation_ops)
478 return -ENOSYS;
479
480 /*
481 * We have cancelled the power transition by running
482 * hibernation_ops->finish() before saving the image, so we should let
483 * the firmware know that we're going to enter the sleep state after all
484 */
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100485 error = hibernation_ops->begin();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700486 if (error)
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100487 goto Close;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700488
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100489 entering_platform_hibernation = true;
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700490 suspend_console();
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800491 saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
Alan Sternd1616302009-05-24 22:05:42 +0200492 error = dpm_suspend_start(PMSG_HIBERNATE);
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200493 if (error) {
494 if (hibernation_ops->recover)
495 hibernation_ops->recover();
496 goto Resume_devices;
497 }
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700498
Alan Sternd1616302009-05-24 22:05:42 +0200499 error = dpm_suspend_noirq(PMSG_HIBERNATE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100500 if (error)
Rafael J. Wysocki32bdfac2009-05-24 21:15:07 +0200501 goto Resume_devices;
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100502
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100503 error = hibernation_ops->prepare();
504 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200505 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100506
507 error = disable_nonboot_cpus();
508 if (error)
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200509 goto Platform_finish;
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100510
511 local_irq_disable();
512 sysdev_suspend(PMSG_HIBERNATE);
513 hibernation_ops->enter();
514 /* We should never get here */
515 while (1);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700516
517 /*
518 * We don't need to reenable the nonboot CPUs or resume consoles, since
519 * the system is going to be halted anyway.
520 */
Thadeu Lima de Souza Cascardoe681c9d2009-07-08 13:23:32 +0200521 Platform_finish:
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700522 hibernation_ops->finish();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100523
Alan Sternd1616302009-05-24 22:05:42 +0200524 dpm_suspend_noirq(PMSG_RESTORE);
Rafael J. Wysocki4aecd672009-03-16 22:34:26 +0100525
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700526 Resume_devices:
Rafael J. Wysockiabfe2d72009-01-19 20:54:54 +0100527 entering_platform_hibernation = false;
Alan Sternd1616302009-05-24 22:05:42 +0200528 dpm_resume_end(PMSG_RESTORE);
Rafael J. Wysocki452aa692010-03-05 13:42:13 -0800529 set_gfp_allowed_mask(saved_mask);
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700530 resume_console();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100531
Rafael J. Wysockicaea99e2008-01-08 00:08:44 +0100532 Close:
533 hibernation_ops->end();
Rafael J. Wysocki2ed8d2b2009-03-16 22:34:06 +0100534
Rafael J. Wysockib1457bc2007-07-19 01:47:31 -0700535 return error;
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700536}
537
538/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700539 * power_down - Shut the machine down for hibernation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 *
Johannes Bergfe0c9352007-04-30 15:09:51 -0700541 * Use the platform driver, if configured so; otherwise try
542 * to power off or reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 */
544
Johannes Bergfe0c9352007-04-30 15:09:51 -0700545static void power_down(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700547 switch (hibernation_mode) {
548 case HIBERNATION_TEST:
549 case HIBERNATION_TESTPROC:
Johannes Bergfe0c9352007-04-30 15:09:51 -0700550 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700551 case HIBERNATION_REBOOT:
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600552 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700554 case HIBERNATION_PLATFORM:
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700555 hibernation_platform_enter();
Rafael J. Wysocki9cd9a002007-10-18 03:04:56 -0700556 case HIBERNATION_SHUTDOWN:
557 kernel_power_off();
558 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
Eric W. Biedermanfdde86a2005-07-26 12:01:17 -0600560 kernel_halt();
Johannes Bergfe0c9352007-04-30 15:09:51 -0700561 /*
562 * Valid image is on the disk, if we continue we risk serious data
563 * corruption after resume.
564 */
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100565 printk(KERN_CRIT "PM: Please power down manually\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 while(1);
567}
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569static int prepare_processes(void)
570{
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800571 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (freeze_processes()) {
574 error = -EBUSY;
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100575 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
Li Shaohua5a72e042005-06-25 14:55:06 -0700577 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700581 * hibernate - The granpappy of the built-in hibernation management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
583
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700584int hibernate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 int error;
587
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700588 mutex_lock(&pm_mutex);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700589 /* The snapshot device should not be opened while we're running */
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700590 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
591 error = -EBUSY;
592 goto Unlock;
593 }
594
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100595 pm_prepare_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700596 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
597 if (error)
598 goto Exit;
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700599
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700600 error = usermodehelper_disable();
601 if (error)
602 goto Exit;
603
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700604 /* Allocate memory management structures */
605 error = create_basic_memory_bitmaps();
606 if (error)
607 goto Exit;
608
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100609 printk(KERN_INFO "PM: Syncing filesystems ... ");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700610 sys_sync();
611 printk("done.\n");
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 error = prepare_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700614 if (error)
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700615 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100617 if (hibernation_test(TEST_FREEZER))
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800618 goto Thaw;
Rafael J. Wysocki4cc79772007-11-19 23:42:31 +0100619
620 if (hibernation_testmode(HIBERNATION_TESTPROC))
621 goto Thaw;
622
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700623 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200624 if (error)
625 goto Thaw;
626
627 if (in_suspend) {
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700628 unsigned int flags = 0;
629
630 if (hibernation_mode == HIBERNATION_PLATFORM)
631 flags |= SF_PLATFORM_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 pr_debug("PM: writing image.\n");
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700633 error = swsusp_write(flags);
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700634 swsusp_free();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (!error)
Johannes Bergfe0c9352007-04-30 15:09:51 -0700636 power_down();
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800637 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 pr_debug("PM: Image restored successfully.\n");
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800639 }
Rafael J. Wysocki64a473c2009-07-08 13:24:05 +0200640
Rafael J. Wysockib918f6e2006-11-02 22:07:19 -0800641 Thaw:
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100642 thaw_processes();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700643 Finish:
644 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700645 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700646 Exit:
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700647 pm_notifier_call_chain(PM_POST_HIBERNATION);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100648 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700649 atomic_inc(&snapshot_device_available);
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700650 Unlock:
651 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return error;
653}
654
655
656/**
657 * software_resume - Resume from a saved image.
658 *
659 * Called as a late_initcall (so all devices are discovered and
660 * initialized), we call swsusp to see if we have a saved image or not.
661 * If so, we quiesce devices, the restore the saved image. We will
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700662 * return above (in hibernate() ) if everything goes well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 * Otherwise, we fail gracefully and return to the normally
664 * scheduled program.
665 *
666 */
667
668static int software_resume(void)
669{
670 int error;
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700671 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Johannes Berg60a0d232007-11-14 17:00:16 -0800673 /*
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100674 * If the user said "noresume".. bail out early.
675 */
676 if (noresume)
677 return 0;
678
679 /*
Johannes Berg60a0d232007-11-14 17:00:16 -0800680 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
681 * is configured into the kernel. Since the regular hibernate
682 * trigger path is via sysfs which takes a buffer mutex before
683 * calling hibernate functions (which take pm_mutex) this can
684 * cause lockdep to complain about a possible ABBA deadlock
685 * which cannot happen since we're in the boot code here and
686 * sysfs can't be invoked yet. Therefore, we use a subclass
687 * here to avoid lockdep complaining.
688 */
689 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200690
691 if (swsusp_resume_device)
692 goto Check_image;
693
694 if (!strlen(resume_file)) {
695 error = -ENOENT;
696 goto Unlock;
697 }
698
699 pr_debug("PM: Checking image partition %s\n", resume_file);
700
701 /* Check if the device is there */
702 swsusp_resume_device = name_to_dev_t(resume_file);
Pavel Machek3efa1472005-07-07 17:56:43 -0700703 if (!swsusp_resume_device) {
Arjan van de Veneed3ee02009-02-14 02:00:19 +0100704 /*
705 * Some device discovery might still be in progress; we need
706 * to wait for this to finish.
707 */
708 wait_for_device_probe();
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200709 /*
710 * We can't depend on SCSI devices being available after loading
711 * one of their modules until scsi_complete_async_scans() is
712 * called and the resume device usually is a SCSI one.
713 */
714 scsi_complete_async_scans();
715
Pavel Machek3efa1472005-07-07 17:56:43 -0700716 swsusp_resume_device = name_to_dev_t(resume_file);
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200717 if (!swsusp_resume_device) {
718 error = -ENODEV;
719 goto Unlock;
720 }
Pavel Machek3efa1472005-07-07 17:56:43 -0700721 }
722
Rafael J. Wysocki0c8454f2009-04-25 00:16:06 +0200723 Check_image:
724 pr_debug("PM: Resume from partition %d:%d\n",
725 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100727 pr_debug("PM: Checking hibernation image.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800728 error = swsusp_check();
729 if (error)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700730 goto Unlock;
731
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700732 /* The snapshot device should not be opened while we're running */
733 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
734 error = -EBUSY;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200735 swsusp_close(FMODE_READ);
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700736 goto Unlock;
737 }
738
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100739 pm_prepare_console();
Alan Sternc3e94d82007-11-19 23:38:25 +0100740 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
741 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200742 goto close_finish;
Alan Sternc3e94d82007-11-19 23:38:25 +0100743
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700744 error = usermodehelper_disable();
745 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200746 goto close_finish;
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700747
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700748 error = create_basic_memory_bitmaps();
749 if (error)
Jiri Slaby76b57e62009-10-07 22:37:35 +0200750 goto close_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 pr_debug("PM: Preparing processes for restore.\n");
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800753 error = prepare_processes();
754 if (error) {
Al Viroc2dd0da2007-10-08 13:21:10 -0400755 swsusp_close(FMODE_READ);
Li Shaohua5a72e042005-06-25 14:55:06 -0700756 goto Done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100759 pr_debug("PM: Reading hibernation image.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700761 error = swsusp_read(&flags);
Jiri Slaby76b57e62009-10-07 22:37:35 +0200762 swsusp_close(FMODE_READ);
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800763 if (!error)
Rafael J. Wysockia634cc12007-07-19 01:47:30 -0700764 hibernation_restore(flags & SF_PLATFORM_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800766 printk(KERN_ERR "PM: Restore failed, recovering.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700767 swsusp_free();
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100768 thaw_processes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 Done:
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700770 free_basic_memory_bitmaps();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700771 usermodehelper_enable();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700772 Finish:
Alan Sternc3e94d82007-11-19 23:38:25 +0100773 pm_notifier_call_chain(PM_POST_RESTORE);
Rafael J. Wysocki5a0a2f32008-01-11 01:25:21 +0100774 pm_restore_console();
Rafael J. Wysocki0709db62007-05-06 14:50:45 -0700775 atomic_inc(&snapshot_device_available);
Shaohua Lidd5d6662005-09-03 15:57:04 -0700776 /* For success case, the suspend path will release the lock */
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700777 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800778 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 pr_debug("PM: Resume from disk failed.\n");
Rafael J. Wysocki7777fab2007-07-19 01:47:29 -0700780 return error;
Jiri Slaby76b57e62009-10-07 22:37:35 +0200781close_finish:
782 swsusp_close(FMODE_READ);
783 goto Finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
786late_initcall(software_resume);
787
788
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700789static const char * const hibernation_modes[] = {
790 [HIBERNATION_PLATFORM] = "platform",
791 [HIBERNATION_SHUTDOWN] = "shutdown",
792 [HIBERNATION_REBOOT] = "reboot",
793 [HIBERNATION_TEST] = "test",
794 [HIBERNATION_TESTPROC] = "testproc",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795};
796
797/**
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700798 * disk - Control hibernation mode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700800 * Suspend-to-disk can be handled in several ways. We have a few options
801 * for putting the system to sleep - using the platform driver (e.g. ACPI
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700802 * or other hibernation_ops), powering off the system or rebooting the
803 * system (for testing) as well as the two test modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700805 * The system can support 'platform', and that is known a priori (and
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700806 * encoded by the presence of hibernation_ops). However, the user may
807 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
808 * test modes, 'test' or 'testproc'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 *
810 * show() will display what the mode is currently set to.
811 * store() will accept one of
812 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 * 'platform'
814 * 'shutdown'
815 * 'reboot'
Johannes Berg11d77d02007-04-30 15:09:53 -0700816 * 'test'
817 * 'testproc'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 *
Johannes Berg11d77d02007-04-30 15:09:53 -0700819 * It will only change to 'platform' if the system
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700820 * supports it (as determined by having hibernation_ops).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 */
822
Kay Sievers386f2752007-11-02 13:47:53 +0100823static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
824 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700826 int i;
827 char *start = buf;
828
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700829 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
830 if (!hibernation_modes[i])
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700831 continue;
832 switch (i) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700833 case HIBERNATION_SHUTDOWN:
834 case HIBERNATION_REBOOT:
835 case HIBERNATION_TEST:
836 case HIBERNATION_TESTPROC:
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700837 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700838 case HIBERNATION_PLATFORM:
839 if (hibernation_ops)
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700840 break;
841 /* not a valid mode, continue with loop */
842 continue;
843 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700844 if (i == hibernation_mode)
845 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700846 else
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700847 buf += sprintf(buf, "%s ", hibernation_modes[i]);
Johannes Bergf0ced9b2007-05-06 14:50:50 -0700848 }
849 buf += sprintf(buf, "\n");
850 return buf-start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
853
Kay Sievers386f2752007-11-02 13:47:53 +0100854static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
855 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 int error = 0;
858 int i;
859 int len;
860 char *p;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700861 int mode = HIBERNATION_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 p = memchr(buf, '\n', n);
864 len = p ? p - buf : n;
865
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800866 mutex_lock(&pm_mutex);
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700867 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700868 if (len == strlen(hibernation_modes[i])
869 && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 mode = i;
871 break;
872 }
873 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700874 if (mode != HIBERNATION_INVALID) {
Johannes Bergfe0c9352007-04-30 15:09:51 -0700875 switch (mode) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700876 case HIBERNATION_SHUTDOWN:
877 case HIBERNATION_REBOOT:
878 case HIBERNATION_TEST:
879 case HIBERNATION_TESTPROC:
880 hibernation_mode = mode;
Johannes Bergfe0c9352007-04-30 15:09:51 -0700881 break;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700882 case HIBERNATION_PLATFORM:
883 if (hibernation_ops)
884 hibernation_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 else
886 error = -EINVAL;
887 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700888 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 error = -EINVAL;
890
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700891 if (!error)
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100892 pr_debug("PM: Hibernation mode set to '%s'\n",
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700893 hibernation_modes[mode]);
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800894 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return error ? error : n;
896}
897
898power_attr(disk);
899
Kay Sievers386f2752007-11-02 13:47:53 +0100900static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
901 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
904 MINOR(swsusp_resume_device));
905}
906
Kay Sievers386f2752007-11-02 13:47:53 +0100907static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
908 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 unsigned int maj, min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 dev_t res;
Andrew Mortona5762192006-01-06 00:09:50 -0800912 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Andrew Mortona5762192006-01-06 00:09:50 -0800914 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
915 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Andrew Mortona5762192006-01-06 00:09:50 -0800917 res = MKDEV(maj,min);
918 if (maj != MAJOR(res) || min != MINOR(res))
919 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800921 mutex_lock(&pm_mutex);
Andrew Mortona5762192006-01-06 00:09:50 -0800922 swsusp_resume_device = res;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800923 mutex_unlock(&pm_mutex);
Rafael J. Wysocki23976722007-12-08 02:09:43 +0100924 printk(KERN_INFO "PM: Starting manual resume from disk\n");
Andrew Mortona5762192006-01-06 00:09:50 -0800925 noresume = 0;
926 software_resume();
927 ret = n;
Rafael J. Wysocki59a493352006-12-06 20:34:44 -0800928 out:
Andrew Mortona5762192006-01-06 00:09:50 -0800929 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
932power_attr(resume);
933
Kay Sievers386f2752007-11-02 13:47:53 +0100934static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
935 char *buf)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800936{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800937 return sprintf(buf, "%lu\n", image_size);
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800938}
939
Kay Sievers386f2752007-11-02 13:47:53 +0100940static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
941 const char *buf, size_t n)
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800942{
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800943 unsigned long size;
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800944
Rafael J. Wysocki853609b2006-02-01 03:05:07 -0800945 if (sscanf(buf, "%lu", &size) == 1) {
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800946 image_size = size;
947 return n;
948 }
949
950 return -EINVAL;
951}
952
953power_attr(image_size);
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955static struct attribute * g[] = {
956 &disk_attr.attr,
957 &resume_attr.attr,
Rafael J. Wysockica0aec02006-01-06 00:15:56 -0800958 &image_size_attr.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 NULL,
960};
961
962
963static struct attribute_group attr_group = {
964 .attrs = g,
965};
966
967
968static int __init pm_disk_init(void)
969{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800970 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
973core_initcall(pm_disk_init);
974
975
976static int __init resume_setup(char *str)
977{
978 if (noresume)
979 return 1;
980
981 strncpy( resume_file, str, 255 );
982 return 1;
983}
984
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -0800985static int __init resume_offset_setup(char *str)
986{
987 unsigned long long offset;
988
989 if (noresume)
990 return 1;
991
992 if (sscanf(str, "%llu", &offset) == 1)
993 swsusp_resume_block = offset;
994
995 return 1;
996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998static int __init noresume_setup(char *str)
999{
1000 noresume = 1;
1001 return 1;
1002}
1003
1004__setup("noresume", noresume_setup);
Rafael J. Wysocki9a154d92006-12-06 20:34:12 -08001005__setup("resume_offset=", resume_offset_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006__setup("resume=", resume_setup);