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