]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - kernel/power/disk.c
swsusp: fix sysfs interface
[mirror_ubuntu-focal-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
48struct hibernation_ops *hibernation_ops;
49
50/**
51 * hibernation_set_ops - set the global hibernate operations
52 * @ops: the hibernation operations to use in subsequent hibernation transitions
53 */
54
55void hibernation_set_ops(struct hibernation_ops *ops)
56{
57 if (ops && !(ops->prepare && ops->enter && ops->finish)) {
58 WARN_ON(1);
59 return;
60 }
61 mutex_lock(&pm_mutex);
62 hibernation_ops = ops;
63 if (ops)
64 hibernation_mode = HIBERNATION_PLATFORM;
65 else if (hibernation_mode == HIBERNATION_PLATFORM)
66 hibernation_mode = HIBERNATION_SHUTDOWN;
67
68 mutex_unlock(&pm_mutex);
69}
70
71
8a05aac2
SS
72/**
73 * platform_prepare - prepare the machine for hibernation using the
74 * platform driver if so configured and return an error code if it fails
75 */
76
a3d25c27 77static int platform_prepare(void)
8a05aac2 78{
a3d25c27
RW
79 return (hibernation_mode == HIBERNATION_PLATFORM && hibernation_ops) ?
80 hibernation_ops->prepare() : 0;
81}
8a05aac2 82
a3d25c27
RW
83/**
84 * platform_finish - switch the machine to the normal mode of operation
85 * using the platform driver (must be called after platform_prepare())
86 */
87
88static void platform_finish(void)
89{
90 if (hibernation_mode == HIBERNATION_PLATFORM && hibernation_ops)
91 hibernation_ops->finish();
8a05aac2
SS
92}
93
1da177e4 94/**
a3d25c27 95 * power_down - Shut the machine down for hibernation.
1da177e4 96 *
fe0c935a
JB
97 * Use the platform driver, if configured so; otherwise try
98 * to power off or reboot.
1da177e4
LT
99 */
100
fe0c935a 101static void power_down(void)
1da177e4 102{
a3d25c27
RW
103 switch (hibernation_mode) {
104 case HIBERNATION_TEST:
105 case HIBERNATION_TESTPROC:
fe0c935a 106 break;
a3d25c27 107 case HIBERNATION_SHUTDOWN:
fdde86ac 108 kernel_power_off();
1da177e4 109 break;
a3d25c27 110 case HIBERNATION_REBOOT:
fdde86ac 111 kernel_restart(NULL);
1da177e4 112 break;
a3d25c27
RW
113 case HIBERNATION_PLATFORM:
114 if (hibernation_ops) {
fe0c935a 115 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
a3d25c27 116 hibernation_ops->enter();
fe0c935a
JB
117 break;
118 }
1da177e4 119 }
fdde86ac 120 kernel_halt();
fe0c935a
JB
121 /*
122 * Valid image is on the disk, if we continue we risk serious data
123 * corruption after resume.
124 */
1da177e4
LT
125 printk(KERN_CRIT "Please power me down manually\n");
126 while(1);
127}
128
ed746e3b
RW
129static void unprepare_processes(void)
130{
131 thaw_processes();
132 pm_restore_console();
133}
134
1da177e4
LT
135static int prepare_processes(void)
136{
b918f6e6 137 int error = 0;
1da177e4
LT
138
139 pm_prepare_console();
1da177e4
LT
140 if (freeze_processes()) {
141 error = -EBUSY;
ed746e3b 142 unprepare_processes();
1da177e4 143 }
5a72e04d 144 return error;
1da177e4
LT
145}
146
1da177e4 147/**
a3d25c27 148 * hibernate - The granpappy of the built-in hibernation management
1da177e4
LT
149 */
150
a3d25c27 151int hibernate(void)
1da177e4
LT
152{
153 int error;
154
0709db60
RW
155 /* The snapshot device should not be opened while we're running */
156 if (!atomic_add_unless(&snapshot_device_available, -1, 0))
157 return -EBUSY;
158
159 /* Allocate memory management structures */
160 error = create_basic_memory_bitmaps();
161 if (error)
162 goto Exit;
163
1da177e4 164 error = prepare_processes();
5a72e04d 165 if (error)
0709db60 166 goto Finish;
1da177e4 167
a3d25c27
RW
168 mutex_lock(&pm_mutex);
169 if (hibernation_mode == HIBERNATION_TESTPROC) {
ed746e3b
RW
170 printk("swsusp debug: Waiting for 5 seconds.\n");
171 mdelay(5000);
172 goto Thaw;
173 }
74dfd666 174
ed746e3b
RW
175 /* Free memory before shutting down devices. */
176 error = swsusp_shrink_memory();
177 if (error)
0709db60 178 goto Thaw;
ed746e3b
RW
179
180 error = platform_prepare();
181 if (error)
0709db60 182 goto Thaw;
b918f6e6 183
97c7801c 184 suspend_console();
99dc7d63 185 error = device_suspend(PMSG_FREEZE);
1da177e4 186 if (error) {
ed746e3b
RW
187 printk(KERN_ERR "PM: Some devices failed to suspend\n");
188 goto Resume_devices;
b918f6e6 189 }
ed746e3b
RW
190 error = disable_nonboot_cpus();
191 if (error)
192 goto Enable_cpus;
b918f6e6 193
a3d25c27 194 if (hibernation_mode == HIBERNATION_TEST) {
b918f6e6
RW
195 printk("swsusp debug: Waiting for 5 seconds.\n");
196 mdelay(5000);
ed746e3b 197 goto Enable_cpus;
1da177e4
LT
198 }
199
1da177e4
LT
200 pr_debug("PM: snapshotting memory.\n");
201 in_suspend = 1;
ed746e3b
RW
202 error = swsusp_suspend();
203 if (error)
204 goto Enable_cpus;
1da177e4
LT
205
206 if (in_suspend) {
ed746e3b
RW
207 enable_nonboot_cpus();
208 platform_finish();
0245b3e7 209 device_resume();
97c7801c 210 resume_console();
1da177e4 211 pr_debug("PM: writing image.\n");
f577eb30 212 error = swsusp_write();
1da177e4 213 if (!error)
fe0c935a 214 power_down();
99dc7d63 215 else {
99dc7d63 216 swsusp_free();
0709db60 217 goto Thaw;
99dc7d63 218 }
b918f6e6 219 } else {
1da177e4 220 pr_debug("PM: Image restored successfully.\n");
b918f6e6 221 }
99dc7d63 222
1da177e4 223 swsusp_free();
ed746e3b
RW
224 Enable_cpus:
225 enable_nonboot_cpus();
226 Resume_devices:
227 platform_finish();
99dc7d63 228 device_resume();
97c7801c 229 resume_console();
b918f6e6 230 Thaw:
a3d25c27 231 mutex_unlock(&pm_mutex);
99dc7d63 232 unprepare_processes();
0709db60
RW
233 Finish:
234 free_basic_memory_bitmaps();
235 Exit:
236 atomic_inc(&snapshot_device_available);
1da177e4
LT
237 return error;
238}
239
240
241/**
242 * software_resume - Resume from a saved image.
243 *
244 * Called as a late_initcall (so all devices are discovered and
245 * initialized), we call swsusp to see if we have a saved image or not.
246 * If so, we quiesce devices, the restore the saved image. We will
a3d25c27 247 * return above (in hibernate() ) if everything goes well.
1da177e4
LT
248 * Otherwise, we fail gracefully and return to the normally
249 * scheduled program.
250 *
251 */
252
253static int software_resume(void)
254{
255 int error;
256
a6d70980 257 mutex_lock(&pm_mutex);
3efa147a 258 if (!swsusp_resume_device) {
dd5d666b 259 if (!strlen(resume_file)) {
a6d70980 260 mutex_unlock(&pm_mutex);
3efa147a 261 return -ENOENT;
dd5d666b 262 }
3efa147a
PM
263 swsusp_resume_device = name_to_dev_t(resume_file);
264 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
265 } else {
266 pr_debug("swsusp: Resume From Partition %d:%d\n",
267 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
268 }
269
1da177e4
LT
270 if (noresume) {
271 /**
272 * FIXME: If noresume is specified, we need to find the partition
273 * and reset it back to normal swap space.
274 */
a6d70980 275 mutex_unlock(&pm_mutex);
1da177e4
LT
276 return 0;
277 }
278
279 pr_debug("PM: Checking swsusp image.\n");
ed746e3b
RW
280 error = swsusp_check();
281 if (error)
74dfd666 282 goto Unlock;
1da177e4 283
0709db60
RW
284 /* The snapshot device should not be opened while we're running */
285 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
286 error = -EBUSY;
287 goto Unlock;
288 }
289
74dfd666
RW
290 error = create_basic_memory_bitmaps();
291 if (error)
0709db60 292 goto Finish;
1da177e4 293
74dfd666 294 pr_debug("PM: Preparing processes for restore.\n");
ed746e3b
RW
295 error = prepare_processes();
296 if (error) {
1da177e4 297 swsusp_close();
5a72e04d 298 goto Done;
1da177e4
LT
299 }
300
301 pr_debug("PM: Reading swsusp image.\n");
302
ed746e3b
RW
303 error = swsusp_read();
304 if (error) {
2c1b4a5c
RW
305 swsusp_free();
306 goto Thaw;
307 }
1da177e4
LT
308
309 pr_debug("PM: Preparing devices for restore.\n");
310
97c7801c 311 suspend_console();
ed746e3b
RW
312 error = device_suspend(PMSG_PRETHAW);
313 if (error)
314 goto Free;
1da177e4 315
ed746e3b
RW
316 error = disable_nonboot_cpus();
317 if (!error)
318 swsusp_resume();
1da177e4 319
ed746e3b
RW
320 enable_nonboot_cpus();
321 Free:
322 swsusp_free();
99dc7d63 323 device_resume();
97c7801c 324 resume_console();
2c1b4a5c 325 Thaw:
ed746e3b 326 printk(KERN_ERR "PM: Restore failed, recovering.\n");
1da177e4
LT
327 unprepare_processes();
328 Done:
74dfd666 329 free_basic_memory_bitmaps();
0709db60
RW
330 Finish:
331 atomic_inc(&snapshot_device_available);
dd5d666b 332 /* For success case, the suspend path will release the lock */
74dfd666 333 Unlock:
a6d70980 334 mutex_unlock(&pm_mutex);
1da177e4
LT
335 pr_debug("PM: Resume from disk failed.\n");
336 return 0;
337}
338
339late_initcall(software_resume);
340
341
a3d25c27
RW
342static const char * const hibernation_modes[] = {
343 [HIBERNATION_PLATFORM] = "platform",
344 [HIBERNATION_SHUTDOWN] = "shutdown",
345 [HIBERNATION_REBOOT] = "reboot",
346 [HIBERNATION_TEST] = "test",
347 [HIBERNATION_TESTPROC] = "testproc",
1da177e4
LT
348};
349
350/**
a3d25c27 351 * disk - Control hibernation mode
1da177e4 352 *
11d77d0c
JB
353 * Suspend-to-disk can be handled in several ways. We have a few options
354 * for putting the system to sleep - using the platform driver (e.g. ACPI
a3d25c27
RW
355 * or other hibernation_ops), powering off the system or rebooting the
356 * system (for testing) as well as the two test modes.
1da177e4 357 *
11d77d0c 358 * The system can support 'platform', and that is known a priori (and
a3d25c27
RW
359 * encoded by the presence of hibernation_ops). However, the user may
360 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
361 * test modes, 'test' or 'testproc'.
1da177e4
LT
362 *
363 * show() will display what the mode is currently set to.
364 * store() will accept one of
365 *
1da177e4
LT
366 * 'platform'
367 * 'shutdown'
368 * 'reboot'
11d77d0c
JB
369 * 'test'
370 * 'testproc'
1da177e4 371 *
11d77d0c 372 * It will only change to 'platform' if the system
a3d25c27 373 * supports it (as determined by having hibernation_ops).
1da177e4
LT
374 */
375
823bccfc 376static ssize_t disk_show(struct kset *kset, char *buf)
1da177e4 377{
f0ced9b2
JB
378 int i;
379 char *start = buf;
380
a3d25c27
RW
381 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
382 if (!hibernation_modes[i])
f0ced9b2
JB
383 continue;
384 switch (i) {
a3d25c27
RW
385 case HIBERNATION_SHUTDOWN:
386 case HIBERNATION_REBOOT:
387 case HIBERNATION_TEST:
388 case HIBERNATION_TESTPROC:
f0ced9b2 389 break;
a3d25c27
RW
390 case HIBERNATION_PLATFORM:
391 if (hibernation_ops)
f0ced9b2
JB
392 break;
393 /* not a valid mode, continue with loop */
394 continue;
395 }
a3d25c27
RW
396 if (i == hibernation_mode)
397 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
f0ced9b2 398 else
a3d25c27 399 buf += sprintf(buf, "%s ", hibernation_modes[i]);
f0ced9b2
JB
400 }
401 buf += sprintf(buf, "\n");
402 return buf-start;
1da177e4
LT
403}
404
405
823bccfc 406static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
1da177e4
LT
407{
408 int error = 0;
409 int i;
410 int len;
411 char *p;
a3d25c27 412 int mode = HIBERNATION_INVALID;
1da177e4
LT
413
414 p = memchr(buf, '\n', n);
415 len = p ? p - buf : n;
416
a6d70980 417 mutex_lock(&pm_mutex);
a3d25c27 418 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
8d98a690
RW
419 if (len == strlen(hibernation_modes[i])
420 && !strncmp(buf, hibernation_modes[i], len)) {
1da177e4
LT
421 mode = i;
422 break;
423 }
424 }
a3d25c27 425 if (mode != HIBERNATION_INVALID) {
fe0c935a 426 switch (mode) {
a3d25c27
RW
427 case HIBERNATION_SHUTDOWN:
428 case HIBERNATION_REBOOT:
429 case HIBERNATION_TEST:
430 case HIBERNATION_TESTPROC:
431 hibernation_mode = mode;
fe0c935a 432 break;
a3d25c27
RW
433 case HIBERNATION_PLATFORM:
434 if (hibernation_ops)
435 hibernation_mode = mode;
1da177e4
LT
436 else
437 error = -EINVAL;
438 }
a3d25c27 439 } else
1da177e4
LT
440 error = -EINVAL;
441
a3d25c27
RW
442 if (!error)
443 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
444 hibernation_modes[mode]);
a6d70980 445 mutex_unlock(&pm_mutex);
1da177e4
LT
446 return error ? error : n;
447}
448
449power_attr(disk);
450
823bccfc 451static ssize_t resume_show(struct kset *kset, char *buf)
1da177e4
LT
452{
453 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
454 MINOR(swsusp_resume_device));
455}
456
823bccfc 457static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
1da177e4 458{
1da177e4 459 unsigned int maj, min;
1da177e4 460 dev_t res;
a576219a 461 int ret = -EINVAL;
1da177e4 462
a576219a
AM
463 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
464 goto out;
1da177e4 465
a576219a
AM
466 res = MKDEV(maj,min);
467 if (maj != MAJOR(res) || min != MINOR(res))
468 goto out;
1da177e4 469
a6d70980 470 mutex_lock(&pm_mutex);
a576219a 471 swsusp_resume_device = res;
a6d70980 472 mutex_unlock(&pm_mutex);
a576219a
AM
473 printk("Attempting manual resume\n");
474 noresume = 0;
475 software_resume();
476 ret = n;
59a49335 477 out:
a576219a 478 return ret;
1da177e4
LT
479}
480
481power_attr(resume);
482
823bccfc 483static ssize_t image_size_show(struct kset *kset, char *buf)
ca0aec0f 484{
853609b6 485 return sprintf(buf, "%lu\n", image_size);
ca0aec0f
RW
486}
487
823bccfc 488static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
ca0aec0f 489{
853609b6 490 unsigned long size;
ca0aec0f 491
853609b6 492 if (sscanf(buf, "%lu", &size) == 1) {
ca0aec0f
RW
493 image_size = size;
494 return n;
495 }
496
497 return -EINVAL;
498}
499
500power_attr(image_size);
501
1da177e4
LT
502static struct attribute * g[] = {
503 &disk_attr.attr,
504 &resume_attr.attr,
ca0aec0f 505 &image_size_attr.attr,
1da177e4
LT
506 NULL,
507};
508
509
510static struct attribute_group attr_group = {
511 .attrs = g,
512};
513
514
515static int __init pm_disk_init(void)
516{
823bccfc 517 return sysfs_create_group(&power_subsys.kobj, &attr_group);
1da177e4
LT
518}
519
520core_initcall(pm_disk_init);
521
522
523static int __init resume_setup(char *str)
524{
525 if (noresume)
526 return 1;
527
528 strncpy( resume_file, str, 255 );
529 return 1;
530}
531
9a154d9d
RW
532static int __init resume_offset_setup(char *str)
533{
534 unsigned long long offset;
535
536 if (noresume)
537 return 1;
538
539 if (sscanf(str, "%llu", &offset) == 1)
540 swsusp_resume_block = offset;
541
542 return 1;
543}
544
1da177e4
LT
545static int __init noresume_setup(char *str)
546{
547 noresume = 1;
548 return 1;
549}
550
551__setup("noresume", noresume_setup);
9a154d9d 552__setup("resume_offset=", resume_offset_setup);
1da177e4 553__setup("resume=", resume_setup);