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