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