Merge tag 'next-fixes-20181012' of git://git.kernel.org/pub/scm/linux/kernel/git...
[muen/linux.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 #define pr_fmt(fmt) "PM: " fmt
12
13 #include <linux/string.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
16 #include <linux/init.h>
17 #include <linux/console.h>
18 #include <linux/cpu.h>
19 #include <linux/cpuidle.h>
20 #include <linux/syscalls.h>
21 #include <linux/gfp.h>
22 #include <linux/io.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/mm.h>
26 #include <linux/slab.h>
27 #include <linux/export.h>
28 #include <linux/suspend.h>
29 #include <linux/syscore_ops.h>
30 #include <linux/swait.h>
31 #include <linux/ftrace.h>
32 #include <trace/events/power.h>
33 #include <linux/compiler.h>
34 #include <linux/moduleparam.h>
35
36 #include "power.h"
37
38 const char * const pm_labels[] = {
39         [PM_SUSPEND_TO_IDLE] = "freeze",
40         [PM_SUSPEND_STANDBY] = "standby",
41         [PM_SUSPEND_MEM] = "mem",
42 };
43 const char *pm_states[PM_SUSPEND_MAX];
44 static const char * const mem_sleep_labels[] = {
45         [PM_SUSPEND_TO_IDLE] = "s2idle",
46         [PM_SUSPEND_STANDBY] = "shallow",
47         [PM_SUSPEND_MEM] = "deep",
48 };
49 const char *mem_sleep_states[PM_SUSPEND_MAX];
50
51 suspend_state_t mem_sleep_current = PM_SUSPEND_TO_IDLE;
52 suspend_state_t mem_sleep_default = PM_SUSPEND_MAX;
53 suspend_state_t pm_suspend_target_state;
54 EXPORT_SYMBOL_GPL(pm_suspend_target_state);
55
56 unsigned int pm_suspend_global_flags;
57 EXPORT_SYMBOL_GPL(pm_suspend_global_flags);
58
59 static const struct platform_suspend_ops *suspend_ops;
60 static const struct platform_s2idle_ops *s2idle_ops;
61 static DECLARE_SWAIT_QUEUE_HEAD(s2idle_wait_head);
62
63 enum s2idle_states __read_mostly s2idle_state;
64 static DEFINE_RAW_SPINLOCK(s2idle_lock);
65
66 void s2idle_set_ops(const struct platform_s2idle_ops *ops)
67 {
68         lock_system_sleep();
69         s2idle_ops = ops;
70         unlock_system_sleep();
71 }
72
73 static void s2idle_begin(void)
74 {
75         s2idle_state = S2IDLE_STATE_NONE;
76 }
77
78 static void s2idle_enter(void)
79 {
80         trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, true);
81
82         raw_spin_lock_irq(&s2idle_lock);
83         if (pm_wakeup_pending())
84                 goto out;
85
86         s2idle_state = S2IDLE_STATE_ENTER;
87         raw_spin_unlock_irq(&s2idle_lock);
88
89         get_online_cpus();
90         cpuidle_resume();
91
92         /* Push all the CPUs into the idle loop. */
93         wake_up_all_idle_cpus();
94         /* Make the current CPU wait so it can enter the idle loop too. */
95         swait_event_exclusive(s2idle_wait_head,
96                     s2idle_state == S2IDLE_STATE_WAKE);
97
98         cpuidle_pause();
99         put_online_cpus();
100
101         raw_spin_lock_irq(&s2idle_lock);
102
103  out:
104         s2idle_state = S2IDLE_STATE_NONE;
105         raw_spin_unlock_irq(&s2idle_lock);
106
107         trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, false);
108 }
109
110 static void s2idle_loop(void)
111 {
112         pm_pr_dbg("suspend-to-idle\n");
113
114         for (;;) {
115                 int error;
116
117                 dpm_noirq_begin();
118
119                 /*
120                  * Suspend-to-idle equals
121                  * frozen processes + suspended devices + idle processors.
122                  * Thus s2idle_enter() should be called right after
123                  * all devices have been suspended.
124                  *
125                  * Wakeups during the noirq suspend of devices may be spurious,
126                  * so prevent them from terminating the loop right away.
127                  */
128                 error = dpm_noirq_suspend_devices(PMSG_SUSPEND);
129                 if (!error)
130                         s2idle_enter();
131                 else if (error == -EBUSY && pm_wakeup_pending())
132                         error = 0;
133
134                 if (!error && s2idle_ops && s2idle_ops->wake)
135                         s2idle_ops->wake();
136
137                 dpm_noirq_resume_devices(PMSG_RESUME);
138
139                 dpm_noirq_end();
140
141                 if (error)
142                         break;
143
144                 if (s2idle_ops && s2idle_ops->sync)
145                         s2idle_ops->sync();
146
147                 if (pm_wakeup_pending())
148                         break;
149
150                 pm_wakeup_clear(false);
151         }
152
153         pm_pr_dbg("resume from suspend-to-idle\n");
154 }
155
156 void s2idle_wake(void)
157 {
158         unsigned long flags;
159
160         raw_spin_lock_irqsave(&s2idle_lock, flags);
161         if (s2idle_state > S2IDLE_STATE_NONE) {
162                 s2idle_state = S2IDLE_STATE_WAKE;
163                 swake_up_one(&s2idle_wait_head);
164         }
165         raw_spin_unlock_irqrestore(&s2idle_lock, flags);
166 }
167 EXPORT_SYMBOL_GPL(s2idle_wake);
168
169 static bool valid_state(suspend_state_t state)
170 {
171         /*
172          * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level
173          * support and need to be valid to the low level
174          * implementation, no valid callback implies that none are valid.
175          */
176         return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
177 }
178
179 void __init pm_states_init(void)
180 {
181         /* "mem" and "freeze" are always present in /sys/power/state. */
182         pm_states[PM_SUSPEND_MEM] = pm_labels[PM_SUSPEND_MEM];
183         pm_states[PM_SUSPEND_TO_IDLE] = pm_labels[PM_SUSPEND_TO_IDLE];
184         /*
185          * Suspend-to-idle should be supported even without any suspend_ops,
186          * initialize mem_sleep_states[] accordingly here.
187          */
188         mem_sleep_states[PM_SUSPEND_TO_IDLE] = mem_sleep_labels[PM_SUSPEND_TO_IDLE];
189 }
190
191 static int __init mem_sleep_default_setup(char *str)
192 {
193         suspend_state_t state;
194
195         for (state = PM_SUSPEND_TO_IDLE; state <= PM_SUSPEND_MEM; state++)
196                 if (mem_sleep_labels[state] &&
197                     !strcmp(str, mem_sleep_labels[state])) {
198                         mem_sleep_default = state;
199                         break;
200                 }
201
202         return 1;
203 }
204 __setup("mem_sleep_default=", mem_sleep_default_setup);
205
206 /**
207  * suspend_set_ops - Set the global suspend method table.
208  * @ops: Suspend operations to use.
209  */
210 void suspend_set_ops(const struct platform_suspend_ops *ops)
211 {
212         lock_system_sleep();
213
214         suspend_ops = ops;
215
216         if (valid_state(PM_SUSPEND_STANDBY)) {
217                 mem_sleep_states[PM_SUSPEND_STANDBY] = mem_sleep_labels[PM_SUSPEND_STANDBY];
218                 pm_states[PM_SUSPEND_STANDBY] = pm_labels[PM_SUSPEND_STANDBY];
219                 if (mem_sleep_default == PM_SUSPEND_STANDBY)
220                         mem_sleep_current = PM_SUSPEND_STANDBY;
221         }
222         if (valid_state(PM_SUSPEND_MEM)) {
223                 mem_sleep_states[PM_SUSPEND_MEM] = mem_sleep_labels[PM_SUSPEND_MEM];
224                 if (mem_sleep_default >= PM_SUSPEND_MEM)
225                         mem_sleep_current = PM_SUSPEND_MEM;
226         }
227
228         unlock_system_sleep();
229 }
230 EXPORT_SYMBOL_GPL(suspend_set_ops);
231
232 /**
233  * suspend_valid_only_mem - Generic memory-only valid callback.
234  *
235  * Platform drivers that implement mem suspend only and only need to check for
236  * that in their .valid() callback can use this instead of rolling their own
237  * .valid() callback.
238  */
239 int suspend_valid_only_mem(suspend_state_t state)
240 {
241         return state == PM_SUSPEND_MEM;
242 }
243 EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
244
245 static bool sleep_state_supported(suspend_state_t state)
246 {
247         return state == PM_SUSPEND_TO_IDLE || (suspend_ops && suspend_ops->enter);
248 }
249
250 static int platform_suspend_prepare(suspend_state_t state)
251 {
252         return state != PM_SUSPEND_TO_IDLE && suspend_ops->prepare ?
253                 suspend_ops->prepare() : 0;
254 }
255
256 static int platform_suspend_prepare_late(suspend_state_t state)
257 {
258         return state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->prepare ?
259                 s2idle_ops->prepare() : 0;
260 }
261
262 static int platform_suspend_prepare_noirq(suspend_state_t state)
263 {
264         return state != PM_SUSPEND_TO_IDLE && suspend_ops->prepare_late ?
265                 suspend_ops->prepare_late() : 0;
266 }
267
268 static void platform_resume_noirq(suspend_state_t state)
269 {
270         if (state != PM_SUSPEND_TO_IDLE && suspend_ops->wake)
271                 suspend_ops->wake();
272 }
273
274 static void platform_resume_early(suspend_state_t state)
275 {
276         if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->restore)
277                 s2idle_ops->restore();
278 }
279
280 static void platform_resume_finish(suspend_state_t state)
281 {
282         if (state != PM_SUSPEND_TO_IDLE && suspend_ops->finish)
283                 suspend_ops->finish();
284 }
285
286 static int platform_suspend_begin(suspend_state_t state)
287 {
288         if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->begin)
289                 return s2idle_ops->begin();
290         else if (suspend_ops && suspend_ops->begin)
291                 return suspend_ops->begin(state);
292         else
293                 return 0;
294 }
295
296 static void platform_resume_end(suspend_state_t state)
297 {
298         if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->end)
299                 s2idle_ops->end();
300         else if (suspend_ops && suspend_ops->end)
301                 suspend_ops->end();
302 }
303
304 static void platform_recover(suspend_state_t state)
305 {
306         if (state != PM_SUSPEND_TO_IDLE && suspend_ops->recover)
307                 suspend_ops->recover();
308 }
309
310 static bool platform_suspend_again(suspend_state_t state)
311 {
312         return state != PM_SUSPEND_TO_IDLE && suspend_ops->suspend_again ?
313                 suspend_ops->suspend_again() : false;
314 }
315
316 #ifdef CONFIG_PM_DEBUG
317 static unsigned int pm_test_delay = 5;
318 module_param(pm_test_delay, uint, 0644);
319 MODULE_PARM_DESC(pm_test_delay,
320                  "Number of seconds to wait before resuming from suspend test");
321 #endif
322
323 static int suspend_test(int level)
324 {
325 #ifdef CONFIG_PM_DEBUG
326         if (pm_test_level == level) {
327                 pr_info("suspend debug: Waiting for %d second(s).\n",
328                                 pm_test_delay);
329                 mdelay(pm_test_delay * 1000);
330                 return 1;
331         }
332 #endif /* !CONFIG_PM_DEBUG */
333         return 0;
334 }
335
336 /**
337  * suspend_prepare - Prepare for entering system sleep state.
338  *
339  * Common code run for every system sleep state that can be entered (except for
340  * hibernation).  Run suspend notifiers, allocate the "suspend" console and
341  * freeze processes.
342  */
343 static int suspend_prepare(suspend_state_t state)
344 {
345         int error, nr_calls = 0;
346
347         if (!sleep_state_supported(state))
348                 return -EPERM;
349
350         pm_prepare_console();
351
352         error = __pm_notifier_call_chain(PM_SUSPEND_PREPARE, -1, &nr_calls);
353         if (error) {
354                 nr_calls--;
355                 goto Finish;
356         }
357
358         trace_suspend_resume(TPS("freeze_processes"), 0, true);
359         error = suspend_freeze_processes();
360         trace_suspend_resume(TPS("freeze_processes"), 0, false);
361         if (!error)
362                 return 0;
363
364         suspend_stats.failed_freeze++;
365         dpm_save_failed_step(SUSPEND_FREEZE);
366  Finish:
367         __pm_notifier_call_chain(PM_POST_SUSPEND, nr_calls, NULL);
368         pm_restore_console();
369         return error;
370 }
371
372 /* default implementation */
373 void __weak arch_suspend_disable_irqs(void)
374 {
375         local_irq_disable();
376 }
377
378 /* default implementation */
379 void __weak arch_suspend_enable_irqs(void)
380 {
381         local_irq_enable();
382 }
383
384 /**
385  * suspend_enter - Make the system enter the given sleep state.
386  * @state: System sleep state to enter.
387  * @wakeup: Returns information that the sleep state should not be re-entered.
388  *
389  * This function should be called after devices have been suspended.
390  */
391 static int suspend_enter(suspend_state_t state, bool *wakeup)
392 {
393         int error;
394
395         error = platform_suspend_prepare(state);
396         if (error)
397                 goto Platform_finish;
398
399         error = dpm_suspend_late(PMSG_SUSPEND);
400         if (error) {
401                 pr_err("late suspend of devices failed\n");
402                 goto Platform_finish;
403         }
404         error = platform_suspend_prepare_late(state);
405         if (error)
406                 goto Devices_early_resume;
407
408         if (state == PM_SUSPEND_TO_IDLE && pm_test_level != TEST_PLATFORM) {
409                 s2idle_loop();
410                 goto Platform_early_resume;
411         }
412
413         error = dpm_suspend_noirq(PMSG_SUSPEND);
414         if (error) {
415                 pr_err("noirq suspend of devices failed\n");
416                 goto Platform_early_resume;
417         }
418         error = platform_suspend_prepare_noirq(state);
419         if (error)
420                 goto Platform_wake;
421
422         if (suspend_test(TEST_PLATFORM))
423                 goto Platform_wake;
424
425         error = disable_nonboot_cpus();
426         if (error || suspend_test(TEST_CPUS))
427                 goto Enable_cpus;
428
429         arch_suspend_disable_irqs();
430         BUG_ON(!irqs_disabled());
431
432         system_state = SYSTEM_SUSPEND;
433
434         error = syscore_suspend();
435         if (!error) {
436                 *wakeup = pm_wakeup_pending();
437                 if (!(suspend_test(TEST_CORE) || *wakeup)) {
438                         trace_suspend_resume(TPS("machine_suspend"),
439                                 state, true);
440                         error = suspend_ops->enter(state);
441                         trace_suspend_resume(TPS("machine_suspend"),
442                                 state, false);
443                 } else if (*wakeup) {
444                         error = -EBUSY;
445                 }
446                 syscore_resume();
447         }
448
449         system_state = SYSTEM_RUNNING;
450
451         arch_suspend_enable_irqs();
452         BUG_ON(irqs_disabled());
453
454  Enable_cpus:
455         enable_nonboot_cpus();
456
457  Platform_wake:
458         platform_resume_noirq(state);
459         dpm_resume_noirq(PMSG_RESUME);
460
461  Platform_early_resume:
462         platform_resume_early(state);
463
464  Devices_early_resume:
465         dpm_resume_early(PMSG_RESUME);
466
467  Platform_finish:
468         platform_resume_finish(state);
469         return error;
470 }
471
472 /**
473  * suspend_devices_and_enter - Suspend devices and enter system sleep state.
474  * @state: System sleep state to enter.
475  */
476 int suspend_devices_and_enter(suspend_state_t state)
477 {
478         int error;
479         bool wakeup = false;
480
481         if (!sleep_state_supported(state))
482                 return -ENOSYS;
483
484         pm_suspend_target_state = state;
485
486         error = platform_suspend_begin(state);
487         if (error)
488                 goto Close;
489
490         suspend_console();
491         suspend_test_start();
492         error = dpm_suspend_start(PMSG_SUSPEND);
493         if (error) {
494                 pr_err("Some devices failed to suspend, or early wake event detected\n");
495                 goto Recover_platform;
496         }
497         suspend_test_finish("suspend devices");
498         if (suspend_test(TEST_DEVICES))
499                 goto Recover_platform;
500
501         do {
502                 error = suspend_enter(state, &wakeup);
503         } while (!error && !wakeup && platform_suspend_again(state));
504
505  Resume_devices:
506         suspend_test_start();
507         dpm_resume_end(PMSG_RESUME);
508         suspend_test_finish("resume devices");
509         trace_suspend_resume(TPS("resume_console"), state, true);
510         resume_console();
511         trace_suspend_resume(TPS("resume_console"), state, false);
512
513  Close:
514         platform_resume_end(state);
515         pm_suspend_target_state = PM_SUSPEND_ON;
516         return error;
517
518  Recover_platform:
519         platform_recover(state);
520         goto Resume_devices;
521 }
522
523 /**
524  * suspend_finish - Clean up before finishing the suspend sequence.
525  *
526  * Call platform code to clean up, restart processes, and free the console that
527  * we've allocated. This routine is not called for hibernation.
528  */
529 static void suspend_finish(void)
530 {
531         suspend_thaw_processes();
532         pm_notifier_call_chain(PM_POST_SUSPEND);
533         pm_restore_console();
534 }
535
536 /**
537  * enter_state - Do common work needed to enter system sleep state.
538  * @state: System sleep state to enter.
539  *
540  * Make sure that no one else is trying to put the system into a sleep state.
541  * Fail if that's not the case.  Otherwise, prepare for system suspend, make the
542  * system enter the given sleep state and clean up after wakeup.
543  */
544 static int enter_state(suspend_state_t state)
545 {
546         int error;
547
548         trace_suspend_resume(TPS("suspend_enter"), state, true);
549         if (state == PM_SUSPEND_TO_IDLE) {
550 #ifdef CONFIG_PM_DEBUG
551                 if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) {
552                         pr_warn("Unsupported test mode for suspend to idle, please choose none/freezer/devices/platform.\n");
553                         return -EAGAIN;
554                 }
555 #endif
556         } else if (!valid_state(state)) {
557                 return -EINVAL;
558         }
559         if (!mutex_trylock(&system_transition_mutex))
560                 return -EBUSY;
561
562         if (state == PM_SUSPEND_TO_IDLE)
563                 s2idle_begin();
564
565 #ifndef CONFIG_SUSPEND_SKIP_SYNC
566         trace_suspend_resume(TPS("sync_filesystems"), 0, true);
567         pr_info("Syncing filesystems ... ");
568         ksys_sync();
569         pr_cont("done.\n");
570         trace_suspend_resume(TPS("sync_filesystems"), 0, false);
571 #endif
572
573         pm_pr_dbg("Preparing system for sleep (%s)\n", mem_sleep_labels[state]);
574         pm_suspend_clear_flags();
575         error = suspend_prepare(state);
576         if (error)
577                 goto Unlock;
578
579         if (suspend_test(TEST_FREEZER))
580                 goto Finish;
581
582         trace_suspend_resume(TPS("suspend_enter"), state, false);
583         pm_pr_dbg("Suspending system (%s)\n", mem_sleep_labels[state]);
584         pm_restrict_gfp_mask();
585         error = suspend_devices_and_enter(state);
586         pm_restore_gfp_mask();
587
588  Finish:
589         events_check_enabled = false;
590         pm_pr_dbg("Finishing wakeup.\n");
591         suspend_finish();
592  Unlock:
593         mutex_unlock(&system_transition_mutex);
594         return error;
595 }
596
597 /**
598  * pm_suspend - Externally visible function for suspending the system.
599  * @state: System sleep state to enter.
600  *
601  * Check if the value of @state represents one of the supported states,
602  * execute enter_state() and update system suspend statistics.
603  */
604 int pm_suspend(suspend_state_t state)
605 {
606         int error;
607
608         if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
609                 return -EINVAL;
610
611         pr_info("suspend entry (%s)\n", mem_sleep_labels[state]);
612         error = enter_state(state);
613         if (error) {
614                 suspend_stats.fail++;
615                 dpm_save_failed_errno(error);
616         } else {
617                 suspend_stats.success++;
618         }
619         pr_info("suspend exit\n");
620         return error;
621 }
622 EXPORT_SYMBOL(pm_suspend);