b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * syscore.c - Execution of system core operations. |
| 4 | * |
| 5 | * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/syscore_ops.h> |
| 9 | #include <linux/mutex.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/suspend.h> |
| 12 | #include <trace/events/power.h> |
| 13 | #include <linux/wakeup_reason.h> |
| 14 | |
| 15 | static LIST_HEAD(syscore_ops_list); |
| 16 | static DEFINE_MUTEX(syscore_ops_lock); |
| 17 | |
| 18 | /** |
| 19 | * register_syscore_ops - Register a set of system core operations. |
| 20 | * @ops: System core operations to register. |
| 21 | */ |
| 22 | void register_syscore_ops(struct syscore_ops *ops) |
| 23 | { |
| 24 | mutex_lock(&syscore_ops_lock); |
| 25 | list_add_tail(&ops->node, &syscore_ops_list); |
| 26 | mutex_unlock(&syscore_ops_lock); |
| 27 | } |
| 28 | EXPORT_SYMBOL_GPL(register_syscore_ops); |
| 29 | |
| 30 | /** |
| 31 | * unregister_syscore_ops - Unregister a set of system core operations. |
| 32 | * @ops: System core operations to unregister. |
| 33 | */ |
| 34 | void unregister_syscore_ops(struct syscore_ops *ops) |
| 35 | { |
| 36 | mutex_lock(&syscore_ops_lock); |
| 37 | list_del(&ops->node); |
| 38 | mutex_unlock(&syscore_ops_lock); |
| 39 | } |
| 40 | EXPORT_SYMBOL_GPL(unregister_syscore_ops); |
| 41 | |
| 42 | #ifdef CONFIG_PM_SLEEP |
| 43 | /** |
| 44 | * syscore_suspend - Execute all the registered system core suspend callbacks. |
| 45 | * |
| 46 | * This function is executed with one CPU on-line and disabled interrupts. |
| 47 | */ |
| 48 | int syscore_suspend(void) |
| 49 | { |
| 50 | struct syscore_ops *ops; |
| 51 | int ret = 0; |
| 52 | |
| 53 | trace_suspend_resume(TPS("syscore_suspend"), 0, true); |
| 54 | pr_debug("Checking wakeup interrupts\n"); |
| 55 | |
| 56 | /* Return error code if there are any wakeup interrupts pending. */ |
| 57 | if (pm_wakeup_pending()) |
| 58 | return -EBUSY; |
| 59 | |
| 60 | WARN_ONCE(!irqs_disabled(), |
| 61 | "Interrupts enabled before system core suspend.\n"); |
| 62 | |
| 63 | list_for_each_entry_reverse(ops, &syscore_ops_list, node) |
| 64 | if (ops->suspend) { |
| 65 | if (initcall_debug) |
| 66 | pr_info("PM: Calling %pS\n", ops->suspend); |
| 67 | ret = ops->suspend(); |
| 68 | if (ret) |
| 69 | goto err_out; |
| 70 | WARN_ONCE(!irqs_disabled(), |
| 71 | "Interrupts enabled after %pS\n", ops->suspend); |
| 72 | } |
| 73 | |
| 74 | trace_suspend_resume(TPS("syscore_suspend"), 0, false); |
| 75 | return 0; |
| 76 | |
| 77 | err_out: |
| 78 | log_suspend_abort_reason("System core suspend callback %pS failed", |
| 79 | ops->suspend); |
| 80 | pr_err("PM: System core suspend callback %pF failed.\n", ops->suspend); |
| 81 | |
| 82 | list_for_each_entry_continue(ops, &syscore_ops_list, node) |
| 83 | if (ops->resume) |
| 84 | ops->resume(); |
| 85 | |
| 86 | return ret; |
| 87 | } |
| 88 | EXPORT_SYMBOL_GPL(syscore_suspend); |
| 89 | |
| 90 | /** |
| 91 | * syscore_resume - Execute all the registered system core resume callbacks. |
| 92 | * |
| 93 | * This function is executed with one CPU on-line and disabled interrupts. |
| 94 | */ |
| 95 | void syscore_resume(void) |
| 96 | { |
| 97 | struct syscore_ops *ops; |
| 98 | |
| 99 | trace_suspend_resume(TPS("syscore_resume"), 0, true); |
| 100 | WARN_ONCE(!irqs_disabled(), |
| 101 | "Interrupts enabled before system core resume.\n"); |
| 102 | |
| 103 | list_for_each_entry(ops, &syscore_ops_list, node) |
| 104 | if (ops->resume) { |
| 105 | if (initcall_debug) |
| 106 | pr_info("PM: Calling %pS\n", ops->resume); |
| 107 | ops->resume(); |
| 108 | WARN_ONCE(!irqs_disabled(), |
| 109 | "Interrupts enabled after %pS\n", ops->resume); |
| 110 | } |
| 111 | trace_suspend_resume(TPS("syscore_resume"), 0, false); |
| 112 | } |
| 113 | EXPORT_SYMBOL_GPL(syscore_resume); |
| 114 | #endif /* CONFIG_PM_SLEEP */ |
| 115 | |
| 116 | /** |
| 117 | * syscore_shutdown - Execute all the registered system core shutdown callbacks. |
| 118 | */ |
| 119 | void syscore_shutdown(void) |
| 120 | { |
| 121 | struct syscore_ops *ops; |
| 122 | |
| 123 | mutex_lock(&syscore_ops_lock); |
| 124 | |
| 125 | list_for_each_entry_reverse(ops, &syscore_ops_list, node) |
| 126 | if (ops->shutdown) { |
| 127 | if (initcall_debug) |
| 128 | pr_info("PM: Calling %pS\n", ops->shutdown); |
| 129 | ops->shutdown(); |
| 130 | } |
| 131 | |
| 132 | mutex_unlock(&syscore_ops_lock); |
| 133 | } |