syscore.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_list);
  13. static DEFINE_MUTEX(syscore_lock);
  14. /**
  15. * register_syscore - Register a set of system core operations.
  16. * @syscore: System core operations to register.
  17. */
  18. void register_syscore(struct syscore *syscore)
  19. {
  20. mutex_lock(&syscore_lock);
  21. list_add_tail(&syscore->node, &syscore_list);
  22. mutex_unlock(&syscore_lock);
  23. }
  24. EXPORT_SYMBOL_GPL(register_syscore);
  25. /**
  26. * unregister_syscore - Unregister a set of system core operations.
  27. * @syscore: System core operations to unregister.
  28. */
  29. void unregister_syscore(struct syscore *syscore)
  30. {
  31. mutex_lock(&syscore_lock);
  32. list_del(&syscore->node);
  33. mutex_unlock(&syscore_lock);
  34. }
  35. EXPORT_SYMBOL_GPL(unregister_syscore);
  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 *syscore;
  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(syscore, &syscore_list, node)
  54. if (syscore->ops->suspend) {
  55. pm_pr_dbg("Calling %pS\n", syscore->ops->suspend);
  56. ret = syscore->ops->suspend(syscore->data);
  57. if (ret)
  58. goto err_out;
  59. WARN_ONCE(!irqs_disabled(),
  60. "Interrupts enabled after %pS\n",
  61. syscore->ops->suspend);
  62. }
  63. trace_suspend_resume(TPS("syscore_suspend"), 0, false);
  64. return 0;
  65. err_out:
  66. pr_err("PM: System core suspend callback %pS failed.\n",
  67. syscore->ops->suspend);
  68. list_for_each_entry_continue(syscore, &syscore_list, node)
  69. if (syscore->ops->resume)
  70. syscore->ops->resume(syscore->data);
  71. return ret;
  72. }
  73. EXPORT_SYMBOL_GPL(syscore_suspend);
  74. /**
  75. * syscore_resume - Execute all the registered system core resume callbacks.
  76. *
  77. * This function is executed with one CPU on-line and disabled interrupts.
  78. */
  79. void syscore_resume(void)
  80. {
  81. struct syscore *syscore;
  82. trace_suspend_resume(TPS("syscore_resume"), 0, true);
  83. WARN_ONCE(!irqs_disabled(),
  84. "Interrupts enabled before system core resume.\n");
  85. list_for_each_entry(syscore, &syscore_list, node)
  86. if (syscore->ops->resume) {
  87. pm_pr_dbg("Calling %pS\n", syscore->ops->resume);
  88. syscore->ops->resume(syscore->data);
  89. WARN_ONCE(!irqs_disabled(),
  90. "Interrupts enabled after %pS\n",
  91. syscore->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 *syscore;
  103. mutex_lock(&syscore_lock);
  104. list_for_each_entry_reverse(syscore, &syscore_list, node)
  105. if (syscore->ops->shutdown) {
  106. if (initcall_debug)
  107. pr_info("PM: Calling %pS\n",
  108. syscore->ops->shutdown);
  109. syscore->ops->shutdown(syscore->data);
  110. }
  111. mutex_unlock(&syscore_lock);
  112. }