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