Greg Kroah-Hartman | 989d42e | 2017-11-07 17:30:07 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 2 | /* |
| 3 | * syscore.c - Execution of system core operations. |
| 4 | * |
| 5 | * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc. |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/syscore_ops.h> |
| 9 | #include <linux/mutex.h> |
| 10 | #include <linux/module.h> |
Thomas Gleixner | 9ce7a25 | 2014-08-29 14:00:16 +0200 | [diff] [blame] | 11 | #include <linux/suspend.h> |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 12 | #include <trace/events/power.h> |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 13 | |
| 14 | static LIST_HEAD(syscore_ops_list); |
| 15 | static 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 | */ |
| 21 | void 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 | } |
| 27 | EXPORT_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 | */ |
| 33 | void 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 | } |
| 39 | EXPORT_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 | */ |
| 47 | int syscore_suspend(void) |
| 48 | { |
| 49 | struct syscore_ops *ops; |
| 50 | int ret = 0; |
| 51 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 52 | trace_suspend_resume(TPS("syscore_suspend"), 0, true); |
Stephen Boyd | 81b1422 | 2020-08-06 14:46:33 -0700 | [diff] [blame] | 53 | pm_pr_dbg("Checking wakeup interrupts\n"); |
Colin Cross | 8875962 | 2011-07-11 10:51:49 +0200 | [diff] [blame] | 54 | |
| 55 | /* Return error code if there are any wakeup interrupts pending. */ |
Thomas Gleixner | 9ce7a25 | 2014-08-29 14:00:16 +0200 | [diff] [blame] | 56 | if (pm_wakeup_pending()) |
| 57 | return -EBUSY; |
Colin Cross | 8875962 | 2011-07-11 10:51:49 +0200 | [diff] [blame] | 58 | |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 59 | 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) { |
Stephen Boyd | 81b1422 | 2020-08-06 14:46:33 -0700 | [diff] [blame] | 64 | pm_pr_dbg("Calling %pS\n", ops->suspend); |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 65 | ret = ops->suspend(); |
| 66 | if (ret) |
| 67 | goto err_out; |
| 68 | WARN_ONCE(!irqs_disabled(), |
Sakari Ailus | d75f773 | 2019-03-25 21:32:28 +0200 | [diff] [blame] | 69 | "Interrupts enabled after %pS\n", ops->suspend); |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 72 | trace_suspend_resume(TPS("syscore_suspend"), 0, false); |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 73 | return 0; |
| 74 | |
| 75 | err_out: |
Sakari Ailus | d75f773 | 2019-03-25 21:32:28 +0200 | [diff] [blame] | 76 | pr_err("PM: System core suspend callback %pS failed.\n", ops->suspend); |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 77 | |
| 78 | list_for_each_entry_continue(ops, &syscore_ops_list, node) |
| 79 | if (ops->resume) |
| 80 | ops->resume(); |
| 81 | |
| 82 | return ret; |
| 83 | } |
Rafael J. Wysocki | 19234c0 | 2011-04-20 00:36:11 +0200 | [diff] [blame] | 84 | EXPORT_SYMBOL_GPL(syscore_suspend); |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * syscore_resume - Execute all the registered system core resume callbacks. |
| 88 | * |
| 89 | * This function is executed with one CPU on-line and disabled interrupts. |
| 90 | */ |
| 91 | void syscore_resume(void) |
| 92 | { |
| 93 | struct syscore_ops *ops; |
| 94 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 95 | trace_suspend_resume(TPS("syscore_resume"), 0, true); |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 96 | WARN_ONCE(!irqs_disabled(), |
| 97 | "Interrupts enabled before system core resume.\n"); |
| 98 | |
| 99 | list_for_each_entry(ops, &syscore_ops_list, node) |
| 100 | if (ops->resume) { |
Stephen Boyd | 81b1422 | 2020-08-06 14:46:33 -0700 | [diff] [blame] | 101 | pm_pr_dbg("Calling %pS\n", ops->resume); |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 102 | ops->resume(); |
| 103 | WARN_ONCE(!irqs_disabled(), |
Sakari Ailus | d75f773 | 2019-03-25 21:32:28 +0200 | [diff] [blame] | 104 | "Interrupts enabled after %pS\n", ops->resume); |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 105 | } |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 106 | trace_suspend_resume(TPS("syscore_resume"), 0, false); |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 107 | } |
Rafael J. Wysocki | 19234c0 | 2011-04-20 00:36:11 +0200 | [diff] [blame] | 108 | EXPORT_SYMBOL_GPL(syscore_resume); |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 109 | #endif /* CONFIG_PM_SLEEP */ |
| 110 | |
| 111 | /** |
| 112 | * syscore_shutdown - Execute all the registered system core shutdown callbacks. |
| 113 | */ |
| 114 | void syscore_shutdown(void) |
| 115 | { |
| 116 | struct syscore_ops *ops; |
| 117 | |
| 118 | mutex_lock(&syscore_ops_lock); |
| 119 | |
| 120 | list_for_each_entry_reverse(ops, &syscore_ops_list, node) |
| 121 | if (ops->shutdown) { |
| 122 | if (initcall_debug) |
Sakari Ailus | d75f773 | 2019-03-25 21:32:28 +0200 | [diff] [blame] | 123 | pr_info("PM: Calling %pS\n", ops->shutdown); |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 124 | ops->shutdown(); |
| 125 | } |
| 126 | |
| 127 | mutex_unlock(&syscore_ops_lock); |
| 128 | } |