blob: 6e076f359dcc70e2f0982ab6b651ebe0fa76e4d9 [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +01002/*
3 * syscore.c - Execution of system core operations.
4 *
5 * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +01006 */
7
8#include <linux/syscore_ops.h>
9#include <linux/mutex.h>
10#include <linux/module.h>
Thomas Gleixner9ce7a252014-08-29 14:00:16 +020011#include <linux/suspend.h>
Todd E Brandtbb3632c2014-06-06 05:40:17 -070012#include <trace/events/power.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010013
14static LIST_HEAD(syscore_ops_list);
15static DEFINE_MUTEX(syscore_ops_lock);
16
17/**
18 * register_syscore_ops - Register a set of system core operations.
19 * @ops: System core operations to register.
20 */
21void register_syscore_ops(struct syscore_ops *ops)
22{
23 mutex_lock(&syscore_ops_lock);
24 list_add_tail(&ops->node, &syscore_ops_list);
25 mutex_unlock(&syscore_ops_lock);
26}
27EXPORT_SYMBOL_GPL(register_syscore_ops);
28
29/**
30 * unregister_syscore_ops - Unregister a set of system core operations.
31 * @ops: System core operations to unregister.
32 */
33void unregister_syscore_ops(struct syscore_ops *ops)
34{
35 mutex_lock(&syscore_ops_lock);
36 list_del(&ops->node);
37 mutex_unlock(&syscore_ops_lock);
38}
39EXPORT_SYMBOL_GPL(unregister_syscore_ops);
40
41#ifdef CONFIG_PM_SLEEP
42/**
43 * syscore_suspend - Execute all the registered system core suspend callbacks.
44 *
45 * This function is executed with one CPU on-line and disabled interrupts.
46 */
47int syscore_suspend(void)
48{
49 struct syscore_ops *ops;
50 int ret = 0;
51
Todd E Brandtbb3632c2014-06-06 05:40:17 -070052 trace_suspend_resume(TPS("syscore_suspend"), 0, true);
Colin Cross88759622011-07-11 10:51:49 +020053 pr_debug("Checking wakeup interrupts\n");
54
55 /* Return error code if there are any wakeup interrupts pending. */
Thomas Gleixner9ce7a252014-08-29 14:00:16 +020056 if (pm_wakeup_pending())
57 return -EBUSY;
Colin Cross88759622011-07-11 10:51:49 +020058
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010059 WARN_ONCE(!irqs_disabled(),
60 "Interrupts enabled before system core suspend.\n");
61
62 list_for_each_entry_reverse(ops, &syscore_ops_list, node)
63 if (ops->suspend) {
64 if (initcall_debug)
65 pr_info("PM: Calling %pF\n", ops->suspend);
66 ret = ops->suspend();
67 if (ret)
68 goto err_out;
69 WARN_ONCE(!irqs_disabled(),
70 "Interrupts enabled after %pF\n", ops->suspend);
71 }
72
Todd E Brandtbb3632c2014-06-06 05:40:17 -070073 trace_suspend_resume(TPS("syscore_suspend"), 0, false);
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010074 return 0;
75
76 err_out:
77 pr_err("PM: System core suspend callback %pF failed.\n", ops->suspend);
78
79 list_for_each_entry_continue(ops, &syscore_ops_list, node)
80 if (ops->resume)
81 ops->resume();
82
83 return ret;
84}
Rafael J. Wysocki19234c02011-04-20 00:36:11 +020085EXPORT_SYMBOL_GPL(syscore_suspend);
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010086
87/**
88 * syscore_resume - Execute all the registered system core resume callbacks.
89 *
90 * This function is executed with one CPU on-line and disabled interrupts.
91 */
92void syscore_resume(void)
93{
94 struct syscore_ops *ops;
95
Todd E Brandtbb3632c2014-06-06 05:40:17 -070096 trace_suspend_resume(TPS("syscore_resume"), 0, true);
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010097 WARN_ONCE(!irqs_disabled(),
98 "Interrupts enabled before system core resume.\n");
99
100 list_for_each_entry(ops, &syscore_ops_list, node)
101 if (ops->resume) {
102 if (initcall_debug)
103 pr_info("PM: Calling %pF\n", ops->resume);
104 ops->resume();
105 WARN_ONCE(!irqs_disabled(),
106 "Interrupts enabled after %pF\n", ops->resume);
107 }
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700108 trace_suspend_resume(TPS("syscore_resume"), 0, false);
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100109}
Rafael J. Wysocki19234c02011-04-20 00:36:11 +0200110EXPORT_SYMBOL_GPL(syscore_resume);
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100111#endif /* CONFIG_PM_SLEEP */
112
113/**
114 * syscore_shutdown - Execute all the registered system core shutdown callbacks.
115 */
116void syscore_shutdown(void)
117{
118 struct syscore_ops *ops;
119
120 mutex_lock(&syscore_ops_lock);
121
122 list_for_each_entry_reverse(ops, &syscore_ops_list, node)
123 if (ops->shutdown) {
124 if (initcall_debug)
125 pr_info("PM: Calling %pF\n", ops->shutdown);
126 ops->shutdown();
127 }
128
129 mutex_unlock(&syscore_ops_lock);
130}