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