Commit | Line | Data |
---|---|---|
a9d70523 RW |
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> | |
5a0e3ad6 | 18 | #include <linux/gfp.h> |
dd4c4f17 MG |
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> | |
6e5fdeed | 24 | #include <linux/export.h> |
dd4c4f17 | 25 | #include <linux/suspend.h> |
40dc166c | 26 | #include <linux/syscore_ops.h> |
443772d4 | 27 | #include <linux/ftrace.h> |
6fa3eb70 | 28 | #include <linux/rtc.h> |
938cfed1 | 29 | #include <trace/events/power.h> |
a9d70523 RW |
30 | |
31 | #include "power.h" | |
32 | ||
e89547b8 | 33 | struct pm_sleep_state pm_states[PM_SUSPEND_MAX] = { |
6fa3eb70 | 34 | #ifdef CONFIG_EARLYSUSPEND |
f29ec40f | 35 | [PM_SUSPEND_ON] = { .label = "on", }, |
6fa3eb70 | 36 | #endif |
5c95fef6 RW |
37 | [PM_SUSPEND_FREEZE] = { .label = "freeze", .state = PM_SUSPEND_FREEZE }, |
38 | [PM_SUSPEND_STANDBY] = { .label = "standby", }, | |
39 | [PM_SUSPEND_MEM] = { .label = "mem", }, | |
a9d70523 RW |
40 | }; |
41 | ||
2f55ac07 | 42 | static const struct platform_suspend_ops *suspend_ops; |
a9d70523 | 43 | |
7e73c5ae ZR |
44 | static bool need_suspend_ops(suspend_state_t state) |
45 | { | |
46 | return !!(state > PM_SUSPEND_FREEZE); | |
47 | } | |
48 | ||
49 | static DECLARE_WAIT_QUEUE_HEAD(suspend_freeze_wait_head); | |
50 | static bool suspend_freeze_wake; | |
51 | ||
52 | static void freeze_begin(void) | |
53 | { | |
54 | suspend_freeze_wake = false; | |
55 | } | |
56 | ||
57 | static void freeze_enter(void) | |
58 | { | |
59 | wait_event(suspend_freeze_wait_head, suspend_freeze_wake); | |
60 | } | |
61 | ||
62 | void freeze_wake(void) | |
63 | { | |
64 | suspend_freeze_wake = true; | |
65 | wake_up(&suspend_freeze_wait_head); | |
66 | } | |
67 | EXPORT_SYMBOL_GPL(freeze_wake); | |
68 | ||
9afc0d8b | 69 | bool valid_state(suspend_state_t state) |
5c95fef6 RW |
70 | { |
71 | /* | |
72 | * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level | |
73 | * support and need to be valid to the low level | |
74 | * implementation, no valid callback implies that none are valid. | |
75 | */ | |
76 | return suspend_ops && suspend_ops->valid && suspend_ops->valid(state); | |
77 | } | |
78 | ||
a9d70523 | 79 | /** |
55ae4519 RW |
80 | * suspend_set_ops - Set the global suspend method table. |
81 | * @ops: Suspend operations to use. | |
a9d70523 | 82 | */ |
2f55ac07 | 83 | void suspend_set_ops(const struct platform_suspend_ops *ops) |
a9d70523 | 84 | { |
5c95fef6 RW |
85 | suspend_state_t i; |
86 | ||
bcda53fa | 87 | lock_system_sleep(); |
5c95fef6 | 88 | |
a9d70523 | 89 | suspend_ops = ops; |
5c95fef6 RW |
90 | for (i = PM_SUSPEND_STANDBY; i <= PM_SUSPEND_MEM; i++) |
91 | pm_states[i].state = valid_state(i) ? i : 0; | |
92 | ||
bcda53fa | 93 | unlock_system_sleep(); |
a9d70523 | 94 | } |
a5e4fd87 | 95 | EXPORT_SYMBOL_GPL(suspend_set_ops); |
a9d70523 | 96 | |
a9d70523 | 97 | /** |
55ae4519 | 98 | * suspend_valid_only_mem - Generic memory-only valid callback. |
a9d70523 | 99 | * |
55ae4519 RW |
100 | * Platform drivers that implement mem suspend only and only need to check for |
101 | * that in their .valid() callback can use this instead of rolling their own | |
102 | * .valid() callback. | |
a9d70523 RW |
103 | */ |
104 | int suspend_valid_only_mem(suspend_state_t state) | |
105 | { | |
106 | return state == PM_SUSPEND_MEM; | |
107 | } | |
a5e4fd87 | 108 | EXPORT_SYMBOL_GPL(suspend_valid_only_mem); |
a9d70523 RW |
109 | |
110 | static int suspend_test(int level) | |
111 | { | |
112 | #ifdef CONFIG_PM_DEBUG | |
113 | if (pm_test_level == level) { | |
114 | printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n"); | |
115 | mdelay(5000); | |
116 | return 1; | |
117 | } | |
118 | #endif /* !CONFIG_PM_DEBUG */ | |
119 | return 0; | |
120 | } | |
121 | ||
122 | /** | |
55ae4519 | 123 | * suspend_prepare - Prepare for entering system sleep state. |
a9d70523 | 124 | * |
55ae4519 RW |
125 | * Common code run for every system sleep state that can be entered (except for |
126 | * hibernation). Run suspend notifiers, allocate the "suspend" console and | |
127 | * freeze processes. | |
a9d70523 | 128 | */ |
7e73c5ae | 129 | static int suspend_prepare(suspend_state_t state) |
a9d70523 RW |
130 | { |
131 | int error; | |
132 | ||
7e73c5ae | 133 | if (need_suspend_ops(state) && (!suspend_ops || !suspend_ops->enter)) |
a9d70523 RW |
134 | return -EPERM; |
135 | ||
136 | pm_prepare_console(); | |
137 | ||
138 | error = pm_notifier_call_chain(PM_SUSPEND_PREPARE); | |
139 | if (error) | |
140 | goto Finish; | |
141 | ||
a9d70523 | 142 | error = suspend_freeze_processes(); |
03afed8b | 143 | if (!error) |
a9d70523 RW |
144 | return 0; |
145 | ||
03afed8b TH |
146 | suspend_stats.failed_freeze++; |
147 | dpm_save_failed_step(SUSPEND_FREEZE); | |
a9d70523 RW |
148 | Finish: |
149 | pm_notifier_call_chain(PM_POST_SUSPEND); | |
150 | pm_restore_console(); | |
151 | return error; | |
152 | } | |
153 | ||
154 | /* default implementation */ | |
155 | void __attribute__ ((weak)) arch_suspend_disable_irqs(void) | |
156 | { | |
157 | local_irq_disable(); | |
158 | } | |
159 | ||
160 | /* default implementation */ | |
161 | void __attribute__ ((weak)) arch_suspend_enable_irqs(void) | |
162 | { | |
163 | local_irq_enable(); | |
164 | } | |
165 | ||
166 | /** | |
55ae4519 RW |
167 | * suspend_enter - Make the system enter the given sleep state. |
168 | * @state: System sleep state to enter. | |
169 | * @wakeup: Returns information that the sleep state should not be re-entered. | |
a9d70523 | 170 | * |
3b5fe852 | 171 | * This function should be called after devices have been suspended. |
a9d70523 | 172 | */ |
3b5fe852 | 173 | static int suspend_enter(suspend_state_t state, bool *wakeup) |
a9d70523 RW |
174 | { |
175 | int error; | |
176 | ||
7e73c5ae | 177 | if (need_suspend_ops(state) && suspend_ops->prepare) { |
a9d70523 RW |
178 | error = suspend_ops->prepare(); |
179 | if (error) | |
ce441011 | 180 | goto Platform_finish; |
a9d70523 RW |
181 | } |
182 | ||
cf579dfb | 183 | error = dpm_suspend_end(PMSG_SUSPEND); |
a9d70523 RW |
184 | if (error) { |
185 | printk(KERN_ERR "PM: Some devices failed to power down\n"); | |
ce441011 | 186 | goto Platform_finish; |
a9d70523 RW |
187 | } |
188 | ||
7e73c5ae | 189 | if (need_suspend_ops(state) && suspend_ops->prepare_late) { |
a9d70523 RW |
190 | error = suspend_ops->prepare_late(); |
191 | if (error) | |
ce441011 | 192 | goto Platform_wake; |
a9d70523 RW |
193 | } |
194 | ||
08605acc ZR |
195 | if (suspend_test(TEST_PLATFORM)) |
196 | goto Platform_wake; | |
197 | ||
7e73c5ae ZR |
198 | /* |
199 | * PM_SUSPEND_FREEZE equals | |
200 | * frozen processes + suspended devices + idle processors. | |
201 | * Thus we should invoke freeze_enter() soon after | |
202 | * all the devices are suspended. | |
203 | */ | |
204 | if (state == PM_SUSPEND_FREEZE) { | |
205 | freeze_enter(); | |
206 | goto Platform_wake; | |
207 | } | |
208 | ||
a9d70523 RW |
209 | error = disable_nonboot_cpus(); |
210 | if (error || suspend_test(TEST_CPUS)) | |
211 | goto Enable_cpus; | |
212 | ||
213 | arch_suspend_disable_irqs(); | |
214 | BUG_ON(!irqs_disabled()); | |
215 | ||
2e711c04 | 216 | error = syscore_suspend(); |
a9d70523 | 217 | if (!error) { |
3b5fe852 MH |
218 | *wakeup = pm_wakeup_pending(); |
219 | if (!(suspend_test(TEST_CORE) || *wakeup)) { | |
a9d70523 | 220 | error = suspend_ops->enter(state); |
c125e96f RW |
221 | events_check_enabled = false; |
222 | } | |
40dc166c | 223 | syscore_resume(); |
a9d70523 RW |
224 | } |
225 | ||
226 | arch_suspend_enable_irqs(); | |
227 | BUG_ON(irqs_disabled()); | |
228 | ||
229 | Enable_cpus: | |
230 | enable_nonboot_cpus(); | |
231 | ||
232 | Platform_wake: | |
7e73c5ae | 233 | if (need_suspend_ops(state) && suspend_ops->wake) |
a9d70523 RW |
234 | suspend_ops->wake(); |
235 | ||
cf579dfb | 236 | dpm_resume_start(PMSG_RESUME); |
a9d70523 | 237 | |
ce441011 | 238 | Platform_finish: |
7e73c5ae | 239 | if (need_suspend_ops(state) && suspend_ops->finish) |
a9d70523 RW |
240 | suspend_ops->finish(); |
241 | ||
242 | return error; | |
243 | } | |
244 | ||
245 | /** | |
55ae4519 RW |
246 | * suspend_devices_and_enter - Suspend devices and enter system sleep state. |
247 | * @state: System sleep state to enter. | |
a9d70523 RW |
248 | */ |
249 | int suspend_devices_and_enter(suspend_state_t state) | |
250 | { | |
251 | int error; | |
3b5fe852 | 252 | bool wakeup = false; |
a9d70523 | 253 | |
7e73c5ae | 254 | if (need_suspend_ops(state) && !suspend_ops) |
a9d70523 RW |
255 | return -ENOSYS; |
256 | ||
6fa3eb70 S |
257 | #ifdef CONFIG_TOI |
258 | drop_pagecache(); | |
259 | #endif | |
260 | ||
938cfed1 | 261 | trace_machine_suspend(state); |
7e73c5ae | 262 | if (need_suspend_ops(state) && suspend_ops->begin) { |
a9d70523 RW |
263 | error = suspend_ops->begin(state); |
264 | if (error) | |
265 | goto Close; | |
266 | } | |
267 | suspend_console(); | |
443772d4 | 268 | ftrace_stop(); |
a9d70523 RW |
269 | suspend_test_start(); |
270 | error = dpm_suspend_start(PMSG_SUSPEND); | |
271 | if (error) { | |
272 | printk(KERN_ERR "PM: Some devices failed to suspend\n"); | |
273 | goto Recover_platform; | |
274 | } | |
275 | suspend_test_finish("suspend devices"); | |
276 | if (suspend_test(TEST_DEVICES)) | |
277 | goto Recover_platform; | |
278 | ||
3b5fe852 MH |
279 | do { |
280 | error = suspend_enter(state, &wakeup); | |
7e73c5ae | 281 | } while (!error && !wakeup && need_suspend_ops(state) |
3b5fe852 | 282 | && suspend_ops->suspend_again && suspend_ops->suspend_again()); |
a9d70523 RW |
283 | |
284 | Resume_devices: | |
285 | suspend_test_start(); | |
286 | dpm_resume_end(PMSG_RESUME); | |
287 | suspend_test_finish("resume devices"); | |
443772d4 | 288 | ftrace_start(); |
a9d70523 RW |
289 | resume_console(); |
290 | Close: | |
7e73c5ae | 291 | if (need_suspend_ops(state) && suspend_ops->end) |
a9d70523 | 292 | suspend_ops->end(); |
938cfed1 | 293 | trace_machine_suspend(PWR_EVENT_EXIT); |
a9d70523 RW |
294 | return error; |
295 | ||
296 | Recover_platform: | |
7e73c5ae | 297 | if (need_suspend_ops(state) && suspend_ops->recover) |
a9d70523 RW |
298 | suspend_ops->recover(); |
299 | goto Resume_devices; | |
300 | } | |
6fa3eb70 | 301 | EXPORT_SYMBOL_GPL(suspend_devices_and_enter); |
a9d70523 RW |
302 | |
303 | /** | |
55ae4519 | 304 | * suspend_finish - Clean up before finishing the suspend sequence. |
a9d70523 | 305 | * |
55ae4519 RW |
306 | * Call platform code to clean up, restart processes, and free the console that |
307 | * we've allocated. This routine is not called for hibernation. | |
a9d70523 RW |
308 | */ |
309 | static void suspend_finish(void) | |
310 | { | |
311 | suspend_thaw_processes(); | |
a9d70523 RW |
312 | pm_notifier_call_chain(PM_POST_SUSPEND); |
313 | pm_restore_console(); | |
314 | } | |
315 | ||
316 | /** | |
55ae4519 RW |
317 | * enter_state - Do common work needed to enter system sleep state. |
318 | * @state: System sleep state to enter. | |
a9d70523 | 319 | * |
55ae4519 RW |
320 | * Make sure that no one else is trying to put the system into a sleep state. |
321 | * Fail if that's not the case. Otherwise, prepare for system suspend, make the | |
322 | * system enter the given sleep state and clean up after wakeup. | |
a9d70523 | 323 | */ |
6fa3eb70 S |
324 | //<20130327> <marc.huang> merge from android kernel 3.0 - modify enter_state function to non-static |
325 | int enter_state(suspend_state_t state) | |
a9d70523 RW |
326 | { |
327 | int error; | |
328 | ||
5c95fef6 RW |
329 | if (state == PM_SUSPEND_FREEZE) { |
330 | #ifdef CONFIG_PM_DEBUG | |
331 | if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) { | |
332 | pr_warning("PM: Unsupported test mode for freeze state," | |
333 | "please choose none/freezer/devices/platform.\n"); | |
334 | return -EAGAIN; | |
335 | } | |
336 | #endif | |
337 | } else if (!valid_state(state)) { | |
338 | return -EINVAL; | |
339 | } | |
a9d70523 RW |
340 | if (!mutex_trylock(&pm_mutex)) |
341 | return -EBUSY; | |
342 | ||
7e73c5ae ZR |
343 | if (state == PM_SUSPEND_FREEZE) |
344 | freeze_begin(); | |
345 | ||
a9d70523 | 346 | printk(KERN_INFO "PM: Syncing filesystems ... "); |
6fa3eb70 | 347 | #if 1 |
a9d70523 | 348 | sys_sync(); |
6fa3eb70 S |
349 | #else /* sys_sync WQ ver2.0 use */ |
350 | //[MTK] | |
351 | suspend_syssync_enqueue(); | |
352 | suspend_check_sys_sync_done(); | |
353 | #endif | |
a9d70523 RW |
354 | printk("done.\n"); |
355 | ||
e89547b8 | 356 | pr_debug("PM: Preparing system for %s sleep\n", pm_states[state].label); |
7e73c5ae | 357 | error = suspend_prepare(state); |
a9d70523 RW |
358 | if (error) |
359 | goto Unlock; | |
360 | ||
361 | if (suspend_test(TEST_FREEZER)) | |
362 | goto Finish; | |
363 | ||
e89547b8 | 364 | pr_debug("PM: Entering %s sleep\n", pm_states[state].label); |
87186475 | 365 | pm_restrict_gfp_mask(); |
a9d70523 | 366 | error = suspend_devices_and_enter(state); |
87186475 | 367 | pm_restore_gfp_mask(); |
a9d70523 RW |
368 | |
369 | Finish: | |
370 | pr_debug("PM: Finishing wakeup.\n"); | |
371 | suspend_finish(); | |
372 | Unlock: | |
373 | mutex_unlock(&pm_mutex); | |
374 | return error; | |
375 | } | |
376 | ||
6fa3eb70 S |
377 | static void pm_suspend_marker(char *annotation) |
378 | { | |
379 | struct timespec ts; | |
380 | struct rtc_time tm; | |
381 | ||
382 | getnstimeofday(&ts); | |
383 | rtc_time_to_tm(ts.tv_sec, &tm); | |
384 | pr_info("PM: suspend %s %d-%02d-%02d %02d:%02d:%02d.%09lu UTC\n", | |
385 | annotation, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, | |
386 | tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec); | |
387 | } | |
388 | ||
a9d70523 | 389 | /** |
55ae4519 RW |
390 | * pm_suspend - Externally visible function for suspending the system. |
391 | * @state: System sleep state to enter. | |
a9d70523 | 392 | * |
55ae4519 RW |
393 | * Check if the value of @state represents one of the supported states, |
394 | * execute enter_state() and update system suspend statistics. | |
a9d70523 RW |
395 | */ |
396 | int pm_suspend(suspend_state_t state) | |
397 | { | |
bc25cf50 RW |
398 | int error; |
399 | ||
400 | if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX) | |
401 | return -EINVAL; | |
402 | ||
6fa3eb70 | 403 | pm_suspend_marker("entry"); |
bc25cf50 RW |
404 | error = enter_state(state); |
405 | if (error) { | |
406 | suspend_stats.fail++; | |
407 | dpm_save_failed_errno(error); | |
408 | } else { | |
409 | suspend_stats.success++; | |
2a77c46d | 410 | } |
6fa3eb70 | 411 | pm_suspend_marker("exit"); |
bc25cf50 | 412 | return error; |
a9d70523 RW |
413 | } |
414 | EXPORT_SYMBOL(pm_suspend); |