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