syscore.c 3.1 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/interrupt.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. pr_debug("Checking wakeup interrupts\n");
  48. /* Return error code if there are any wakeup interrupts pending. */
  49. ret = check_wakeup_irqs();
  50. if (ret)
  51. return ret;
  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. return 0;
  65. err_out:
  66. log_suspend_abort_reason("System core suspend callback %pF 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. 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. }
  94. EXPORT_SYMBOL_GPL(syscore_resume);
  95. #endif /* CONFIG_PM_SLEEP */
  96. /**
  97. * syscore_shutdown - Execute all the registered system core shutdown callbacks.
  98. */
  99. void syscore_shutdown(void)
  100. {
  101. struct syscore_ops *ops;
  102. mutex_lock(&syscore_ops_lock);
  103. list_for_each_entry_reverse(ops, &syscore_ops_list, node)
  104. if (ops->shutdown) {
  105. if (initcall_debug)
  106. pr_info("PM: Calling %pF\n", ops->shutdown);
  107. ops->shutdown();
  108. }
  109. mutex_unlock(&syscore_ops_lock);
  110. }