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