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