Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Andrew Morton | d53d9f1 | 2005-07-12 13:58:07 -0700 | [diff] [blame] | 19 | #include <linux/mount.h> |
Eric W. Biederman | 88d10bb | 2005-09-22 21:43:46 -0700 | [diff] [blame] | 20 | #include <linux/pm.h> |
Rafael J. Wysocki | 97c7801 | 2006-10-11 01:20:45 -0700 | [diff] [blame] | 21 | #include <linux/console.h> |
Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 22 | #include <linux/cpu.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 23 | #include <linux/freezer.h> |
Andrew Morton | d53d9f1 | 2005-07-12 13:58:07 -0700 | [diff] [blame] | 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "power.h" |
| 26 | |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | static int noresume = 0; |
| 29 | char resume_file[256] = CONFIG_PM_STD_PARTITION; |
| 30 | dev_t swsusp_resume_device; |
Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 31 | sector_t swsusp_resume_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
Stefan Seyfried | 8a05aac | 2006-12-06 20:34:21 -0800 | [diff] [blame] | 34 | * platform_prepare - prepare the machine for hibernation using the |
| 35 | * platform driver if so configured and return an error code if it fails |
| 36 | */ |
| 37 | |
| 38 | static inline int platform_prepare(void) |
| 39 | { |
| 40 | int error = 0; |
| 41 | |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 42 | switch (pm_disk_mode) { |
| 43 | case PM_DISK_TEST: |
| 44 | case PM_DISK_TESTPROC: |
| 45 | case PM_DISK_SHUTDOWN: |
| 46 | case PM_DISK_REBOOT: |
| 47 | break; |
| 48 | default: |
Stefan Seyfried | 8a05aac | 2006-12-06 20:34:21 -0800 | [diff] [blame] | 49 | if (pm_ops && pm_ops->prepare) |
| 50 | error = pm_ops->prepare(PM_SUSPEND_DISK); |
| 51 | } |
| 52 | return error; |
| 53 | } |
| 54 | |
| 55 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | * power_down - Shut machine down for hibernate. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | * |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 58 | * Use the platform driver, if configured so; otherwise try |
| 59 | * to power off or reboot. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | */ |
| 61 | |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 62 | static void power_down(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 64 | switch (pm_disk_mode) { |
| 65 | case PM_DISK_TEST: |
| 66 | case PM_DISK_TESTPROC: |
| 67 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | case PM_DISK_SHUTDOWN: |
Eric W. Biederman | fdde86a | 2005-07-26 12:01:17 -0600 | [diff] [blame] | 69 | kernel_power_off(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | break; |
| 71 | case PM_DISK_REBOOT: |
Eric W. Biederman | fdde86a | 2005-07-26 12:01:17 -0600 | [diff] [blame] | 72 | kernel_restart(NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | break; |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 74 | default: |
| 75 | if (pm_ops && pm_ops->enter) { |
| 76 | kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK); |
| 77 | pm_ops->enter(PM_SUSPEND_DISK); |
| 78 | break; |
| 79 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
Eric W. Biederman | fdde86a | 2005-07-26 12:01:17 -0600 | [diff] [blame] | 81 | kernel_halt(); |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 82 | /* |
| 83 | * Valid image is on the disk, if we continue we risk serious data |
| 84 | * corruption after resume. |
| 85 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | printk(KERN_CRIT "Please power me down manually\n"); |
| 87 | while(1); |
| 88 | } |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | static inline void platform_finish(void) |
| 91 | { |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 92 | switch (pm_disk_mode) { |
| 93 | case PM_DISK_TEST: |
| 94 | case PM_DISK_TESTPROC: |
| 95 | case PM_DISK_SHUTDOWN: |
| 96 | case PM_DISK_REBOOT: |
| 97 | break; |
| 98 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | if (pm_ops && pm_ops->finish) |
| 100 | pm_ops->finish(PM_SUSPEND_DISK); |
| 101 | } |
| 102 | } |
| 103 | |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 104 | static void unprepare_processes(void) |
| 105 | { |
| 106 | thaw_processes(); |
| 107 | pm_restore_console(); |
| 108 | } |
| 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | static int prepare_processes(void) |
| 111 | { |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 112 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | pm_prepare_console(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | if (freeze_processes()) { |
| 116 | error = -EBUSY; |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 117 | unprepare_processes(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 119 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | /** |
David Brownell | f1cc0a8 | 2006-08-14 23:11:08 -0700 | [diff] [blame] | 123 | * pm_suspend_disk - The granpappy of hibernation power management. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | * If not, then call swsusp to do its thing, then figure out how |
| 126 | * to power down the system. |
| 127 | */ |
| 128 | |
| 129 | int pm_suspend_disk(void) |
| 130 | { |
| 131 | int error; |
| 132 | |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame^] | 133 | /* The snapshot device should not be opened while we're running */ |
| 134 | if (!atomic_add_unless(&snapshot_device_available, -1, 0)) |
| 135 | return -EBUSY; |
| 136 | |
| 137 | /* Allocate memory management structures */ |
| 138 | error = create_basic_memory_bitmaps(); |
| 139 | if (error) |
| 140 | goto Exit; |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | error = prepare_processes(); |
Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 143 | if (error) |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame^] | 144 | goto Finish; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 146 | if (pm_disk_mode == PM_DISK_TESTPROC) { |
| 147 | printk("swsusp debug: Waiting for 5 seconds.\n"); |
| 148 | mdelay(5000); |
| 149 | goto Thaw; |
| 150 | } |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 151 | |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 152 | /* Free memory before shutting down devices. */ |
| 153 | error = swsusp_shrink_memory(); |
| 154 | if (error) |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame^] | 155 | goto Thaw; |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 156 | |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 157 | error = platform_prepare(); |
| 158 | if (error) |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame^] | 159 | goto Thaw; |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 160 | |
Rafael J. Wysocki | 97c7801 | 2006-10-11 01:20:45 -0700 | [diff] [blame] | 161 | suspend_console(); |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 162 | error = device_suspend(PMSG_FREEZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | if (error) { |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 164 | printk(KERN_ERR "PM: Some devices failed to suspend\n"); |
| 165 | goto Resume_devices; |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 166 | } |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 167 | error = disable_nonboot_cpus(); |
| 168 | if (error) |
| 169 | goto Enable_cpus; |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 170 | |
| 171 | if (pm_disk_mode == PM_DISK_TEST) { |
| 172 | printk("swsusp debug: Waiting for 5 seconds.\n"); |
| 173 | mdelay(5000); |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 174 | goto Enable_cpus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | pr_debug("PM: snapshotting memory.\n"); |
| 178 | in_suspend = 1; |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 179 | error = swsusp_suspend(); |
| 180 | if (error) |
| 181 | goto Enable_cpus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
| 183 | if (in_suspend) { |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 184 | enable_nonboot_cpus(); |
| 185 | platform_finish(); |
Rafael J. Wysocki | 0245b3e | 2005-10-30 15:00:01 -0800 | [diff] [blame] | 186 | device_resume(); |
Rafael J. Wysocki | 97c7801 | 2006-10-11 01:20:45 -0700 | [diff] [blame] | 187 | resume_console(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | pr_debug("PM: writing image.\n"); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 189 | error = swsusp_write(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | if (!error) |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 191 | power_down(); |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 192 | else { |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 193 | swsusp_free(); |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame^] | 194 | goto Thaw; |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 195 | } |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 196 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | pr_debug("PM: Image restored successfully.\n"); |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 198 | } |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | swsusp_free(); |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 201 | Enable_cpus: |
| 202 | enable_nonboot_cpus(); |
| 203 | Resume_devices: |
| 204 | platform_finish(); |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 205 | device_resume(); |
Rafael J. Wysocki | 97c7801 | 2006-10-11 01:20:45 -0700 | [diff] [blame] | 206 | resume_console(); |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 207 | Thaw: |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 208 | unprepare_processes(); |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame^] | 209 | Finish: |
| 210 | free_basic_memory_bitmaps(); |
| 211 | Exit: |
| 212 | atomic_inc(&snapshot_device_available); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | return error; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | /** |
| 218 | * software_resume - Resume from a saved image. |
| 219 | * |
| 220 | * Called as a late_initcall (so all devices are discovered and |
| 221 | * initialized), we call swsusp to see if we have a saved image or not. |
| 222 | * If so, we quiesce devices, the restore the saved image. We will |
| 223 | * return above (in pm_suspend_disk() ) if everything goes well. |
| 224 | * Otherwise, we fail gracefully and return to the normally |
| 225 | * scheduled program. |
| 226 | * |
| 227 | */ |
| 228 | |
| 229 | static int software_resume(void) |
| 230 | { |
| 231 | int error; |
| 232 | |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 233 | mutex_lock(&pm_mutex); |
Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 234 | if (!swsusp_resume_device) { |
Shaohua Li | dd5d666 | 2005-09-03 15:57:04 -0700 | [diff] [blame] | 235 | if (!strlen(resume_file)) { |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 236 | mutex_unlock(&pm_mutex); |
Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 237 | return -ENOENT; |
Shaohua Li | dd5d666 | 2005-09-03 15:57:04 -0700 | [diff] [blame] | 238 | } |
Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 239 | swsusp_resume_device = name_to_dev_t(resume_file); |
| 240 | pr_debug("swsusp: Resume From Partition %s\n", resume_file); |
| 241 | } else { |
| 242 | pr_debug("swsusp: Resume From Partition %d:%d\n", |
| 243 | MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device)); |
| 244 | } |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | if (noresume) { |
| 247 | /** |
| 248 | * FIXME: If noresume is specified, we need to find the partition |
| 249 | * and reset it back to normal swap space. |
| 250 | */ |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 251 | mutex_unlock(&pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | pr_debug("PM: Checking swsusp image.\n"); |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 256 | error = swsusp_check(); |
| 257 | if (error) |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 258 | goto Unlock; |
| 259 | |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame^] | 260 | /* The snapshot device should not be opened while we're running */ |
| 261 | if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { |
| 262 | error = -EBUSY; |
| 263 | goto Unlock; |
| 264 | } |
| 265 | |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 266 | error = create_basic_memory_bitmaps(); |
| 267 | if (error) |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame^] | 268 | goto Finish; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
| 270 | pr_debug("PM: Preparing processes for restore.\n"); |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 271 | error = prepare_processes(); |
| 272 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | swsusp_close(); |
Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 274 | goto Done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | pr_debug("PM: Reading swsusp image.\n"); |
| 278 | |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 279 | error = swsusp_read(); |
| 280 | if (error) { |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 281 | swsusp_free(); |
| 282 | goto Thaw; |
| 283 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
| 285 | pr_debug("PM: Preparing devices for restore.\n"); |
| 286 | |
Rafael J. Wysocki | 97c7801 | 2006-10-11 01:20:45 -0700 | [diff] [blame] | 287 | suspend_console(); |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 288 | error = device_suspend(PMSG_PRETHAW); |
| 289 | if (error) |
| 290 | goto Free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 292 | error = disable_nonboot_cpus(); |
| 293 | if (!error) |
| 294 | swsusp_resume(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 296 | enable_nonboot_cpus(); |
| 297 | Free: |
| 298 | swsusp_free(); |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 299 | device_resume(); |
Rafael J. Wysocki | 97c7801 | 2006-10-11 01:20:45 -0700 | [diff] [blame] | 300 | resume_console(); |
Rafael J. Wysocki | 2c1b4a5 | 2005-10-30 14:59:58 -0800 | [diff] [blame] | 301 | Thaw: |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 302 | printk(KERN_ERR "PM: Restore failed, recovering.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | unprepare_processes(); |
| 304 | Done: |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 305 | free_basic_memory_bitmaps(); |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame^] | 306 | Finish: |
| 307 | atomic_inc(&snapshot_device_available); |
Shaohua Li | dd5d666 | 2005-09-03 15:57:04 -0700 | [diff] [blame] | 308 | /* For success case, the suspend path will release the lock */ |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 309 | Unlock: |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 310 | mutex_unlock(&pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | pr_debug("PM: Resume from disk failed.\n"); |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | late_initcall(software_resume); |
| 316 | |
| 317 | |
Andreas Mohr | 3b364b8 | 2006-06-25 05:47:56 -0700 | [diff] [blame] | 318 | static const char * const pm_disk_modes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | [PM_DISK_PLATFORM] = "platform", |
| 320 | [PM_DISK_SHUTDOWN] = "shutdown", |
| 321 | [PM_DISK_REBOOT] = "reboot", |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 322 | [PM_DISK_TEST] = "test", |
| 323 | [PM_DISK_TESTPROC] = "testproc", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | /** |
| 327 | * disk - Control suspend-to-disk mode |
| 328 | * |
Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 329 | * Suspend-to-disk can be handled in several ways. We have a few options |
| 330 | * for putting the system to sleep - using the platform driver (e.g. ACPI |
| 331 | * or other pm_ops), powering off the system or rebooting the system |
| 332 | * (for testing) as well as the two test modes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | * |
Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 334 | * The system can support 'platform', and that is known a priori (and |
| 335 | * encoded in pm_ops). However, the user may choose 'shutdown' or 'reboot' |
| 336 | * as alternatives, as well as the test modes 'test' and 'testproc'. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | * |
| 338 | * show() will display what the mode is currently set to. |
| 339 | * store() will accept one of |
| 340 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | * 'platform' |
| 342 | * 'shutdown' |
| 343 | * 'reboot' |
Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 344 | * 'test' |
| 345 | * 'testproc' |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | * |
Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 347 | * It will only change to 'platform' if the system |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | * supports it (as determined from pm_ops->pm_disk_mode). |
| 349 | */ |
| 350 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 351 | static ssize_t disk_show(struct kset *kset, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | { |
| 353 | return sprintf(buf, "%s\n", pm_disk_modes[pm_disk_mode]); |
| 354 | } |
| 355 | |
| 356 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 357 | static ssize_t disk_store(struct kset *kset, const char *buf, size_t n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | { |
| 359 | int error = 0; |
| 360 | int i; |
| 361 | int len; |
| 362 | char *p; |
| 363 | suspend_disk_method_t mode = 0; |
| 364 | |
| 365 | p = memchr(buf, '\n', n); |
| 366 | len = p ? p - buf : n; |
| 367 | |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 368 | mutex_lock(&pm_mutex); |
Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 369 | for (i = PM_DISK_PLATFORM; i < PM_DISK_MAX; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | if (!strncmp(buf, pm_disk_modes[i], len)) { |
| 371 | mode = i; |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | if (mode) { |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 376 | switch (mode) { |
| 377 | case PM_DISK_SHUTDOWN: |
| 378 | case PM_DISK_REBOOT: |
| 379 | case PM_DISK_TEST: |
| 380 | case PM_DISK_TESTPROC: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | pm_disk_mode = mode; |
Johannes Berg | fe0c935a | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 382 | break; |
| 383 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | if (pm_ops && pm_ops->enter && |
| 385 | (mode == pm_ops->pm_disk_mode)) |
| 386 | pm_disk_mode = mode; |
| 387 | else |
| 388 | error = -EINVAL; |
| 389 | } |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 390 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | error = -EINVAL; |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 392 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
| 394 | pr_debug("PM: suspend-to-disk mode set to '%s'\n", |
| 395 | pm_disk_modes[mode]); |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 396 | mutex_unlock(&pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | return error ? error : n; |
| 398 | } |
| 399 | |
| 400 | power_attr(disk); |
| 401 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 402 | static ssize_t resume_show(struct kset *kset, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | { |
| 404 | return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device), |
| 405 | MINOR(swsusp_resume_device)); |
| 406 | } |
| 407 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 408 | static ssize_t resume_store(struct kset *kset, const char *buf, size_t n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | unsigned int maj, min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | dev_t res; |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 412 | int ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 414 | if (sscanf(buf, "%u:%u", &maj, &min) != 2) |
| 415 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 417 | res = MKDEV(maj,min); |
| 418 | if (maj != MAJOR(res) || min != MINOR(res)) |
| 419 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 421 | mutex_lock(&pm_mutex); |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 422 | swsusp_resume_device = res; |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 423 | mutex_unlock(&pm_mutex); |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 424 | printk("Attempting manual resume\n"); |
| 425 | noresume = 0; |
| 426 | software_resume(); |
| 427 | ret = n; |
Rafael J. Wysocki | 59a49335 | 2006-12-06 20:34:44 -0800 | [diff] [blame] | 428 | out: |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 429 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | power_attr(resume); |
| 433 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 434 | static ssize_t image_size_show(struct kset *kset, char *buf) |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 435 | { |
Rafael J. Wysocki | 853609b | 2006-02-01 03:05:07 -0800 | [diff] [blame] | 436 | return sprintf(buf, "%lu\n", image_size); |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 439 | static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n) |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 440 | { |
Rafael J. Wysocki | 853609b | 2006-02-01 03:05:07 -0800 | [diff] [blame] | 441 | unsigned long size; |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 442 | |
Rafael J. Wysocki | 853609b | 2006-02-01 03:05:07 -0800 | [diff] [blame] | 443 | if (sscanf(buf, "%lu", &size) == 1) { |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 444 | image_size = size; |
| 445 | return n; |
| 446 | } |
| 447 | |
| 448 | return -EINVAL; |
| 449 | } |
| 450 | |
| 451 | power_attr(image_size); |
| 452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | static struct attribute * g[] = { |
| 454 | &disk_attr.attr, |
| 455 | &resume_attr.attr, |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 456 | &image_size_attr.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | NULL, |
| 458 | }; |
| 459 | |
| 460 | |
| 461 | static struct attribute_group attr_group = { |
| 462 | .attrs = g, |
| 463 | }; |
| 464 | |
| 465 | |
| 466 | static int __init pm_disk_init(void) |
| 467 | { |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 468 | return sysfs_create_group(&power_subsys.kobj, &attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | core_initcall(pm_disk_init); |
| 472 | |
| 473 | |
| 474 | static int __init resume_setup(char *str) |
| 475 | { |
| 476 | if (noresume) |
| 477 | return 1; |
| 478 | |
| 479 | strncpy( resume_file, str, 255 ); |
| 480 | return 1; |
| 481 | } |
| 482 | |
Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 483 | static int __init resume_offset_setup(char *str) |
| 484 | { |
| 485 | unsigned long long offset; |
| 486 | |
| 487 | if (noresume) |
| 488 | return 1; |
| 489 | |
| 490 | if (sscanf(str, "%llu", &offset) == 1) |
| 491 | swsusp_resume_block = offset; |
| 492 | |
| 493 | return 1; |
| 494 | } |
| 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | static int __init noresume_setup(char *str) |
| 497 | { |
| 498 | noresume = 1; |
| 499 | return 1; |
| 500 | } |
| 501 | |
| 502 | __setup("noresume", noresume_setup); |
Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 503 | __setup("resume_offset=", resume_offset_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | __setup("resume=", resume_setup); |