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