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