syscore.c 3.2 KB

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