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