syscore.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * syscore.c - Execution of system core operations.
  3. *
  4. * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
  5. *
  6. * This file is released under the GPLv2.
  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. 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. pr_debug("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. if (initcall_debug)
  57. pr_info("PM: Calling %pF\n", ops->suspend);
  58. ret = ops->suspend();
  59. if (ret)
  60. goto err_out;
  61. WARN_ONCE(!irqs_disabled(),
  62. "Interrupts enabled after %pF\n", ops->suspend);
  63. }
  64. trace_suspend_resume(TPS("syscore_suspend"), 0, false);
  65. return 0;
  66. err_out:
  67. pr_err("PM: System core suspend callback %pF failed.\n", ops->suspend);
  68. list_for_each_entry_continue(ops, &syscore_ops_list, node)
  69. if (ops->resume)
  70. ops->resume();
  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_ops *ops;
  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(ops, &syscore_ops_list, node)
  86. if (ops->resume) {
  87. if (initcall_debug)
  88. pr_info("PM: Calling %pF\n", ops->resume);
  89. ops->resume();
  90. WARN_ONCE(!irqs_disabled(),
  91. "Interrupts enabled after %pF\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 %pF\n", ops->shutdown);
  108. ops->shutdown();
  109. }
  110. mutex_unlock(&syscore_ops_lock);
  111. }