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