qdf_cpuhp.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2017 The Linux Foundation. All rights reserved.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for
  5. * any purpose with or without fee is hereby granted, provided that the
  6. * above copyright notice and this permission notice appear in all
  7. * copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  10. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  11. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  12. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  14. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  16. * PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /**
  19. * DOC: qdf_cpuhp (CPU hotplug)
  20. * QCA driver framework (QDF) CPU hotplug APIs
  21. */
  22. #include "qdf_cpuhp.h"
  23. #include "i_qdf_cpuhp.h"
  24. #include "qdf_list.h"
  25. #include "qdf_lock.h"
  26. static qdf_mutex_t qdf_cpuhp_lock;
  27. static qdf_list_t qdf_cpuhp_handlers;
  28. struct qdf_cpuhp_handler {
  29. qdf_list_node_t node;
  30. void *context;
  31. qdf_cpuhp_callback up_callback;
  32. qdf_cpuhp_callback down_callback;
  33. };
  34. static void qdf_cpuhp_on_up(uint32_t cpu)
  35. {
  36. QDF_STATUS status;
  37. qdf_list_node_t *node;
  38. qdf_mutex_acquire(&qdf_cpuhp_lock);
  39. status = qdf_list_peek_front(&qdf_cpuhp_handlers, &node);
  40. while (QDF_IS_STATUS_SUCCESS(status)) {
  41. struct qdf_cpuhp_handler *handler =
  42. qdf_container_of(node, struct qdf_cpuhp_handler, node);
  43. if (handler->up_callback)
  44. handler->up_callback(handler->context, cpu);
  45. status = qdf_list_peek_next(&qdf_cpuhp_handlers, node, &node);
  46. }
  47. qdf_mutex_release(&qdf_cpuhp_lock);
  48. }
  49. static void qdf_cpuhp_on_down(uint32_t cpu)
  50. {
  51. QDF_STATUS status;
  52. qdf_list_node_t *node;
  53. qdf_mutex_acquire(&qdf_cpuhp_lock);
  54. status = qdf_list_peek_front(&qdf_cpuhp_handlers, &node);
  55. while (QDF_IS_STATUS_SUCCESS(status)) {
  56. struct qdf_cpuhp_handler *handler =
  57. qdf_container_of(node, struct qdf_cpuhp_handler, node);
  58. if (handler->down_callback)
  59. handler->down_callback(handler->context, cpu);
  60. status = qdf_list_peek_next(&qdf_cpuhp_handlers, node, &node);
  61. }
  62. qdf_mutex_release(&qdf_cpuhp_lock);
  63. }
  64. QDF_STATUS qdf_cpuhp_init(void)
  65. {
  66. QDF_STATUS status;
  67. status = qdf_mutex_create(&qdf_cpuhp_lock);
  68. if (QDF_IS_STATUS_ERROR(status))
  69. return status;
  70. qdf_list_create(&qdf_cpuhp_handlers, 0);
  71. __qdf_cpuhp_os_init(qdf_cpuhp_on_up, qdf_cpuhp_on_down);
  72. return QDF_STATUS_SUCCESS;
  73. }
  74. QDF_STATUS qdf_cpuhp_deinit(void)
  75. {
  76. __qdf_cpuhp_os_deinit();
  77. qdf_list_destroy(&qdf_cpuhp_handlers);
  78. return qdf_mutex_destroy(&qdf_cpuhp_lock);
  79. }
  80. QDF_STATUS qdf_cpuhp_register(struct qdf_cpuhp_handler **out_handler,
  81. void *context,
  82. qdf_cpuhp_callback up_callback,
  83. qdf_cpuhp_callback down_callback)
  84. {
  85. QDF_STATUS status;
  86. struct qdf_cpuhp_handler *handler;
  87. *out_handler = NULL;
  88. handler = qdf_mem_malloc(sizeof(*handler));
  89. if (!handler)
  90. return QDF_STATUS_E_NOMEM;
  91. handler->context = context;
  92. handler->up_callback = up_callback;
  93. handler->down_callback = down_callback;
  94. status = qdf_mutex_acquire(&qdf_cpuhp_lock);
  95. if (QDF_IS_STATUS_ERROR(status))
  96. goto free_handler;
  97. status = qdf_list_insert_back(&qdf_cpuhp_handlers, &handler->node);
  98. if (QDF_IS_STATUS_ERROR(status))
  99. goto release_lock;
  100. /* this can fail, but there isn't a good way to recover... */
  101. qdf_mutex_release(&qdf_cpuhp_lock);
  102. *out_handler = handler;
  103. return QDF_STATUS_SUCCESS;
  104. release_lock:
  105. qdf_mutex_release(&qdf_cpuhp_lock);
  106. free_handler:
  107. qdf_mem_free(handler);
  108. return status;
  109. }
  110. void qdf_cpuhp_unregister(struct qdf_cpuhp_handler **out_handler)
  111. {
  112. struct qdf_cpuhp_handler *handler = *out_handler;
  113. QDF_BUG(handler);
  114. if (!handler)
  115. return;
  116. qdf_mutex_acquire(&qdf_cpuhp_lock);
  117. qdf_list_remove_node(&qdf_cpuhp_handlers, &handler->node);
  118. qdf_mutex_release(&qdf_cpuhp_lock);
  119. qdf_mem_free(handler);
  120. *out_handler = NULL;
  121. }