]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/power/disk.c
Hibernation: Use temporary page tables for kernel text mapping on x86_64
[mirror_ubuntu-artful-kernel.git] / kernel / power / disk.c
CommitLineData
1da177e4
LT
1/*
2 * kernel/power/disk.c - Suspend-to-disk support.
3 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 * Copyright (c) 2004 Pavel Machek <pavel@suse.cz>
7 *
8 * This file is released under the GPLv2.
9 *
10 */
11
12#include <linux/suspend.h>
13#include <linux/syscalls.h>
14#include <linux/reboot.h>
15#include <linux/string.h>
16#include <linux/device.h>
17#include <linux/delay.h>
18#include <linux/fs.h>
d53d9f16 19#include <linux/mount.h>
88d10bba 20#include <linux/pm.h>
97c7801c 21#include <linux/console.h>
e3920fb4 22#include <linux/cpu.h>
7dfb7103 23#include <linux/freezer.h>
d53d9f16 24
1da177e4
LT
25#include "power.h"
26
27
1da177e4
LT
28static int noresume = 0;
29char resume_file[256] = CONFIG_PM_STD_PARTITION;
30dev_t swsusp_resume_device;
9a154d9d 31sector_t swsusp_resume_block;
1da177e4 32
a3d25c27
RW
33enum {
34 HIBERNATION_INVALID,
35 HIBERNATION_PLATFORM,
36 HIBERNATION_TEST,
37 HIBERNATION_TESTPROC,
38 HIBERNATION_SHUTDOWN,
39 HIBERNATION_REBOOT,
40 /* keep last */
41 __HIBERNATION_AFTER_LAST
42};
43#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
44#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
45
46static int hibernation_mode = HIBERNATION_SHUTDOWN;
47
b3dac3b3 48static struct platform_hibernation_ops *hibernation_ops;
a3d25c27
RW
49
50/**
51 * hibernation_set_ops - set the global hibernate operations
52 * @ops: the hibernation operations to use in subsequent hibernation transitions
53 */
54
b3dac3b3 55void hibernation_set_ops(struct platform_hibernation_ops *ops)
a3d25c27 56{
74f270af
RW
57 if (ops && !(ops->start && ops->pre_snapshot && ops->finish
58 && ops->prepare && ops->enter && ops->pre_restore
59 && ops->restore_cleanup)) {
a3d25c27
RW
60 WARN_ON(1);
61 return;
62 }
63 mutex_lock(&pm_mutex);
64 hibernation_ops = ops;
65 if (ops)
66 hibernation_mode = HIBERNATION_PLATFORM;
67 else if (hibernation_mode == HIBERNATION_PLATFORM)
68 hibernation_mode = HIBERNATION_SHUTDOWN;
69
70 mutex_unlock(&pm_mutex);
71}
72
74f270af
RW
73/**
74 * platform_start - tell the platform driver that we're starting
75 * hibernation
76 */
77
78static int platform_start(int platform_mode)
79{
80 return (platform_mode && hibernation_ops) ?
81 hibernation_ops->start() : 0;
82}
a3d25c27 83
8a05aac2 84/**
74f270af 85 * platform_pre_snapshot - prepare the machine for hibernation using the
8a05aac2
SS
86 * platform driver if so configured and return an error code if it fails
87 */
88
74f270af 89static int platform_pre_snapshot(int platform_mode)
8a05aac2 90{
7777fab9 91 return (platform_mode && hibernation_ops) ?
74f270af 92 hibernation_ops->pre_snapshot() : 0;
a3d25c27 93}
8a05aac2 94
a3d25c27
RW
95/**
96 * platform_finish - switch the machine to the normal mode of operation
97 * using the platform driver (must be called after platform_prepare())
98 */
99
7777fab9 100static void platform_finish(int platform_mode)
a3d25c27 101{
7777fab9 102 if (platform_mode && hibernation_ops)
a3d25c27 103 hibernation_ops->finish();
8a05aac2
SS
104}
105
a634cc10
RW
106/**
107 * platform_pre_restore - prepare the platform for the restoration from a
108 * hibernation image. If the restore fails after this function has been
109 * called, platform_restore_cleanup() must be called.
110 */
111
112static int platform_pre_restore(int platform_mode)
113{
114 return (platform_mode && hibernation_ops) ?
115 hibernation_ops->pre_restore() : 0;
116}
117
118/**
119 * platform_restore_cleanup - switch the platform to the normal mode of
120 * operation after a failing restore. If platform_pre_restore() has been
121 * called before the failing restore, this function must be called too,
122 * regardless of the result of platform_pre_restore().
123 */
124
125static void platform_restore_cleanup(int platform_mode)
126{
127 if (platform_mode && hibernation_ops)
128 hibernation_ops->restore_cleanup();
129}
130
7777fab9
RW
131/**
132 * hibernation_snapshot - quiesce devices and create the hibernation
133 * snapshot image.
134 * @platform_mode - if set, use the platform driver, if available, to
135 * prepare the platform frimware for the power transition.
136 *
137 * Must be called with pm_mutex held
138 */
139
140int hibernation_snapshot(int platform_mode)
141{
142 int error;
143
144 /* Free memory before shutting down devices. */
145 error = swsusp_shrink_memory();
146 if (error)
10a1803d 147 return error;
7777fab9 148
74f270af
RW
149 error = platform_start(platform_mode);
150 if (error)
151 return error;
152
7777fab9
RW
153 suspend_console();
154 error = device_suspend(PMSG_FREEZE);
10a1803d
RW
155 if (error)
156 goto Resume_console;
157
74f270af 158 error = platform_pre_snapshot(platform_mode);
7777fab9
RW
159 if (error)
160 goto Resume_devices;
161
162 error = disable_nonboot_cpus();
163 if (!error) {
164 if (hibernation_mode != HIBERNATION_TEST) {
165 in_suspend = 1;
166 error = swsusp_suspend();
167 /* Control returns here after successful restore */
168 } else {
169 printk("swsusp debug: Waiting for 5 seconds.\n");
170 mdelay(5000);
171 }
172 }
173 enable_nonboot_cpus();
174 Resume_devices:
175 platform_finish(platform_mode);
176 device_resume();
10a1803d 177 Resume_console:
7777fab9 178 resume_console();
7777fab9
RW
179 return error;
180}
181
182/**
183 * hibernation_restore - quiesce devices and restore the hibernation
184 * snapshot image. If successful, control returns in hibernation_snaphot()
a634cc10
RW
185 * @platform_mode - if set, use the platform driver, if available, to
186 * prepare the platform frimware for the transition.
7777fab9
RW
187 *
188 * Must be called with pm_mutex held
189 */
190
a634cc10 191int hibernation_restore(int platform_mode)
7777fab9
RW
192{
193 int error;
194
195 pm_prepare_console();
196 suspend_console();
197 error = device_suspend(PMSG_PRETHAW);
198 if (error)
199 goto Finish;
200
a634cc10
RW
201 error = platform_pre_restore(platform_mode);
202 if (!error) {
203 error = disable_nonboot_cpus();
204 if (!error)
205 error = swsusp_resume();
206 enable_nonboot_cpus();
207 }
208 platform_restore_cleanup(platform_mode);
7777fab9 209 device_resume();
10a1803d 210 Finish:
7777fab9
RW
211 resume_console();
212 pm_restore_console();
213 return error;
214}
215
216/**
217 * hibernation_platform_enter - enter the hibernation state using the
218 * platform driver (if available)
219 */
220
221int hibernation_platform_enter(void)
222{
b1457bcc
RW
223 int error;
224
7777fab9
RW
225 if (hibernation_ops) {
226 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
b1457bcc
RW
227 /*
228 * We have cancelled the power transition by running
229 * hibernation_ops->finish() before saving the image, so we
230 * should let the firmware know that we're going to enter the
231 * sleep state after all
232 */
233 error = hibernation_ops->prepare();
58b3b71d 234 sysdev_shutdown();
b1457bcc
RW
235 if (!error)
236 error = hibernation_ops->enter();
7777fab9 237 } else {
b1457bcc 238 error = -ENOSYS;
7777fab9 239 }
b1457bcc 240 return error;
7777fab9
RW
241}
242
1da177e4 243/**
a3d25c27 244 * power_down - Shut the machine down for hibernation.
1da177e4 245 *
fe0c935a
JB
246 * Use the platform driver, if configured so; otherwise try
247 * to power off or reboot.
1da177e4
LT
248 */
249
fe0c935a 250static void power_down(void)
1da177e4 251{
a3d25c27
RW
252 switch (hibernation_mode) {
253 case HIBERNATION_TEST:
254 case HIBERNATION_TESTPROC:
fe0c935a 255 break;
a3d25c27 256 case HIBERNATION_SHUTDOWN:
fdde86ac 257 kernel_power_off();
1da177e4 258 break;
a3d25c27 259 case HIBERNATION_REBOOT:
fdde86ac 260 kernel_restart(NULL);
1da177e4 261 break;
a3d25c27 262 case HIBERNATION_PLATFORM:
7777fab9 263 hibernation_platform_enter();
1da177e4 264 }
fdde86ac 265 kernel_halt();
fe0c935a
JB
266 /*
267 * Valid image is on the disk, if we continue we risk serious data
268 * corruption after resume.
269 */
1da177e4
LT
270 printk(KERN_CRIT "Please power me down manually\n");
271 while(1);
272}
273
ed746e3b
RW
274static void unprepare_processes(void)
275{
276 thaw_processes();
277 pm_restore_console();
278}
279
1da177e4
LT
280static int prepare_processes(void)
281{
b918f6e6 282 int error = 0;
1da177e4
LT
283
284 pm_prepare_console();
1da177e4
LT
285 if (freeze_processes()) {
286 error = -EBUSY;
ed746e3b 287 unprepare_processes();
1da177e4 288 }
5a72e04d 289 return error;
1da177e4
LT
290}
291
1da177e4 292/**
a3d25c27 293 * hibernate - The granpappy of the built-in hibernation management
1da177e4
LT
294 */
295
a3d25c27 296int hibernate(void)
1da177e4
LT
297{
298 int error;
299
b10d9117 300 mutex_lock(&pm_mutex);
0709db60 301 /* The snapshot device should not be opened while we're running */
b10d9117
RW
302 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
303 error = -EBUSY;
304 goto Unlock;
305 }
306
307 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
308 if (error)
309 goto Exit;
0709db60
RW
310
311 /* Allocate memory management structures */
312 error = create_basic_memory_bitmaps();
313 if (error)
314 goto Exit;
315
232b1432
RW
316 printk("Syncing filesystems ... ");
317 sys_sync();
318 printk("done.\n");
319
1da177e4 320 error = prepare_processes();
5a72e04d 321 if (error)
0709db60 322 goto Finish;
1da177e4 323
a3d25c27 324 if (hibernation_mode == HIBERNATION_TESTPROC) {
ed746e3b
RW
325 printk("swsusp debug: Waiting for 5 seconds.\n");
326 mdelay(5000);
327 goto Thaw;
328 }
7777fab9
RW
329 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
330 if (in_suspend && !error) {
a634cc10
RW
331 unsigned int flags = 0;
332
333 if (hibernation_mode == HIBERNATION_PLATFORM)
334 flags |= SF_PLATFORM_MODE;
1da177e4 335 pr_debug("PM: writing image.\n");
a634cc10 336 error = swsusp_write(flags);
7777fab9 337 swsusp_free();
1da177e4 338 if (!error)
fe0c935a 339 power_down();
b918f6e6 340 } else {
1da177e4 341 pr_debug("PM: Image restored successfully.\n");
7777fab9 342 swsusp_free();
b918f6e6 343 }
b918f6e6 344 Thaw:
99dc7d63 345 unprepare_processes();
0709db60
RW
346 Finish:
347 free_basic_memory_bitmaps();
348 Exit:
b10d9117 349 pm_notifier_call_chain(PM_POST_HIBERNATION);
0709db60 350 atomic_inc(&snapshot_device_available);
b10d9117
RW
351 Unlock:
352 mutex_unlock(&pm_mutex);
1da177e4
LT
353 return error;
354}
355
356
357/**
358 * software_resume - Resume from a saved image.
359 *
360 * Called as a late_initcall (so all devices are discovered and
361 * initialized), we call swsusp to see if we have a saved image or not.
362 * If so, we quiesce devices, the restore the saved image. We will
a3d25c27 363 * return above (in hibernate() ) if everything goes well.
1da177e4
LT
364 * Otherwise, we fail gracefully and return to the normally
365 * scheduled program.
366 *
367 */
368
369static int software_resume(void)
370{
371 int error;
a634cc10 372 unsigned int flags;
1da177e4 373
a6d70980 374 mutex_lock(&pm_mutex);
3efa147a 375 if (!swsusp_resume_device) {
dd5d666b 376 if (!strlen(resume_file)) {
a6d70980 377 mutex_unlock(&pm_mutex);
3efa147a 378 return -ENOENT;
dd5d666b 379 }
3efa147a
PM
380 swsusp_resume_device = name_to_dev_t(resume_file);
381 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
382 } else {
383 pr_debug("swsusp: Resume From Partition %d:%d\n",
384 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
385 }
386
1da177e4
LT
387 if (noresume) {
388 /**
389 * FIXME: If noresume is specified, we need to find the partition
390 * and reset it back to normal swap space.
391 */
a6d70980 392 mutex_unlock(&pm_mutex);
1da177e4
LT
393 return 0;
394 }
395
396 pr_debug("PM: Checking swsusp image.\n");
ed746e3b
RW
397 error = swsusp_check();
398 if (error)
74dfd666 399 goto Unlock;
1da177e4 400
0709db60
RW
401 /* The snapshot device should not be opened while we're running */
402 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
403 error = -EBUSY;
404 goto Unlock;
405 }
406
74dfd666
RW
407 error = create_basic_memory_bitmaps();
408 if (error)
0709db60 409 goto Finish;
1da177e4 410
74dfd666 411 pr_debug("PM: Preparing processes for restore.\n");
ed746e3b
RW
412 error = prepare_processes();
413 if (error) {
1da177e4 414 swsusp_close();
5a72e04d 415 goto Done;
1da177e4
LT
416 }
417
418 pr_debug("PM: Reading swsusp image.\n");
419
a634cc10 420 error = swsusp_read(&flags);
ed746e3b 421 if (!error)
a634cc10 422 hibernation_restore(flags & SF_PLATFORM_MODE);
1da177e4 423
ed746e3b 424 printk(KERN_ERR "PM: Restore failed, recovering.\n");
7777fab9 425 swsusp_free();
1da177e4
LT
426 unprepare_processes();
427 Done:
74dfd666 428 free_basic_memory_bitmaps();
0709db60
RW
429 Finish:
430 atomic_inc(&snapshot_device_available);
dd5d666b 431 /* For success case, the suspend path will release the lock */
74dfd666 432 Unlock:
a6d70980 433 mutex_unlock(&pm_mutex);
1da177e4 434 pr_debug("PM: Resume from disk failed.\n");
7777fab9 435 return error;
1da177e4
LT
436}
437
438late_initcall(software_resume);
439
440
a3d25c27
RW
441static const char * const hibernation_modes[] = {
442 [HIBERNATION_PLATFORM] = "platform",
443 [HIBERNATION_SHUTDOWN] = "shutdown",
444 [HIBERNATION_REBOOT] = "reboot",
445 [HIBERNATION_TEST] = "test",
446 [HIBERNATION_TESTPROC] = "testproc",
1da177e4
LT
447};
448
449/**
a3d25c27 450 * disk - Control hibernation mode
1da177e4 451 *
11d77d0c
JB
452 * Suspend-to-disk can be handled in several ways. We have a few options
453 * for putting the system to sleep - using the platform driver (e.g. ACPI
a3d25c27
RW
454 * or other hibernation_ops), powering off the system or rebooting the
455 * system (for testing) as well as the two test modes.
1da177e4 456 *
11d77d0c 457 * The system can support 'platform', and that is known a priori (and
a3d25c27
RW
458 * encoded by the presence of hibernation_ops). However, the user may
459 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
460 * test modes, 'test' or 'testproc'.
1da177e4
LT
461 *
462 * show() will display what the mode is currently set to.
463 * store() will accept one of
464 *
1da177e4
LT
465 * 'platform'
466 * 'shutdown'
467 * 'reboot'
11d77d0c
JB
468 * 'test'
469 * 'testproc'
1da177e4 470 *
11d77d0c 471 * It will only change to 'platform' if the system
a3d25c27 472 * supports it (as determined by having hibernation_ops).
1da177e4
LT
473 */
474
823bccfc 475static ssize_t disk_show(struct kset *kset, char *buf)
1da177e4 476{
f0ced9b2
JB
477 int i;
478 char *start = buf;
479
a3d25c27
RW
480 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
481 if (!hibernation_modes[i])
f0ced9b2
JB
482 continue;
483 switch (i) {
a3d25c27
RW
484 case HIBERNATION_SHUTDOWN:
485 case HIBERNATION_REBOOT:
486 case HIBERNATION_TEST:
487 case HIBERNATION_TESTPROC:
f0ced9b2 488 break;
a3d25c27
RW
489 case HIBERNATION_PLATFORM:
490 if (hibernation_ops)
f0ced9b2
JB
491 break;
492 /* not a valid mode, continue with loop */
493 continue;
494 }
a3d25c27
RW
495 if (i == hibernation_mode)
496 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
f0ced9b2 497 else
a3d25c27 498 buf += sprintf(buf, "%s ", hibernation_modes[i]);
f0ced9b2
JB
499 }
500 buf += sprintf(buf, "\n");
501 return buf-start;
1da177e4
LT
502}
503
504
823bccfc 505static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
1da177e4
LT
506{
507 int error = 0;
508 int i;
509 int len;
510 char *p;
a3d25c27 511 int mode = HIBERNATION_INVALID;
1da177e4
LT
512
513 p = memchr(buf, '\n', n);
514 len = p ? p - buf : n;
515
a6d70980 516 mutex_lock(&pm_mutex);
a3d25c27 517 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
8d98a690
RW
518 if (len == strlen(hibernation_modes[i])
519 && !strncmp(buf, hibernation_modes[i], len)) {
1da177e4
LT
520 mode = i;
521 break;
522 }
523 }
a3d25c27 524 if (mode != HIBERNATION_INVALID) {
fe0c935a 525 switch (mode) {
a3d25c27
RW
526 case HIBERNATION_SHUTDOWN:
527 case HIBERNATION_REBOOT:
528 case HIBERNATION_TEST:
529 case HIBERNATION_TESTPROC:
530 hibernation_mode = mode;
fe0c935a 531 break;
a3d25c27
RW
532 case HIBERNATION_PLATFORM:
533 if (hibernation_ops)
534 hibernation_mode = mode;
1da177e4
LT
535 else
536 error = -EINVAL;
537 }
a3d25c27 538 } else
1da177e4
LT
539 error = -EINVAL;
540
a3d25c27
RW
541 if (!error)
542 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
543 hibernation_modes[mode]);
a6d70980 544 mutex_unlock(&pm_mutex);
1da177e4
LT
545 return error ? error : n;
546}
547
548power_attr(disk);
549
823bccfc 550static ssize_t resume_show(struct kset *kset, char *buf)
1da177e4
LT
551{
552 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
553 MINOR(swsusp_resume_device));
554}
555
823bccfc 556static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
1da177e4 557{
1da177e4 558 unsigned int maj, min;
1da177e4 559 dev_t res;
a576219a 560 int ret = -EINVAL;
1da177e4 561
a576219a
AM
562 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
563 goto out;
1da177e4 564
a576219a
AM
565 res = MKDEV(maj,min);
566 if (maj != MAJOR(res) || min != MINOR(res))
567 goto out;
1da177e4 568
a6d70980 569 mutex_lock(&pm_mutex);
a576219a 570 swsusp_resume_device = res;
a6d70980 571 mutex_unlock(&pm_mutex);
a576219a
AM
572 printk("Attempting manual resume\n");
573 noresume = 0;
574 software_resume();
575 ret = n;
59a49335 576 out:
a576219a 577 return ret;
1da177e4
LT
578}
579
580power_attr(resume);
581
823bccfc 582static ssize_t image_size_show(struct kset *kset, char *buf)
ca0aec0f 583{
853609b6 584 return sprintf(buf, "%lu\n", image_size);
ca0aec0f
RW
585}
586
823bccfc 587static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
ca0aec0f 588{
853609b6 589 unsigned long size;
ca0aec0f 590
853609b6 591 if (sscanf(buf, "%lu", &size) == 1) {
ca0aec0f
RW
592 image_size = size;
593 return n;
594 }
595
596 return -EINVAL;
597}
598
599power_attr(image_size);
600
1da177e4
LT
601static struct attribute * g[] = {
602 &disk_attr.attr,
603 &resume_attr.attr,
ca0aec0f 604 &image_size_attr.attr,
1da177e4
LT
605 NULL,
606};
607
608
609static struct attribute_group attr_group = {
610 .attrs = g,
611};
612
613
614static int __init pm_disk_init(void)
615{
823bccfc 616 return sysfs_create_group(&power_subsys.kobj, &attr_group);
1da177e4
LT
617}
618
619core_initcall(pm_disk_init);
620
621
622static int __init resume_setup(char *str)
623{
624 if (noresume)
625 return 1;
626
627 strncpy( resume_file, str, 255 );
628 return 1;
629}
630
9a154d9d
RW
631static int __init resume_offset_setup(char *str)
632{
633 unsigned long long offset;
634
635 if (noresume)
636 return 1;
637
638 if (sscanf(str, "%llu", &offset) == 1)
639 swsusp_resume_block = offset;
640
641 return 1;
642}
643
1da177e4
LT
644static int __init noresume_setup(char *str)
645{
646 noresume = 1;
647 return 1;
648}
649
650__setup("noresume", noresume_setup);
9a154d9d 651__setup("resume_offset=", resume_offset_setup);
1da177e4 652__setup("resume=", resume_setup);