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