syscore.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. 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. pr_debug("Checking wakeup interrupts\n");
  47. /* Return error code if there are any wakeup interrupts pending. */
  48. ret = check_wakeup_irqs();
  49. if (ret)
  50. return ret;
  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. if (initcall_debug)
  56. pr_info("PM: Calling %pF\n", ops->suspend);
  57. ret = ops->suspend();
  58. if (ret)
  59. goto err_out;
  60. WARN_ONCE(!irqs_disabled(),
  61. "Interrupts enabled after %pF\n", ops->suspend);
  62. }
  63. return 0;
  64. err_out:
  65. pr_err("PM: System core suspend callback %pF 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. WARN_ONCE(!irqs_disabled(),
  81. "Interrupts enabled before system core resume.\n");
  82. list_for_each_entry(ops, &syscore_ops_list, node)
  83. if (ops->resume) {
  84. if (initcall_debug)
  85. pr_info("PM: Calling %pF\n", ops->resume);
  86. ops->resume();
  87. WARN_ONCE(!irqs_disabled(),
  88. "Interrupts enabled after %pF\n", ops->resume);
  89. }
  90. }
  91. EXPORT_SYMBOL_GPL(syscore_resume);
  92. #endif /* CONFIG_PM_SLEEP */
  93. /**
  94. * syscore_shutdown - Execute all the registered system core shutdown callbacks.
  95. */
  96. void syscore_shutdown(void)
  97. {
  98. struct syscore_ops *ops;
  99. mutex_lock(&syscore_ops_lock);
  100. list_for_each_entry_reverse(ops, &syscore_ops_list, node)
  101. if (ops->shutdown) {
  102. if (initcall_debug)
  103. pr_info("PM: Calling %pF\n", ops->shutdown);
  104. ops->shutdown();
  105. }
  106. mutex_unlock(&syscore_ops_lock);
  107. }