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