syscore.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include <linux/syscore_ops.h>
  8. #include <linux/mutex.h>
  9. #include <linux/module.h>
  10. #include <linux/suspend.h>
  11. #include <trace/events/power.h>
  12. #include <linux/wakeup_reason.h>
  13. static LIST_HEAD(syscore_ops_list);
  14. static DEFINE_MUTEX(syscore_ops_lock);
  15. /**
  16. * register_syscore_ops - Register a set of system core operations.
  17. * @ops: System core operations to register.
  18. */
  19. void register_syscore_ops(struct syscore_ops *ops)
  20. {
  21. mutex_lock(&syscore_ops_lock);
  22. list_add_tail(&ops->node, &syscore_ops_list);
  23. mutex_unlock(&syscore_ops_lock);
  24. }
  25. EXPORT_SYMBOL_GPL(register_syscore_ops);
  26. /**
  27. * unregister_syscore_ops - Unregister a set of system core operations.
  28. * @ops: System core operations to unregister.
  29. */
  30. void unregister_syscore_ops(struct syscore_ops *ops)
  31. {
  32. mutex_lock(&syscore_ops_lock);
  33. list_del(&ops->node);
  34. mutex_unlock(&syscore_ops_lock);
  35. }
  36. EXPORT_SYMBOL_GPL(unregister_syscore_ops);
  37. #ifdef CONFIG_PM_SLEEP
  38. /**
  39. * syscore_suspend - Execute all the registered system core suspend callbacks.
  40. *
  41. * This function is executed with one CPU on-line and disabled interrupts.
  42. */
  43. int syscore_suspend(void)
  44. {
  45. struct syscore_ops *ops;
  46. int ret = 0;
  47. trace_suspend_resume(TPS("syscore_suspend"), 0, true);
  48. pm_pr_dbg("Checking wakeup interrupts\n");
  49. /* Return error code if there are any wakeup interrupts pending. */
  50. if (pm_wakeup_pending())
  51. return -EBUSY;
  52. WARN_ONCE(!irqs_disabled(),
  53. "Interrupts enabled before system core suspend.\n");
  54. list_for_each_entry_reverse(ops, &syscore_ops_list, node)
  55. if (ops->suspend) {
  56. pm_pr_dbg("Calling %pS\n", ops->suspend);
  57. ret = ops->suspend();
  58. if (ret)
  59. goto err_out;
  60. WARN_ONCE(!irqs_disabled(),
  61. "Interrupts enabled after %pS\n", ops->suspend);
  62. }
  63. trace_suspend_resume(TPS("syscore_suspend"), 0, false);
  64. return 0;
  65. err_out:
  66. log_suspend_abort_reason("System core suspend callback %pS failed",
  67. ops->suspend);
  68. pr_err("PM: System core suspend callback %pF failed.\n", ops->suspend);
  69. list_for_each_entry_continue(ops, &syscore_ops_list, node)
  70. if (ops->resume)
  71. ops->resume();
  72. return ret;
  73. }
  74. EXPORT_SYMBOL_GPL(syscore_suspend);
  75. /**
  76. * syscore_resume - Execute all the registered system core resume callbacks.
  77. *
  78. * This function is executed with one CPU on-line and disabled interrupts.
  79. */
  80. void syscore_resume(void)
  81. {
  82. struct syscore_ops *ops;
  83. trace_suspend_resume(TPS("syscore_resume"), 0, true);
  84. WARN_ONCE(!irqs_disabled(),
  85. "Interrupts enabled before system core resume.\n");
  86. list_for_each_entry(ops, &syscore_ops_list, node)
  87. if (ops->resume) {
  88. pm_pr_dbg("Calling %pS\n", ops->resume);
  89. ops->resume();
  90. WARN_ONCE(!irqs_disabled(),
  91. "Interrupts enabled after %pS\n", ops->resume);
  92. }
  93. trace_suspend_resume(TPS("syscore_resume"), 0, false);
  94. }
  95. EXPORT_SYMBOL_GPL(syscore_resume);
  96. #endif /* CONFIG_PM_SLEEP */
  97. /**
  98. * syscore_shutdown - Execute all the registered system core shutdown callbacks.
  99. */
  100. void syscore_shutdown(void)
  101. {
  102. struct syscore_ops *ops;
  103. mutex_lock(&syscore_ops_lock);
  104. list_for_each_entry_reverse(ops, &syscore_ops_list, node)
  105. if (ops->shutdown) {
  106. if (initcall_debug)
  107. pr_info("PM: Calling %pS\n", ops->shutdown);
  108. ops->shutdown();
  109. }
  110. mutex_unlock(&syscore_ops_lock);
  111. }