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 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 33 | enum { |
| 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 | |
| 46 | static int hibernation_mode = HIBERNATION_SHUTDOWN; |
| 47 | |
Rafael J. Wysocki | b3dac3b | 2007-10-18 03:04:43 -0700 | [diff] [blame] | 48 | static struct platform_hibernation_ops *hibernation_ops; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * hibernation_set_ops - set the global hibernate operations |
| 52 | * @ops: the hibernation operations to use in subsequent hibernation transitions |
| 53 | */ |
| 54 | |
Rafael J. Wysocki | b3dac3b | 2007-10-18 03:04:43 -0700 | [diff] [blame] | 55 | void hibernation_set_ops(struct platform_hibernation_ops *ops) |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 56 | { |
Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame^] | 57 | if (ops && !(ops->begin && ops->end && ops->pre_snapshot |
| 58 | && ops->prepare && ops->finish && ops->enter && ops->pre_restore |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 59 | && ops->restore_cleanup)) { |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 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 | |
Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 73 | #ifdef CONFIG_PM_DEBUG |
| 74 | static void hibernation_debug_sleep(void) |
| 75 | { |
| 76 | printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n"); |
| 77 | mdelay(5000); |
| 78 | } |
| 79 | |
| 80 | static int hibernation_testmode(int mode) |
| 81 | { |
| 82 | if (hibernation_mode == mode) { |
| 83 | hibernation_debug_sleep(); |
| 84 | return 1; |
| 85 | } |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int hibernation_test(int level) |
| 90 | { |
| 91 | if (pm_test_level == level) { |
| 92 | hibernation_debug_sleep(); |
| 93 | return 1; |
| 94 | } |
| 95 | return 0; |
| 96 | } |
| 97 | #else /* !CONFIG_PM_DEBUG */ |
| 98 | static int hibernation_testmode(int mode) { return 0; } |
| 99 | static int hibernation_test(int level) { return 0; } |
| 100 | #endif /* !CONFIG_PM_DEBUG */ |
| 101 | |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 102 | /** |
Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame^] | 103 | * platform_begin - tell the platform driver that we're starting |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 104 | * hibernation |
| 105 | */ |
| 106 | |
Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame^] | 107 | static int platform_begin(int platform_mode) |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 108 | { |
| 109 | return (platform_mode && hibernation_ops) ? |
Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame^] | 110 | hibernation_ops->begin() : 0; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * platform_end - tell the platform driver that we've entered the |
| 115 | * working state |
| 116 | */ |
| 117 | |
| 118 | static void platform_end(int platform_mode) |
| 119 | { |
| 120 | if (platform_mode && hibernation_ops) |
| 121 | hibernation_ops->end(); |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 122 | } |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | /** |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 125 | * platform_pre_snapshot - prepare the machine for hibernation using the |
Stefan Seyfried | 8a05aac | 2006-12-06 20:34:21 -0800 | [diff] [blame] | 126 | * platform driver if so configured and return an error code if it fails |
| 127 | */ |
| 128 | |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 129 | static int platform_pre_snapshot(int platform_mode) |
Stefan Seyfried | 8a05aac | 2006-12-06 20:34:21 -0800 | [diff] [blame] | 130 | { |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 131 | return (platform_mode && hibernation_ops) ? |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 132 | hibernation_ops->pre_snapshot() : 0; |
Stefan Seyfried | 8a05aac | 2006-12-06 20:34:21 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 136 | * platform_leave - prepare the machine for switching to the normal mode |
| 137 | * of operation using the platform driver (called with interrupts disabled) |
| 138 | */ |
| 139 | |
| 140 | static void platform_leave(int platform_mode) |
| 141 | { |
| 142 | if (platform_mode && hibernation_ops) |
| 143 | hibernation_ops->leave(); |
| 144 | } |
| 145 | |
| 146 | /** |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 147 | * platform_finish - switch the machine to the normal mode of operation |
| 148 | * using the platform driver (must be called after platform_prepare()) |
| 149 | */ |
| 150 | |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 151 | static void platform_finish(int platform_mode) |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 152 | { |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 153 | if (platform_mode && hibernation_ops) |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 154 | hibernation_ops->finish(); |
| 155 | } |
| 156 | |
| 157 | /** |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 158 | * platform_pre_restore - prepare the platform for the restoration from a |
| 159 | * hibernation image. If the restore fails after this function has been |
| 160 | * called, platform_restore_cleanup() must be called. |
| 161 | */ |
| 162 | |
| 163 | static int platform_pre_restore(int platform_mode) |
| 164 | { |
| 165 | return (platform_mode && hibernation_ops) ? |
| 166 | hibernation_ops->pre_restore() : 0; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * platform_restore_cleanup - switch the platform to the normal mode of |
| 171 | * operation after a failing restore. If platform_pre_restore() has been |
| 172 | * called before the failing restore, this function must be called too, |
| 173 | * regardless of the result of platform_pre_restore(). |
| 174 | */ |
| 175 | |
| 176 | static void platform_restore_cleanup(int platform_mode) |
| 177 | { |
| 178 | if (platform_mode && hibernation_ops) |
| 179 | hibernation_ops->restore_cleanup(); |
| 180 | } |
| 181 | |
| 182 | /** |
Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 183 | * create_image - freeze devices that need to be frozen with interrupts |
| 184 | * off, create the hibernation image and thaw those devices. Control |
| 185 | * reappears in this routine after a restore. |
| 186 | */ |
| 187 | |
| 188 | int create_image(int platform_mode) |
| 189 | { |
| 190 | int error; |
| 191 | |
| 192 | error = arch_prepare_suspend(); |
| 193 | if (error) |
| 194 | return error; |
| 195 | |
| 196 | local_irq_disable(); |
| 197 | /* At this point, device_suspend() has been called, but *not* |
| 198 | * device_power_down(). We *must* call device_power_down() now. |
| 199 | * Otherwise, drivers for some devices (e.g. interrupt controllers) |
| 200 | * become desynchronized with the actual state of the hardware |
| 201 | * at resume time, and evil weirdness ensues. |
| 202 | */ |
| 203 | error = device_power_down(PMSG_FREEZE); |
| 204 | if (error) { |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 205 | printk(KERN_ERR "PM: Some devices failed to power down, " |
| 206 | "aborting hibernation\n"); |
Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 207 | goto Enable_irqs; |
| 208 | } |
| 209 | |
Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 210 | if (hibernation_test(TEST_CORE)) |
| 211 | goto Power_up; |
| 212 | |
| 213 | in_suspend = 1; |
Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 214 | save_processor_state(); |
| 215 | error = swsusp_arch_suspend(); |
| 216 | if (error) |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 217 | printk(KERN_ERR "PM: Error %d creating hibernation image\n", |
| 218 | error); |
Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 219 | /* Restore control flow magically appears here */ |
| 220 | restore_processor_state(); |
| 221 | if (!in_suspend) |
| 222 | platform_leave(platform_mode); |
Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 223 | Power_up: |
Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 224 | /* NOTE: device_power_up() is just a resume() for devices |
| 225 | * that suspended with irqs off ... no overall powerup. |
| 226 | */ |
| 227 | device_power_up(); |
| 228 | Enable_irqs: |
| 229 | local_irq_enable(); |
| 230 | return error; |
| 231 | } |
| 232 | |
| 233 | /** |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 234 | * hibernation_snapshot - quiesce devices and create the hibernation |
| 235 | * snapshot image. |
| 236 | * @platform_mode - if set, use the platform driver, if available, to |
| 237 | * prepare the platform frimware for the power transition. |
| 238 | * |
| 239 | * Must be called with pm_mutex held |
| 240 | */ |
| 241 | |
| 242 | int hibernation_snapshot(int platform_mode) |
| 243 | { |
| 244 | int error; |
| 245 | |
| 246 | /* Free memory before shutting down devices. */ |
| 247 | error = swsusp_shrink_memory(); |
| 248 | if (error) |
Rafael J. Wysocki | 10a1803 | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 249 | return error; |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 250 | |
Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame^] | 251 | error = platform_begin(platform_mode); |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 252 | if (error) |
Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame^] | 253 | goto Close; |
Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 254 | |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 255 | suspend_console(); |
| 256 | error = device_suspend(PMSG_FREEZE); |
| 257 | if (error) |
Rafael J. Wysocki | 10a1803 | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 258 | goto Resume_console; |
| 259 | |
Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 260 | if (hibernation_test(TEST_DEVICES)) |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 261 | goto Resume_devices; |
| 262 | |
Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 263 | error = platform_pre_snapshot(platform_mode); |
| 264 | if (error || hibernation_test(TEST_PLATFORM)) |
| 265 | goto Finish; |
| 266 | |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 267 | error = disable_nonboot_cpus(); |
| 268 | if (!error) { |
Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 269 | if (hibernation_test(TEST_CPUS)) |
| 270 | goto Enable_cpus; |
| 271 | |
| 272 | if (hibernation_testmode(HIBERNATION_TEST)) |
| 273 | goto Enable_cpus; |
| 274 | |
| 275 | error = create_image(platform_mode); |
| 276 | /* Control returns here after successful restore */ |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 277 | } |
Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 278 | Enable_cpus: |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 279 | enable_nonboot_cpus(); |
Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 280 | Finish: |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 281 | platform_finish(platform_mode); |
Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 282 | Resume_devices: |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 283 | device_resume(); |
Rafael J. Wysocki | 10a1803 | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 284 | Resume_console: |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 285 | resume_console(); |
Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame^] | 286 | Close: |
| 287 | platform_end(platform_mode); |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 288 | return error; |
| 289 | } |
| 290 | |
| 291 | /** |
Rafael J. Wysocki | 72df68c | 2007-12-08 02:04:21 +0100 | [diff] [blame] | 292 | * resume_target_kernel - prepare devices that need to be suspended with |
| 293 | * interrupts off, restore the contents of highmem that have not been |
| 294 | * restored yet from the image and run the low level code that will restore |
| 295 | * the remaining contents of memory and switch to the just restored target |
| 296 | * kernel. |
| 297 | */ |
| 298 | |
| 299 | static int resume_target_kernel(void) |
| 300 | { |
| 301 | int error; |
| 302 | |
| 303 | local_irq_disable(); |
| 304 | error = device_power_down(PMSG_PRETHAW); |
| 305 | if (error) { |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 306 | printk(KERN_ERR "PM: Some devices failed to power down, " |
Rafael J. Wysocki | 72df68c | 2007-12-08 02:04:21 +0100 | [diff] [blame] | 307 | "aborting resume\n"); |
| 308 | goto Enable_irqs; |
| 309 | } |
| 310 | /* We'll ignore saved state, but this gets preempt count (etc) right */ |
| 311 | save_processor_state(); |
| 312 | error = restore_highmem(); |
| 313 | if (!error) { |
| 314 | error = swsusp_arch_resume(); |
| 315 | /* |
| 316 | * The code below is only ever reached in case of a failure. |
| 317 | * Otherwise execution continues at place where |
| 318 | * swsusp_arch_suspend() was called |
| 319 | */ |
| 320 | BUG_ON(!error); |
| 321 | /* This call to restore_highmem() undos the previous one */ |
| 322 | restore_highmem(); |
| 323 | } |
| 324 | /* |
| 325 | * The only reason why swsusp_arch_resume() can fail is memory being |
| 326 | * very tight, so we have to free it as soon as we can to avoid |
| 327 | * subsequent failures |
| 328 | */ |
| 329 | swsusp_free(); |
| 330 | restore_processor_state(); |
| 331 | touch_softlockup_watchdog(); |
| 332 | device_power_up(); |
| 333 | Enable_irqs: |
| 334 | local_irq_enable(); |
| 335 | return error; |
| 336 | } |
| 337 | |
| 338 | /** |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 339 | * hibernation_restore - quiesce devices and restore the hibernation |
| 340 | * snapshot image. If successful, control returns in hibernation_snaphot() |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 341 | * @platform_mode - if set, use the platform driver, if available, to |
| 342 | * prepare the platform frimware for the transition. |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 343 | * |
| 344 | * Must be called with pm_mutex held |
| 345 | */ |
| 346 | |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 347 | int hibernation_restore(int platform_mode) |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 348 | { |
| 349 | int error; |
| 350 | |
| 351 | pm_prepare_console(); |
| 352 | suspend_console(); |
| 353 | error = device_suspend(PMSG_PRETHAW); |
| 354 | if (error) |
| 355 | goto Finish; |
| 356 | |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 357 | error = platform_pre_restore(platform_mode); |
| 358 | if (!error) { |
| 359 | error = disable_nonboot_cpus(); |
| 360 | if (!error) |
Rafael J. Wysocki | 72df68c | 2007-12-08 02:04:21 +0100 | [diff] [blame] | 361 | error = resume_target_kernel(); |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 362 | enable_nonboot_cpus(); |
| 363 | } |
| 364 | platform_restore_cleanup(platform_mode); |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 365 | device_resume(); |
Rafael J. Wysocki | 10a1803 | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 366 | Finish: |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 367 | resume_console(); |
| 368 | pm_restore_console(); |
| 369 | return error; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * hibernation_platform_enter - enter the hibernation state using the |
| 374 | * platform driver (if available) |
| 375 | */ |
| 376 | |
| 377 | int hibernation_platform_enter(void) |
| 378 | { |
Rafael J. Wysocki | b1457bc | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 379 | int error; |
| 380 | |
Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 381 | if (!hibernation_ops) |
| 382 | return -ENOSYS; |
| 383 | |
| 384 | /* |
| 385 | * We have cancelled the power transition by running |
| 386 | * hibernation_ops->finish() before saving the image, so we should let |
| 387 | * the firmware know that we're going to enter the sleep state after all |
| 388 | */ |
Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame^] | 389 | error = hibernation_ops->begin(); |
Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 390 | if (error) |
Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame^] | 391 | goto Close; |
Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 392 | |
| 393 | suspend_console(); |
| 394 | error = device_suspend(PMSG_SUSPEND); |
| 395 | if (error) |
| 396 | goto Resume_console; |
| 397 | |
| 398 | error = hibernation_ops->prepare(); |
| 399 | if (error) |
| 400 | goto Resume_devices; |
| 401 | |
| 402 | error = disable_nonboot_cpus(); |
| 403 | if (error) |
| 404 | goto Finish; |
| 405 | |
| 406 | local_irq_disable(); |
| 407 | error = device_power_down(PMSG_SUSPEND); |
| 408 | if (!error) { |
| 409 | hibernation_ops->enter(); |
| 410 | /* We should never get here */ |
| 411 | while (1); |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 412 | } |
Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 413 | local_irq_enable(); |
| 414 | |
| 415 | /* |
| 416 | * We don't need to reenable the nonboot CPUs or resume consoles, since |
| 417 | * the system is going to be halted anyway. |
| 418 | */ |
| 419 | Finish: |
| 420 | hibernation_ops->finish(); |
| 421 | Resume_devices: |
| 422 | device_resume(); |
| 423 | Resume_console: |
| 424 | resume_console(); |
Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame^] | 425 | Close: |
| 426 | hibernation_ops->end(); |
Rafael J. Wysocki | b1457bc | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 427 | return error; |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /** |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 431 | * power_down - Shut the machine down for hibernation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | * |
Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 433 | * Use the platform driver, if configured so; otherwise try |
| 434 | * to power off or reboot. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | */ |
| 436 | |
Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 437 | static void power_down(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | { |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 439 | switch (hibernation_mode) { |
| 440 | case HIBERNATION_TEST: |
| 441 | case HIBERNATION_TESTPROC: |
Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 442 | break; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 443 | case HIBERNATION_REBOOT: |
Eric W. Biederman | fdde86a | 2005-07-26 12:01:17 -0600 | [diff] [blame] | 444 | kernel_restart(NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | break; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 446 | case HIBERNATION_PLATFORM: |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 447 | hibernation_platform_enter(); |
Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 448 | case HIBERNATION_SHUTDOWN: |
| 449 | kernel_power_off(); |
| 450 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | } |
Eric W. Biederman | fdde86a | 2005-07-26 12:01:17 -0600 | [diff] [blame] | 452 | kernel_halt(); |
Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 453 | /* |
| 454 | * Valid image is on the disk, if we continue we risk serious data |
| 455 | * corruption after resume. |
| 456 | */ |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 457 | printk(KERN_CRIT "PM: Please power down manually\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | while(1); |
| 459 | } |
| 460 | |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 461 | static void unprepare_processes(void) |
| 462 | { |
| 463 | thaw_processes(); |
| 464 | pm_restore_console(); |
| 465 | } |
| 466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | static int prepare_processes(void) |
| 468 | { |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 469 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
| 471 | pm_prepare_console(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | if (freeze_processes()) { |
| 473 | error = -EBUSY; |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 474 | unprepare_processes(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } |
Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 476 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | /** |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 480 | * hibernate - The granpappy of the built-in hibernation management |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | */ |
| 482 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 483 | int hibernate(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | { |
| 485 | int error; |
| 486 | |
Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 487 | mutex_lock(&pm_mutex); |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 488 | /* The snapshot device should not be opened while we're running */ |
Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 489 | if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { |
| 490 | error = -EBUSY; |
| 491 | goto Unlock; |
| 492 | } |
| 493 | |
| 494 | error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE); |
| 495 | if (error) |
| 496 | goto Exit; |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 497 | |
| 498 | /* Allocate memory management structures */ |
| 499 | error = create_basic_memory_bitmaps(); |
| 500 | if (error) |
| 501 | goto Exit; |
| 502 | |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 503 | printk(KERN_INFO "PM: Syncing filesystems ... "); |
Rafael J. Wysocki | 232b143 | 2007-10-18 03:04:44 -0700 | [diff] [blame] | 504 | sys_sync(); |
| 505 | printk("done.\n"); |
| 506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | error = prepare_processes(); |
Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 508 | if (error) |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 509 | goto Finish; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 511 | if (hibernation_test(TEST_FREEZER)) |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 512 | goto Thaw; |
Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 513 | |
| 514 | if (hibernation_testmode(HIBERNATION_TESTPROC)) |
| 515 | goto Thaw; |
| 516 | |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 517 | error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM); |
| 518 | if (in_suspend && !error) { |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 519 | unsigned int flags = 0; |
| 520 | |
| 521 | if (hibernation_mode == HIBERNATION_PLATFORM) |
| 522 | flags |= SF_PLATFORM_MODE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | pr_debug("PM: writing image.\n"); |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 524 | error = swsusp_write(flags); |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 525 | swsusp_free(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | if (!error) |
Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 527 | power_down(); |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 528 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | pr_debug("PM: Image restored successfully.\n"); |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 530 | swsusp_free(); |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 531 | } |
Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 532 | Thaw: |
Pavel Machek | 99dc7d6 | 2005-09-03 15:57:05 -0700 | [diff] [blame] | 533 | unprepare_processes(); |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 534 | Finish: |
| 535 | free_basic_memory_bitmaps(); |
| 536 | Exit: |
Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 537 | pm_notifier_call_chain(PM_POST_HIBERNATION); |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 538 | atomic_inc(&snapshot_device_available); |
Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 539 | Unlock: |
| 540 | mutex_unlock(&pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | return error; |
| 542 | } |
| 543 | |
| 544 | |
| 545 | /** |
| 546 | * software_resume - Resume from a saved image. |
| 547 | * |
| 548 | * Called as a late_initcall (so all devices are discovered and |
| 549 | * initialized), we call swsusp to see if we have a saved image or not. |
| 550 | * If so, we quiesce devices, the restore the saved image. We will |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 551 | * return above (in hibernate() ) if everything goes well. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | * Otherwise, we fail gracefully and return to the normally |
| 553 | * scheduled program. |
| 554 | * |
| 555 | */ |
| 556 | |
| 557 | static int software_resume(void) |
| 558 | { |
| 559 | int error; |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 560 | unsigned int flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Johannes Berg | 60a0d23 | 2007-11-14 17:00:16 -0800 | [diff] [blame] | 562 | /* |
| 563 | * name_to_dev_t() below takes a sysfs buffer mutex when sysfs |
| 564 | * is configured into the kernel. Since the regular hibernate |
| 565 | * trigger path is via sysfs which takes a buffer mutex before |
| 566 | * calling hibernate functions (which take pm_mutex) this can |
| 567 | * cause lockdep to complain about a possible ABBA deadlock |
| 568 | * which cannot happen since we're in the boot code here and |
| 569 | * sysfs can't be invoked yet. Therefore, we use a subclass |
| 570 | * here to avoid lockdep complaining. |
| 571 | */ |
| 572 | mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING); |
Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 573 | if (!swsusp_resume_device) { |
Shaohua Li | dd5d666 | 2005-09-03 15:57:04 -0700 | [diff] [blame] | 574 | if (!strlen(resume_file)) { |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 575 | mutex_unlock(&pm_mutex); |
Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 576 | return -ENOENT; |
Shaohua Li | dd5d666 | 2005-09-03 15:57:04 -0700 | [diff] [blame] | 577 | } |
Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 578 | swsusp_resume_device = name_to_dev_t(resume_file); |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 579 | pr_debug("PM: Resume from partition %s\n", resume_file); |
Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 580 | } else { |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 581 | pr_debug("PM: Resume from partition %d:%d\n", |
| 582 | MAJOR(swsusp_resume_device), |
| 583 | MINOR(swsusp_resume_device)); |
Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | if (noresume) { |
| 587 | /** |
Rafael J. Wysocki | 9575809 | 2007-12-08 02:06:57 +0100 | [diff] [blame] | 588 | * FIXME: If noresume is specified, we need to find the |
| 589 | * partition and reset it back to normal swap space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | */ |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 591 | mutex_unlock(&pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | return 0; |
| 593 | } |
| 594 | |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 595 | pr_debug("PM: Checking hibernation image.\n"); |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 596 | error = swsusp_check(); |
| 597 | if (error) |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 598 | goto Unlock; |
| 599 | |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 600 | /* The snapshot device should not be opened while we're running */ |
| 601 | if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { |
| 602 | error = -EBUSY; |
| 603 | goto Unlock; |
| 604 | } |
| 605 | |
Alan Stern | c3e94d8 | 2007-11-19 23:38:25 +0100 | [diff] [blame] | 606 | error = pm_notifier_call_chain(PM_RESTORE_PREPARE); |
| 607 | if (error) |
| 608 | goto Finish; |
| 609 | |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 610 | error = create_basic_memory_bitmaps(); |
| 611 | if (error) |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 612 | goto Finish; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
| 614 | pr_debug("PM: Preparing processes for restore.\n"); |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 615 | error = prepare_processes(); |
| 616 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | swsusp_close(); |
Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 618 | goto Done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 621 | pr_debug("PM: Reading hibernation image.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 623 | error = swsusp_read(&flags); |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 624 | if (!error) |
Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 625 | hibernation_restore(flags & SF_PLATFORM_MODE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 627 | printk(KERN_ERR "PM: Restore failed, recovering.\n"); |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 628 | swsusp_free(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | unprepare_processes(); |
| 630 | Done: |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 631 | free_basic_memory_bitmaps(); |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 632 | Finish: |
Alan Stern | c3e94d8 | 2007-11-19 23:38:25 +0100 | [diff] [blame] | 633 | pm_notifier_call_chain(PM_POST_RESTORE); |
Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 634 | atomic_inc(&snapshot_device_available); |
Shaohua Li | dd5d666 | 2005-09-03 15:57:04 -0700 | [diff] [blame] | 635 | /* For success case, the suspend path will release the lock */ |
Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 636 | Unlock: |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 637 | mutex_unlock(&pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | pr_debug("PM: Resume from disk failed.\n"); |
Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 639 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | late_initcall(software_resume); |
| 643 | |
| 644 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 645 | static const char * const hibernation_modes[] = { |
| 646 | [HIBERNATION_PLATFORM] = "platform", |
| 647 | [HIBERNATION_SHUTDOWN] = "shutdown", |
| 648 | [HIBERNATION_REBOOT] = "reboot", |
| 649 | [HIBERNATION_TEST] = "test", |
| 650 | [HIBERNATION_TESTPROC] = "testproc", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | }; |
| 652 | |
| 653 | /** |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 654 | * disk - Control hibernation mode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | * |
Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 656 | * Suspend-to-disk can be handled in several ways. We have a few options |
| 657 | * for putting the system to sleep - using the platform driver (e.g. ACPI |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 658 | * or other hibernation_ops), powering off the system or rebooting the |
| 659 | * system (for testing) as well as the two test modes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | * |
Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 661 | * The system can support 'platform', and that is known a priori (and |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 662 | * encoded by the presence of hibernation_ops). However, the user may |
| 663 | * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the |
| 664 | * test modes, 'test' or 'testproc'. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | * |
| 666 | * show() will display what the mode is currently set to. |
| 667 | * store() will accept one of |
| 668 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | * 'platform' |
| 670 | * 'shutdown' |
| 671 | * 'reboot' |
Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 672 | * 'test' |
| 673 | * 'testproc' |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | * |
Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 675 | * It will only change to 'platform' if the system |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 676 | * supports it (as determined by having hibernation_ops). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | */ |
| 678 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 679 | static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 680 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | { |
Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 682 | int i; |
| 683 | char *start = buf; |
| 684 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 685 | for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { |
| 686 | if (!hibernation_modes[i]) |
Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 687 | continue; |
| 688 | switch (i) { |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 689 | case HIBERNATION_SHUTDOWN: |
| 690 | case HIBERNATION_REBOOT: |
| 691 | case HIBERNATION_TEST: |
| 692 | case HIBERNATION_TESTPROC: |
Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 693 | break; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 694 | case HIBERNATION_PLATFORM: |
| 695 | if (hibernation_ops) |
Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 696 | break; |
| 697 | /* not a valid mode, continue with loop */ |
| 698 | continue; |
| 699 | } |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 700 | if (i == hibernation_mode) |
| 701 | buf += sprintf(buf, "[%s] ", hibernation_modes[i]); |
Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 702 | else |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 703 | buf += sprintf(buf, "%s ", hibernation_modes[i]); |
Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 704 | } |
| 705 | buf += sprintf(buf, "\n"); |
| 706 | return buf-start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 710 | static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 711 | const char *buf, size_t n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | { |
| 713 | int error = 0; |
| 714 | int i; |
| 715 | int len; |
| 716 | char *p; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 717 | int mode = HIBERNATION_INVALID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | |
| 719 | p = memchr(buf, '\n', n); |
| 720 | len = p ? p - buf : n; |
| 721 | |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 722 | mutex_lock(&pm_mutex); |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 723 | for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { |
Rafael J. Wysocki | 8d98a69 | 2007-05-16 22:11:19 -0700 | [diff] [blame] | 724 | if (len == strlen(hibernation_modes[i]) |
| 725 | && !strncmp(buf, hibernation_modes[i], len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | mode = i; |
| 727 | break; |
| 728 | } |
| 729 | } |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 730 | if (mode != HIBERNATION_INVALID) { |
Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 731 | switch (mode) { |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 732 | case HIBERNATION_SHUTDOWN: |
| 733 | case HIBERNATION_REBOOT: |
| 734 | case HIBERNATION_TEST: |
| 735 | case HIBERNATION_TESTPROC: |
| 736 | hibernation_mode = mode; |
Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 737 | break; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 738 | case HIBERNATION_PLATFORM: |
| 739 | if (hibernation_ops) |
| 740 | hibernation_mode = mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | else |
| 742 | error = -EINVAL; |
| 743 | } |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 744 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | error = -EINVAL; |
| 746 | |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 747 | if (!error) |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 748 | pr_debug("PM: Hibernation mode set to '%s'\n", |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 749 | hibernation_modes[mode]); |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 750 | mutex_unlock(&pm_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | return error ? error : n; |
| 752 | } |
| 753 | |
| 754 | power_attr(disk); |
| 755 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 756 | static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 757 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | { |
| 759 | return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device), |
| 760 | MINOR(swsusp_resume_device)); |
| 761 | } |
| 762 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 763 | static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 764 | const char *buf, size_t n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | unsigned int maj, min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | dev_t res; |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 768 | int ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 770 | if (sscanf(buf, "%u:%u", &maj, &min) != 2) |
| 771 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 773 | res = MKDEV(maj,min); |
| 774 | if (maj != MAJOR(res) || min != MINOR(res)) |
| 775 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 777 | mutex_lock(&pm_mutex); |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 778 | swsusp_resume_device = res; |
Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 779 | mutex_unlock(&pm_mutex); |
Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 780 | printk(KERN_INFO "PM: Starting manual resume from disk\n"); |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 781 | noresume = 0; |
| 782 | software_resume(); |
| 783 | ret = n; |
Rafael J. Wysocki | 59a49335 | 2006-12-06 20:34:44 -0800 | [diff] [blame] | 784 | out: |
Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 785 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | power_attr(resume); |
| 789 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 790 | static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 791 | char *buf) |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 792 | { |
Rafael J. Wysocki | 853609b | 2006-02-01 03:05:07 -0800 | [diff] [blame] | 793 | return sprintf(buf, "%lu\n", image_size); |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 794 | } |
| 795 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 796 | static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 797 | const char *buf, size_t n) |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 798 | { |
Rafael J. Wysocki | 853609b | 2006-02-01 03:05:07 -0800 | [diff] [blame] | 799 | unsigned long size; |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 800 | |
Rafael J. Wysocki | 853609b | 2006-02-01 03:05:07 -0800 | [diff] [blame] | 801 | if (sscanf(buf, "%lu", &size) == 1) { |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 802 | image_size = size; |
| 803 | return n; |
| 804 | } |
| 805 | |
| 806 | return -EINVAL; |
| 807 | } |
| 808 | |
| 809 | power_attr(image_size); |
| 810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | static struct attribute * g[] = { |
| 812 | &disk_attr.attr, |
| 813 | &resume_attr.attr, |
Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 814 | &image_size_attr.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | NULL, |
| 816 | }; |
| 817 | |
| 818 | |
| 819 | static struct attribute_group attr_group = { |
| 820 | .attrs = g, |
| 821 | }; |
| 822 | |
| 823 | |
| 824 | static int __init pm_disk_init(void) |
| 825 | { |
Greg Kroah-Hartman | d76e15f | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 826 | return sysfs_create_group(power_kobj, &attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | core_initcall(pm_disk_init); |
| 830 | |
| 831 | |
| 832 | static int __init resume_setup(char *str) |
| 833 | { |
| 834 | if (noresume) |
| 835 | return 1; |
| 836 | |
| 837 | strncpy( resume_file, str, 255 ); |
| 838 | return 1; |
| 839 | } |
| 840 | |
Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 841 | static int __init resume_offset_setup(char *str) |
| 842 | { |
| 843 | unsigned long long offset; |
| 844 | |
| 845 | if (noresume) |
| 846 | return 1; |
| 847 | |
| 848 | if (sscanf(str, "%llu", &offset) == 1) |
| 849 | swsusp_resume_block = offset; |
| 850 | |
| 851 | return 1; |
| 852 | } |
| 853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | static int __init noresume_setup(char *str) |
| 855 | { |
| 856 | noresume = 1; |
| 857 | return 1; |
| 858 | } |
| 859 | |
| 860 | __setup("noresume", noresume_setup); |
Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 861 | __setup("resume_offset=", resume_offset_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | __setup("resume=", resume_setup); |