syscore.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. static LIST_HEAD(syscore_ops_list);
  12. static DEFINE_MUTEX(syscore_ops_lock);
  13. /**
  14. * register_syscore_ops - Register a set of system core operations.
  15. * @ops: System core operations to register.
  16. */
  17. void register_syscore_ops(struct syscore_ops *ops)
  18. {
  19. mutex_lock(&syscore_ops_lock);
  20. list_add_tail(&ops->node, &syscore_ops_list);
  21. mutex_unlock(&syscore_ops_lock);
  22. }
  23. EXPORT_SYMBOL_GPL(register_syscore_ops);
  24. /**
  25. * unregister_syscore_ops - Unregister a set of system core operations.
  26. * @ops: System core operations to unregister.
  27. */
  28. void unregister_syscore_ops(struct syscore_ops *ops)
  29. {
  30. mutex_lock(&syscore_ops_lock);
  31. list_del(&ops->node);
  32. mutex_unlock(&syscore_ops_lock);
  33. }
  34. EXPORT_SYMBOL_GPL(unregister_syscore_ops);
  35. #ifdef CONFIG_PM_SLEEP
  36. /**
  37. * syscore_suspend - Execute all the registered system core suspend callbacks.
  38. *
  39. * This function is executed with one CPU on-line and disabled interrupts.
  40. */
  41. int syscore_suspend(void)
  42. {
  43. struct syscore_ops *ops;
  44. int ret = 0;
  45. WARN_ONCE(!irqs_disabled(),
  46. "Interrupts enabled before system core suspend.\n");
  47. list_for_each_entry_reverse(ops, &syscore_ops_list, node)
  48. if (ops->suspend) {
  49. if (initcall_debug)
  50. pr_info("PM: Calling %pF\n", ops->suspend);
  51. ret = ops->suspend();
  52. if (ret)
  53. goto err_out;
  54. WARN_ONCE(!irqs_disabled(),
  55. "Interrupts enabled after %pF\n", ops->suspend);
  56. }
  57. return 0;
  58. err_out:
  59. pr_err("PM: System core suspend callback %pF failed.\n", ops->suspend);
  60. list_for_each_entry_continue(ops, &syscore_ops_list, node)
  61. if (ops->resume)
  62. ops->resume();
  63. return ret;
  64. }
  65. /**
  66. * syscore_resume - Execute all the registered system core resume callbacks.
  67. *
  68. * This function is executed with one CPU on-line and disabled interrupts.
  69. */
  70. void syscore_resume(void)
  71. {
  72. struct syscore_ops *ops;
  73. WARN_ONCE(!irqs_disabled(),
  74. "Interrupts enabled before system core resume.\n");
  75. list_for_each_entry(ops, &syscore_ops_list, node)
  76. if (ops->resume) {
  77. if (initcall_debug)
  78. pr_info("PM: Calling %pF\n", ops->resume);
  79. ops->resume();
  80. WARN_ONCE(!irqs_disabled(),
  81. "Interrupts enabled after %pF\n", ops->resume);
  82. }
  83. }
  84. #endif /* CONFIG_PM_SLEEP */
  85. /**
  86. * syscore_shutdown - Execute all the registered system core shutdown callbacks.
  87. */
  88. void syscore_shutdown(void)
  89. {
  90. struct syscore_ops *ops;
  91. mutex_lock(&syscore_ops_lock);
  92. list_for_each_entry_reverse(ops, &syscore_ops_list, node)
  93. if (ops->shutdown) {
  94. if (initcall_debug)
  95. pr_info("PM: Calling %pF\n", ops->shutdown);
  96. ops->shutdown();
  97. }
  98. mutex_unlock(&syscore_ops_lock);
  99. }