| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 1 | /* | 
|  | 2 | * kernel/power/suspend.c - Suspend to RAM and standby functionality. | 
|  | 3 | * | 
|  | 4 | * Copyright (c) 2003 Patrick Mochel | 
|  | 5 | * Copyright (c) 2003 Open Source Development Lab | 
|  | 6 | * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc. | 
|  | 7 | * | 
|  | 8 | * This file is released under the GPLv2. | 
|  | 9 | */ | 
|  | 10 |  | 
|  | 11 | #include <linux/string.h> | 
|  | 12 | #include <linux/delay.h> | 
|  | 13 | #include <linux/errno.h> | 
|  | 14 | #include <linux/init.h> | 
|  | 15 | #include <linux/console.h> | 
|  | 16 | #include <linux/cpu.h> | 
|  | 17 | #include <linux/syscalls.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/gfp.h> | 
| Matthew Garrett | dd4c4f1 | 2010-05-28 16:32:14 -0400 | [diff] [blame] | 19 | #include <linux/io.h> | 
|  | 20 | #include <linux/kernel.h> | 
|  | 21 | #include <linux/list.h> | 
|  | 22 | #include <linux/mm.h> | 
|  | 23 | #include <linux/slab.h> | 
|  | 24 | #include <linux/suspend.h> | 
| Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 25 | #include <linux/syscore_ops.h> | 
| Jean Pihet | 938cfed | 2011-01-05 19:49:01 +0100 | [diff] [blame] | 26 | #include <trace/events/power.h> | 
| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 27 |  | 
|  | 28 | #include "power.h" | 
|  | 29 |  | 
|  | 30 | const char *const pm_states[PM_SUSPEND_MAX] = { | 
| Arve Hjønnevåg | b0dc343 | 2008-10-09 19:17:11 -0700 | [diff] [blame] | 31 | #ifdef CONFIG_EARLYSUSPEND | 
|  | 32 | [PM_SUSPEND_ON]		= "on", | 
|  | 33 | #endif | 
| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 34 | [PM_SUSPEND_STANDBY]	= "standby", | 
|  | 35 | [PM_SUSPEND_MEM]	= "mem", | 
|  | 36 | }; | 
|  | 37 |  | 
| Lionel Debroux | 2f55ac0 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 38 | static const struct platform_suspend_ops *suspend_ops; | 
| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 39 |  | 
|  | 40 | /** | 
|  | 41 | *	suspend_set_ops - Set the global suspend method table. | 
|  | 42 | *	@ops:	Pointer to ops structure. | 
|  | 43 | */ | 
| Lionel Debroux | 2f55ac0 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 44 | void suspend_set_ops(const struct platform_suspend_ops *ops) | 
| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 45 | { | 
|  | 46 | mutex_lock(&pm_mutex); | 
|  | 47 | suspend_ops = ops; | 
|  | 48 | mutex_unlock(&pm_mutex); | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | bool valid_state(suspend_state_t state) | 
|  | 52 | { | 
|  | 53 | /* | 
|  | 54 | * All states need lowlevel support and need to be valid to the lowlevel | 
|  | 55 | * implementation, no valid callback implies that none are valid. | 
|  | 56 | */ | 
|  | 57 | return suspend_ops && suspend_ops->valid && suspend_ops->valid(state); | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | /** | 
|  | 61 | * suspend_valid_only_mem - generic memory-only valid callback | 
|  | 62 | * | 
|  | 63 | * Platform drivers that implement mem suspend only and only need | 
|  | 64 | * to check for that in their .valid callback can use this instead | 
|  | 65 | * of rolling their own .valid callback. | 
|  | 66 | */ | 
|  | 67 | int suspend_valid_only_mem(suspend_state_t state) | 
|  | 68 | { | 
|  | 69 | return state == PM_SUSPEND_MEM; | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | static int suspend_test(int level) | 
|  | 73 | { | 
|  | 74 | #ifdef CONFIG_PM_DEBUG | 
|  | 75 | if (pm_test_level == level) { | 
|  | 76 | printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n"); | 
|  | 77 | mdelay(5000); | 
|  | 78 | return 1; | 
|  | 79 | } | 
|  | 80 | #endif /* !CONFIG_PM_DEBUG */ | 
|  | 81 | return 0; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | /** | 
|  | 85 | *	suspend_prepare - Do prep work before entering low-power state. | 
|  | 86 | * | 
|  | 87 | *	This is common code that is called for each state that we're entering. | 
|  | 88 | *	Run suspend notifiers, allocate a console and stop all processes. | 
|  | 89 | */ | 
|  | 90 | static int suspend_prepare(void) | 
|  | 91 | { | 
|  | 92 | int error; | 
|  | 93 |  | 
|  | 94 | if (!suspend_ops || !suspend_ops->enter) | 
|  | 95 | return -EPERM; | 
|  | 96 |  | 
|  | 97 | pm_prepare_console(); | 
|  | 98 |  | 
|  | 99 | error = pm_notifier_call_chain(PM_SUSPEND_PREPARE); | 
|  | 100 | if (error) | 
|  | 101 | goto Finish; | 
|  | 102 |  | 
|  | 103 | error = usermodehelper_disable(); | 
|  | 104 | if (error) | 
|  | 105 | goto Finish; | 
|  | 106 |  | 
|  | 107 | error = suspend_freeze_processes(); | 
|  | 108 | if (!error) | 
|  | 109 | return 0; | 
|  | 110 |  | 
|  | 111 | suspend_thaw_processes(); | 
|  | 112 | usermodehelper_enable(); | 
|  | 113 | Finish: | 
|  | 114 | pm_notifier_call_chain(PM_POST_SUSPEND); | 
|  | 115 | pm_restore_console(); | 
|  | 116 | return error; | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | /* default implementation */ | 
|  | 120 | void __attribute__ ((weak)) arch_suspend_disable_irqs(void) | 
|  | 121 | { | 
|  | 122 | local_irq_disable(); | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | /* default implementation */ | 
|  | 126 | void __attribute__ ((weak)) arch_suspend_enable_irqs(void) | 
|  | 127 | { | 
|  | 128 | local_irq_enable(); | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | /** | 
|  | 132 | *	suspend_enter - enter the desired system sleep state. | 
|  | 133 | *	@state:		state to enter | 
|  | 134 | * | 
|  | 135 | *	This function should be called after devices have been suspended. | 
|  | 136 | */ | 
|  | 137 | static int suspend_enter(suspend_state_t state) | 
|  | 138 | { | 
|  | 139 | int error; | 
|  | 140 |  | 
|  | 141 | if (suspend_ops->prepare) { | 
|  | 142 | error = suspend_ops->prepare(); | 
|  | 143 | if (error) | 
| Rafael J. Wysocki | ce44101 | 2010-07-07 23:43:45 +0200 | [diff] [blame] | 144 | goto Platform_finish; | 
| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 145 | } | 
|  | 146 |  | 
|  | 147 | error = dpm_suspend_noirq(PMSG_SUSPEND); | 
|  | 148 | if (error) { | 
|  | 149 | printk(KERN_ERR "PM: Some devices failed to power down\n"); | 
| Rafael J. Wysocki | ce44101 | 2010-07-07 23:43:45 +0200 | [diff] [blame] | 150 | goto Platform_finish; | 
| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 151 | } | 
|  | 152 |  | 
|  | 153 | if (suspend_ops->prepare_late) { | 
|  | 154 | error = suspend_ops->prepare_late(); | 
|  | 155 | if (error) | 
| Rafael J. Wysocki | ce44101 | 2010-07-07 23:43:45 +0200 | [diff] [blame] | 156 | goto Platform_wake; | 
| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 157 | } | 
|  | 158 |  | 
|  | 159 | if (suspend_test(TEST_PLATFORM)) | 
|  | 160 | goto Platform_wake; | 
|  | 161 |  | 
|  | 162 | error = disable_nonboot_cpus(); | 
|  | 163 | if (error || suspend_test(TEST_CPUS)) | 
|  | 164 | goto Enable_cpus; | 
|  | 165 |  | 
|  | 166 | arch_suspend_disable_irqs(); | 
|  | 167 | BUG_ON(!irqs_disabled()); | 
|  | 168 |  | 
| Rafael J. Wysocki | 2e711c0 | 2011-04-26 19:15:07 +0200 | [diff] [blame] | 169 | error = syscore_suspend(); | 
| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 170 | if (!error) { | 
| Rafael J. Wysocki | a2867e0 | 2010-12-03 22:58:31 +0100 | [diff] [blame] | 171 | if (!(suspend_test(TEST_CORE) || pm_wakeup_pending())) { | 
| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 172 | error = suspend_ops->enter(state); | 
| Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 173 | events_check_enabled = false; | 
|  | 174 | } | 
| Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 175 | syscore_resume(); | 
| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 176 | } | 
|  | 177 |  | 
|  | 178 | arch_suspend_enable_irqs(); | 
|  | 179 | BUG_ON(irqs_disabled()); | 
|  | 180 |  | 
|  | 181 | Enable_cpus: | 
|  | 182 | enable_nonboot_cpus(); | 
|  | 183 |  | 
|  | 184 | Platform_wake: | 
|  | 185 | if (suspend_ops->wake) | 
|  | 186 | suspend_ops->wake(); | 
|  | 187 |  | 
| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 188 | dpm_resume_noirq(PMSG_RESUME); | 
|  | 189 |  | 
| Rafael J. Wysocki | ce44101 | 2010-07-07 23:43:45 +0200 | [diff] [blame] | 190 | Platform_finish: | 
| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 191 | if (suspend_ops->finish) | 
|  | 192 | suspend_ops->finish(); | 
|  | 193 |  | 
|  | 194 | return error; | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | /** | 
|  | 198 | *	suspend_devices_and_enter - suspend devices and enter the desired system | 
|  | 199 | *				    sleep state. | 
|  | 200 | *	@state:		  state to enter | 
|  | 201 | */ | 
|  | 202 | int suspend_devices_and_enter(suspend_state_t state) | 
|  | 203 | { | 
|  | 204 | int error; | 
|  | 205 |  | 
|  | 206 | if (!suspend_ops) | 
|  | 207 | return -ENOSYS; | 
|  | 208 |  | 
| Jean Pihet | 938cfed | 2011-01-05 19:49:01 +0100 | [diff] [blame] | 209 | trace_machine_suspend(state); | 
| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 210 | if (suspend_ops->begin) { | 
|  | 211 | error = suspend_ops->begin(state); | 
|  | 212 | if (error) | 
|  | 213 | goto Close; | 
|  | 214 | } | 
|  | 215 | suspend_console(); | 
|  | 216 | suspend_test_start(); | 
|  | 217 | error = dpm_suspend_start(PMSG_SUSPEND); | 
|  | 218 | if (error) { | 
|  | 219 | printk(KERN_ERR "PM: Some devices failed to suspend\n"); | 
|  | 220 | goto Recover_platform; | 
|  | 221 | } | 
|  | 222 | suspend_test_finish("suspend devices"); | 
|  | 223 | if (suspend_test(TEST_DEVICES)) | 
|  | 224 | goto Recover_platform; | 
|  | 225 |  | 
| MyungJoo Ham | 3c43193 | 2011-04-22 22:00:54 +0200 | [diff] [blame] | 226 | error = suspend_enter(state); | 
| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 227 |  | 
|  | 228 | Resume_devices: | 
|  | 229 | suspend_test_start(); | 
|  | 230 | dpm_resume_end(PMSG_RESUME); | 
|  | 231 | suspend_test_finish("resume devices"); | 
| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 232 | resume_console(); | 
|  | 233 | Close: | 
|  | 234 | if (suspend_ops->end) | 
|  | 235 | suspend_ops->end(); | 
| Jean Pihet | 938cfed | 2011-01-05 19:49:01 +0100 | [diff] [blame] | 236 | trace_machine_suspend(PWR_EVENT_EXIT); | 
| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 237 | return error; | 
|  | 238 |  | 
|  | 239 | Recover_platform: | 
|  | 240 | if (suspend_ops->recover) | 
|  | 241 | suspend_ops->recover(); | 
|  | 242 | goto Resume_devices; | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | /** | 
|  | 246 | *	suspend_finish - Do final work before exiting suspend sequence. | 
|  | 247 | * | 
|  | 248 | *	Call platform code to clean up, restart processes, and free the | 
|  | 249 | *	console that we've allocated. This is not called for suspend-to-disk. | 
|  | 250 | */ | 
|  | 251 | static void suspend_finish(void) | 
|  | 252 | { | 
|  | 253 | suspend_thaw_processes(); | 
|  | 254 | usermodehelper_enable(); | 
|  | 255 | pm_notifier_call_chain(PM_POST_SUSPEND); | 
|  | 256 | pm_restore_console(); | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | /** | 
|  | 260 | *	enter_state - Do common work of entering low-power state. | 
|  | 261 | *	@state:		pm_state structure for state we're entering. | 
|  | 262 | * | 
|  | 263 | *	Make sure we're the only ones trying to enter a sleep state. Fail | 
|  | 264 | *	if someone has beat us to it, since we don't want anything weird to | 
|  | 265 | *	happen when we wake up. | 
|  | 266 | *	Then, do the setup for suspend, enter the state, and cleaup (after | 
|  | 267 | *	we've woken up). | 
|  | 268 | */ | 
|  | 269 | int enter_state(suspend_state_t state) | 
|  | 270 | { | 
|  | 271 | int error; | 
|  | 272 |  | 
|  | 273 | if (!valid_state(state)) | 
|  | 274 | return -ENODEV; | 
|  | 275 |  | 
|  | 276 | if (!mutex_trylock(&pm_mutex)) | 
|  | 277 | return -EBUSY; | 
|  | 278 |  | 
|  | 279 | printk(KERN_INFO "PM: Syncing filesystems ... "); | 
|  | 280 | sys_sync(); | 
|  | 281 | printk("done.\n"); | 
|  | 282 |  | 
|  | 283 | pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]); | 
|  | 284 | error = suspend_prepare(); | 
|  | 285 | if (error) | 
|  | 286 | goto Unlock; | 
|  | 287 |  | 
|  | 288 | if (suspend_test(TEST_FREEZER)) | 
|  | 289 | goto Finish; | 
|  | 290 |  | 
|  | 291 | pr_debug("PM: Entering %s sleep\n", pm_states[state]); | 
| Rafael J. Wysocki | 8718647 | 2011-05-10 21:09:53 +0200 | [diff] [blame] | 292 | pm_restrict_gfp_mask(); | 
| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 293 | error = suspend_devices_and_enter(state); | 
| Rafael J. Wysocki | 8718647 | 2011-05-10 21:09:53 +0200 | [diff] [blame] | 294 | pm_restore_gfp_mask(); | 
| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 295 |  | 
|  | 296 | Finish: | 
|  | 297 | pr_debug("PM: Finishing wakeup.\n"); | 
|  | 298 | suspend_finish(); | 
|  | 299 | Unlock: | 
|  | 300 | mutex_unlock(&pm_mutex); | 
|  | 301 | return error; | 
|  | 302 | } | 
|  | 303 |  | 
|  | 304 | /** | 
|  | 305 | *	pm_suspend - Externally visible function for suspending system. | 
|  | 306 | *	@state:		Enumerated value of state to enter. | 
|  | 307 | * | 
|  | 308 | *	Determine whether or not value is within range, get state | 
|  | 309 | *	structure, and enter (above). | 
|  | 310 | */ | 
|  | 311 | int pm_suspend(suspend_state_t state) | 
|  | 312 | { | 
|  | 313 | if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX) | 
|  | 314 | return enter_state(state); | 
|  | 315 | return -EINVAL; | 
|  | 316 | } | 
|  | 317 | EXPORT_SYMBOL(pm_suspend); |