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